Skip to content

Commit

Permalink
Demo: fix line_buffer issue in ParseDemoMarkerTags
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Dec 6, 2023
1 parent 8205803 commit 7681fa0
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9051,6 +9051,7 @@ namespace ImGuiDemoMarkerCodeViewer_Impl
ImVector<DemoMarkerTag> r;
{
char line_buffer[2048];
memset(line_buffer, 0, 2048);
char tag_buffer[IMGUI_DEMO_MARKER_MAX_TAG_LENGTH];
for (int line_number = 0; line_number < lines.size(); ++line_number)
{
Expand Down Expand Up @@ -9087,9 +9088,9 @@ namespace ImGuiDemoMarkerCodeViewer_Impl
~DemoCodeWindow()
{
if (SourceCode)
IM_DELETE(SourceCode);
IM_FREE(SourceCode);
if (SourceLineNumbersStr)
IM_DELETE(SourceLineNumbersStr);
IM_FREE(SourceLineNumbersStr);
}

void NavigateTo(int line_number)
Expand Down Expand Up @@ -9207,17 +9208,19 @@ namespace ImGuiDemoMarkerCodeViewer_Impl

void ReadSourceCodeContent(const char* source_file)
{
FILE *f = fopen(source_file, "r");
if (!f)
FILE *f = fopen(source_file, "rb"); // binary mode for windows (do not translate \n)
if (f == NULL)
{
SourceCode = NULL;
return;
}
fseek(f, 0, SEEK_END);
size_t file_size = (size_t) ftell(f);
SourceCode = (char *) IM_ALLOC(file_size * sizeof(char));
SourceCode = (char *) IM_ALLOC((file_size + 1)* sizeof(char));
rewind(f);
fread(SourceCode, sizeof(char), file_size, f);
size_t nread = fread(SourceCode, sizeof(char), file_size, f);
IM_ASSERT(nread == file_size);
SourceCode[file_size] = '\0';
fclose(f);
}

Expand Down Expand Up @@ -9257,19 +9260,23 @@ namespace ImGuiDemoMarkerCodeViewer_Impl
bool ShowFilterResults;
};

DemoCodeWindow GDemoCodeWindow;
DemoCodeWindow& GDemoCodeWindow()
{
static DemoCodeWindow demoCodeWindow;
return demoCodeWindow;
}

} // namespace ImGuiDemoMarkerCodeViewer_Impl

namespace ImGuiDemoMarkerCodeViewer
{
void ShowCodeViewer()
{
ImGuiDemoMarkerCodeViewer_Impl::GDemoCodeWindow.Gui();
ImGuiDemoMarkerCodeViewer_Impl::GDemoCodeWindow().Gui();
}
void NavigateTo(int line_number)
{
ImGuiDemoMarkerCodeViewer_Impl::GDemoCodeWindow.NavigateTo(line_number);
ImGuiDemoMarkerCodeViewer_Impl::GDemoCodeWindow().NavigateTo(line_number);
}
}

Expand Down Expand Up @@ -9299,11 +9306,15 @@ void BrowseToUrl(const char *url)
#elif TARGET_OS_OSX
char cmd[1024];
snprintf(cmd, 1024, "open %s", url);
system(cmd);
int result = system(cmd);
if (result != 0)
fprintf(stderr, "Error when calling system(%s)\n", cmd);
#elif defined(__linux__)
char cmd[1024];
snprintf(cmd, 1024, "xdg-open %s", url);
system(cmd);
snprintf(cmd, 1024, "xdg-open %s", url);
int result = system(cmd);
if (result != 0)
fprintf(stderr, "Please install xdg-open to open links\n");
#endif
}

Expand Down

0 comments on commit 7681fa0

Please sign in to comment.