Pārlūkot izejas kodu

Created static utility library

Thomas Buck 10 gadus atpakaļ
vecāks
revīzija
d87735b48c
7 mainītis faili ar 296 papildinājumiem un 255 dzēšanām
  1. 1
    0
      include/OpenRaider.h
  2. 1
    49
      include/System.h
  3. 60
    0
      include/utils/strings.h
  4. 6
    0
      src/CMakeLists.txt
  5. 0
    206
      src/System.cpp
  6. 8
    0
      src/utils/CMakeLists.txt
  7. 220
    0
      src/utils/strings.cpp

+ 1
- 0
include/OpenRaider.h Parādīt failu

@@ -20,6 +20,7 @@
20 20
 #include "SDLSystem.h"
21 21
 #include "Network.h"
22 22
 #include "World.h"
23
+#include "utils/strings.h"
23 24
 
24 25
 /*!
25 26
  * \brief OpenRaider key events.

+ 1
- 49
include/System.h Parādīt failu

@@ -14,6 +14,7 @@
14 14
 #include <map>
15 15
 
16 16
 #include "Vector.h"
17
+#include "utils/strings.h"
17 18
 
18 19
 //! \todo Replace with unicode compatible key codes
19 20
 #define SYS_MOUSE_LEFT    6000
@@ -67,37 +68,6 @@ public:
67 68
     virtual ~System();
68 69
 
69 70
     /*!
70
-     * \brief Generates a buffered string for the printf call
71
-     * \param string Format string like for printf
72
-     * \returns string in a buffer
73
-     */
74
-    static char *bufferString(const char *string, ...) __attribute__((format(printf, 1, 2)));
75
-
76
-    /*!
77
-     * \brief Expansion of unix home enviroment char.
78
-     * Also makes sure string ends in "end" char.
79
-     * \param path path string
80
-     * \param end end character. 0 appends no additional char
81
-     * \returns allocated string of path with expansions
82
-     */
83
-    static char *fullPath(const char *path, char end);
84
-
85
-    /*!
86
-     * \brief Only returns last part of a path string.
87
-     * \param filename Path to a file
88
-     * \returns Name of the file in filename, without path in front
89
-     */
90
-    static char *getFileFromFullPath(char *filename);
91
-
92
-    /*!
93
-     * \brief Check if a string ends with another string.
94
-     * \param str string to check
95
-     * \param suffix suffix for which to check
96
-     * \returns true if str ends with suffix
97
-     */
98
-    static bool stringEndsWith(const char *str, const char *suffix);
99
-
100
-    /*!
101 71
      * \brief Gets the game tick
102 72
      * \returns number of milliseconds since start of program
103 73
      */
@@ -211,27 +181,9 @@ protected:
211 181
     unsigned int mConsoleKey;          //!< Console toggle event now handled lower
212 182
 };
213 183
 
214
-
215 184
 //! \todo Could make these static methods later, depends on API evolution
216 185
 
217 186
 /*!
218
- * \brief Checks if Command matches Symbol.
219
- * Returns the rest of the argument list back in command buffer, if any
220
- * \param symbol command string
221
- * \param command with arguments
222
- * \returns true if command matches symbol
223
- */
224
-bool rc_command(const char *symbol, char *command);
225
-
226
-/*!
227
- * \brief Interpret a string as a bool
228
- * \param buffer "true" or "false"
229
- * \param val is set to boolean interpretation of buffer
230
- * \returns -1 for null string, -2 if string is not "true" or "false"
231
- */
232
-int rc_get_bool(char *buffer, bool *val);
233
-
234
-/*!
235 187
  * \brief Sets timer state and returns number of ticks
236 188
  * \param state 0 - reset, 1 - get number of ticks
237 189
  * \returns number of ticks

+ 60
- 0
include/utils/strings.h Parādīt failu

@@ -0,0 +1,60 @@
1
+/*!
2
+ * \file include/utils/strings.h
3
+ * \brief String handling utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _UTILS_STRINGS_H_
9
+#define _UTILS_STRINGS_H_
10
+
11
+
12
+/*!
13
+ * \brief Generates a buffered string for the printf call
14
+ * \param string Format string like for printf
15
+ * \returns string in a buffer
16
+ */
17
+char *bufferString(const char *string, ...) __attribute__((format(printf, 1, 2)));
18
+
19
+/*!
20
+ * \brief Expansion of unix home enviroment char.
21
+ * Also makes sure string ends in "end" char.
22
+ * \param path path string
23
+ * \param end end character. 0 appends no additional char
24
+ * \returns allocated string of path with expansions
25
+ */
26
+char *fullPath(const char *path, char end);
27
+
28
+/*!
29
+ * \brief Only returns last part of a path string.
30
+ * \param filename Path to a file
31
+ * \returns Name of the file in filename, without path in front
32
+ */
33
+char *getFileFromFullPath(char *filename);
34
+
35
+/*!
36
+ * \brief Check if a string ends with another string.
37
+ * \param str string to check
38
+ * \param suffix suffix for which to check
39
+ * \returns true if str ends with suffix
40
+ */
41
+bool stringEndsWith(const char *str, const char *suffix);
42
+
43
+/*!
44
+ * \brief Checks if Command matches Symbol.
45
+ * Returns the rest of the argument list back in command buffer, if any
46
+ * \param symbol command string
47
+ * \param command with arguments
48
+ * \returns true if command matches symbol
49
+ */
50
+bool rc_command(const char *symbol, char *command);
51
+
52
+/*!
53
+ * \brief Interpret a string as a bool
54
+ * \param buffer "true" or "false"
55
+ * \param val is set to boolean interpretation of buffer
56
+ * \returns -1 for null string, -2 if string is not "true" or "false"
57
+ */
58
+int rc_get_bool(char *buffer, bool *val);
59
+
60
+#endif

+ 6
- 0
src/CMakeLists.txt Parādīt failu

@@ -50,6 +50,9 @@ endif (APPLE)
50 50
 
51 51
 #################################################################
52 52
 
53
+# Add utils directory
54
+add_subdirectory (utils)
55
+
53 56
 # Add Executable
54 57
 add_executable (OpenRaider MACOSX_BUNDLE ${SRCS})
55 58
 
@@ -165,6 +168,9 @@ set (LIBS ${LIBS} ${ZLIB_LIBRARIES})
165 168
 find_package (Threads REQUIRED)
166 169
 set (LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT})
167 170
 
171
+# Add utils Library
172
+set (LIBS ${LIBS} utilities)
173
+
168 174
 # Link to all found libs
169 175
 target_link_libraries (OpenRaider ${LIBS})
170 176
 

+ 0
- 206
src/System.cpp Parādīt failu

@@ -73,163 +73,6 @@ System::~System()
73 73
 // Public Accessors
74 74
 ////////////////////////////////////////////////////////////
75 75
 
76
-bool System::stringEndsWith(const char *str, const char *suffix) {
77
-    if (!str || !suffix)
78
-        return false;
79
-
80
-    size_t lenstr = strlen(str);
81
-    size_t lensuffix = strlen(suffix);
82
-
83
-    if (lensuffix > lenstr)
84
-        return false;
85
-
86
-    return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
87
-}
88
-
89
-char *System::bufferString(const char *string, ...)
90
-{
91
-    int sz = 60;
92
-    int n;
93
-    char *text;
94
-    va_list args;
95
-
96
-
97
-    // Mongoose 2002.01.01, Only allow valid strings
98
-    //   we must assume it's NULL terminated also if it passes...
99
-    if (!string || !string[0])
100
-    {
101
-        return NULL;
102
-    }
103
-
104
-    text = new char[sz];
105
-
106
-    va_start(args, string);
107
-
108
-    // Mongoose 2002.01.01, Get exact size needed if the first try fails
109
-    n = vsnprintf(text, sz, string, args);
110
-
111
-    // Mongoose 2002.01.01, Realloc correct amount if truncated
112
-    while (1)
113
-    {
114
-        if (n > -1 && n < sz)
115
-        {
116
-            break;
117
-        }
118
-
119
-        // Mongoose 2002.01.01, For glibc 2.1
120
-        if (n > -1)
121
-        {
122
-            sz = n + 1;
123
-            delete [] text;
124
-            text = new char[sz];
125
-            n = vsnprintf(text, sz, string, args);
126
-            break;
127
-        }
128
-        else // glibc 2.0
129
-        {
130
-            sz *= 2;
131
-            delete [] text;
132
-            text = new char[sz];
133
-            n = vsnprintf(text, sz, string, args);
134
-        }
135
-    }
136
-
137
-    va_end(args);
138
-
139
-    return text;
140
-}
141
-
142
-
143
-char *System::fullPath(const char *path, char end)
144
-{
145
-    unsigned int i, lenPath, lenEnv, len;
146
-    char *env, *dir;
147
-
148
-
149
-    if (!path || !path[0])
150
-        return 0;
151
-
152
-    if (path[0] == '~')
153
-    {
154
-#if defined(unix) || defined(__APPLE__)
155
-        env = getenv("HOME");
156
-
157
-        if (!env || !env[0])
158
-        {
159
-            return 0;
160
-        }
161
-
162
-        lenEnv = strlen(env);
163
-        lenPath = strlen(path);
164
-        len = lenEnv + lenPath;
165
-
166
-        dir = new char[len+1];
167
-
168
-        // Mongoose 2002.08.17, Copy ENV,  strip '~', append rest of path
169
-        for (i = 0; i < len; ++i)
170
-        {
171
-            if (i < lenEnv)
172
-            {
173
-                dir[i] = env[i];
174
-            }
175
-            else
176
-            {
177
-                dir[i] = path[1+(i-lenEnv)];
178
-            }
179
-        }
180
-#else
181
-#error Platform not supported!
182
-#endif
183
-    }
184
-    else
185
-    {
186
-        lenPath = strlen(path);
187
-        dir = new char[lenPath+1];
188
-        strncpy(dir, path, lenPath);
189
-
190
-        i = lenPath;
191
-    }
192
-
193
-    // Make sure ends in "end" char
194
-    if (end && dir[i-1] != end)
195
-    {
196
-        dir[i++] = end;
197
-    }
198
-
199
-    dir[i] = 0;
200
-
201
-    return dir;
202
-}
203
-
204
-
205
-char *System::getFileFromFullPath(char *filename)
206
-{
207
-    int i, j, len;
208
-    char *str;
209
-
210
-
211
-    len = strlen(filename);
212
-
213
-    for (i = len, j = 0; i > 0; --i, ++j)
214
-    {
215
-        if (filename[i] == '/' || filename[i] == '\\')
216
-            break;
217
-    }
218
-
219
-    j--;
220
-
221
-    str = new char[len - j + 1];
222
-
223
-    for (i = 0; i < len - j; ++i)
224
-    {
225
-        str[i] = filename[i + len - j];
226
-    }
227
-
228
-    str[i] = 0;
229
-
230
-    return str;
231
-}
232
-
233 76
 
234 77
 unsigned int System::getTicks()
235 78
 {
@@ -568,55 +411,6 @@ void System::resizeGL(unsigned int w, unsigned int h)
568 411
 // Gobal helper functions
569 412
 ////////////////////////////////////////////////////////////
570 413
 
571
-// Mongoose 2002.03.23, Checks command to see if it's same
572
-//   as symbol, then returns the arg list in command buffer
573
-bool rc_command(const char *symbol, char *command)
574
-{
575
-    int i, j, lens, lenc;
576
-
577
-
578
-    if (!symbol || !symbol[0] || !command || !command[0])
579
-    {
580
-        return false;
581
-    }
582
-
583
-    lens = strlen(symbol);
584
-
585
-    if (strncmp(command, symbol, lens) == 0)
586
-    {
587
-        lenc = strlen(command);
588
-
589
-        // lens+1 skips '=' or ' '
590
-        for (i = 0, j = lens+1; j < lenc; ++i, ++j)
591
-        {
592
-            command[i] = command[j];
593
-            command[i+1] = 0;
594
-        }
595
-
596
-        return true;
597
-    }
598
-
599
-    return false;
600
-}
601
-
602
-
603
-int rc_get_bool(char *buffer, bool *val)
604
-{
605
-    if (!buffer || !buffer[0])
606
-    {
607
-        return -1;
608
-    }
609
-
610
-    if ((strncmp(buffer, "true", 4) == 0) || (buffer[0] == '1'))
611
-        *val = true;
612
-    else if ((strncmp(buffer, "false", 5) == 0) || (buffer[0] == '0'))
613
-        *val = false;
614
-    else
615
-        return -2;
616
-
617
-    return 0;
618
-}
619
-
620 414
 
621 415
 unsigned int system_timer(int state)
622 416
 {

+ 8
- 0
src/utils/CMakeLists.txt Parādīt failu

@@ -0,0 +1,8 @@
1
+# Source files
2
+set (UTIL_SRCS ${UTIL_SRCS} "strings.cpp")
3
+
4
+# Include directory
5
+include_directories ("${PROJECT_SOURCE_DIR}/include")
6
+
7
+# Add library
8
+add_library (utilities ${UTIL_SRCS})

+ 220
- 0
src/utils/strings.cpp Parādīt failu

@@ -0,0 +1,220 @@
1
+/*!
2
+ * \file include/utils/strings.h
3
+ * \brief String handling utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include <cstdarg>
9
+#include <cstdlib>
10
+#include <stdio.h>
11
+#include <string.h>
12
+
13
+#include "utils/strings.h"
14
+
15
+bool stringEndsWith(const char *str, const char *suffix) {
16
+    if (!str || !suffix)
17
+        return false;
18
+
19
+    size_t lenstr = strlen(str);
20
+    size_t lensuffix = strlen(suffix);
21
+
22
+    if (lensuffix > lenstr)
23
+        return false;
24
+
25
+    return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
26
+}
27
+
28
+char *bufferString(const char *string, ...)
29
+{
30
+    int sz = 60;
31
+    int n;
32
+    char *text;
33
+    va_list args;
34
+
35
+
36
+    // Mongoose 2002.01.01, Only allow valid strings
37
+    //   we must assume it's NULL terminated also if it passes...
38
+    if (!string || !string[0])
39
+    {
40
+        return NULL;
41
+    }
42
+
43
+    text = new char[sz];
44
+
45
+    va_start(args, string);
46
+
47
+    // Mongoose 2002.01.01, Get exact size needed if the first try fails
48
+    n = vsnprintf(text, sz, string, args);
49
+
50
+    // Mongoose 2002.01.01, Realloc correct amount if truncated
51
+    while (1)
52
+    {
53
+        if (n > -1 && n < sz)
54
+        {
55
+            break;
56
+        }
57
+
58
+        // Mongoose 2002.01.01, For glibc 2.1
59
+        if (n > -1)
60
+        {
61
+            sz = n + 1;
62
+            delete [] text;
63
+            text = new char[sz];
64
+            n = vsnprintf(text, sz, string, args);
65
+            break;
66
+        }
67
+        else // glibc 2.0
68
+        {
69
+            sz *= 2;
70
+            delete [] text;
71
+            text = new char[sz];
72
+            n = vsnprintf(text, sz, string, args);
73
+        }
74
+    }
75
+
76
+    va_end(args);
77
+
78
+    return text;
79
+}
80
+
81
+
82
+char *fullPath(const char *path, char end)
83
+{
84
+    unsigned int i, lenPath, lenEnv, len;
85
+    char *env, *dir;
86
+
87
+
88
+    if (!path || !path[0])
89
+        return 0;
90
+
91
+    if (path[0] == '~')
92
+    {
93
+#if defined(unix) || defined(__APPLE__)
94
+        env = getenv("HOME");
95
+
96
+        if (!env || !env[0])
97
+        {
98
+            return 0;
99
+        }
100
+
101
+        lenEnv = strlen(env);
102
+        lenPath = strlen(path);
103
+        len = lenEnv + lenPath;
104
+
105
+        dir = new char[len+1];
106
+
107
+        // Mongoose 2002.08.17, Copy ENV,  strip '~', append rest of path
108
+        for (i = 0; i < len; ++i)
109
+        {
110
+            if (i < lenEnv)
111
+            {
112
+                dir[i] = env[i];
113
+            }
114
+            else
115
+            {
116
+                dir[i] = path[1+(i-lenEnv)];
117
+            }
118
+        }
119
+#else
120
+#error Platform not supported!
121
+#endif
122
+    }
123
+    else
124
+    {
125
+        lenPath = strlen(path);
126
+        dir = new char[lenPath+1];
127
+        strncpy(dir, path, lenPath);
128
+
129
+        i = lenPath;
130
+    }
131
+
132
+    // Make sure ends in "end" char
133
+    if (end && dir[i-1] != end)
134
+    {
135
+        dir[i++] = end;
136
+    }
137
+
138
+    dir[i] = 0;
139
+
140
+    return dir;
141
+}
142
+
143
+
144
+char *getFileFromFullPath(char *filename)
145
+{
146
+    int i, j, len;
147
+    char *str;
148
+
149
+
150
+    len = strlen(filename);
151
+
152
+    for (i = len, j = 0; i > 0; --i, ++j)
153
+    {
154
+        if (filename[i] == '/' || filename[i] == '\\')
155
+            break;
156
+    }
157
+
158
+    j--;
159
+
160
+    str = new char[len - j + 1];
161
+
162
+    for (i = 0; i < len - j; ++i)
163
+    {
164
+        str[i] = filename[i + len - j];
165
+    }
166
+
167
+    str[i] = 0;
168
+
169
+    return str;
170
+}
171
+
172
+// Mongoose 2002.03.23, Checks command to see if it's same
173
+//   as symbol, then returns the arg list in command buffer
174
+bool rc_command(const char *symbol, char *command)
175
+{
176
+    int i, j, lens, lenc;
177
+
178
+
179
+    if (!symbol || !symbol[0] || !command || !command[0])
180
+    {
181
+        return false;
182
+    }
183
+
184
+    lens = strlen(symbol);
185
+
186
+    if (strncmp(command, symbol, lens) == 0)
187
+    {
188
+        lenc = strlen(command);
189
+
190
+        // lens+1 skips '=' or ' '
191
+        for (i = 0, j = lens+1; j < lenc; ++i, ++j)
192
+        {
193
+            command[i] = command[j];
194
+            command[i+1] = 0;
195
+        }
196
+
197
+        return true;
198
+    }
199
+
200
+    return false;
201
+}
202
+
203
+
204
+int rc_get_bool(char *buffer, bool *val)
205
+{
206
+    if (!buffer || !buffer[0])
207
+    {
208
+        return -1;
209
+    }
210
+
211
+    if ((strncmp(buffer, "true", 4) == 0) || (buffer[0] == '1'))
212
+        *val = true;
213
+    else if ((strncmp(buffer, "false", 5) == 0) || (buffer[0] == '0'))
214
+        *val = false;
215
+    else
216
+        return -2;
217
+
218
+    return 0;
219
+}
220
+

Notiek ielāde…
Atcelt
Saglabāt