diff options
-rw-r--r-- | testing/toxic/main.c | 113 |
1 files changed, 68 insertions, 45 deletions
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 40ff5d09..27a34e09 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -30,6 +30,8 @@ extern void disable_chatwin(int f_num); | |||
30 | extern int add_req(uint8_t *public_key); // XXX | 30 | extern int add_req(uint8_t *public_key); // XXX |
31 | extern unsigned char *hex_string_to_bin(char hex_string[]); | 31 | extern unsigned char *hex_string_to_bin(char hex_string[]); |
32 | 32 | ||
33 | static int store_data(char*); | ||
34 | |||
33 | /* Holds status of chat windows */ | 35 | /* Holds status of chat windows */ |
34 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; | 36 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; |
35 | 37 | ||
@@ -41,6 +43,7 @@ static ToxWindow windows[MAX_WINDOW_SLOTS]; | |||
41 | static ToxWindow* prompt; | 43 | static ToxWindow* prompt; |
42 | 44 | ||
43 | static Messenger *m; | 45 | static Messenger *m; |
46 | static char *DATA_FILE; | ||
44 | 47 | ||
45 | int w_num; | 48 | int w_num; |
46 | int active_window; | 49 | int active_window; |
@@ -105,7 +108,11 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l | |||
105 | 108 | ||
106 | void on_friendadded(int friendnumber) | 109 | void on_friendadded(int friendnumber) |
107 | { | 110 | { |
108 | friendlist_onFriendAdded(m, friendnumber); | 111 | friendlist_onFriendAdded(m, friendnumber); |
112 | int st; | ||
113 | if ((st = store_data(DATA_FILE)) != 0) { | ||
114 | wprintw(prompt->window, "\nCould not store messenger, error code: %d\n", st); | ||
115 | } | ||
109 | } | 116 | } |
110 | /* CALLBACKS END */ | 117 | /* CALLBACKS END */ |
111 | 118 | ||
@@ -279,61 +286,76 @@ static void do_tox() | |||
279 | doMessenger(m); | 286 | doMessenger(m); |
280 | } | 287 | } |
281 | 288 | ||
282 | static void load_data(char *path) | 289 | /* |
290 | * Store Messenger data to path | ||
291 | * Return 0 Messenger stored successfully | ||
292 | * Return 1 malloc failed | ||
293 | * Return 2 fopen failed | ||
294 | * Return 3 fwrite failed | ||
295 | */ | ||
296 | static int store_data(char *path) | ||
283 | { | 297 | { |
284 | FILE *fd; | 298 | FILE *fd; |
285 | size_t len; | 299 | size_t len; |
286 | uint8_t *buf; | 300 | uint8_t *buf; |
287 | |||
288 | if ((fd = fopen(path, "r")) != NULL) { | ||
289 | fseek(fd, 0, SEEK_END); | ||
290 | len = ftell(fd); | ||
291 | fseek(fd, 0, SEEK_SET); | ||
292 | 301 | ||
293 | buf = malloc(len); | ||
294 | if (buf == NULL) { | ||
295 | fprintf(stderr, "malloc() failed.\n"); | ||
296 | fclose(fd); | ||
297 | endwin(); | ||
298 | exit(1); | ||
299 | } | ||
300 | if (fread(buf, len, 1, fd) != 1){ | ||
301 | fprintf(stderr, "fread() failed.\n"); | ||
302 | free(buf); | ||
303 | fclose(fd); | ||
304 | endwin(); | ||
305 | exit(1); | ||
306 | } | ||
307 | Messenger_load(m, buf, len); | ||
308 | } | ||
309 | else { | ||
310 | len = Messenger_size(m); | 302 | len = Messenger_size(m); |
311 | buf = malloc(len); | 303 | buf = malloc(len); |
312 | if (buf == NULL) { | 304 | if (buf == NULL) { |
313 | fprintf(stderr, "malloc() failed.\n"); | 305 | return 1; |
314 | endwin(); | ||
315 | exit(1); | ||
316 | } | 306 | } |
317 | Messenger_save(m, buf); | 307 | Messenger_save(m, buf); |
318 | 308 | ||
319 | fd = fopen(path, "w"); | 309 | fd = fopen(path, "w"); |
320 | if (fd == NULL) { | 310 | if (fd == NULL) { |
321 | fprintf(stderr, "fopen() failed.\n"); | 311 | return 2; |
322 | free(buf); | ||
323 | endwin(); | ||
324 | exit(1); | ||
325 | } | 312 | } |
326 | 313 | ||
327 | if (fwrite(buf, len, 1, fd) != 1){ | 314 | if (fwrite(buf, len, 1, fd) != 1) { |
328 | fprintf(stderr, "fwrite() failed.\n"); | 315 | return 3; |
329 | free(buf); | 316 | } |
330 | fclose(fd); | 317 | |
331 | endwin(); | 318 | free(buf); |
332 | exit(1); | 319 | fclose(fd); |
320 | |||
321 | return 0; | ||
322 | } | ||
323 | |||
324 | static void load_data(char *path) { | ||
325 | FILE *fd; | ||
326 | size_t len; | ||
327 | uint8_t *buf; | ||
328 | |||
329 | if ((fd = fopen(path, "r")) != NULL) { | ||
330 | fseek(fd, 0, SEEK_END); | ||
331 | len = ftell(fd); | ||
332 | fseek(fd, 0, SEEK_SET); | ||
333 | |||
334 | buf = malloc(len); | ||
335 | if (buf == NULL) { | ||
336 | fprintf(stderr, "malloc() failed.\n"); | ||
337 | fclose(fd); | ||
338 | endwin(); | ||
339 | exit(1); | ||
340 | } | ||
341 | if (fread(buf, len, 1, fd) != 1) { | ||
342 | fprintf(stderr, "fread() failed.\n"); | ||
343 | free(buf); | ||
344 | fclose(fd); | ||
345 | endwin(); | ||
346 | exit(1); | ||
347 | } | ||
348 | Messenger_load(m, buf, len); | ||
349 | free(buf); | ||
350 | fclose(fd); | ||
351 | } else { | ||
352 | int st; | ||
353 | if ((st = store_data(path)) != 0) { | ||
354 | fprintf(stderr, "storing messenger failed with error code: %d", st); | ||
355 | endwin(); | ||
356 | exit(1); | ||
357 | } | ||
333 | } | 358 | } |
334 | } | ||
335 | free(buf); | ||
336 | fclose(fd); | ||
337 | } | 359 | } |
338 | 360 | ||
339 | static void draw_bar() | 361 | static void draw_bar() |
@@ -419,7 +441,6 @@ int main(int argc, char *argv[]) | |||
419 | int ch; | 441 | int ch; |
420 | ToxWindow* a; | 442 | ToxWindow* a; |
421 | char *user_config_dir = get_user_config_dir(); | 443 | char *user_config_dir = get_user_config_dir(); |
422 | char *DATA_FILE; | ||
423 | int config_err = create_user_config_dir(user_config_dir); | 444 | int config_err = create_user_config_dir(user_config_dir); |
424 | if(config_err) { | 445 | if(config_err) { |
425 | DATA_FILE = "data"; | 446 | DATA_FILE = "data"; |
@@ -457,7 +478,6 @@ int main(int argc, char *argv[]) | |||
457 | 478 | ||
458 | if(f_loadfromfile) | 479 | if(f_loadfromfile) |
459 | load_data(DATA_FILE); | 480 | load_data(DATA_FILE); |
460 | free(DATA_FILE); | ||
461 | 481 | ||
462 | if (f_flag == -1) { | 482 | if (f_flag == -1) { |
463 | attron(COLOR_PAIR(3) | A_BOLD); | 483 | attron(COLOR_PAIR(3) | A_BOLD); |
@@ -490,6 +510,9 @@ int main(int argc, char *argv[]) | |||
490 | else if (ch != ERR) | 510 | else if (ch != ERR) |
491 | a->onKey(a, m, ch); | 511 | a->onKey(a, m, ch); |
492 | } | 512 | } |
513 | |||
493 | cleanupMessenger(m); | 514 | cleanupMessenger(m); |
515 | free(DATA_FILE); | ||
516 | |||
494 | return 0; | 517 | return 0; |
495 | } | 518 | } |