src/stringtable.cxx

changeset 72
03e4d9db3fd9
parent 71
11f23fabf8a6
child 73
1ee9b312dc18
equal deleted inserted replaced
71:11f23fabf8a6 72:03e4d9db3fd9
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 __STRINGTABLE_CXX__
42 #include <stdio.h> 1 #include <stdio.h>
43 #include <stdlib.h> 2 #include <stdlib.h>
44 #include <string.h> 3 #include <string.h>
45 #include "common.h" 4 #include "main.h"
46 #include "bots.h" 5 #include "bots.h"
47 #include "stringtable.h" 6 #include "stringtable.h"
48 7
8 static string_list g_string_table;
9
49 // ============================================================================ 10 // ============================================================================
50 // Initializes the string table 11 //
51 void InitStringTable() { 12 const string_list& get_string_table()
52 // Zero out everything first. 13 {
53 for (unsigned int a = 0; a < MAX_LIST_STRINGS; a++) 14 return g_string_table;
54 for (unsigned int b = 0; b < MAX_STRING_LENGTH; b++)
55 g_StringTable[a][b] = 0;
56 } 15 }
57 16
58 // ============================================================================ 17 // ============================================================================
59 // Potentially adds a string to the table and returns the index of it. 18 // Potentially adds a string to the table and returns the index of it.
60 unsigned int PushToStringTable (char* s) { 19 //
20 int get_string_table_index (const string& a)
21 {
61 // Must not be too long. 22 // Must not be too long.
62 if (strlen (s) >= MAX_STRING_LENGTH) 23 if (strlen (a) >= MAX_STRING_LENGTH)
63 error ("string `%s` too long (%d characters max)\n", s, strlen (s)); 24 error ("string `%s` too long (%d characters, max is %d)\n",
64 25 a.c_str(), a.length(), MAX_STRING_LENGTH);
65 // Find a free slot in the table. 26
66 unsigned int a; 27 // Find a free slot in the table.
67 for (a = 0; a < MAX_LIST_STRINGS; a++) { 28 int idx;
29
30 for (idx = 0; idx < g_string_table.size(); idx++)
31 {
68 // String is already in the table, thus return it. 32 // String is already in the table, thus return it.
69 if (!strcmp (s, g_StringTable[a])) 33 if (g_string_table[idx] == a)
70 return a; 34 return idx;
71
72 // String is empty, thus it's free.
73 if (!strlen (g_StringTable[a]))
74 break;
75 } 35 }
76 36
77 // no free slots! 37 // Check if the table is already full
78 if (a == MAX_LIST_STRINGS) 38 if (g_string_table.size() == MAX_LIST_STRINGS - 1)
79 error ("too many strings defined!\n"); 39 error ("too many strings!\n");
80 40
81 // Determine the length
82 size_t l1 = strlen (s);
83 size_t l2 = MAX_LIST_STRINGS - 1;
84 size_t len = (l1 < l2) ? l1 : l2;
85
86 // Now, dump the string into the slot 41 // Now, dump the string into the slot
87 strncpy (g_StringTable[a], s, len); 42 g_string_table[idx] = a;
88 g_StringTable[a][len] = 0; 43
89 44 return idx;
90 return a;
91 } 45 }
92 46
93 // ============================================================================ 47 // ============================================================================
94 // Counts the amount of strings in the table. 48 // Counts the amount of strings in the table.
95 unsigned int CountStringTable () { 49 //
96 unsigned int count = 0; 50 int num_strings_in_table()
97 for (unsigned int a = 0; a < MAX_LIST_STRINGS; a++) { 51 {
98 if (!strlen (g_StringTable[a])) 52 return g_string_table.size();
99 break;
100 else
101 count++;
102 }
103 return count;
104 } 53 }

mercurial