Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 875b195

Browse files
authored
V081
Expose CSV2LIST helper, that will generate a LIST from a comma separated string
1 parent 927ee44 commit 875b195

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

Sources/GridShell/CGridShell.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ std::tuple<int, String> CGridShell::Run(const String& rstrBASFile, const String&
378378
// Additional functions
379379
mb_register_func(bas, "READ", _read); // V08
380380
mb_register_func(bas, "READLINE", _readline); // V081
381+
mb_register_func(bas, "CSV2LIST", _csvtolist); // V081
381382
mb_register_func(bas, "WRITE", _write); // V08
382383
mb_register_func(bas, "SHA1", _sha1); // V08
383384
mb_register_func(bas, "SHA256", _sha256); // V08

Sources/GridShell/CGridShell.h

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ static int _sha1(struct mb_interpreter_t* s, void** l) {
198198
int result = MB_FUNC_OK;
199199

200200
mb_check(mb_attempt_open_bracket(s, l));
201-
202201
char* m;
203202

204203
mb_check(mb_pop_string(s, l, &m));
@@ -211,6 +210,53 @@ static int _sha1(struct mb_interpreter_t* s, void** l) {
211210
mb_check(mb_push_string(s, l, mb_memdup(buf, (unsigned)(strlen(buf) + 1))));
212211
return result;
213212
}
213+
/*---------*/
214+
static int _csvtolist(struct mb_interpreter_t* s, void** l) {
215+
int result = MB_FUNC_OK;
216+
217+
mb_check(mb_attempt_open_bracket(s, l));
218+
219+
char* strInputString;
220+
221+
mb_check(mb_pop_string(s, l, &strInputString));
222+
mb_check(mb_attempt_close_bracket(s, l));
223+
224+
std::vector<String> vValues;
225+
String strInput(strInputString);
226+
int start = 0;
227+
int end = strInput.indexOf(',');
228+
229+
while (end != -1) {
230+
vValues.push_back(strInput.substring(start, end));
231+
start = end + 1;
232+
end = strInput.indexOf(',', start);
233+
}
234+
235+
// Add the last substring (or the entire string if no comma was found)
236+
vValues.push_back(strInput.substring(start));
237+
238+
mb_value_t val;
239+
mb_make_nil(val);
240+
241+
mb_value_t coll;
242+
coll.type = MB_DT_LIST;
243+
mb_init_coll(s, l, &coll);
244+
245+
int feedback_index = 0;
246+
for (size_t i = 0; i < vValues.size(); ++i) {
247+
mb_value_t mb_feedback_index;
248+
mb_value_t feedback_value;
249+
mb_make_int(mb_feedback_index, feedback_index++);
250+
mb_make_string(feedback_value, const_cast<char*>(vValues[i].c_str()));
251+
mb_set_coll(s, l, coll, mb_feedback_index, feedback_value);
252+
}
253+
254+
255+
// Push the comparison result onto the stack
256+
mb_check(mb_push_value(s, l, coll));
257+
return result;
258+
}
259+
214260
/*---------*/
215261
static int _hextobin(struct mb_interpreter_t* s, void** l) {
216262
int result = MB_FUNC_OK;

0 commit comments

Comments
 (0)