Skip to content

Commit 92acc0b

Browse files
committed
COMMON: code cleanup, fix hash
1 parent 0a2ab9d commit 92acc0b

File tree

6 files changed

+86
-61
lines changed

6 files changed

+86
-61
lines changed

samples/distro-examples/tests/hash.bas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ dim foo
33
key="blah"
44
foo(key) = "something"
55
foo("other") = 123
6+
foo(100) = "cats"
67

78
? foo(key)
89
? foo("other")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
something
2+
123
3+
[blah=something,other=123,100=cats]

src/common/brun.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ addr_t getarrayidx(var_t *array, var_t **var_hash_val) {
223223
eval(&var);
224224
IF_ERR_RETURN_0;
225225

226-
if (var.type == V_STR) {
227-
// array elemement is a string - convert array to hash
226+
if (var.type == V_STR || array->type == V_HASH) {
227+
// array elemement is a string or element is addressing a hash
228228
hash_get_value(array, &var, var_hash_val);
229229

230230
if (code_peek() == kwTYPE_LEVEL_END) {

src/common/proc.c

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ int sys_search_path(const char *path, const char *file, char *retbuf) {
5555
#else
5656
p = strchr(ps, ';');
5757
#endif
58-
if (!p)
58+
if (!p) {
5959
strcpy(cur_path, ps);
60-
else {
60+
} else {
6161
strncpy(cur_path, ps, p - ps);
6262
cur_path[p - ps] = '\0';
6363
ps = p + 1;
@@ -72,10 +72,11 @@ int sys_search_path(const char *path, const char *file, char *retbuf) {
7272
#if defined(_UnixOS)
7373
sprintf(cur_path, "%s/%s", getenv("HOME"), old_path);
7474
#else
75-
if (getenv("HOME"))
75+
if (getenv("HOME")) {
7676
sprintf(cur_path, "%s\\%s", getenv("HOME"), old_path);
77-
else
77+
} else {
7878
sprintf(cur_path, "%s\\%s", getenv("HOMEPATH"), old_path);
79+
}
7980
#endif
8081
tmp_free(old_path);
8182
}
@@ -87,11 +88,6 @@ int sys_search_path(const char *path, const char *file, char *retbuf) {
8788
#endif
8889
strcat(cur_path, file);
8990

90-
// TODO: probably in DOS/Win we must remove double dir-seps
91-
// (c:\\home\\\\user)
92-
93-
// check it
94-
// printf("sp:[%s]\n", cur_path);
9591
if (access(cur_path, R_OK) == 0) {
9692
if (retbuf) {
9793
strcpy(retbuf, cur_path);
@@ -111,7 +107,7 @@ int sys_search_path(const char *path, const char *file, char *retbuf) {
111107
* var - the variable (the X)
112108
* ip - expression's address
113109
*/
114-
void exec_usefunc(var_t * var, addr_t ip) {
110+
void exec_usefunc(var_t *var, addr_t ip) {
115111
var_t *old_x;
116112

117113
// save X
@@ -136,7 +132,7 @@ void exec_usefunc(var_t * var, addr_t ip) {
136132
* var2 - the second variable (the Y)
137133
* ip - expression's address
138134
*/
139-
void exec_usefunc2(var_t * var1, var_t * var2, addr_t ip) {
135+
void exec_usefunc2(var_t *var1, var_t *var2, addr_t ip) {
140136
var_t *old_x, *old_y;
141137

142138
// save X
@@ -168,7 +164,7 @@ void exec_usefunc2(var_t * var1, var_t * var2, addr_t ip) {
168164
* var3 - the thrid variable (the Z)
169165
* ip - expression's address
170166
*/
171-
void exec_usefunc3(var_t * var1, var_t * var2, var_t * var3, addr_t ip) {
167+
void exec_usefunc3(var_t *var1, var_t *var2, var_t *var3, addr_t ip) {
172168
var_t *old_x, *old_y, *old_z;
173169

174170
// save X
@@ -207,7 +203,6 @@ void lwrite(const char *buf) {
207203
int log_dev; /* logfile file handle */
208204
char log_name[OS_PATHNAME_SIZE + 1]; /* LOGFILE filename */
209205

210-
// //////
211206
// open
212207
#if defined(_Win32) || defined(__MINGW32__)
213208
if (getenv("SBLOG")) {
@@ -229,7 +224,6 @@ void lwrite(const char *buf) {
229224
panic("LOG: Error on creating log file");
230225
}
231226

232-
// /////////
233227
// write
234228
if (write(log_dev, buf, strlen(buf)) == -1) {
235229
panic("LOG: write failed");
@@ -248,7 +242,7 @@ void pv_write(char *str, int method, int handle) {
248242

249243
switch (method) {
250244
case PV_FILE:
251-
dev_fwrite(handle, (byte *) str, strlen(str));
245+
dev_fwrite(handle, (byte *)str, strlen(str));
252246
break;
253247
case PV_LOG:
254248
lwrite(str);
@@ -270,15 +264,15 @@ void pv_write(char *str, int method, int handle) {
270264
/*
271265
* just prints the value of variable 'var'
272266
*/
273-
void pv_writevar(var_t * var, int method, int handle) {
267+
void pv_writevar(var_t *var, int method, int handle) {
274268
char tmpsb[64];
275269

276270
// start with a clean buffer
277271
memset(tmpsb, 0, sizeof(tmpsb));
278272

279273
switch (var->type) {
280274
case V_STR:
281-
pv_write((char *) var->v.p.ptr, method, handle);
275+
pv_write((char *)var->v.p.ptr, method, handle);
282276
break;
283277
case V_UDS:
284278
uds_write(var, method, handle);
@@ -302,7 +296,6 @@ void pv_writevar(var_t * var, int method, int handle) {
302296
// open array
303297
pv_write("[", method, handle);
304298

305-
//
306299
if (var->v.a.maxdim == 2) {
307300
int rows, cols;
308301
var_t *e;
@@ -317,22 +310,24 @@ void pv_writevar(var_t * var, int method, int handle) {
317310
pos = i * cols + j;
318311
e = (var_t *) (var->v.a.ptr + (sizeof(var_t) * pos));
319312
pv_writevar(e, method, handle);
320-
if (j != cols - 1)
313+
if (j != cols - 1) {
321314
pv_write(",", method, handle); // add space?
315+
}
322316
}
323-
if (i != rows - 1)
317+
if (i != rows - 1) {
324318
pv_write(";", method, handle); // add space?
319+
}
325320
}
326-
327321
} else {
328322
var_t *e;
329323
int i;
330324

331325
for (i = 0; i < var->v.a.size; i++) {
332326
e = (var_t *) (var->v.a.ptr + (sizeof(var_t) * i));
333327
pv_writevar(e, method, handle);
334-
if (i != var->v.a.size - 1)
328+
if (i != var->v.a.size - 1) {
335329
pv_write(",", method, handle); // add space?
330+
}
336331
}
337332
}
338333

@@ -345,21 +340,21 @@ void pv_writevar(var_t * var, int method, int handle) {
345340
/*
346341
* write a variable to console
347342
*/
348-
void print_var(var_t * v) {
343+
void print_var(var_t *v) {
349344
pv_writevar(v, PV_CONSOLE, 0);
350345
}
351346

352347
/*
353348
* write a variable to a file
354349
*/
355-
void fprint_var(int handle, var_t * v) {
350+
void fprint_var(int handle, var_t *v) {
356351
pv_writevar(v, PV_FILE, handle);
357352
}
358353

359354
/*
360355
* write a variable to log-file
361356
*/
362-
void logprint_var(var_t * v) {
357+
void logprint_var(var_t *v) {
363358
pv_writevar(v, PV_LOG, 0);
364359
}
365360

@@ -368,11 +363,7 @@ void logprint_var(var_t * v) {
368363
*/
369364
void par_skip() {
370365
byte exitf = 0, code;
371-
#if defined(ADDR16)
372-
word len;
373-
#else
374366
dword len;
375-
#endif
376367
int level = 0;
377368

378369
do {
@@ -437,14 +428,12 @@ void par_skip() {
437428
prog_ip++;
438429
}
439430
} while (!exitf);
440-
441-
// printf("exit at %d\n", prog_ip);
442431
}
443432

444433
/*
445434
* get next parameter as var_t
446435
*/
447-
void par_getvar(var_t * var) {
436+
void par_getvar(var_t *var) {
448437
byte code;
449438

450439
code = code_peek();
@@ -503,7 +492,7 @@ var_t *par_getvar_ptr() {
503492
/*
504493
* get next parameter as var_t
505494
*/
506-
void par_getstr(var_t * var) {
495+
void par_getstr(var_t *var) {
507496
byte code;
508497

509498
code = code_peek();
@@ -713,7 +702,7 @@ ipt_t par_getipt() {
713702
/*
714703
* retrieve a 2D polyline
715704
*/
716-
int par_getpoly(pt_t ** poly_pp) {
705+
int par_getpoly(pt_t **poly_pp) {
717706
pt_t *poly = NULL;
718707
var_t *var, *el;
719708
int count = 0;
@@ -799,9 +788,9 @@ int par_getpoly(pt_t ** poly_pp) {
799788

800789
for (i = j = 0; i < count; i++, j += 2) {
801790
// error check
802-
if (prog_error)
791+
if (prog_error) {
803792
break;
804-
793+
}
805794
// store point
806795
poly[i].x = v_getreal(v_elem(var, j));
807796
poly[i].y = v_getreal(v_elem(var, j + 1));
@@ -824,7 +813,7 @@ int par_getpoly(pt_t ** poly_pp) {
824813
/*
825814
* retrieve a 2D polyline (integers)
826815
*/
827-
int par_getipoly(ipt_t ** poly_pp) {
816+
int par_getipoly(ipt_t **poly_pp) {
828817
ipt_t *poly = NULL;
829818
var_t *var, *el;
830819
int count = 0;
@@ -944,7 +933,7 @@ int par_isonevar() {
944933
/*
945934
* destroy a parameter-table which was created by par_getparlist
946935
*/
947-
void par_freepartable(par_t ** ptable_pp, int pcount) {
936+
void par_freepartable(par_t **ptable_pp, int pcount) {
948937
int i;
949938
par_t *ptable;
950939

@@ -973,7 +962,7 @@ void par_freepartable(par_t ** ptable_pp, int pcount) {
973962
* YOU MUST FREE THAT TABLE BY USING par_freepartable()
974963
* IF THERE IS NO ERROR, CALL TO par_freepartable IS NOT NEEDED
975964
*/
976-
int par_getpartable(par_t ** ptable_pp, const char *valid_sep) {
965+
int par_getpartable(par_t **ptable_pp, const char *valid_sep) {
977966
byte ready, last_sep = 0;
978967
par_t *ptable;
979968
addr_t ofs;
@@ -986,11 +975,11 @@ int par_getpartable(par_t ** ptable_pp, const char *valid_sep) {
986975
*/
987976
ptable = *ptable_pp = tmp_alloc(sizeof(par_t) * 256);
988977

989-
if (valid_sep)
978+
if (valid_sep) {
990979
strcpy(vsep, valid_sep);
991-
else
980+
} else {
992981
strcpy(vsep, ",");
993-
982+
}
994983
/*
995984
* start
996985
*/
@@ -1008,18 +997,18 @@ int par_getpartable(par_t ** ptable_pp, const char *valid_sep) {
1008997

1009998
// check parameters separator
1010999
if (strchr(vsep, (last_sep = code_getnext())) == NULL) {
1011-
if (strlen(vsep) <= 1)
1000+
if (strlen(vsep) <= 1) {
10121001
err_syntaxsep(',');
1013-
else
1002+
} else {
10141003
err_syntaxanysep(vsep);
1015-
1004+
}
10161005
par_freepartable(ptable_pp, pcount);
10171006
return -1;
10181007
}
10191008
// update par.next_sep
1020-
if (pcount)
1009+
if (pcount) {
10211010
ptable[pcount - 1].next_sep = last_sep;
1022-
1011+
}
10231012
break;
10241013
case kwTYPE_VAR: // variable
10251014
ofs = prog_ip; // store IP
@@ -1063,7 +1052,7 @@ int par_getpartable(par_t ** ptable_pp, const char *valid_sep) {
10631052

10641053
/*
10651054
*/
1066-
int par_massget_type_check(char fmt, par_t * par) {
1055+
int par_massget_type_check(char fmt, par_t *par) {
10671056
switch (fmt) {
10681057
case 'S':
10691058
case 's':
@@ -1137,10 +1126,11 @@ int par_massget(const char *fmt, ...) {
11371126
fmt_p = (char *)fmt;
11381127
rqcount = optcount = 0;
11391128
while (*fmt_p) {
1140-
if (*fmt_p >= 'a')
1129+
if (*fmt_p >= 'a') {
11411130
optcount++;
1142-
else
1131+
} else {
11431132
rqcount++;
1133+
}
11441134
fmt_p++;
11451135
}
11461136

@@ -1237,7 +1227,6 @@ int par_massget(const char *fmt, ...) {
12371227
va_end(ap);
12381228
}
12391229

1240-
//
12411230
par_freepartable(&ptable, pcount);
12421231
if (prog_error) {
12431232
return -1;

src/common/var.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ void v_add(var_t *result, var_t *a, var_t *b) {
415415

416416
if (a->type == V_STR && b->type == V_STR) {
417417
result->type = V_STR;
418-
result->v.p.ptr = (byte *) tmp_alloc(strlen((char *)a->v.p.ptr) + strlen((char *)b->v.p.ptr) +
419-
1);
418+
result->v.p.ptr = (byte *)tmp_alloc(strlen((char *)a->v.p.ptr) +
419+
strlen((char *)b->v.p.ptr) + 1);
420420
strcpy((char *) result->v.p.ptr, (char *) a->v.p.ptr);
421421
strcat((char *) result->v.p.ptr, (char *) b->v.p.ptr);
422422
result->v.p.size = strlen((char *) result->v.p.ptr) + 1;
@@ -509,7 +509,8 @@ void v_set(var_t *dest, const var_t *src) {
509509
dest->const_flag = 0;
510510
switch (src->type) {
511511
case V_STR:
512-
dest->v.p.ptr = (byte *) tmp_alloc(strlen((char *)src->v.p.ptr) + 1);
512+
dest->v.p.size = strlen((char *)src->v.p.ptr) + 1;
513+
dest->v.p.ptr = (byte *) tmp_alloc(dest->v.p.size);
513514
strcpy((char *) dest->v.p.ptr, (char *) src->v.p.ptr);
514515
break;
515516

0 commit comments

Comments
 (0)