Parcourir la source

Fixed fullPath()

Thomas Buck il y a 10 ans
Parent
révision
34fda27205
4 fichiers modifiés avec 39 ajouts et 42 suppressions
  1. 2
    1
      ChangeLog
  2. 1
    1
      src/OpenRaider.cpp
  3. 1
    1
      src/System.cpp
  4. 35
    39
      src/utils/strings.cpp

+ 2
- 1
ChangeLog Voir le fichier

@@ -7,7 +7,8 @@
7 7
 
8 8
 	[ 20140306 ]
9 9
 	* Created utility library
10
-	* rewrote bufferString()
10
+	* Rewrote bufferString()
11
+	* Rewrote and fixed fullPath(), it worked more or less by accident
11 12
 
12 13
 	[ 20140305 ]
13 14
 	* Now using cmake build system

+ 1
- 1
src/OpenRaider.cpp Voir le fichier

@@ -3415,7 +3415,7 @@ void OpenRaider::handleCommand(char *cmd, unsigned int mode)
3415 3415
             }
3416 3416
             else if (rc_command("Font", cmd))
3417 3417
             {
3418
-                gFontFilename = fullPath(cmd, '/');
3418
+                gFontFilename = fullPath(cmd, 0);
3419 3419
             }
3420 3420
             else if (rc_command("Driver", cmd))
3421 3421
             {

+ 1
- 1
src/System.cpp Voir le fichier

@@ -221,7 +221,7 @@ int System::loadResourceFile(const char *filename)
221 221
 
222 222
                         printf("Importing '%s'\n", buffer);
223 223
 
224
-                        loadResourceFile(fullPath(buffer, '/'));
224
+                        loadResourceFile(fullPath(buffer, 0));
225 225
                     }
226 226
                 }
227 227
                 else

+ 35
- 39
src/utils/strings.cpp Voir le fichier

@@ -10,6 +10,10 @@
10 10
 #include <stdio.h>
11 11
 #include <string.h>
12 12
 
13
+#if defined(unix) || defined(__APPLE__)
14
+#include <wordexp.h>
15
+#endif
16
+
13 17
 #include "utils/strings.h"
14 18
 
15 19
 bool stringEndsWith(const char *str, const char *suffix) {
@@ -56,64 +60,56 @@ char *bufferString(const char *string, ...) {
56 60
 }
57 61
 
58 62
 
59
-char *fullPath(const char *path, char end)
60
-{
61
-    unsigned int i, lenPath, lenEnv, len;
62
-    char *env, *dir;
63
-
63
+char *fullPath(const char *path, char end) {
64
+    unsigned int lenPath, offset;
65
+    wordexp_t word;
66
+    char *dir;
64 67
 
65 68
     if (!path || !path[0])
66
-        return 0;
69
+        return NULL;
67 70
 
68
-    if (path[0] == '~')
69
-    {
71
+    if (path[0] == '~') {
70 72
 #if defined(unix) || defined(__APPLE__)
71
-        env = getenv("HOME");
72
-
73
-        if (!env || !env[0])
74
-        {
75
-            return 0;
73
+        // Expand string into segments
74
+        if (wordexp(path, &word, 0) != 0) {
75
+            return NULL;
76 76
         }
77 77
 
78
-        lenEnv = strlen(env);
79
-        lenPath = strlen(path);
80
-        len = lenEnv + lenPath;
78
+        // Get length of complete string
79
+        lenPath = 0;
80
+        for (unsigned int i = 0; i < word.we_wordc; i++) {
81
+            lenPath += strlen(word.we_wordv[i]);
82
+        }
81 83
 
82
-        dir = new char[len+1];
84
+        // Allocate new string
85
+        dir = new char[lenPath + 2]; // space for end char
83 86
 
84
-        // Mongoose 2002.08.17, Copy ENV,  strip '~', append rest of path
85
-        for (i = 0; i < len; ++i)
86
-        {
87
-            if (i < lenEnv)
88
-            {
89
-                dir[i] = env[i];
90
-            }
91
-            else
92
-            {
93
-                dir[i] = path[1+(i-lenEnv)];
94
-            }
87
+        // Copy segments into new string
88
+        offset = 0;
89
+        for (unsigned int i = 0; i < word.we_wordc; i++) {
90
+            unsigned int len = strlen(word.we_wordv[i]);
91
+            strncpy(dir + offset, word.we_wordv[i], len);
92
+            offset += len;
95 93
         }
94
+
95
+        wordfree(&word);
96 96
 #else
97 97
 #error Platform not supported!
98 98
 #endif
99
-    }
100
-    else
101
-    {
99
+    } else {
102 100
         lenPath = strlen(path);
103
-        dir = new char[lenPath+1];
101
+        dir = new char[lenPath + 2]; // space for end char
104 102
         strncpy(dir, path, lenPath);
105
-
106
-        i = lenPath;
107 103
     }
108 104
 
109 105
     // Make sure ends in "end" char
110
-    if (end && dir[i-1] != end)
111
-    {
112
-        dir[i++] = end;
106
+    if (end && (dir[lenPath - 1] != end)) {
107
+        dir[lenPath] = end;
108
+        dir[lenPath + 1] = 0;
109
+    } else {
110
+        dir[lenPath] = 0;
113 111
     }
114 112
 
115
-    dir[i] = 0;
116
-
117 113
     return dir;
118 114
 }
119 115
 

Chargement…
Annuler
Enregistrer