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.