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

This commit is contained in:
ocornut 2016-02-08 08:44:45 +01:00
parent 2c6bc95dd5
commit dafedc3246
2 changed files with 10 additions and 9 deletions

View File

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

View File

@ -1894,6 +1894,11 @@ struct ExampleAppConsole
free(History[i]); 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() void ClearLog()
{ {
for (int i = 0; i < Items.Size; i++) for (int i = 0; i < Items.Size; i++)
@ -1910,7 +1915,7 @@ struct ExampleAppConsole
vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
buf[IM_ARRAYSIZE(buf)-1] = 0; buf[IM_ARRAYSIZE(buf)-1] = 0;
va_end(args); va_end(args);
Items.push_back(strdup(buf)); Items.push_back(Strdup(buf));
ScrollToBottom = true; ScrollToBottom = true;
} }
@ -1987,9 +1992,6 @@ struct ExampleAppConsole
ImGui::End(); 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) void ExecCommand(const char* command_line)
{ {
AddLog("# %s\n", command_line); AddLog("# %s\n", command_line);
@ -2003,7 +2005,7 @@ struct ExampleAppConsole
History.erase(History.begin() + i); History.erase(History.begin() + i);
break; break;
} }
History.push_back(strdup(command_line)); History.push_back(Strdup(command_line));
// Process command // Process command
if (Stricmp(command_line, "CLEAR") == 0) if (Stricmp(command_line, "CLEAR") == 0)