Thread: pointers...

  1. #1
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    pointers...

    I'm writing a plugin for RPGToolkit's RPGCode2 scripting language that allows the user to use read and write commands.

    Here's the code:

    Code:
    //////////////////GCN_ZELDA'S READ/WRITE PLUGIN\\\\\\\\\\\\\\\\\\\\
    //////////////////You can change it if you want, but leave some credit to me
    #include "stdafx.h"
    #include "tkplugin.h"
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    ///////////////////////////////////////////////////////
    //Start
    ///////////////////////////////////////////////////////
    
    void APIENTRY TKPlugBegin()
    {
    	//TBD: Add startup code here.
    	CBDebugMessage("Read/Write plugin initialized");
    }
    
    
    ///////////////////////////////////////////////////////
    //FileOpen
    //////////////////////////////////////////////////////
    
    int APIENTRY TKPlugQuery(char* pstrQuery)
    {
    	//TBD: Add query code here.
    	if (strcmp(pstrQuery, "fileopen")==0) {
    		//we support this command
    		return 1;
    	}
    
    	//if we make it here, we don't support this command.
    	return 0;
    }
    
    int APIENTRY TKPlugExecute(char* pstrCommand)
    {
    	//TBD: Add execution code here.
    
    	int nRet = 0;
    	char* cmdName = CBGetCommandName(pstrCommand);
    	if (strcmp(cmdName, "#fileopen")==0) 
    	{
    		//get the contents of the brackets...
    		char* br = CBGetBrackets(pstrCommand);
    		//count the number of elements in the brackets...
    		int num = CBCountBracketElements(br);
    		if (num!=1)
    		{
    			//there should be 2 elements!
    			CBDebugMessage("Error: #FileOpen() required 1 data element!");
    			//let's bail!
    			return 1;
    		}
    
    		//ok, get the two elements from the brackets...
    		char* e1 = CBGetBracketElement(br, 1);
    
    		//now check the types of these elements (should be lit)...
    		int e1Type = CBGetElementType(e1);
    		if (e1Type != 1) 
    		{
    			//if we get in here, then improper types were passed in...
    			CBDebugMessage("Error: #FileOpen() data types should be lit!");
    			//let's bail!
    			return 1;
    		}
    
    		//ok, if we made it here, all's well.
    		//let's do what we were supposed to do...
    		
    		//first, get the text value (the question...)
    		char* file = CBGetStringElementValue(e1);
    		fstream *writingtofile(file);
    		//clean up...
    		delete [] br;
    		delete [] e1;
    		nRet = 1;
    	}
    	delete [] cmdName;  //just cleaning up!
    	return nRet;
    }
    
    
    ///////////////////////////////////////////////////////
    //Write
    //////////////////////////////////////////////////////
    
    int APIENTRY TKPlugQuery(char* pstrQuery)
    {
    	//TBD: Add query code here.
    	if (strcmp(pstrQuery, "write")==0) {
    		//we support this command
    		return 1;
    	}
    
    	//if we make it here, we don't support this command.
    	return 0;
    }
    
    int APIENTRY TKPlugExecute(char* pstrCommand)
    {
    	//TBD: Add execution code here.
    
    	int nRet = 0;
    	char* cmdName = CBGetCommandName(pstrCommand);
    	if (strcmp(cmdName, "#write")==0) 
    	{
    		//get the contents of the brackets...
    		char* br = CBGetBrackets(pstrCommand);
    		//count the number of elements in the brackets...
    		int num = CBCountBracketElements(br);
    		if (num!=1)
    		{
    			//there should be 2 elements!
    			CBDebugMessage("Error: #Write() required 1 data elements!");
    			//let's bail!
    			return 1;
    		}
    		//ok, get the two elements from the brackets...
    		char* e1 = CBGetBracketElement(br, 1);
    		//now check the types of these elements (should be lit then num)...
    		int e1Type = CBGetElementType(e1);
    		if (e1Type != 1) 
    		{
    			//if we get in here, then improper types were passed in...
    			CBDebugMessage("Error: #Write() data type should be lit!");
    			//let's bail!
    			return 1;
    		}
    
    		//ok, if we made it here, all's well.
    		//let's do what we were supposed to do...
    		
    		//first, get the text value (the question...)
    		char* txt = CBGetStringElementValue(e1);
    		&writingtofile >> txt >> endl;
    
    		//clean up...
    		delete [] br;
    		delete [] e1;
    		delete [] txt;
    		nRet = 1;
    	}
    	delete [] cmdName;	//just cleaning up!
    	return nRet;
    }
    ///////////////////////////////////////////////
    //Read
    ////////////////////////////////////////////
    int APIENTRY TKPlugQuery(char* pstrQuery)
    {
    	//TBD: Add query code here.
    	if (strcmp(pstrQuery, "read")==0) {
    		//we support this command
    		return 1;
    	}
    
    	//if we make it here, we don't support this command.
    	return 0;
    }
    int APIENTRY TKPlugExecute(char* pstrCommand)
    {
    	//TBD: Add execution code here.
    
    	int nRet = 0;
    	char* cmdName = CBGetCommandName(pstrCommand);
    	if (strcmp(cmdName, "#read")==0) 
    	{
    		//get the contents of the brackets...
    		char* br = CBGetBrackets(pstrCommand);
    		//count the number of elements in the brackets...
    		int num = CBCountBracketElements(br);
    		if (num!=1)
    		{
    			//there should be 2 elements!
    			CBDebugMessage("Error: #Read() required 1 data elements!");
    			//let's bail!
    			return 1;
    		}
    
    		//ok, get the two elements from the brackets...
    		char* e1 = CBGetBracketElement(br, 1);
    		//now check the types of these elements (should be lit then num)...
    		int e1Type = CBGetElementType(e1);
    		if (e1Type != 1) 
    		{
    			//if we get in here, then improper types were passed in...
    			CBDebugMessage("Error: #Read() data types should be lit!");
    			//let's bail!
    			return 1;
    		}
    
    		//ok, if we made it here, all's well.
    		//let's do what we were supposed to do...
    		
    		//first, get the text value (the question...)
    		char* txt = CBGetStringElementValue(e1);
    		&writingtofile << txt << endl;
    
    		//clean up...
    		delete [] br;
    		delete [] e1;
    		delete [] txt;
    		nRet = 1;
    	}
    	delete [] cmdName;	//just cleaning up!
    	return nRet;
    }
    
    //////////////////////////
    //FileClose
    /////////////////////////
    int APIENTRY TKPlugQuery(char* pstrQuery)
    {
    	//TBD: Add query code here.
    	if (strcmp(pstrQuery, "fileclose")==0) {
    		//we support this command
    		return 1;
    	}
    
    	//if we make it here, we don't support this command.
    	return 0;
    }
    
    int APIENTRY TKPlugExecute(char* pstrCommand)
    {
    	//TBD: Add execution code here.
    
    	int nRet = 0;
    	char* cmdName = CBGetCommandName(pstrCommand);
    	if (strcmp(cmdName, "#fileclose")==0) 
    	{
    		//get the contents of the brackets...
    		char* br = CBGetBrackets(pstrCommand);
    		//count the number of elements in the brackets...
    		int num = CBCountBracketElements(br);
    		if (num!=1)
    		{
    			//there should be 2 elements!
    			CBDebugMessage("Error: #FileClose() required 1 data element!");
    			//let's bail!
    			return 1;
    		}
    
    		//ok, get the two elements from the brackets...
    		char* e1 = CBGetBracketElement(br, 1);
    
    		//now check the types of these elements (should be lit)...
    		int e1Type = CBGetElementType(e1);
    		if (e1Type != 1) 
    		{
    			//if we get in here, then improper types were passed in...
    			CBDebugMessage("Error: #FileOpen() data types should be lit!");
    			//let's bail!
    			return 1;
    		}
    
    		//ok, if we made it here, all's well.
    		//let's do what we were supposed to do...
    		
    		//first, get the text value (the question...)
    		char* file = CBGetStringElementValue(e1);
    		&writingtofile.close();		
    		//clean up...
    		delete [] br;
    		delete [] e1;
    		nRet = 1;
    	}
    	delete [] cmdName;	//just cleaning up!
    	return nRet;
    }
    
    ///////////////////////////////////////////////////////
    //
    // Function: TKPlugEnd
    //
    // Parameters: 
    //
    // Action: called when the Toolkit unloads
    //				the plugin.  Allows you to
    //				perform deallocations
    //
    // Returns: 
    //
    ///////////////////////////////////////////////////////
    void APIENTRY TKPlugEnd()
    {
    	//TBD: Add finishing code here.
    	CBDebugMessage("Read/Write plugin destroyed");
    }
    The problems are the

    "writingtofile" thing

    I don't know how to make the
    fstream writingtofile
    work with all of the commands. All of the callback functions are correct and stuff.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Wow that's a lot of code. I'm not quite sure what your question is, and I didn't read the code in detail (just found what you mentioned)

    Is this what you meant?
    Code:
    ifstream *input(file);
    
    input >> int;
    input >> char;
    input >> float;
    
    close input;
    
    ofstream *output(file);
    output << int;
    output <<char;
    output << float;
    
    close output;
    It's pretty easy to use... it's just like cout and cin
    Away.

  3. #3
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    ok, well, that's not what I was looking for, but I figured it out anyways. Thanks! Your help came in handy too. I always mix up the file handling stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM