Skip to content

Commit

Permalink
Show a helpful popup for malformed json (#49)
Browse files Browse the repository at this point in the history
* Add error popup for malformed json

* Add file, line no, and col no to parse error popup

* Allocate sufficient space for null terminator
  • Loading branch information
Drayux authored Jun 4, 2024
1 parent a9dd557 commit 44acbd5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,9 @@ static LSAppWindow* ls_app_window_new(LSApp* app)

static void ls_app_window_open(LSAppWindow* win, const char* file)
{
char* error_msg = NULL;
GtkWidget* error_popup;

if (win->timer) {
ls_app_window_clear_game(win);
ls_timer_release(win->timer);
Expand All @@ -673,8 +676,22 @@ static void ls_app_window_open(LSAppWindow* win, const char* file)
ls_game_release(win->game);
win->game = 0;
}
if (ls_game_create(&win->game, file)) {
if (ls_game_create(&win->game, file, &error_msg)) {
win->game = 0;
if (error_msg) {
error_popup = gtk_message_dialog_new(
GTK_WINDOW(win),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"JSON parse error: %s\n%s",
error_msg,
file);
gtk_dialog_run(GTK_DIALOG(error_popup));

free(error_msg);
gtk_widget_destroy(error_popup);
}
} else if (ls_timer_create(&win->timer, win->game)) {
win->timer = 0;
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void ls_game_release(ls_game* game)
}
}

int ls_game_create(ls_game** game_ptr, const char* path)
int ls_game_create(ls_game** game_ptr, const char* path, char** error_msg)
{
int error = 0;
ls_game* game;
Expand All @@ -178,6 +178,9 @@ int ls_game_create(ls_game** game_ptr, const char* path)
json = json_load_file(game->path, 0, &json_error);
if (!json) {
error = 1;
size_t msg_len = snprintf(NULL, 0, "%s (%d:%d)", json_error.text, json_error.line, json_error.column);
*error_msg = calloc(msg_len + 1, sizeof(char));
sprintf(*error_msg, "%s (%d:%d)", json_error.text, json_error.line, json_error.column);
goto game_create_done;
}
// copy title
Expand Down
2 changes: 1 addition & 1 deletion src/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void ls_split_string(char* string, long long time);

void ls_delta_string(char* string, long long time);

int ls_game_create(ls_game** game_ptr, const char* path);
int ls_game_create(ls_game** game_ptr, const char* path, char** error_msg);

void ls_game_update_splits(ls_game* game, const ls_timer* timer);

Expand Down

0 comments on commit 44acbd5

Please sign in to comment.