stringtable.cxx

Sun, 29 Jul 2012 04:02:07 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 29 Jul 2012 04:02:07 +0300
changeset 36
a8838b5f1213
parent 21
ae602e667879
child 38
e4bbd540663b
permissions
-rw-r--r--

Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!

20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * botc source code
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012 Santeri `Dusk` Piippo
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 * All rights reserved.
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 *
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 *
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright notice,
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * this list of conditions and the following disclaimer.
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * this list of conditions and the following disclaimer in the documentation
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * and/or other materials provided with the distribution.
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 * 3. Neither the name of the developer nor the names of its contributors may
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * be used to endorse or promote products derived from this software without
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * specific prior written permission.
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 * 4. Redistributions in any form must be accompanied by information on how to
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 * obtain complete source code for the software and any accompanying
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 * software that uses the software. The source code must either be included
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 * in the distribution or be available for no more than the cost of
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 * distribution plus a nominal fee, and must be freely redistributable
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 * under reasonable conditions. For an executable file, complete source
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 * code means the source code for all modules it contains. It does not
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 * include source code for modules or files that typically accompany the
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 * major components of the operating system on which the executable file
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 * runs.
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 *
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 * POSSIBILITY OF SUCH DAMAGE.
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 */
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 #define __STRINGTABLE_CXX__
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 #include <stdio.h>
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 #include <stdlib.h>
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 #include <string.h>
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 #include "common.h"
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 #include "bots.h"
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 #include "stringtable.h"
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
49 // ============================================================================
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
50 // Initializes the string table
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
51 void InitStringTable() {
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 // Zero out everything first.
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 for (unsigned int a = 0; a < MAX_LIST_STRINGS; a++)
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 for (unsigned int b = 0; b < MAX_STRING_LENGTH; b++)
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
55 g_StringTable[a][b] = 0;
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 }
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
58 // ============================================================================
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
59 // Potentially adds a string to the table and returns the index of it.
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
60 unsigned int PushToStringTable (char* s) {
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 // Find a free slot in the table.
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 unsigned int a;
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 for (a = 0; a < MAX_LIST_STRINGS; a++) {
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
64 // String is already in the table, thus return it.
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
65 if (!strcmp (s, g_StringTable[a]))
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 return a;
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 // String is empty, thus it's free.
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
69 if (!strlen (g_StringTable[a]))
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 break;
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 }
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 // no free slots!
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 if (a == MAX_LIST_STRINGS)
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 error ("too many strings defined!");
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
77 // Determine the length
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
78 size_t l1 = strlen (s);
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
79 size_t l2 = MAX_LIST_STRINGS - 1;
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
80 size_t len = (l1 < l2) ? l1 : l2;
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
81
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 // Now, dump the string into the slot
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
83 strncpy (g_StringTable[a], s, len);
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
84 g_StringTable[a][len] = 0;
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 return a;
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 }
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
89 // ============================================================================
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 21
diff changeset
90 // Counts the amount of strings in the table.
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
91 unsigned int CountStringTable () {
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 unsigned int count = 0;
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 for (unsigned int a = 0; a < MAX_LIST_STRINGS; a++) {
21
ae602e667879 Added -l command line parameter, if given, botc will list all available commands and exit.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
94 if (!strlen (g_StringTable[a]))
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 break;
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 else
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 count++;
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 }
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 return count;
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 }

mercurial