Parcourir la source

Started implementing file system utilities

Thomas Buck il y a 10 ans
Parent
révision
5b458f42fe

+ 2
- 0
ChangeLog.md Voir le fichier

@@ -6,6 +6,8 @@
6 6
     * Script parser successfully loading level scripts
7 7
     * Can also read TR3 script now
8 8
     * Also working with TR2/TR3 PSX Script (TOMBPSX.DAT)
9
+    * Using getenv() instead of wordexp() for tilde expansion
10
+    * Started implementing file-system handling utilities
9 11
 
10 12
     [ 20140806 ]
11 13
     * Improved Script reader and its Unit Test

+ 2
- 0
TODO.md Voir le fichier

@@ -21,6 +21,8 @@
21 21
 
22 22
 ## Future Features
23 23
 
24
+* File system utility layer, so mixed-case on case-sensitive filesystems is working transparently
25
+* Add ability to play the FMVs. Format? VLC can play them!
24 26
 * Cut TGA image reader, currently only used for menu background?!
25 27
     * Need useful, always available image writer alternative for screenshots then
26 28
 * When cutscene rendering is working, use TR4/5 style menu?

+ 6
- 0
include/config.h.in Voir le fichier

@@ -24,6 +24,12 @@ extern const char *BUILD_HOST;
24 24
 #cmakedefine HAVE_CLOSEDIR
25 25
 #cmakedefine HAVE_DT_DIR
26 26
 
27
+#cmakedefine HAVE_UNISTD_H
28
+#cmakedefine HAVE_GETCWD
29
+
30
+#cmakedefine HAVE_STDLIB_H
31
+#cmakedefine HAVE_GETENV
32
+
27 33
 #cmakedefine HAVE_SYS_TIME_H
28 34
 #cmakedefine HAVE_GETTIMEOFDAY
29 35
 

+ 29
- 0
include/global.h Voir le fichier

@@ -133,6 +133,26 @@ template<typename T, typename U>
133 133
     abort();
134 134
 }
135 135
 
136
+template<typename T, typename U>
137
+[[noreturn]] void assertNotEqualImplementation(const char *exp, T a, U b, const char *file, int line, bool print) {
138
+    const unsigned int maxSize = 128;
139
+    void *callstack[maxSize];
140
+    int frames = backtrace(callstack, maxSize);
141
+    char **strs = backtrace_symbols(callstack, frames);
142
+
143
+    std::cout << std::endl << "assertion failed:" << std::endl;
144
+    std::cout << "\t" << exp << std::endl;
145
+    if (print)
146
+        std::cout << "\t (" << a << " == " << b << ")" << std::endl;
147
+    std::cout << "in " << file << ":" << line << std::endl << std::endl;
148
+
149
+    for (int i = 0; i < frames; i++)
150
+        std::cout << strs[i] << std::endl;
151
+
152
+    delete [] strs;
153
+    abort();
154
+}
155
+
136 156
 // Evaluating x or y could have side-effects
137 157
 // So we only do it once!
138 158
 
@@ -149,10 +169,18 @@ template<typename T, typename U>
149 169
         assertEqualImplementation(#x " == " #y, assertEvalTemp, assertEvalTemp2, __FILE__, __LINE__, true); \
150 170
 }
151 171
 
172
+#define assertNotEqual(x, y) { \
173
+    auto assertEvalTemp = x; \
174
+    auto assertEvalTemp2 = y; \
175
+    if (assertEvalTemp == assertEvalTemp2) \
176
+        assertNotEqualImplementation(#x " != " #y, assertEvalTemp, assertEvalTemp2, __FILE__, __LINE__, true); \
177
+}
178
+
152 179
 #else // NDEBUG
153 180
 
154 181
 #define assert(x)
155 182
 #define assertEqual(x, y)
183
+#define assertNotEqual(x, y)
156 184
 
157 185
 #endif // NDEBUG
158 186
 
@@ -161,6 +189,7 @@ template<typename T, typename U>
161 189
 // Fall back to the default C assert
162 190
 #include <cassert>
163 191
 #define assertEqual(x, y) assert((x) == (y))
192
+#define assertNotEqual(x, y) assert((x) != (y))
164 193
 
165 194
 #endif // EXECINFO
166 195
 

+ 30
- 0
include/utils/File.h Voir le fichier

@@ -0,0 +1,30 @@
1
+/*!
2
+ * \file include/utils/File.h
3
+ * \brief Recursive file-system walking utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _UTILS_FILE_H_
9
+#define _UTILS_FILE_H_
10
+
11
+#include <memory>
12
+#include <string>
13
+
14
+class Folder;
15
+
16
+class File {
17
+public:
18
+    File(std::string file);
19
+    ~File();
20
+
21
+    std::shared_ptr<Folder> getParent();
22
+
23
+private:
24
+    std::string name;
25
+    std::string path;
26
+    std::weak_ptr<Folder> parent;
27
+};
28
+
29
+#endif
30
+

+ 29
- 0
include/utils/Folder.h Voir le fichier

@@ -0,0 +1,29 @@
1
+/*!
2
+ * \file include/utils/FileSystem.h
3
+ * \brief Recursive file-system walking utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _UTILS_FOLDER_H_
9
+#define _UTILS_FOLDER_H_
10
+
11
+#include <memory>
12
+#include <string>
13
+#include <vector>
14
+
15
+class File;
16
+
17
+class Folder {
18
+public:
19
+    Folder(std::string folder);
20
+    ~Folder();
21
+
22
+private:
23
+    std::string name;
24
+    std::string path;
25
+    std::vector<std::shared_ptr<File>> files;
26
+};
27
+
28
+#endif
29
+

+ 18
- 0
include/utils/filesystem.h Voir le fichier

@@ -0,0 +1,18 @@
1
+/*!
2
+ * \file include/utils/filesystem.h
3
+ * \brief file-system utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _UTILS_FILESYSTEM_H_
9
+#define _UTILS_FILESYSTEM_H_
10
+
11
+#include <string>
12
+
13
+std::string getCurrentWorkingDirectory();
14
+
15
+std::string getHomeDirectory();
16
+
17
+#endif
18
+

+ 8
- 0
src/CMakeLists.txt Voir le fichier

@@ -121,6 +121,14 @@ check_function_exists (readdir_r HAVE_READDIR_R)
121 121
 check_function_exists (closedir HAVE_CLOSEDIR)
122 122
 check_symbol_exists (DT_DIR "dirent.h" HAVE_DT_DIR)
123 123
 
124
+# getcwd() for the current working directory
125
+check_include_files (unistd.h HAVE_UNISTD_H)
126
+check_function_exists (getcwd HAVE_GETCWD)
127
+
128
+# getenv() for reading environment variables
129
+check_include_files (stdlib.h HAVE_STDLIB_H)
130
+check_function_exists (getenv HAVE_GETENV)
131
+
124 132
 # gettimeofday() for timing
125 133
 check_include_files (sys/time.h HAVE_SYS_TIME_H)
126 134
 check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)

+ 2
- 0
src/Menu.cpp Voir le fichier

@@ -94,6 +94,7 @@ void Menu::loadPakFolderRecursive(const char *dir) {
94 94
                 if (stringEndsWith(lowerPath, ".phd")
95 95
                      || stringEndsWith(lowerPath, ".tr2")
96 96
                      || stringEndsWith(lowerPath, ".tr4")
97
+                    // || stringEndsWith(lowerPath, ".psx")
97 98
                      || stringEndsWith(lowerPath, ".trc")) {
98 99
                     int error = TombRaider::checkMime(fullPathMap);
99 100
                     if (error == 0) {
@@ -134,6 +135,7 @@ void Menu::loadPakFolderRecursive(const char *dir) {
134 135
                 if (stringEndsWith(lowerPath, ".phd")
135 136
                      || stringEndsWith(lowerPath, ".tr2")
136 137
                      || stringEndsWith(lowerPath, ".tr4")
138
+                    // || stringEndsWith(lowerPath, ".psx")
137 139
                      || stringEndsWith(lowerPath, ".trc")) {
138 140
                     int error = TombRaider::checkMime(fullPathMap);
139 141
                     if (error == 0) {

+ 3
- 0
src/utils/CMakeLists.txt Voir le fichier

@@ -1,5 +1,8 @@
1 1
 # Source files
2 2
 set (UTIL_SRCS ${UTIL_SRCS} "binary.cpp")
3
+set (UTIL_SRCS ${UTIL_SRCS} "File.cpp")
4
+set (UTIL_SRCS ${UTIL_SRCS} "filesystem.cpp")
5
+set (UTIL_SRCS ${UTIL_SRCS} "Folder.cpp")
3 6
 set (UTIL_SRCS ${UTIL_SRCS} "pcx.cpp")
4 7
 set (UTIL_SRCS ${UTIL_SRCS} "pixel.cpp")
5 8
 set (UTIL_SRCS ${UTIL_SRCS} "strings.cpp")

+ 19
- 0
src/utils/File.cpp Voir le fichier

@@ -0,0 +1,19 @@
1
+/*!
2
+ * \file src/utils/File.cpp
3
+ * \brief Recursive file-system walking utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "utils/File.h"
10
+#include "utils/Folder.h"
11
+
12
+File::File(std::string file) {
13
+
14
+}
15
+
16
+File::~File() {
17
+
18
+}
19
+

+ 19
- 0
src/utils/Folder.cpp Voir le fichier

@@ -0,0 +1,19 @@
1
+/*!
2
+ * \file src/utils/Folder.cpp
3
+ * \brief Recursive file-system walking utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "utils/File.h"
10
+#include "utils/Folder.h"
11
+
12
+Folder::Folder(std::string folder) {
13
+
14
+}
15
+
16
+Folder::~Folder() {
17
+
18
+}
19
+

+ 63
- 0
src/utils/filesystem.cpp Voir le fichier

@@ -0,0 +1,63 @@
1
+/*!
2
+ * \file src/utils/filesystem.cpp
3
+ * \brief file-system utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "utils/filesystem.h"
10
+
11
+#if defined(HAVE_UNISTD_H) && defined(HAVE_GETCWD)
12
+#include <unistd.h>
13
+#endif
14
+
15
+#if defined(HAVE_STDLIB_H) && defined(HAVE_GETENV)
16
+#include <stdlib.h>
17
+#endif
18
+
19
+#ifdef _WIN32
20
+#include <windows.h>
21
+#include <shlobj.h>
22
+#endif
23
+
24
+std::string getCurrentWorkingDirectory() {
25
+#if defined(HAVE_UNISTD_H) && defined(HAVE_GETCWD)
26
+
27
+    char path[1024];
28
+    assertEqual(getcwd(path, 1024), path);
29
+    return std::string(path);
30
+
31
+#else
32
+
33
+    assert(false);
34
+    return std::string();
35
+
36
+#endif
37
+}
38
+
39
+std::string getHomeDirectory() {
40
+#if defined(HAVE_STDLIB_H) && defined(HAVE_GETENV)
41
+
42
+    char *path = getenv("HOME");
43
+    assert(path != nullptr);
44
+    return path;
45
+
46
+#elif defined(_WIN32)
47
+
48
+    char path[MAX_PATH];
49
+    assertEqual(SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, 0, path), S_OK);
50
+    size_t lenPath = strlen(path);
51
+    for (unsigned int i = 0; i < lenPath; i++)
52
+        if (dir[i] == '\\')
53
+            dir[i] = '/';
54
+    return std::string(path);
55
+
56
+#else
57
+
58
+    assert(false);
59
+    return std::string();
60
+
61
+#endif
62
+}
63
+

+ 8
- 70
src/utils/strings.cpp Voir le fichier

@@ -10,17 +10,12 @@
10 10
 #include <stdio.h>
11 11
 #include <string.h>
12 12
 
13
-#if defined(unix) || defined(__APPLE__) || defined(__linux__)
14
-#include <wordexp.h>
15
-#elif defined(_WIN32)
16
-#include <Windows.h>
17
-#include <shlobj.h>
18
-#ifndef va_copy
13
+#if defined(_WIN32) && (!defined(va_copy))
19 14
 #define va_copy(d,s) ((d) = (s))
20 15
 #endif
21
-#endif
22 16
 
23 17
 #include "global.h"
18
+#include "utils/filesystem.h"
24 19
 #include "utils/strings.h"
25 20
 
26 21
 char *stringRemoveQuotes(const char *s) {
@@ -131,70 +126,13 @@ char *fullPath(const char *path, char end) {
131 126
     assert(path[0] != '\0');
132 127
 
133 128
     if (path[0] == '~') {
134
-#if defined(unix) || defined(__APPLE__) || defined(__linux__)
135
-
136
-        wordexp_t word;
137
-
138
-#ifdef __APPLE__
139
-        // Workunsigned long systemTimerStart = 0;around for Mac OS X. See:
140
-        // http://stackoverflow.com/questions/20534788/why-does-wordexp-fail-with-wrde-syntax-on-os-x
141
-        signal(SIGCHLD, SIG_DFL);
142
-#endif
143
-
144
-        // Expand string into segments
145
-        int res = wordexp(path, &word, 0);
146
-
147
-#ifdef __APPLE__
148
-        signal(SIGCHLD, SIG_IGN);
149
-#endif
150
-
151
-        if (res != 0) {
152
-            printf("fullPath> wordexp() failed: %d\n", res);
153
-            return NULL;
154
-        }
155
-
156
-        // Get length of complete string
157
-        lenPath = 0;
158
-        for (unsigned int i = 0; i < word.we_wordc; i++) {
159
-            lenPath += strlen(word.we_wordv[i]);
160
-        }
161
-
162
-        // Allocate new string
163
-        dir = new char[lenPath + 2]; // space for end char
164
-
165
-        // Copy segments into new string
166
-        size_t offset = 0;
167
-        for (unsigned int i = 0; i < word.we_wordc; i++) {
168
-            size_t len = strlen(word.we_wordv[i]);
169
-            strncpy(dir + offset, word.we_wordv[i], len);
170
-            offset += len;
171
-        }
172
-
173
-        wordfree(&word);
174
-#elif defined(_WIN32)
175
-        char newPath[MAX_PATH];
176
-        if (SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, 0, newPath) == S_OK) {
177
-            lenPath = strlen(newPath);
178
-            size_t lenPath2 = strlen(path);
179
-            dir = new char[lenPath + lenPath2 + 2]; // space for end char
180
-            strncpy(dir, newPath, lenPath);
181
-            strncpy((dir + lenPath), (path + 1), lenPath2 - 1);
182
-            lenPath += lenPath2 - 1;
183
-            for (unsigned int i = 0; i < lenPath; i++)
184
-                if(dir[i] == '\\')
185
-                    dir[i] = '/';
186
-        } else {
187
-            printf("WARNING: Could not get home folder location for tilde expansion!\n");
188
-            lenPath = strlen(path);
189
-            dir = new char[lenPath + 2]; // space for end char
190
-            strncpy(dir, path, lenPath);
191
-        }
192
-#else
193
-        printf("WARNING: Tilde expansion not supported on this platform:\n\t%s\n", path);
129
+        std::string home = getHomeDirectory();
130
+        assert(home.length() > 0);
194 131
         lenPath = strlen(path);
195
-        dir = new char[lenPath + 2]; // space for end char
196
-        strncpy(dir, path, lenPath);
197
-#endif
132
+        dir = new char[home.length() + lenPath + 3]; // space for end char
133
+        strncpy(dir, home.c_str(), home.length());
134
+        strncpy(dir + home.length(), path + 1, lenPath - 1);
135
+        lenPath = home.length() + lenPath - 1;
198 136
     } else {
199 137
         lenPath = strlen(path);
200 138
         dir = new char[lenPath + 2]; // space for end char

Chargement…
Annuler
Enregistrer