Просмотр исходного кода

Simpler expandDirectoryNames()

Thomas Buck 11 лет назад
Родитель
Сommit
3c64acd2d2
3 измененных файлов: 34 добавлений и 47 удалений
  1. 1
    0
      ChangeLog.md
  2. 3
    3
      include/OpenRaider.h
  3. 30
    44
      src/Command.cpp

+ 1
- 0
ChangeLog.md Просмотреть файл

6
     * Created methods to convert strings of names to ActionEvents
6
     * Created methods to convert strings of names to ActionEvents
7
       and KeyboardButtons.
7
       and KeyboardButtons.
8
     * Simplified bind commands
8
     * Simplified bind commands
9
+    * Simplified expandDirectoryNames()
9
     * Only show debug info if menu is not visible
10
     * Only show debug info if menu is not visible
10
 
11
 
11
     [ 20140601 ]
12
     [ 20140601 ]

+ 3
- 3
include/OpenRaider.h Просмотреть файл

62
 
62
 
63
 private:
63
 private:
64
 
64
 
65
-    int command(std::stringstream &command);
66
     char *expandDirectoryNames(const char *s);
65
     char *expandDirectoryNames(const char *s);
67
-    int help(std::string &cmd);
68
-    int set(std::stringstream &command);
66
+    int set(std::istream &command);
69
     int bind(const char *action, const char *key);
67
     int bind(const char *action, const char *key);
70
 
68
 
69
+    static int help(std::string &cmd);
70
+
71
     bool mRunning;
71
     bool mRunning;
72
     bool mFPS;
72
     bool mFPS;
73
 
73
 

+ 30
- 44
src/Command.cpp Просмотреть файл

48
     return 0;
48
     return 0;
49
 }
49
 }
50
 
50
 
51
-int OpenRaider::command(std::string &command) {
52
-    // Remove comment, if any
53
-    size_t comment = command.find_first_of('#');
54
-    if (comment != std::string::npos)
55
-        command.erase(comment);
56
-
57
-    // Execute command
58
-    std::stringstream stream(command);
59
-    return this->command(stream);
60
-}
61
-
62
 int OpenRaider::command(const char *command) {
51
 int OpenRaider::command(const char *command) {
63
     std::string tmp(command);
52
     std::string tmp(command);
64
     return this->command(tmp);
53
     return this->command(tmp);
65
 }
54
 }
66
 
55
 
67
-int OpenRaider::command(std::stringstream &command) {
56
+int OpenRaider::command(std::string &c) {
57
+    // Remove comment, if any
58
+    size_t comment = c.find_first_of('#');
59
+    if (comment != std::string::npos)
60
+        c.erase(comment);
61
+
62
+    // Execute command
63
+    std::stringstream command(c);
68
     std::string cmd;
64
     std::string cmd;
69
     command >> cmd;
65
     command >> cmd;
70
 
66
 
575
     assert(s != NULL);
571
     assert(s != NULL);
576
     assert(s[0] != '\0');
572
     assert(s[0] != '\0');
577
 
573
 
578
-    if (mBaseDir != NULL) {
579
-        const char *base = "$(basedir)";
580
-        if (strstr(s, base) != NULL) {
581
-            return stringReplace(s, base, mBaseDir);
582
-        }
583
-    }
574
+    char *result = bufferString("%s", s);
584
 
575
 
585
     if (mPakDir != NULL) {
576
     if (mPakDir != NULL) {
586
-        const char *pak = "$(pakdir)";
587
-        if (strstr(s, pak) != NULL) {
588
-            return stringReplace(s, pak, mPakDir);
589
-        }
577
+        char *tmp = stringReplace(s, "$(pakdir)", mPakDir);
578
+        delete [] result;
579
+        result = tmp;
590
     }
580
     }
591
 
581
 
592
     if (mAudioDir != NULL) {
582
     if (mAudioDir != NULL) {
593
-        const char *audio = "$(audiodir)";
594
-        if (strstr(s, audio) != NULL) {
595
-            return stringReplace(s, audio, mAudioDir);
596
-        }
583
+        char *tmp = stringReplace(s, "$(audiodir)", mAudioDir);
584
+        delete [] result;
585
+        result = tmp;
597
     }
586
     }
598
 
587
 
599
     if (mDataDir != NULL) {
588
     if (mDataDir != NULL) {
600
-        const char *data = "$(datadir)";
601
-        if (strstr(s, data) != NULL) {
602
-            return stringReplace(s, data, mDataDir);
603
-        }
589
+        char *tmp = stringReplace(s, "$(datadir)", mDataDir);
590
+        delete [] result;
591
+        result = tmp;
604
     }
592
     }
605
 
593
 
606
-    return NULL;
594
+    if (mBaseDir != NULL) {
595
+        char *tmp = stringReplace(result, "$(basedir)", mBaseDir);
596
+        delete [] result;
597
+        result = tmp;
598
+    }
599
+
600
+    return result;
607
 }
601
 }
608
 
602
 
609
 #define CHANGE_DIR_WITH_EXPANSION(a) do {     \
603
 #define CHANGE_DIR_WITH_EXPANSION(a) do {     \
612
     const char *value = temp.c_str();         \
606
     const char *value = temp.c_str();         \
613
     char *quotes = stringRemoveQuotes(value); \
607
     char *quotes = stringRemoveQuotes(value); \
614
     char *tmp = expandDirectoryNames(quotes); \
608
     char *tmp = expandDirectoryNames(quotes); \
615
-    if (tmp == NULL) {                        \
616
-        a = fullPath(quotes, 0);              \
617
-    } else {                                  \
618
-        a = fullPath(tmp, 0);                 \
619
-        delete [] tmp;                        \
620
-    }                                         \
609
+    a = fullPath(tmp, 0);                     \
610
+    delete [] tmp;                            \
621
     delete [] quotes;                         \
611
     delete [] quotes;                         \
622
 } while(false)
612
 } while(false)
623
 
613
 
624
-int OpenRaider::set(std::stringstream &command) {
614
+int OpenRaider::set(std::istream &command) {
625
     std::string var;
615
     std::string var;
626
     command >> var >> std::boolalpha;
616
     command >> var >> std::boolalpha;
627
 
617
 
692
         const char *value = temp.c_str();
682
         const char *value = temp.c_str();
693
         char *quotes = stringReplace(value, "\"", "");
683
         char *quotes = stringReplace(value, "\"", "");
694
         char *tmp = expandDirectoryNames(quotes);
684
         char *tmp = expandDirectoryNames(quotes);
695
-        if (tmp == NULL) {
696
-            getWindow().setFont(quotes);
697
-        } else {
698
-            getWindow().setFont(tmp);
699
-            delete [] tmp;
700
-        }
685
+        getWindow().setFont(tmp);
686
+        delete [] tmp;
701
         delete [] quotes;
687
         delete [] quotes;
702
     } else {
688
     } else {
703
         getConsole().print("set-Error: Unknown variable (%s)", var.c_str());
689
         getConsole().print("set-Error: Unknown variable (%s)", var.c_str());

Загрузка…
Отмена
Сохранить