|
1 /* |
|
2 * botc source code |
|
3 * Copyright (C) 2012 Santeri `Dusk` Piippo |
|
4 * All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright notice, |
|
10 * this list of conditions and the following disclaimer. |
|
11 * 2. Redistributions in binary form must reproduce the above copyright notice, |
|
12 * this list of conditions and the following disclaimer in the documentation |
|
13 * and/or other materials provided with the distribution. |
|
14 * 3. Neither the name of the developer nor the names of its contributors may |
|
15 * be used to endorse or promote products derived from this software without |
|
16 * specific prior written permission. |
|
17 * 4. Redistributions in any form must be accompanied by information on how to |
|
18 * obtain complete source code for the software and any accompanying |
|
19 * software that uses the software. The source code must either be included |
|
20 * in the distribution or be available for no more than the cost of |
|
21 * distribution plus a nominal fee, and must be freely redistributable |
|
22 * under reasonable conditions. For an executable file, complete source |
|
23 * code means the source code for all modules it contains. It does not |
|
24 * include source code for modules or files that typically accompany the |
|
25 * major components of the operating system on which the executable file |
|
26 * runs. |
|
27 * |
|
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
38 * POSSIBILITY OF SUCH DAMAGE. |
|
39 */ |
|
40 |
|
41 #define __COMMANDS_CXX__ |
|
42 #include <stdlib.h> |
|
43 #include <stdio.h> |
|
44 #include "common.h" |
|
45 #include "scriptreader.h" |
|
46 #include "str.h" |
|
47 #include "commands.h" |
|
48 |
|
49 CommandDef* g_CommDef; |
|
50 |
|
51 void ReadCommands () { |
|
52 ScriptReader* r = new ScriptReader ((char*)"commands.def"); |
|
53 g_CommDef = NULL; |
|
54 CommandDef* curdef = g_CommDef; |
|
55 unsigned int numCommDefs = 0; |
|
56 |
|
57 while (r->Next()) { |
|
58 CommandDef* comm = new CommandDef; |
|
59 |
|
60 int n = 0; |
|
61 str t = ""; |
|
62 for (unsigned int u = 0; u < r->token.len(); u++) { |
|
63 // If we're at the last character of the string, we need |
|
64 // to both add the character to t and check it. Thus, |
|
65 // we do the addition, exceptionally, here. |
|
66 if (u == r->token.len() - 1 && r->token[u] != ':') |
|
67 t += r->token[u]; |
|
68 |
|
69 if (r->token[u] == ':' || u == r->token.len() - 1) { |
|
70 int i = atoi (t.chars()); |
|
71 switch (n) { |
|
72 case 0: |
|
73 // Number |
|
74 comm->number = i; |
|
75 break; |
|
76 case 1: |
|
77 // Name |
|
78 comm->name = t; |
|
79 break; |
|
80 case 2: |
|
81 // Return value |
|
82 t.tolower(); |
|
83 if (!t.compare ("int")) |
|
84 comm->returnvalue = RETURNVAL_INT; |
|
85 else if (!t.compare ("str")) |
|
86 comm->returnvalue = RETURNVAL_STRING; |
|
87 else if (!t.compare ("void")) |
|
88 comm->returnvalue = RETURNVAL_VOID; |
|
89 else if (!t.compare ("bool")) |
|
90 comm->returnvalue = RETURNVAL_BOOLEAN; |
|
91 else |
|
92 r->ParseError ("bad return value type `%s`", t.chars()); |
|
93 break; |
|
94 case 3: |
|
95 // Num args |
|
96 comm->numargs = i; |
|
97 break; |
|
98 case 4: |
|
99 // Max args |
|
100 comm->maxargs = i; |
|
101 break; |
|
102 } |
|
103 |
|
104 n++; |
|
105 t = ""; |
|
106 } else { |
|
107 t += r->token[u]; |
|
108 } |
|
109 } |
|
110 |
|
111 comm->next = NULL; |
|
112 |
|
113 if (!g_CommDef) |
|
114 g_CommDef = comm; |
|
115 |
|
116 if (!curdef) { |
|
117 curdef = comm; |
|
118 } else { |
|
119 curdef->next = comm; |
|
120 curdef = comm; |
|
121 } |
|
122 numCommDefs++; |
|
123 } |
|
124 |
|
125 /* |
|
126 CommandDef* c; |
|
127 ITERATE_COMMANDS (c) { |
|
128 printf ("[%u] %s: %d, %d arguments, return type %d\n", |
|
129 c->number, c->name.chars(), c->numargs, c->maxargs, c->returnvalue); |
|
130 } |
|
131 */ |
|
132 |
|
133 delete r; |
|
134 printf ("%d command definitions read.\n", numCommDefs); |
|
135 } |