Command definitions are now read into memory.

Sat, 14 Jul 2012 01:49:32 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 14 Jul 2012 01:49:32 +0300
changeset 6
0005527cad62
parent 5
2b75b0bac4db
child 7
118d3d5db64f

Command definitions are now read into memory.

Makefile file | annotate | diff | comparison | revisions
commands.cxx file | annotate | diff | comparison | revisions
commands.h file | annotate | diff | comparison | revisions
events.cxx file | annotate | diff | comparison | revisions
main.cxx file | annotate | diff | comparison | revisions
str.cxx file | annotate | diff | comparison | revisions
str.h file | annotate | diff | comparison | revisions
--- a/Makefile	Fri Jul 13 20:35:18 2012 +0300
+++ b/Makefile	Sat Jul 14 01:49:32 2012 +0300
@@ -5,7 +5,8 @@
 	g++ -Wall -c -o main.o main.cxx
 	g++ -Wall -c -o parser.o parser.cxx
 	g++ -Wall -c -o events.o events.cxx
-	g++ -Wall -o botc scriptreader.o objwriter.o str.o main.o parser.o events.o
+	g++ -Wall -c -o commands.o commands.cxx
+	g++ -Wall -o botc scriptreader.o objwriter.o str.o main.o parser.o events.o commands.o
 
 clean:
 	rm -f *.o *~ botc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/commands.cxx	Sat Jul 14 01:49:32 2012 +0300
@@ -0,0 +1,135 @@
+/*
+ *	botc source code
+ *	Copyright (C) 2012 Santeri `Dusk` Piippo
+ *	All rights reserved.
+ *	
+ *	Redistribution and use in source and binary forms, with or without
+ *	modification, are permitted provided that the following conditions are met:
+ *	
+ *	1. Redistributions of source code must retain the above copyright notice,
+ *	   this list of conditions and the following disclaimer.
+ *	2. Redistributions in binary form must reproduce the above copyright notice,
+ *	   this list of conditions and the following disclaimer in the documentation
+ *	   and/or other materials provided with the distribution.
+ *	3. Neither the name of the developer nor the names of its contributors may
+ *	   be used to endorse or promote products derived from this software without
+ *	   specific prior written permission.
+ *	4. Redistributions in any form must be accompanied by information on how to
+ *	   obtain complete source code for the software and any accompanying
+ *	   software that uses the software. The source code must either be included
+ *	   in the distribution or be available for no more than the cost of
+ *	   distribution plus a nominal fee, and must be freely redistributable
+ *	   under reasonable conditions. For an executable file, complete source
+ *	   code means the source code for all modules it contains. It does not
+ *	   include source code for modules or files that typically accompany the
+ *	   major components of the operating system on which the executable file
+ *	   runs.
+ *	
+ *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ *	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ *	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ *	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ *	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ *	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ *	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ *	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ *	POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define __COMMANDS_CXX__
+#include <stdlib.h>
+#include <stdio.h>
+#include "common.h"
+#include "scriptreader.h"
+#include "str.h"
+#include "commands.h"
+
+CommandDef* g_CommDef;
+
+void ReadCommands () {
+	ScriptReader* r = new ScriptReader ((char*)"commands.def");
+	g_CommDef = NULL;
+	CommandDef* curdef = g_CommDef;
+	unsigned int numCommDefs = 0;
+	
+	while (r->Next()) {
+		CommandDef* comm = new CommandDef;
+		
+		int n = 0;
+		str t = "";
+		for (unsigned int u = 0; u < r->token.len(); u++) {
+			// If we're at the last character of the string, we need
+			// to both add the character to t and check it. Thus,
+			// we do the addition, exceptionally, here.
+			if (u == r->token.len() - 1 && r->token[u] != ':')
+				t += r->token[u];
+			
+			if (r->token[u] == ':' || u == r->token.len() - 1) {
+				int i = atoi (t.chars());
+				switch (n) {
+				case 0:
+					// Number
+					comm->number = i;
+					break;
+				case 1:
+					// Name
+					comm->name = t;
+					break;
+				case 2:
+					// Return value
+					t.tolower();
+					if (!t.compare ("int"))
+						comm->returnvalue = RETURNVAL_INT;
+					else if (!t.compare ("str"))
+						comm->returnvalue = RETURNVAL_STRING;
+					else if (!t.compare ("void"))
+						comm->returnvalue = RETURNVAL_VOID;
+					else if (!t.compare ("bool"))
+						comm->returnvalue = RETURNVAL_BOOLEAN;
+					else
+						r->ParseError ("bad return value type `%s`", t.chars());
+					break;
+				case 3:
+					// Num args
+					comm->numargs = i;
+					break;
+				case 4:
+					// Max args
+					comm->maxargs = i;
+					break;
+				}
+				
+				n++;
+				t = "";
+			} else {
+				t += r->token[u];
+			}
+		}
+		
+		comm->next = NULL;
+		
+		if (!g_CommDef)
+			g_CommDef = comm;
+		
+		if (!curdef) {
+			curdef = comm;
+		} else {
+			curdef->next = comm;
+			curdef = comm;
+		}
+		numCommDefs++;
+	}
+	
+	/*
+	CommandDef* c;
+	ITERATE_COMMANDS (c) {
+		printf ("[%u] %s: %d, %d arguments, return type %d\n",
+			c->number, c->name.chars(), c->numargs, c->maxargs, c->returnvalue);
+	}
+	*/
+	
+	delete r;
+	printf ("%d command definitions read.\n", numCommDefs);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/commands.h	Sat Jul 14 01:49:32 2012 +0300
@@ -0,0 +1,61 @@
+/*
+ *	botc source code
+ *	Copyright (C) 2012 Santeri `Dusk` Piippo
+ *	All rights reserved.
+ *	
+ *	Redistribution and use in source and binary forms, with or without
+ *	modification, are permitted provided that the following conditions are met:
+ *	
+ *	1. Redistributions of source code must retain the above copyright notice,
+ *	   this list of conditions and the following disclaimer.
+ *	2. Redistributions in binary form must reproduce the above copyright notice,
+ *	   this list of conditions and the following disclaimer in the documentation
+ *	   and/or other materials provided with the distribution.
+ *	3. Neither the name of the developer nor the names of its contributors may
+ *	   be used to endorse or promote products derived from this software without
+ *	   specific prior written permission.
+ *	4. Redistributions in any form must be accompanied by information on how to
+ *	   obtain complete source code for the software and any accompanying
+ *	   software that uses the software. The source code must either be included
+ *	   in the distribution or be available for no more than the cost of
+ *	   distribution plus a nominal fee, and must be freely redistributable
+ *	   under reasonable conditions. For an executable file, complete source
+ *	   code means the source code for all modules it contains. It does not
+ *	   include source code for modules or files that typically accompany the
+ *	   major components of the operating system on which the executable file
+ *	   runs.
+ *	
+ *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ *	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ *	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ *	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ *	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ *	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ *	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ *	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ *	POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef __COMMANDS_H__
+#define __COMMANDS_H__
+
+#include "str.h"
+#include "botcommands.h"
+
+#define ITERATE_COMMANDS(comm) \
+	for (comm = g_CommDef; comm->next != NULL; comm = comm->next)
+
+struct CommandDef {
+	str name;
+	int number;
+	int numargs;
+	int maxargs;
+	RETURNVAL_e returnvalue;
+	CommandDef* next;
+};
+
+void ReadCommands ();
+
+#endif // __COMMANDS_H__
\ No newline at end of file
--- a/events.cxx	Fri Jul 13 20:35:18 2012 +0300
+++ b/events.cxx	Sat Jul 14 01:49:32 2012 +0300
@@ -38,6 +38,7 @@
  *	POSSIBILITY OF SUCH DAMAGE.
  */
 
+#define __EVENTS_CXX__
 #include <stdlib.h>
 #include <stdio.h>
 #include "common.h"
--- a/main.cxx	Fri Jul 13 20:35:18 2012 +0300
+++ b/main.cxx	Sat Jul 14 01:49:32 2012 +0300
@@ -47,6 +47,7 @@
 #include "scriptreader.h"
 #include "objwriter.h"
 #include "events.h"
+#include "commands.h"
 
 #include "bots.h"
 #include "botcommands.h"
@@ -64,8 +65,9 @@
 	headerline.repeat ((header.len()/2)-1);
 	printf ("%s\n%s\n", header.chars(), headerline.chars());
 	
-	// Read the event definitions
+	// Read definitions
 	ReadEvents ();
+	ReadCommands ();
 	
 	// Prepare reader and writer
 	ScriptReader *r = new ScriptReader (argv[1]);
--- a/str.cxx	Fri Jul 13 20:35:18 2012 +0300
+++ b/str.cxx	Sat Jul 14 01:49:32 2012 +0300
@@ -373,6 +373,53 @@
 }
 
 // ============================================================================
+unsigned int str::count (char* c) {
+	unsigned int r = 0;
+	unsigned int tmp = 0;
+	ITERATE_STRING (u) {
+		if (text[u] == c[r]) {
+			r++;
+			if (r == strlen (c)) {
+				r = 0;
+				tmp++;
+			}
+		} else {
+			if (r != 0)
+				u--;
+			r = 0;
+		}
+	}
+	
+	return tmp;
+}
+
+// ============================================================================
+#if 0
+str** str::split (char* del) {
+	unsigned int arrcount = count (del) + 1;
+	str** arr = new str* [arrcount];
+	
+	unsigned int a = 0;
+	unsigned int index = 0;
+	while (1) {
+		unsigned int b = first (del, a+1);
+		printf ("next: %u (<-> %u)\n", b, len());
+		
+		if (b == len())
+			break;
+		
+		str* x = new str;
+		x->append (substr (a, b));
+		arr[index] = x;
+		index++;
+		a = b;
+	}
+	
+	return arr;
+}
+#endif
+
+// ============================================================================
 // OPERATORS
 str str::operator + (str& c) {
 	append (c);
--- a/str.h	Fri Jul 13 20:35:18 2012 +0300
+++ b/str.h	Sat Jul 14 01:49:32 2012 +0300
@@ -137,6 +137,13 @@
 	int compare (const char* c);
 	int compare (str c);
 	
+	// Counts the amount of substrings in the string
+	unsigned int count (char* s);
+	
+#if 0
+	str** split (char* del);
+#endif
+	
 	// ======================================================================
 	// OPERATORS
 	str operator + (str& c);

mercurial