Browse Source

Rewrote bufferString()

Thomas Buck 10 years ago
parent
commit
9bec5d5bd9
2 changed files with 13 additions and 32 deletions
  1. 4
    0
      ChangeLog
  2. 9
    32
      src/utils/strings.cpp

+ 4
- 0
ChangeLog View File

@@ -5,6 +5,10 @@
5 5
 
6 6
  OpenRaider (0.1.2) xythobuz <xythobuz@xythobuz.de>
7 7
 
8
+	[ 20140306 ]
9
+	* Created utility library
10
+	* rewrote bufferString()
11
+
8 12
 	[ 20140305 ]
9 13
 	* Now using cmake build system
10 14
 	* Pak file extensions are checked to see if they could be valid

+ 9
- 32
src/utils/strings.cpp View File

@@ -25,52 +25,29 @@ bool stringEndsWith(const char *str, const char *suffix) {
25 25
     return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
26 26
 }
27 27
 
28
-char *bufferString(const char *string, ...)
29
-{
28
+char *bufferString(const char *string, ...) {
30 29
     int sz = 60;
31 30
     int n;
32 31
     char *text;
33 32
     va_list args;
34 33
 
35
-
36
-    // Mongoose 2002.01.01, Only allow valid strings
37
-    //   we must assume it's NULL terminated also if it passes...
38 34
     if (!string || !string[0])
39
-    {
40 35
         return NULL;
41
-    }
42 36
 
43 37
     text = new char[sz];
44 38
 
45 39
     va_start(args, string);
46 40
 
47
-    // Mongoose 2002.01.01, Get exact size needed if the first try fails
48 41
     n = vsnprintf(text, sz, string, args);
49 42
 
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
-        }
43
+    if (n < 0) {
44
+        delete [] text;
45
+        return NULL; // encoding error
46
+    } else if (n >= sz) {
47
+        sz = n + 1; // buffer too small
48
+        delete [] text;
49
+        text = new char[sz];
50
+        n = vsnprintf(text, sz, string, args);
74 51
     }
75 52
 
76 53
     va_end(args);

Loading…
Cancel
Save