Browse Source

Removed C-Style string handling code.

This resolves #4 as bufferString() no longer exists!
Thomas Buck 9 years ago
parent
commit
6541e0c674

+ 1
- 0
ChangeLog.md View File

@@ -5,6 +5,7 @@
5 5
     [ 20140901 ]
6 6
     * Created abstract UI class handling “windows” like menu and console. Windows
7 7
       can be stacked arbitrarily. The top most gets keyboard/mouse/action events.
8
+    * Removed old C-String handling utility methods, now using std::string
8 9
 
9 10
     [ 20140831 ]
10 11
     * Moved command specific code from OpenRaider to static Command methods

+ 0
- 5
TODO.md View File

@@ -4,7 +4,6 @@
4 4
 
5 5
 * Move to newer OpenGL (GL ES or 2.1 or 3.x or 4.x?)
6 6
 * Don't use C-Style code, try to replace with C++ lib
7
-    * Use std::strings
8 7
 * Mesh has 2 different approaches of storing the same data (eg. mColors and mColorArray), but half of ‘em isn’t implemented. Unify this, probably even combining Mesh and StaticMesh...
9 8
 * Don’t use float everywhere just because (eg. float colors)
10 9
 * Add verbose command line flag for debug output also in release builds
@@ -31,7 +30,3 @@
31 30
 * Cut TGA image reader, currently only used for menu background?!
32 31
     * Need useful, always available image writer alternative for screenshots then
33 32
 
34
-* Create abstract interface from Game/Menu/Console, use this to do real “windows” that can be layered independently and can receive keyboard/mouse input
35
-* Put keyboard/mouse/windowing logic into UI or UIManager class
36
-* Move small rest of OpenRaider into main or a config class or something, remove OpenRaider class
37
-

+ 2
- 2
include/Font.h View File

@@ -21,7 +21,7 @@ public:
21 21
      */
22 22
     virtual ~Font();
23 23
 
24
-    virtual void setFont(const char *font);
24
+    virtual void setFont(std::string font);
25 25
 
26 26
     virtual int initialize() = 0;
27 27
 
@@ -41,7 +41,7 @@ public:
41 41
 
42 42
 protected:
43 43
     bool mFontInit;
44
-    char *mFontName;
44
+    std::string mFontName;
45 45
 };
46 46
 
47 47
 Font &getFont();

+ 2
- 2
include/FontManager.h View File

@@ -39,11 +39,11 @@ public:
39 39
 
40 40
 private:
41 41
 
42
-    void add(Font *f, const char *e);
42
+    void add(Font *f, std::string e);
43 43
 
44 44
     int font;
45 45
     std::vector<Font *> fonts;
46
-    std::vector<const char *> extensions;
46
+    std::vector<std::string> extensions;
47 47
 };
48 48
 
49 49
 #endif

+ 1
- 1
include/FontTRLE.h View File

@@ -40,7 +40,7 @@ public:
40 40
 
41 41
 private:
42 42
 
43
-    void loadLPS(const char *f);
43
+    void loadLPS(std::string f);
44 44
     void writeChar(unsigned int index, unsigned int xDraw, unsigned int yDraw,
45 45
             float scale, const unsigned char color[4]);
46 46
 

+ 15
- 14
include/OpenRaider.h View File

@@ -16,37 +16,38 @@
16 16
 class OpenRaider {
17 17
 public:
18 18
 
19
-    /*!
20
-     * \brief Constructs an object of OpenRaider
21
-     */
22 19
     OpenRaider();
23 20
 
24
-    /*!
25
-     * \brief Deconstructs an object of OpenRaider
26
-     */
27
-    ~OpenRaider();
28
-
29 21
     int initialize();
30 22
 
31 23
     /*!
32 24
      * \brief Load the configuration file
33 25
      * \returns 0 on success
34 26
      */
35
-    int loadConfig(const char *config);
27
+    int loadConfig(std::string config);
36 28
 
37
-    int command(std::string command);
29
+    std::string getBaseDir();
30
+    void setBaseDir(std::string dir);
31
+    std::string getPakDir();
32
+    void setPakDir(std::string dir);
33
+    std::string getAudioDir();
34
+    void setAudioDir(std::string dir);
35
+    std::string getDataDir();
36
+    void setDataDir(std::string dir);
38 37
 
39 38
     void run();
40 39
     void frame();
41 40
 
42 41
     //! \fixme should be private
43
-    char *mBaseDir;
44
-    char *mPakDir;
45
-    char *mAudioDir;
46
-    char *mDataDir;
47 42
     KeyboardButton keyBindings[ActionEventCount];
48 43
     bool mRunning;
49 44
     bool mFPS;
45
+
46
+private:
47
+    std::string baseDir;
48
+    std::string pakDir;
49
+    std::string audioDir;
50
+    std::string dataDir;
50 51
 };
51 52
 
52 53
 OpenRaider &getOpenRaider();

+ 4
- 29
include/utils/strings.h View File

@@ -3,43 +3,18 @@
3 3
  * \brief String handling utilities
4 4
  *
5 5
  * \author xythobuz
6
- * \author Mongoose
7 6
  */
8 7
 
9 8
 #ifndef _UTILS_STRINGS_H_
10 9
 #define _UTILS_STRINGS_H_
11 10
 
12
-#include <cstdarg>
11
+#include <string>
13 12
 
14
-char *stringRemoveQuotes(const char *s);
13
+std::string findAndReplace(std::string s, std::string find, std::string replace);
15 14
 
16
-char *stringReplace(const char *s, const char *search, const char *replace);
15
+std::string expandHomeDirectory(std::string s);
17 16
 
18
-int readBool(const char *value, bool *var);
19
-
20
-/*!
21
- * \brief Check if a string ends with another string.
22
- * \param str string to check
23
- * \param suffix suffix for which to check
24
- * \returns true if str ends with suffix
25
- */
26
-bool stringEndsWith(const char *str, const char *suffix);
27
-
28
-/*!
29
- * \brief Generates a buffered string for the printf call
30
- * \param string format string like for printf
31
- * \returns string in a buffer
32
- */
33
-char *bufferString(const char *string, ...) __attribute__((format(printf, 1, 2)));
34
-
35
-/*!
36
- * \brief Expansion of unix home enviroment char.
37
- * Also makes sure string ends in "end" char.
38
- * \param path path string
39
- * \param end end character. 0 appends no additional char
40
- * \returns allocated string of path with expansions
41
- */
42
-char *fullPath(const char *path, char end);
17
+bool stringEndsWith(std::string s, std::string suffix);
43 18
 
44 19
 #endif
45 20
 

+ 2
- 2
src/Console.cpp View File

@@ -8,8 +8,8 @@
8 8
 #include <iostream>
9 9
 
10 10
 #include "global.h"
11
+#include "commands/Command.h"
11 12
 #include "Font.h"
12
-#include "OpenRaider.h"
13 13
 #include "utf8-cpp/utf8.h"
14 14
 #include "utils/strings.h"
15 15
 #include "utils/time.h"
@@ -104,7 +104,7 @@ void Console::handleKeyboard(KeyboardButton key, bool pressed) {
104 104
         if (mInputBuffer.length() > 0) {
105 105
             (*this) << "> " << mInputBuffer.c_str() << endl;
106 106
             mCommandHistory.push_back(mInputBuffer.c_str());
107
-            int error = getOpenRaider().command(mInputBuffer);
107
+            int error = Command::command(mInputBuffer);
108 108
             if (error != 0) {
109 109
                 (*this) << "Error Code: " << error << endl;
110 110
             }

+ 2
- 7
src/Font.cpp View File

@@ -11,16 +11,11 @@
11 11
 #include "Font.h"
12 12
 
13 13
 Font::~Font() {
14
-    delete [] mFontName;
15
-    mFontName = NULL;
16 14
 }
17 15
 
18
-void Font::setFont(const char *font) {
19
-    assert(font != NULL);
20
-    assert(font[0] != '\0');
16
+void Font::setFont(std::string font) {
21 17
     assert(mFontInit == false);
22
-
23
-    mFontName = fullPath(font, 0);
18
+    mFontName = font;
24 19
 }
25 20
 
26 21
 void Font::drawTextCentered(unsigned int x, unsigned int y, float scale,

+ 1
- 1
src/FontManager.cpp View File

@@ -32,7 +32,7 @@ FontManager::~FontManager() {
32 32
     }
33 33
 }
34 34
 
35
-void FontManager::add(Font *f, const char *e) {
35
+void FontManager::add(Font *f, std::string e) {
36 36
     fonts.push_back(f);
37 37
     extensions.push_back(e);
38 38
 }

+ 1
- 4
src/FontSDL.cpp View File

@@ -13,7 +13,6 @@
13 13
 FontSDL::FontSDL() {
14 14
     mFont = NULL;
15 15
     mFontInit = false;
16
-    mFontName = NULL;
17 16
     mFontTexture = 0;
18 17
 }
19 18
 
@@ -29,15 +28,13 @@ FontSDL::~FontSDL() {
29 28
 
30 29
 int FontSDL::initialize() {
31 30
     assert(mFontInit == false);
32
-    assert(mFontName != NULL);
33
-    assert(mFontName[0] != '\0');
34 31
 
35 32
     if (TTF_Init() != 0) {
36 33
         printf("Could not initialize SDL-TTF!\n");
37 34
         return -1;
38 35
     }
39 36
 
40
-    mFont = TTF_OpenFont(mFontName, 24);
37
+    mFont = TTF_OpenFont(mFontName.c_str(), 24);
41 38
     if (mFont == NULL) {
42 39
         printf("TTF_OpenFont Error: %s\n", TTF_GetError());
43 40
         return -2;

+ 2
- 6
src/FontTRLE.cpp View File

@@ -15,7 +15,6 @@
15 15
 
16 16
 FontTRLE::FontTRLE() {
17 17
     mFontInit = false;
18
-    mFontName = NULL;
19 18
     mFontTexture = 0;
20 19
 }
21 20
 
@@ -24,8 +23,6 @@ FontTRLE::~FontTRLE() {
24 23
 
25 24
 int FontTRLE::initialize() {
26 25
     assert(mFontInit == false);
27
-    assert(mFontName != NULL);
28
-    assert(mFontName[0] != '\0');
29 26
     assert(stringEndsWith(mFontName, ".pc") == true);
30 27
 
31 28
     // Load .pc file...
@@ -53,15 +50,14 @@ int FontTRLE::initialize() {
53 50
     delete [] pixels;
54 51
 
55 52
     // Try to load .lps file, overwriting default glyph positions
56
-    char *lpsFile = stringReplace(mFontName, ".pc", ".lps");
53
+    std::string lpsFile = findAndReplace(mFontName, ".pc", ".lps");
57 54
     loadLPS(lpsFile);
58
-    delete [] lpsFile;
59 55
 
60 56
     mFontInit = true;
61 57
     return 0;
62 58
 }
63 59
 
64
-void FontTRLE::loadLPS(const char *f) {
60
+void FontTRLE::loadLPS(std::string f) {
65 61
     std::ifstream file(f);
66 62
     if (!file)
67 63
         return;

+ 4
- 4
src/MenuFolder.cpp View File

@@ -8,6 +8,7 @@
8 8
 #include "global.h"
9 9
 #include "Console.h"
10 10
 #include "Font.h"
11
+#include "Game.h"
11 12
 #include "loader/Loader.h"
12 13
 #include "OpenRaider.h"
13 14
 #include "Window.h"
@@ -32,7 +33,7 @@ MenuFolder::~MenuFolder() {
32 33
 }
33 34
 
34 35
 int MenuFolder::initialize() {
35
-    return initialize(getOpenRaider().mPakDir);
36
+    return initialize(getOpenRaider().getPakDir());
36 37
 }
37 38
 
38 39
 int MenuFolder::initialize(std::string s) {
@@ -118,9 +119,8 @@ void MenuFolder::loadOrOpen() {
118 119
             showDialog("Error reading subfolder!", "OK", "");
119 120
         }
120 121
     } else {
121
-        std::string tmp = "load ";
122
-        tmp += mapFolder->getFile((unsigned long)mCursor - 1 - mapFolder->folderCount()).getPath();
123
-        int error = getOpenRaider().command(tmp.c_str());
122
+        int error = getGame().loadLevel(mapFolder->getFile((unsigned long)mCursor
123
+                    - 1 - mapFolder->folderCount()).getPath().c_str());
124 124
         if (error == 0) {
125 125
             makeInvisible();
126 126
         } else {

+ 29
- 22
src/OpenRaider.cpp View File

@@ -26,10 +26,6 @@
26 26
 OpenRaider::OpenRaider() {
27 27
     mRunning = false;
28 28
     mFPS = false;
29
-    mBaseDir = NULL;
30
-    mPakDir = NULL;
31
-    mAudioDir = NULL;
32
-    mDataDir = NULL;
33 29
 
34 30
     for (int i = 0; i < ActionEventCount; i++)
35 31
         keyBindings[i] = unknownKey;
@@ -37,25 +33,40 @@ OpenRaider::OpenRaider() {
37 33
     Command::fillCommandList();
38 34
 }
39 35
 
40
-OpenRaider::~OpenRaider() {
41
-    delete mBaseDir;
42
-    mBaseDir = NULL;
36
+std::string OpenRaider::getBaseDir() {
37
+    return baseDir;
38
+}
39
+
40
+void OpenRaider::setBaseDir(std::string dir) {
41
+    baseDir = dir;
42
+}
43
+
44
+std::string OpenRaider::getPakDir() {
45
+    return pakDir;
46
+}
47
+
48
+void OpenRaider::setPakDir(std::string dir) {
49
+    pakDir = dir;
50
+}
43 51
 
44
-    delete mPakDir;
45
-    mPakDir = NULL;
52
+std::string OpenRaider::getAudioDir() {
53
+    return audioDir;
54
+}
46 55
 
47
-    delete mAudioDir;
48
-    mAudioDir = NULL;
56
+void OpenRaider::setAudioDir(std::string dir) {
57
+    audioDir = dir;
58
+}
49 59
 
50
-    delete mDataDir;
51
-    mDataDir = NULL;
60
+std::string OpenRaider::getDataDir() {
61
+    return dataDir;
52 62
 }
53 63
 
54
-int OpenRaider::loadConfig(const char *config) {
55
-    assert(config != NULL);
56
-    assert(config[0] != '\0');
64
+void OpenRaider::setDataDir(std::string dir) {
65
+    dataDir = dir;
66
+}
57 67
 
58
-    char *configFile = fullPath(config, 0);
68
+int OpenRaider::loadConfig(std::string config) {
69
+    std::string configFile = expandHomeDirectory(config);
59 70
     getConsole() << "Loading config from \"" << configFile << "\"..." << Console::endl;
60 71
 
61 72
     std::ifstream file(configFile);
@@ -68,7 +79,7 @@ int OpenRaider::loadConfig(const char *config) {
68 79
         if (line.length() == 0)
69 80
             continue;
70 81
 
71
-        int error = command(line);
82
+        int error = Command::command(line);
72 83
         if (error != 0)
73 84
             getConsole() << "Error Code: " << error << Console::endl;
74 85
     }
@@ -78,10 +89,6 @@ int OpenRaider::loadConfig(const char *config) {
78 89
     return 0;
79 90
 }
80 91
 
81
-int OpenRaider::command(std::string c) {
82
-    return Command::command(c);
83
-}
84
-
85 92
 int OpenRaider::initialize() {
86 93
     // Initialize Windowing
87 94
     int error = getWindow().initialize();

+ 2
- 2
src/TextureManager.cpp View File

@@ -46,11 +46,11 @@ int TextureManager::initialize() {
46 46
     delete [] image;
47 47
 
48 48
     //! \fixme Temporary
49
-    std::string filename(getOpenRaider().mPakDir);
49
+    std::string filename(getOpenRaider().getPakDir());
50 50
     filename += "/tr2/TITLE.PCX";
51 51
     if (loadPCX(filename.c_str()) < 0) {
52 52
         //! \fixme Error Checking. Return negative error code, check in calling place too
53
-        filename = getOpenRaider().mDataDir;
53
+        filename = getOpenRaider().getDataDir();
54 54
         filename += "/splash.tga";
55 55
         loadTGA(filename.c_str());
56 56
     }

+ 1
- 1
src/commands/CommandEngine.cpp View File

@@ -60,7 +60,7 @@ int CommandScreenshot::execute(std::istream& args) {
60 60
         return -1;
61 61
     }
62 62
 
63
-    std::string filename(getOpenRaider().mBaseDir);
63
+    std::string filename(getOpenRaider().getBaseDir());
64 64
     filename += "/sshots/";
65 65
     filename += VERSION_SHORT;
66 66
 

+ 28
- 48
src/commands/CommandSet.cpp View File

@@ -43,51 +43,28 @@ void CommandSet::printHelp() {
43 43
 }
44 44
 
45 45
 namespace {
46
-    char *expandDirectoryNames(const char *s) {
47
-        assert(s != NULL);
48
-        assert(s[0] != '\0');
49
-
50
-        char *result = bufferString("%s", s);
51
-
52
-        if (getOpenRaider().mPakDir != NULL) {
53
-            char *tmp = stringReplace(s, "$(pakdir)", getOpenRaider().mPakDir);
54
-            delete [] result;
55
-            result = tmp;
56
-        }
57
-
58
-        if (getOpenRaider().mAudioDir != NULL) {
59
-            char *tmp = stringReplace(s, "$(audiodir)", getOpenRaider().mAudioDir);
60
-            delete [] result;
61
-            result = tmp;
46
+    std::string expandNames(std::string s) {
47
+        // Remove quotes
48
+        if ((s.length() >= 3) &&
49
+                (((s[0] == '"') && (s[s.length() - 1] == '"'))
50
+                || ((s[0] == '\'') && (s[s.length() - 1] == '\'')))) {
51
+            s.erase(0, 1);
52
+            s.erase(s.length() - 1, 1);
62 53
         }
63 54
 
64
-        if (getOpenRaider().mDataDir != NULL) {
65
-            char *tmp = stringReplace(s, "$(datadir)", getOpenRaider().mDataDir);
66
-            delete [] result;
67
-            result = tmp;
68
-        }
55
+        // Expand Names
56
+        s = findAndReplace(s, "$(pakdir)", getOpenRaider().getPakDir());
57
+        s = findAndReplace(s, "$(audiodir)", getOpenRaider().getAudioDir());
58
+        s = findAndReplace(s, "$(datadir)", getOpenRaider().getDataDir());
59
+        s = findAndReplace(s, "$(basedir)", getOpenRaider().getBaseDir());
69 60
 
70
-        if (getOpenRaider().mBaseDir != NULL) {
71
-            char *tmp = stringReplace(result, "$(basedir)", getOpenRaider().mBaseDir);
72
-            delete [] result;
73
-            result = tmp;
74
-        }
61
+        // Full path
62
+        s = expandHomeDirectory(s);
75 63
 
76
-        return result;
64
+        return s;
77 65
     }
78 66
 }
79 67
 
80
-#define CHANGE_DIR_WITH_EXPANSION(a) do {     \
81
-    std::string temp;                         \
82
-    args >> temp;                             \
83
-    const char *value = temp.c_str();         \
84
-    char *quotes = stringRemoveQuotes(value); \
85
-    char *tmp = expandDirectoryNames(quotes); \
86
-    a = fullPath(tmp, 0);                     \
87
-    delete [] tmp;                            \
88
-    delete [] quotes;                         \
89
-} while(false)
90
-
91 68
 int CommandSet::execute(std::istream& args) {
92 69
     std::string var;
93 70
     args >> var;
@@ -142,22 +119,25 @@ int CommandSet::execute(std::istream& args) {
142 119
         }
143 120
         getOpenRaider().mFPS = fps;
144 121
     } else if (var.compare("basedir") == 0) {
145
-        CHANGE_DIR_WITH_EXPANSION(getOpenRaider().mBaseDir);
122
+        std::string temp;
123
+        args >> temp;
124
+        getOpenRaider().setBaseDir(expandNames(temp));
146 125
     } else if (var.compare("pakdir") == 0) {
147
-        CHANGE_DIR_WITH_EXPANSION(getOpenRaider().mPakDir);
126
+        std::string temp;
127
+        args >> temp;
128
+        getOpenRaider().setPakDir(expandNames(temp));
148 129
     } else if (var.compare("audiodir") == 0) {
149
-        CHANGE_DIR_WITH_EXPANSION(getOpenRaider().mAudioDir);
130
+        std::string temp;
131
+        args >> temp;
132
+        getOpenRaider().setAudioDir(expandNames(temp));
150 133
     } else if (var.compare("datadir") == 0) {
151
-        CHANGE_DIR_WITH_EXPANSION(getOpenRaider().mDataDir);
134
+        std::string temp;
135
+        args >> temp;
136
+        getOpenRaider().setDataDir(expandNames(temp));
152 137
     } else if (var.compare("font") == 0) {
153 138
         std::string temp;
154 139
         args >> temp;
155
-        const char *value = temp.c_str();
156
-        char *quotes = stringReplace(value, "\"", "");
157
-        char *tmp = expandDirectoryNames(quotes);
158
-        getFont().setFont(tmp);
159
-        delete [] tmp;
160
-        delete [] quotes;
140
+        getFont().setFont(expandNames(temp).c_str());
161 141
     } else {
162 142
         getConsole() << "set-Error: Unknown variable (" << var.c_str() << ")" << Console::endl;
163 143
         return -1;

+ 17
- 122
src/utils/strings.cpp View File

@@ -3,141 +3,36 @@
3 3
  * \brief String handling utilities
4 4
  *
5 5
  * \author xythobuz
6
- * \author Mongoose
7 6
  */
8 7
 
9
-#include <cstdlib>
10
-#include <stdio.h>
11
-#include <string.h>
12
-
13
-#if defined(_WIN32) && (!defined(va_copy))
14
-#define va_copy(d,s) ((d) = (s))
15
-#endif
16
-
17 8
 #include "global.h"
18 9
 #include "utils/filesystem.h"
19 10
 #include "utils/strings.h"
20 11
 
21
-char *stringRemoveQuotes(const char *s) {
22
-    size_t length = strlen(s);
23
-    if ((s[0] == '"') && (s[length - 1] == '"')) {
24
-        char *buf = new char[length - 1];
25
-        for (size_t i = 1; i < (length - 1); i++)
26
-            buf[i - 1] = s[i];
27
-        buf[length - 2] = '\0';
28
-        return buf;
29
-    } else {
30
-        return bufferString("%s", s);
12
+std::string findAndReplace(std::string s, std::string find, std::string replace) {
13
+    size_t p = s.find(find);
14
+    while (p != std::string::npos) {
15
+        s.erase(p, find.length());
16
+        s.insert(p, replace);
17
+        p = s.find(find);
31 18
     }
19
+    return s;
32 20
 }
33 21
 
34
-char *stringReplace(const char *s, const char *search, const char *replace) {
35
-    const char *tmp = strstr(s, search);
36
-    if (tmp == NULL)
37
-        return bufferString("%s", s);
38
-    size_t offset = tmp - s;
39
-
40
-    size_t length = strlen(s) - strlen(search) + strlen(replace);
41
-    char *buf = new char[length + 1];
42
-    buf[length] = '\0';
43
-
44
-    for (size_t i = 0; i < offset; i++)
45
-        buf[i] = s[i];
46
-
47
-    for (size_t i = 0; i < strlen(replace); i++)
48
-        buf[offset + i] = replace[i];
49
-
50
-    for (size_t i = (offset + strlen(search)); i < strlen(s); i++)
51
-        buf[i + strlen(replace) - strlen(search)] = s[i];
52
-
53
-    char *ret = stringReplace(buf, search, replace);
54
-    delete [] buf;
55
-    return ret;
56
-}
57
-
58
-int readBool(const char *value, bool *var) {
59
-    if ((strcmp(value, "1") == 0) || (strcmp(value, "true") == 0) || (strcmp(value, "on") == 0)) {
60
-        *var = true;
61
-    } else if ((strcmp(value, "0") == 0) || (strcmp(value, "false") == 0) || (strcmp(value, "off") == 0)) {
62
-        *var = false;
63
-    } else {
64
-        return -1;
22
+std::string expandHomeDirectory(std::string s) {
23
+    if ((s.length() > 0) && (s[0] == '~')) {
24
+        s.erase(0, 1);
25
+        s.insert(0, getHomeDirectory());
65 26
     }
66
-
67
-    return 0;
27
+    return s;
68 28
 }
69 29
 
70
-bool stringEndsWith(const char *str, const char *suffix) {
71
-    assert(str != NULL);
72
-    assert(suffix != NULL);
73
-
74
-    size_t lenstr = strlen(str);
75
-    size_t lensuffix = strlen(suffix);
76
-
77
-    if (lensuffix > lenstr)
78
-        return false;
79
-
80
-    return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
81
-}
82
-
83
-char *bufferString(const char *string, ...) {
84
-    va_list args, tmp;
85
-    int sz = 60;
86
-    int n;
87
-    char *text;
88
-
89
-    assert(string != NULL);
90
-    assert(string[0] != '\0');
91
-
92
-    text = new char[sz];
93
-
94
-    va_start(args, string);
95
-    n = vsnprintf(text, sz, string, args);
96
-
97
-    if (n < 0) {
98
-        delete [] text;
99
-        va_end(args);
100
-        return NULL; // encoding error
101
-    } else if (n >= sz) {
102
-        sz = n + 1; // buffer too small
103
-        delete [] text;
104
-        text = new char[sz + 1];
105
-        vsnprintf(text, sz, string, args);
106
-    }
107
-
108
-    va_end(args);
109
-    return text;
110
-}
111
-
112
-char *fullPath(const char *path, char end) {
113
-    size_t lenPath;
114
-    char *dir;
115
-
116
-    assert(path != NULL);
117
-    assert(path[0] != '\0');
118
-
119
-    if (path[0] == '~') {
120
-        std::string home = getHomeDirectory();
121
-        assert(home.length() > 0);
122
-        lenPath = strlen(path);
123
-        dir = new char[home.length() + lenPath + 3]; // space for end char
124
-        strncpy(dir, home.c_str(), home.length());
125
-        strncpy(dir + home.length(), path + 1, lenPath - 1);
126
-        lenPath = home.length() + lenPath - 1;
127
-    } else {
128
-        lenPath = strlen(path);
129
-        dir = new char[lenPath + 2]; // space for end char
130
-        strncpy(dir, path, lenPath);
131
-    }
132
-
133
-    // Make sure ends in "end" char
134
-    if ((lenPath > 0) && (end != 0) && (dir[lenPath - 1] != end)) {
135
-        dir[lenPath] = end;
136
-        dir[lenPath + 1] = '\0';
137
-    } else {
138
-        dir[lenPath] = '\0';
30
+bool stringEndsWith(std::string s, std::string suffix) {
31
+    if (s.length() >= suffix.length()) {
32
+        std::string end = s.substr(s.length() - suffix.length());
33
+        return (end == suffix);
139 34
     }
140 35
 
141
-    return dir;
36
+    return false;
142 37
 }
143 38
 

Loading…
Cancel
Save