////////////////////////////////////////////////////////
//
// script.cpp
-//
+//
// Implementaion of a basic basic :) interpreter
//
//
{
string source;
- /* We have to do it that way (doublepointer) because MinGw
+ /* We have to do it that way (doublepointer) because MinGw
calls "delete" at the end of function otherwise. */
- (*script) = new SCRIPT;
+ (*script) = new SCRIPT;
// Load file to string
ifstream file(path, ios_base::in);
getline(file, source, '\0');
// make sure last char is a new line
- source += "\n";
-
+ source += "\n";
+
// Are all subs and strings closed ?
// FIXME: Just a quick hack sould be both checked line by line
- if(FindCount(source, "\"")%2) // if count is uneven not all strings are closed
+ if(FindCount(source, "\"")%2) // if count is uneven not all strings are closed
return ERR_SYNATX;
if(FindCount(source, "Sub ") != FindCount(source, "End Sub\n"))
for (i=0; i < (*script)->code.size(); i++) // code.size() is the cout of lines
{
SUB sub;
-
+
if((*script)->code[i].substr(0,4) != "sub ")
return ERR_SYNATX; // script has to start with sub
i++;
//if script does not end with "end sub" we got a problem
if (i>(*script)->code.size())
- return ERR_SYNATX;
+ return ERR_SYNATX;
}
sub.end = i;
// call the function
for (a=script->subs[nr].start; a<script->subs[nr].end; a++)
{
- // create a temporarry buffer
+ // create a temporarry buffer
buffer = new char[script->code[a].size()];
strcpy(buffer, script->code[a].c_str());
// make the fist argument the function's name
argv[0] = &buffer[0];
-
+
int buffer_size = (int)strlen(buffer);
for (b=0; b<buffer_size+1; b++)
{
buffer[b] = '\0';
// we don't want buffer overflows
- if(argc == 99)
+ if(argc == 99)
return ERR_GENERIC;
}
Log("* excute command: ");
for(c=0; c<argc+1; c++)
{
- LogAdd(argv[c]);
+ LogAdd(argv[c]);
LogAdd(" ");
}
int FindCount (string where, string what, int start, int end)
{
int counter = 0, pos;
-
+
while(true)
{
pos = (int)where.find (what, start);
- //could could not be found or is outside of search area
- if (pos == (int)string::npos || (end!=-1 && pos>end))
+ //could could not be found or is outside of search area
+ if (pos == (int)string::npos || (end!=-1 && pos>end))
break;
start = pos+1;
counter++;
{
int pos = (int)where.find (what, start);
- //could could not be found or is outside of search area
- if (pos == (int)string::npos || (end!=-1 && pos>end))
+ //could could not be found or is outside of search area
+ if (pos == (int)string::npos || (end!=-1 && pos>end))
return -1;
- // if the count of this quotes is eaven we are in string
+ // if the count of this quotes is eaven we are in string
int isInString = FindCount(where, "\"", start, pos)%2;
- // if so we go on searching
+ // if so we go on searching
if(isInString == instring)
return Find (where, what, pos+1, end, instring);