Menu

[b84aab]: / src / modules / mod_utils.c  Maximize  Restore  History

Download this file

257 lines (217 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// This file is part of SmallBASIC
//
// Module Support Routines
//
// Also provides access to var_t routines for platforms that do
// not support backlinking (such as windows).
//
// This program is distributed under the terms of the GPL v2.0 or later
// Download the GNU Public License (GPL) from www.gnu.org
//
// Copyright(C) 2000 Nicholas Christopoulos
#include "mod_utils.h"
#if defined(__linux__) && defined(_UnixOS)
#define LNX_EXTLIB
#endif
int mod_parint(int n, slib_par_t *params, int param_count, int *val) {
var_t *param;
if (n < 0 || n >= param_count) {
return 0;
}
param = params[n].var_p;
*val = v_igetval(param);
return 1;
}
int mod_opt_parint(int n, slib_par_t *params, int param_count, int *val,
int def_val) {
var_t *param;
if (n < 0) {
return 0;
}
if (n < param_count) {
param = params[n].var_p;
*val = v_igetval(param);
}
else {
*val = def_val;
}
return 1;
}
int mod_parstr_ptr(int n, slib_par_t *params, int param_count, char **ptr) {
var_t *param;
if (n < 0 || n >= param_count) {
return 0;
}
param = params[n].var_p;
if (param->type != V_STR) {
v_tostr(param);
}
*ptr = param->v.p.ptr;
return 1;
}
int mod_opt_parstr_ptr(int n, slib_par_t *params, int param_count, char **ptr,
const char *def_val) {
var_t *param;
if (n < 0) {
return 0;
}
if (n < param_count) {
param = params[n].var_p;
if (param->type != V_STR) {
v_tostr(param);
}
*ptr = param->v.p.ptr;
}
else {
*ptr = def_val;
}
return 1;
}
#if !defined(LNX_EXTLIB)
int opt_base = 0;
/*
*initialize a variable
*/
void v_init(var_t *v) {
v->type = V_INT;
v->const_flag = 0;
v->v.i = 0;
}
void v_setint(var_t *var, int32 i) {
v_free(var);
var->type = V_INT;
var->v.i = i;
}
/*
*release variable
*/
void v_free(var_t *v) {
int i;
var_t *elem;
switch (v->type) {
case V_STR:
if (v->v.p.ptr) {
tmp_free(v->v.p.ptr);
}
v->v.p.ptr = NULL;
v->v.p.size = 0;
break;
case V_ARRAY:
if (v->v.a.size) {
if (v->v.a.ptr) {
for (i = 0; i < v->v.a.size; i++) {
elem = (var_t *) (v->v.a.ptr + (sizeof(var_t) *i));
v_free(elem);
}
tmp_free(v->v.a.ptr);
v->v.a.ptr = NULL;
v->v.a.size = 0;
}
}
break;
case V_UDS:
break;
}
v_init(v);
}
/*
*returns the integer value of the variable v
*/
long v_igetval(var_t *v) {
if (v == 0) {
//err_evsyntax();
}
else {
switch (v->type) {
case V_UDS:
return 0;;
case V_PTR:
return v->v.ap.p;
case V_INT:
return v->v.i;
case V_NUM:
return v->v.n;
case V_STR:
return 0;// numexpr_strtol((char *)v->v.p.ptr);
default:
//err_varisarray();
break;
}
}
return 0;
}
/*
*converts the variable to string-variable
*/
void v_tostr(var_t *arg) {
if (arg->type != V_STR) {
char *tmp;
int l;
tmp = tmp_alloc(64);
switch (arg->type) {
case V_UDS:
break;
case V_PTR:
//ltostr(arg->v.ap.p, tmp);
break;
case V_INT:
//ltostr(arg->v.i, tmp);
break;
case V_NUM:
//ftostr(arg->v.n, tmp);
break;
default:
//err_varisarray();
tmp_free(tmp);
return;
}
l = strlen(tmp) + 1;
arg->type = V_STR;
arg->v.p.ptr = tmp_alloc(l);
arg->v.p.size = l;
strcpy(arg->v.p.ptr, tmp);
tmp_free(tmp);
}
}
void v_setstr(var_t *var, const char *string) {
v_free(var);
var->type = V_STR;
var->v.p.size = strlen(string) + 1;
var->v.p.ptr = tmp_alloc(var->v.p.size);
strcpy(var->v.p.ptr, string);
}
/*
*set the value of 'var' to string
*/
void v_setstrf(var_t *var, const char *fmt, ...) {
char *buf;
va_list ap;
va_start(ap, fmt);
buf = tmp_alloc(1024);
vsnprintf(buf, 1024, fmt, ap);
v_setstr(var, buf);
tmp_free(buf);
va_end(ap);
}
/*
*create RxC array
*/
void v_tomatrix(var_t *v, int r, int c) {
var_t *e;
int i;
v_free(v);
v->type = V_ARRAY;
// create data
v->v.a.size = r *c;
v->v.a.ptr = tmp_alloc(sizeof(var_t) *v->v.a.size);
for (i = 0; i < r *c; i++) {
e = (var_t *) (v->v.a.ptr + (sizeof(var_t) *i));
v_init(e);
}
// array info
v->v.a.lbound[0] = v->v.a.lbound[1] = opt_base;
v->v.a.ubound[0] = opt_base + (r - 1);
v->v.a.ubound[1] = opt_base + (c - 1);
v->v.a.maxdim = 2;
}
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.