Skip to content

Commit aa630d9

Browse files
authored
Merge pull request #59 from chrisws/0_12_10
0 12 10
2 parents cbf8b04 + b0cf981 commit aa630d9

File tree

26 files changed

+227
-134
lines changed

26 files changed

+227
-134
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2017-11-28 (0.12.10/11)
2+
COMMON: owner strings
3+
14
2017-11-28 (0.12.10)
25
COMMON: Fix some coverty warnings
36

ide/android/assets/main.bas

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const setupId = "_setup"
2525
const aboutId = "_about"
2626
const backId = "_back"
2727
const scratchId = "_scratch"
28+
const scratch_file = HOME + "scratch.bas"
2829

2930
func mk_bn(value, lab, fg)
3031
local bn
@@ -50,10 +51,9 @@ end
5051

5152
func mk_scratch()
5253
local text
53-
local file = "scratch.bas"
5454
local result = false
5555

56-
if (not exist(file)) then
56+
if (not exist(scratch_file)) then
5757
dim text
5858
text << "rem Welcome to SmallBASIC"
5959
text << "rem"
@@ -66,7 +66,7 @@ func mk_scratch()
6666
text << "rem Press the 3 vertical dots for menu options."
6767
endif
6868
try
69-
tsave file, text
69+
tsave scratch_file, text
7070
result = true
7171
catch e
7272
local wnd = window()
@@ -232,6 +232,7 @@ end
232232

233233
sub loadFileList(path, byref basList)
234234
erase basList
235+
local emptyNode
235236

236237
func walker(node)
237238
if (node.depth==0) then
@@ -246,6 +247,14 @@ sub loadFileList(path, byref basList)
246247
return node.depth == 0
247248
end
248249
dirwalk path, "", use walker(x)
250+
251+
if (path = "/" && len(basList) == 0 && !is_sdl) then
252+
emptyNode.name = "sdcard"
253+
emptyNode.dir = true
254+
emptyNode.size = ""
255+
emptyNode.mtime = 0
256+
basList << emptyNode
257+
endif
249258
end
250259

251260
sub listFiles(byref frm, path, sortDir, byref basList)
@@ -641,7 +650,7 @@ sub main
641650
frm = makeUI(path, sortDir)
642651
elif frm.value == scratchId then
643652
if (mk_scratch())
644-
frm.close("scratch.bas")
653+
frm.close(scratch_file)
645654
endif
646655
elif frm.value == backId then
647656
cls

samples/distro-examples/games/sokoban.bas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ sub main
383383
frm.inputs(0).value = game_names
384384
frm.inputs(1).label = "Open"
385385
frm.inputs(2).label = "Play"
386+
frm.focus = 0
386387
frm = form(frm)
387388
end
388389

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
TEST: Arrays, unound, lbound
2-
array: {"cat":{"name":"lots"},"zz":"thing","zz":"memleak","other":"thing"}
2+
array: {"cat":{"name":"lots"},"zz":"memleak","other":"thing"}

src/common/bc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,13 @@ void bc_add_creal(bc_t *bc, var_num_t v) {
174174
*/
175175
void bc_add_strn(bc_t *bc, const char *str, int len) {
176176
bc_add_code(bc, kwTYPE_STR);
177-
bc_add_dword(bc, len);
177+
bc_add_dword(bc, len + 1);
178178
if (bc->count + len >= bc->size) {
179179
bc_resize(bc, bc->size + BC_ALLOC_INCR + len);
180180
}
181181
memcpy(bc->ptr + bc->count, str, len);
182182
bc->count += len;
183+
bc_add1(bc, 0);
183184
}
184185

185186
/*

src/common/blib.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ void cmd_print(int output) {
562562
vuser_p->type = V_STR;
563563
vuser_p->v.p.ptr = NULL;
564564
vuser_p->v.p.length = 0;
565+
vuser_p->v.p.owner = 1;
565566
handle = (intptr_t)vuser_p;
566567
}
567568

@@ -1964,6 +1965,7 @@ void cmd_read() {
19641965
prog_dp += OS_STRLEN;
19651966

19661967
vp->v.p.ptr = malloc(len + 1);
1968+
vp->v.p.owner = 1;
19671969
memcpy(vp->v.p.ptr, prog_source + prog_dp, len);
19681970
*((char *) (vp->v.p.ptr + len)) = '\0';
19691971
vp->v.p.length = len;
@@ -2288,6 +2290,7 @@ void cmd_wjoin() {
22882290
v_free(str);
22892291
str->type = V_STR;
22902292
str->v.p.ptr = malloc(size);
2293+
str->v.p.owner = 1;
22912294
str->v.p.ptr[0] = '\0';
22922295

22932296
for (i = 0; i < v_asize(var_p); i++) {

src/common/blib_db.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,7 @@ void cmd_floadln() {
589589
// build var for line
590590
var_p = v_elem(array_p, index);
591591
int size = GROW_SIZE;
592-
var_p->type = V_STR;
593-
var_p->v.p.ptr = malloc(size);
592+
v_init_str(var_p, size);
594593
index++;
595594

596595
// process the next line
@@ -654,9 +653,7 @@ void cmd_floadln() {
654653
} else { // if type=1
655654
// build string
656655
v_free(var_p);
657-
var_p->type = V_STR;
658-
var_p->v.p.length = dev_flength(handle) + 1;
659-
var_p->v.p.ptr = malloc(var_p->v.p.length);
656+
v_init_str(var_p, dev_flength(handle));
660657
if (var_p->v.p.length > 1) {
661658
dev_fread(handle, (byte *)var_p->v.p.ptr, var_p->v.p.length - 1);
662659
}

src/common/blib_func.c

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -933,8 +933,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
933933
// str <- CBS$(str)
934934
// convert C-Style string to BASIC-style string
935935
//
936-
v_tostr(arg);
937-
IF_ERR_RETURN;
938936
r->v.p.ptr = cstrdup(arg->v.p.ptr);
939937
r->v.p.length = strlen(r->v.p.ptr) + 1;
940938
break;
@@ -943,9 +941,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
943941
// str <- BCS$(str)
944942
// convert BASIC-Style string to C-style string
945943
//
946-
v_tostr(arg);
947-
IF_ERR_RETURN;
948-
949944
r->v.p.ptr = bstrdup(arg->v.p.ptr);
950945
r->v.p.length = strlen(r->v.p.ptr) + 1;
951946
break;
@@ -988,8 +983,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
988983
//
989984
// str <- LCASE$(s)
990985
//
991-
v_tostr(arg);
992-
IF_ERR_RETURN;
993986
r->v.p.ptr = (char *)malloc(strlen(arg->v.p.ptr) + 1);
994987
strcpy(r->v.p.ptr, arg->v.p.ptr);
995988
p = r->v.p.ptr;
@@ -1003,8 +996,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
1003996
//
1004997
// str <- UCASE$(s)
1005998
//
1006-
v_tostr(arg);
1007-
IF_ERR_RETURN;
1008999
r->v.p.ptr = (char *)malloc(strlen(arg->v.p.ptr) + 1);
10091000
strcpy(r->v.p.ptr, arg->v.p.ptr);
10101001
p = r->v.p.ptr;
@@ -1018,8 +1009,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
10181009
//
10191010
// str <- LTRIM$(s)
10201011
//
1021-
v_tostr(arg);
1022-
IF_ERR_RETURN;
10231012
p = arg->v.p.ptr;
10241013
while (is_wspace(*p)) {
10251014
p++;
@@ -1036,8 +1025,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
10361025
//
10371026
// str <- RTRIM$(s)
10381027
//
1039-
v_tostr(arg);
1040-
IF_ERR_RETURN;
10411028
p = arg->v.p.ptr;
10421029
if (*p != '\0') {
10431030
while (*p) {
@@ -1144,8 +1131,6 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
11441131
//
11451132
// str <- ENVIRON$(str)
11461133
//
1147-
v_tostr(arg);
1148-
IF_ERR_RETURN;
11491134
if (*arg->v.p.ptr != '\0') {
11501135
// return the variable
11511136
const char *v = dev_getenv(arg->v.p.ptr);
@@ -1293,6 +1278,7 @@ void cmd_str0(long funcCode, var_t *r) {
12931278
tms = *localtime(&now);
12941279
r->type = V_STR;
12951280
r->v.p.ptr = malloc(32);
1281+
r->v.p.owner = 1;
12961282
sprintf(r->v.p.ptr, "%02d/%02d/%04d", tms.tm_mday, tms.tm_mon + 1, tms.tm_year + 1900);
12971283
r->v.p.length = strlen(r->v.p.ptr) + 1;
12981284
break;
@@ -1304,6 +1290,7 @@ void cmd_str0(long funcCode, var_t *r) {
13041290
tms = *localtime(&now);
13051291
r->type = V_STR;
13061292
r->v.p.ptr = malloc(32);
1293+
r->v.p.owner = 1;
13071294
sprintf(r->v.p.ptr, "%02d:%02d:%02d", tms.tm_hour, tms.tm_min, tms.tm_sec);
13081295
r->v.p.length = strlen(r->v.p.ptr) + 1;
13091296
break;
@@ -2216,9 +2203,7 @@ void cmd_genfunc(long funcCode, var_t *r) {
22162203

22172204
if (funcCode == kwDATEFMT) {
22182205
// format
2219-
r->type = V_STR;
2220-
r->v.p.ptr = date_fmt(arg.v.p.ptr, d, m, y);
2221-
r->v.p.length = strlen(r->v.p.ptr) + 1;
2206+
v_move_str(r, date_fmt(arg.v.p.ptr, d, m, y));
22222207
v_free(&arg);
22232208
} else {
22242209
// weekday
@@ -2247,6 +2232,7 @@ void cmd_genfunc(long funcCode, var_t *r) {
22472232
r->type = V_STR;
22482233
r->v.p.ptr = malloc((count << 1) + 1);
22492234
r->v.p.ptr[0] = '\0';
2235+
r->v.p.owner = 1;
22502236
len = 0;
22512237
char tmp[3];
22522238
for (int i = 0; i < count; i++) {
@@ -2279,9 +2265,7 @@ void cmd_genfunc(long funcCode, var_t *r) {
22792265
r->v.p.ptr[len] = '\0';
22802266
} else {
22812267
// file
2282-
r->type = V_STR;
2283-
r->v.p.ptr = malloc(count + 1);
2284-
r->v.p.length = count + 1;
2268+
v_init_str(r, count);
22852269
dev_fread(handle, (byte *)r->v.p.ptr, count);
22862270
r->v.p.ptr[count] = '\0';
22872271
}
@@ -2837,9 +2821,7 @@ void cmd_genfunc(long funcCode, var_t *r) {
28372821
// add the entries
28382822
for (int i = 0; i < count; i++) {
28392823
var_t *elem_p = v_elem(r, i);
2840-
elem_p->type = V_STR;
2841-
elem_p->v.p.length = strlen(list[i]) + 1;
2842-
elem_p->v.p.ptr = malloc(elem_p->v.p.length);
2824+
v_init_str(elem_p, strlen(list[i]));
28432825
strcpy(elem_p->v.p.ptr, list[i]);
28442826
}
28452827
} else {

src/common/brun.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -269,37 +269,38 @@ void exec_setup_predefined_variables() {
269269
setsysvar_var(SYSVAR_NONE, 0, V_NIL);
270270
setsysvar_num(SYSVAR_MAXINT, VAR_MAX_INT);
271271

272-
#if defined(_UnixOS)
272+
#if defined(_ANDROID)
273+
strcpy(homedir, "/sdcard/");
274+
#else
275+
#if defined(_Win32)
276+
if (getenv("HOMEPATH")) {
277+
strlcpy(homedir, getenv("HOMEPATH"), sizeof(homedir));
278+
}
279+
else {
280+
GetModuleFileName(NULL, homedir, sizeof(homedir) - 1);
281+
char *p = strrchr(homedir, '\\');
282+
if (p) {
283+
*p = '\0';
284+
}
285+
}
286+
for (char *p = homedir; *p; p++) {
287+
if (*p == '\\') {
288+
*p = '/';
289+
}
290+
}
291+
#elif defined(_UnixOS)
273292
if (getenv("HOME")) {
274293
strlcpy(homedir, getenv("HOME"), sizeof(homedir));
275294
}
276295
else {
277296
strcpy(homedir, "/tmp/");
278297
}
298+
#endif
279299
int l = strlen(homedir);
280300
if (homedir[l - 1] != OS_DIRSEP) {
281301
homedir[l] = OS_DIRSEP;
282302
homedir[l + 1] = '\0';
283303
}
284-
#elif defined(_Win32)
285-
if (getenv("HOME")) {
286-
// this works on cygwin
287-
strlcpy(homedir, getenv("HOME"), sizeof(homedir));
288-
}
289-
else {
290-
GetModuleFileName(NULL, homedir, sizeof(homedir) - 1);
291-
char *p = strrchr(homedir, '\\');
292-
*p = '\0';
293-
strcat(homedir, "\\");
294-
if (OS_DIRSEP == '/') {
295-
p = homedir;
296-
while (*p) {
297-
if (*p == '\\')
298-
*p = '/';
299-
p++;
300-
}
301-
}
302-
}
303304
#endif
304305
setsysvar_str(SYSVAR_HOME, homedir);
305306
}
@@ -1643,7 +1644,8 @@ void sbasic_exec_prepare(const char *filename) {
16431644
* remember the directory location of the running program
16441645
*/
16451646
void sbasic_set_bas_dir(const char *bas_file) {
1646-
int path_len = strrchr(bas_file, OS_DIRSEP) - bas_file;
1647+
const char *sep = strrchr(bas_file, OS_DIRSEP);
1648+
int path_len = sep == NULL ? 0 : (sep - bas_file);
16471649
char cwd[OS_PATHNAME_SIZE + 1];
16481650

16491651
cwd[0] = '\0';

src/common/eval.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ static inline void eval_push(var_t *r) {
795795
len = r->v.p.length;
796796
eval_stk[eval_sp].type = V_STR;
797797
eval_stk[eval_sp].v.p.ptr = malloc(len + 1);
798+
eval_stk[eval_sp].v.p.owner = 1;
798799
strcpy(eval_stk[eval_sp].v.p.ptr, r->v.p.ptr);
799800
eval_stk[eval_sp].v.p.length = len;
800801
break;
@@ -852,6 +853,7 @@ static inline void eval_callf_str1(long fcode, var_t *r) {
852853
IP++;
853854
r->type = V_STR;
854855
r->v.p.ptr = NULL;
856+
r->v.p.owner = 1;
855857
cmd_str1(fcode, &vtmp, r);
856858
v_free(&vtmp);
857859
}
@@ -865,6 +867,7 @@ static inline void eval_callf_strn(long fcode, var_t *r) {
865867
err_missing_lp();
866868
} else {
867869
r->type = V_STR;
870+
r->v.p.owner = 1;
868871
r->v.p.ptr = NULL;
869872
IP++; // '('
870873
cmd_strN(fcode, r);

0 commit comments

Comments
 (0)