comparison dwtest.c @ 2717:43d630b2b37f

Add error checking to the file load handler and display an error notification on failure, instead of just always announcing success. This makes it easier to see the problems on Android and iOS. Android is getting permission denied on load from the file picker intent... previously iOS was having issues loading files too but is now working.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 06 Dec 2021 14:05:11 +0000
parents 1ee59f231f6c
children c1f8e598960d
comparison
equal deleted inserted replaced
2716:a1fea6b9f308 2717:43d630b2b37f
4 * By: Brian Smith and Mark Hessling 4 * By: Brian Smith and Mark Hessling
5 */ 5 */
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <string.h> 7 #include <string.h>
8 #include <stdio.h> 8 #include <stdio.h>
9 #include <errno.h>
9 #include "dw.h" 10 #include "dw.h"
10 /* For snprintf, strdup etc on old Windows SDK */ 11 /* For snprintf, strdup etc on old Windows SDK */
11 #if defined(__WIN32__) || defined(__OS2__) 12 #if defined(__WIN32__) || defined(__OS2__)
12 #include "dwcompat.h" 13 #include "dwcompat.h"
13 #endif 14 #endif
291 update_render(); 292 update_render();
292 } 293 }
293 return TRUE; 294 return TRUE;
294 } 295 }
295 296
296 void read_file(void) 297 char *read_file(char *filename)
297 { 298 {
298 int i,len; 299 char *errors = NULL;
299 fp = fopen(current_file, "r" ); 300
300 if(fp) 301 fp = fopen(filename, "r" );
301 { 302 if(!fp)
302 lp = (char **)calloc(1000,sizeof(char *)); 303 errors = strerror(errno);
303 /* should test for out of memory */ 304 else
304 max_linewidth=0; 305 {
305 for(i=0; i<1000; i++) 306 int i,len;
306 { 307
308 lp = (char **)calloc(1000,sizeof(char *));
309 /* should test for out of memory */
310 max_linewidth=0;
311 for(i=0; i<1000; i++)
312 {
307 lp[i] = (char *)calloc(1, 1025); 313 lp[i] = (char *)calloc(1, 1025);
308 if (fgets( lp[i], 1024, fp ) == NULL) 314 if (fgets( lp[i], 1024, fp) == NULL)
309 break; 315 break;
310 len = (int)strlen( lp[i] ); 316 len = (int)strlen( lp[i]);
311 if (len > max_linewidth) 317 if (len > max_linewidth)
312 max_linewidth = len; 318 max_linewidth = len;
313 if(lp[i][len - 1] == '\n') 319 if(lp[i][len - 1] == '\n')
314 lp[i][len - 1] = '\0'; 320 lp[i][len - 1] = '\0';
315 } 321 }
316 num_lines = i; 322 num_lines = i;
317 fclose(fp); 323 fclose(fp);
318 dw_scrollbar_set_range(hscrollbar, max_linewidth, cols); 324 dw_scrollbar_set_range(hscrollbar, max_linewidth, cols);
319 dw_scrollbar_set_pos(hscrollbar, 0); 325 dw_scrollbar_set_pos(hscrollbar, 0);
320 dw_scrollbar_set_range(vscrollbar, num_lines, rows); 326 dw_scrollbar_set_range(vscrollbar, num_lines, rows);
321 dw_scrollbar_set_pos(vscrollbar, 0); 327 dw_scrollbar_set_pos(vscrollbar, 0);
322 } 328 }
329 return errors;
323 } 330 }
324 331
325 /* When hpma is not NULL we are printing.. so handle things differently */ 332 /* When hpma is not NULL we are printing.. so handle things differently */
326 void draw_file(int row, int col, int nrows, int fheight, HPIXMAP hpma) 333 void draw_file(int row, int col, int nrows, int fheight, HPIXMAP hpma)
327 { 334 {
611 { 618 {
612 char *tmp; 619 char *tmp;
613 tmp = dw_file_browse("Pick a file", "dwtest.c", "c", DW_FILE_OPEN); 620 tmp = dw_file_browse("Pick a file", "dwtest.c", "c", DW_FILE_OPEN);
614 if(tmp) 621 if(tmp)
615 { 622 {
616 HWND notification = dw_notification_new("New file loaded", "image/test.png", "dwtest loaded \"%s\" into the file browser on the Render tab, with \"File Display\" selected from the drop down list.", tmp); 623 char *errors = read_file(tmp);
624 char *title = "New file load";
625 char *image = "image/test.png";
626 HWND notification;
627
628 if(errors)
629 notification = dw_notification_new(title, image,"dwtest failed to load \"%s\" into the file browser, %s.", tmp, errors);
630 else
631 notification = dw_notification_new(title, image,"dwtest loaded \"%s\" into the file browser on the Render tab, with \"File Display\" selected from the drop down list.", tmp);
617 632
618 if(current_file) 633 if(current_file)
619 {
620 dw_free(current_file); 634 dw_free(current_file);
621 }
622 current_file = tmp; 635 current_file = tmp;
623 dw_window_set_text(entryfield, current_file); 636 dw_window_set_text(entryfield, current_file);
624 read_file();
625 current_col = current_row = 0; 637 current_col = current_row = 0;
626 render_draw(); 638 render_draw();
627 dw_signal_connect(notification, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(notification_clicked_callback), NULL); 639 dw_signal_connect(notification, DW_SIGNAL_CLICKED, DW_SIGNAL_FUNC(notification_clicked_callback), NULL);
628 dw_notification_send(notification); 640 dw_notification_send(notification);
629 } 641 }