From 040ec8d4822449430ac0910effdc260c4eee4284 Mon Sep 17 00:00:00 2001 From: Lucas GAUTHERON Date: Tue, 14 Jun 2011 11:20:13 +0000 Subject: [PATCH] looplist command git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@6534 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- config/docs.cfg | 5 ++++ docs/reference.xml | 19 ++++++++++++ source/src/command.cpp | 65 ++++++++++++++++++++---------------------- 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/config/docs.cfg b/config/docs.cfg index fcb64644..4c7c4b95 100644 --- a/config/docs.cfg +++ b/config/docs.cfg @@ -111,6 +111,11 @@ docargument [N] [the amount of loops] [] [0]; docargument [body] [the body to execute on each iteration] [] [0]; docremark [This command sets the alias you choose, as first argument, from 0 to N-1 for every iteration.]; docexample [loop i 10 [ echo $i ]] []; +docident [looplist] [Browses a list and executes a body for each element]; +docargument [L] [the list] [0]; +docargument [E] [the alias containing the current element] [0]; +docargument [body] [the body to execute for each element] [0]; +docexample [looplist "zero one two three" number [ echo $number ]] []; docident [mod] [Performs a modulo operation.]; docargument [A] [the dividend] [] [0]; docargument [B] [the divisor] [] [0]; diff --git a/docs/reference.xml b/docs/reference.xml index 4c5954b5..c8adddc6 100644 --- a/docs/reference.xml +++ b/docs/reference.xml @@ -612,6 +612,25 @@ + + Browses a list and executes a body for each element + + + + + + + + + + + + + + + + + Aborts a loop created with a 'loop' or 'while' command. diff --git a/source/src/command.cpp b/source/src/command.cpp index 38ad1b96..51626285 100644 --- a/source/src/command.cpp +++ b/source/src/command.cpp @@ -1044,37 +1044,6 @@ void format(char **args, int numargs) #define whitespaceskip s += strspn(s, "\n\t \r") #define elementskip *s=='"' ? (++s, s += strcspn(s, "\"\n\0"), s += *s=='"') : s += strcspn(s, "\n\t \0") -void looplista(char *s, char *var, char *body) -{ - int n = 0; - whitespaceskip; - loop_level++; - - ident *id = newident(var, execcontext); - if(id->type!=ID_ALIAS) return; - char *buf = newstring(MAXSTRLEN); - - for(; *s; n++) - { - char *e = strtok(newstring(s), "\"\n\0\t \r"); - if(buf != id->action) - { - if(id->action != id->executing) delete[] id->action; - id->action = buf = newstring(MAXSTRLEN); - } - copystring(id->action, e); - execute(body); - if(loop_skip) loop_skip = false; - if(loop_break) - { - loop_break = false; - break; - } - elementskip, whitespaceskip; - } - loop_level--; -} - void explodelist(const char *s, vector &elems) { whitespaceskip; @@ -1087,6 +1056,36 @@ void explodelist(const char *s, vector &elems) } } +void looplist(char *list, char *var, char *body) +{ + ident *id = newident(var, execcontext); + if(id->type!=ID_ALIAS) return; + char *buf = newstring(MAXSTRLEN); + + vector elems; + explodelist(list, elems); + + loop_level++; + loopv(elems) + { + const char *elem = elems[i]; + if(buf != id->action) + { + if(id->action != id->executing) delete[] id->action; + id->action = buf = newstring(MAXSTRLEN); + } + copystring(id->action, elem); + execute(body); + if(loop_skip) loop_skip = false; + if(loop_break) + { + loop_break = false; + break; + } + } + loop_level--; +} + char *indexlist(const char *s, int pos) { whitespaceskip; @@ -1114,8 +1113,6 @@ int listlen(char *s) return n; } - - void at(char *s, char *pos) { commandret = indexlist(s, ATOI(pos)); @@ -1271,7 +1268,7 @@ void strreplace (const char *source, const char *search, const char *replace) COMMANDN(c, colora, ARG_1STR); COMMANDN(loop, loopa, ARG_3STR); -COMMANDN(looplist, looplista, ARG_3STR); +COMMAND(looplist, ARG_3STR); COMMANDN(while, whilea, ARG_2STR); COMMANDN(break, breaka, ARG_NONE); COMMANDN(continue, continuea, ARG_NONE);