Skip to content

Commit

Permalink
Demo: use locally defined strdup because compilers/standard librairie…
Browse files Browse the repository at this point in the history
…s are an annoyance (#520)
  • Loading branch information
ocornut committed Feb 8, 2016
1 parent 2c6bc95 commit dafedc3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
7 changes: 3 additions & 4 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down

0 comments on commit dafedc3

Please sign in to comment.