Skip to content

Commit bb10539

Browse files
committed
COMMON: Fixed TLOAD to work correctly with TRY/CATCH
1 parent 9951cca commit bb10539

File tree

5 files changed

+107
-31
lines changed

5 files changed

+107
-31
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
Fixed issues with TRY/CATCH
3131
Fixed using POINT to retrieve IMAGE data
3232
Fixed issues with CHAIN
33+
Fixed TLOAD to work correctly with TRY/CATCH
3334

3435
2016-02-11
3536
Added export to mobile command (SDL)

samples/distro-examples/tests/trycatch.bas

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,62 @@ end try
131131
if (!caughtError) then
132132
throw "Error not caught!!!"
133133
endif
134+
135+
' some more tests from Shain
136+
137+
Found = 0
138+
139+
Try
140+
Tload "__xyz__abc__0123", lines
141+
? "Error"
142+
Catch err
143+
Found++
144+
End Try
145+
146+
Try
147+
Tsave ":foo-name:/~~~", lines
148+
? "Error"
149+
Catch err
150+
Found++
151+
End Try
152+
153+
' catch file error outside sub or function
154+
Try
155+
Open "xyz__xyz__012" For Input As #1
156+
? "Error"
157+
Catch err
158+
Found++
159+
End Try
160+
161+
' catch file error within function
162+
Func open_safe(filename)
163+
Local fn = Freefile
164+
Try
165+
Open filename For Input As #fn
166+
open_safe = fn
167+
? "Error!"
168+
Catch err
169+
Found++
170+
open_safe = 0
171+
End Try
172+
End Func
173+
174+
' catch file error within nested function:
175+
Func call_safe(filename)
176+
Local fn
177+
fn = open_safe(filename)
178+
call_safe = fn
179+
End Func
180+
181+
' catch file error within function:
182+
fn = open_safe("xyz__xyz__012")
183+
184+
' catch file error within nested functions
185+
fn = call_safe("xyz__xyz__012")
186+
187+
if (Found != 5) then
188+
throw "Failed: " + Found
189+
endif
190+
191+
192+

src/common/blib_db.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -551,19 +551,9 @@ void cmd_mkdir() {
551551
*/
552552
#define LDLN_INC 256
553553
#define GROW_SIZE 1024
554-
#define BUFMAX 256
555-
556-
#define CHK_ERR_CLEANUP(s) \
557-
if (prog_error) { \
558-
v_free(&file_name); \
559-
rt_raise(s); \
560-
return; \
561-
}
562-
#define CHK_ERR(s) \
563-
if (prog_error) { \
564-
rt_raise(s); \
565-
return; \
566-
}
554+
#define BUFMAX 256
555+
#define CHK_ERR_CLEANUP(s) if (err_handle_error(s, &file_name)) return;
556+
#define CHK_ERR(s) if (err_handle_error(s, NULL)) return;
567557

568558
void cmd_floadln() {
569559
var_t file_name, *array_p = NULL, *var_p = NULL;
@@ -574,6 +564,7 @@ void cmd_floadln() {
574564
int eof, eol, bufLen, bufIndex;
575565
dword unreadBytes;
576566

567+
err_reset();
577568
if (code_peek() == kwTYPE_SEP) {
578569
// "filename" is an already open file number
579570
flags = 0;
@@ -655,7 +646,7 @@ void cmd_floadln() {
655646
unreadBytes -= bufLen;
656647

657648
dev_fread(handle, (byte *)buf, bufLen);
658-
if (prog_error) {
649+
if (err_has_error()) {
659650
eof = 1;
660651
break;
661652
}
@@ -675,7 +666,8 @@ void cmd_floadln() {
675666
}
676667
} // read line
677668

678-
if (prog_error) { // clear & exit
669+
if (err_has_error()) {
670+
// clear & exit
679671
v_free(array_p);
680672
v_init(array_p);
681673
break;
@@ -724,6 +716,7 @@ void cmd_fsaveln() {
724716
int flags = DEV_FILE_OUTPUT;
725717
int handle, i;
726718

719+
err_reset();
727720
if (code_peek() == kwTYPE_SEP) {
728721
// "filename" is an already open file number
729722
flags = 0;

src/common/sberr.c

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <string.h>
1515
#include <errno.h>
1616

17+
int error_caught;
18+
1719
/**
1820
* common message handler
1921
*/
@@ -145,18 +147,6 @@ void err_parm_num(int found, int expected) {
145147
rt_raise(ERR_PARAM_NUM, found, expected);
146148
}
147149

148-
void err_file(dword code) {
149-
char buf[1024], *p;
150-
151-
strcpy(buf, strerror(code));
152-
p = buf;
153-
while (*p) {
154-
*p = to_upper(*p);
155-
p++;
156-
}
157-
err_throw(FSERR_FMT, code, buf);
158-
}
159-
160150
void err_stackoverflow(void) {
161151
rt_raise(ERR_STACK_OVERFLOW);
162152
}
@@ -435,6 +425,7 @@ void err_throw_str(const char *err) {
435425
prog_error = 0x80;
436426
err_common_msg(WORD_RTE, prog_file, prog_line, err);
437427
}
428+
error_caught = caught;
438429
}
439430

440431
// throw internal error
@@ -468,3 +459,36 @@ void cmd_throw() {
468459
}
469460
}
470461

462+
void err_file(dword code) {
463+
if (!gsb_last_error) {
464+
char *err = malloc(SB_TEXTLINE_SIZE + 1);
465+
sprintf(err, FSERR_FMT, code, strerror(code));
466+
strupper(err);
467+
err_throw_str(err);
468+
free(err);
469+
}
470+
}
471+
472+
void err_reset() {
473+
error_caught = 0;
474+
}
475+
476+
int err_has_error() {
477+
return error_caught || prog_error;
478+
}
479+
480+
int err_handle_error(const char *err, var_p_t var) {
481+
int result;
482+
if (error_caught == 1) {
483+
result = 1;
484+
} else if (prog_error) {
485+
rt_raise(err);
486+
result = 1;
487+
} else {
488+
result = 0;
489+
}
490+
if (result && var) {
491+
v_free(var);
492+
}
493+
return result;
494+
}

src/common/sberr.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,13 @@ void err_ref_var();
7171
void err_ref_circ_var();
7272
void err_array();
7373
void err_form_input();
74-
75-
#define err_type_mismatch() err_typemismatch()
76-
7774
void inf_done(void);
7875
void inf_break(int pline);
79-
8076
void err_throw(const char *fmt, ...);
8177
void cmd_throw();
78+
void err_reset();
79+
int err_handle_error(const char *err, var_p_t var);
80+
int err_has_error();
8281

8382
#if defined(__cplusplus)
8483
}

0 commit comments

Comments
 (0)