Browse Source

Demo: use locally defined strdup because compilers/standard librairies are an annoyance (#520)

ocornut 9 years ago
parent
commit
dafedc3246
2 changed files with 10 additions and 9 deletions
  1. 3 4
      imgui.cpp
  2. 7 5
      imgui_demo.cpp

+ 3 - 4
imgui.cpp

@@ -830,10 +830,9 @@ int ImStrnicmp(const char* str1, const char* str2, int count)
 
 char* ImStrdup(const char *str)
 {
-    char *buff = (char*)ImGui::MemAlloc(strlen(str) + 1);
-    IM_ASSERT(buff);
-    strcpy(buff, str);
-    return buff;
+    size_t len = strlen(str) + 1;
+    void* buff = ImGui::MemAlloc(len);
+    return (char*)memcpy(buff, (const void*)str, len);
 }
 
 int ImStrlenW(const ImWchar* str)

+ 7 - 5
imgui_demo.cpp

@@ -1894,6 +1894,11 @@ struct ExampleAppConsole
             free(History[i]);
     }
 
+    // Portable helpers
+    static int   Stricmp(const char* str1, const char* str2)         { int d; while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } return d; }
+    static int   Strnicmp(const char* str1, const char* str2, int n) { int d = 0; while (n > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; n--; } return d; }
+    static char* Strdup(const char *str)                             { size_t len = strlen(str) + 1; void* buff = ImGui::MemAlloc(len); return (char*)memcpy(buff, (const void*)str, len); }
+
     void    ClearLog()
     {
         for (int i = 0; i < Items.Size; i++)
@@ -1910,7 +1915,7 @@ struct ExampleAppConsole
         vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
         buf[IM_ARRAYSIZE(buf)-1] = 0;
         va_end(args);
-        Items.push_back(strdup(buf));
+        Items.push_back(Strdup(buf));
         ScrollToBottom = true;
     }
 
@@ -1987,9 +1992,6 @@ struct ExampleAppConsole
         ImGui::End();
     }
 
-    static int Stricmp(const char* str1, const char* str2)               { int d; while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } return d; }
-    static int Strnicmp(const char* str1, const char* str2, int count)   { int d = 0; while (count > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; count--; } return d; }
-
     void    ExecCommand(const char* command_line)
     {
         AddLog("# %s\n", command_line);
@@ -2003,7 +2005,7 @@ struct ExampleAppConsole
                 History.erase(History.begin() + i);
                 break;
             }
-        History.push_back(strdup(command_line));
+        History.push_back(Strdup(command_line));
 
         // Process command
         if (Stricmp(command_line, "CLEAR") == 0)