looplistconcat commands

master
lsalzman 2013-06-03 10:46:09 +03:00
parent 857337d815
commit b499920eec
1 changed files with 75 additions and 36 deletions

View File

@ -565,6 +565,12 @@ float getfvarmax(const char *name)
_GETVAR(id, ID_FVAR, name, 0);
return id->maxvalf;
}
ICOMMAND(getvarmin, "s", (char *s), intret(getvarmin(s)));
ICOMMAND(getvarmax, "s", (char *s), intret(getvarmax(s)));
ICOMMAND(getfvarmin, "s", (char *s), floatret(getfvarmin(s)));
ICOMMAND(getfvarmax, "s", (char *s), floatret(getfvarmax(s)));
bool identexists(const char *name) { return idents.access(name)!=NULL; }
ident *getident(const char *name) { return idents.access(name); }
@ -587,6 +593,8 @@ const char *getalias(const char *name)
return i && i->type==ID_ALIAS && (i->index >= MAXARGS || aliasstack->usedargs&(1<<i->index)) ? i->getstr() : "";
}
ICOMMAND(getalias, "s", (char *s), result(getalias(s)));
int clampvar(ident *id, int val, int minval, int maxval)
{
if(val < minval) val = minval;
@ -2138,6 +2146,7 @@ bool execfile(const char *cfgfile, bool msg)
delete[] buf;
return true;
}
ICOMMAND(exec, "s", (char *file), execfile(file));
const char *escapestring(const char *s)
{
@ -2160,6 +2169,15 @@ const char *escapestring(const char *s)
return buf.getbuf();
}
ICOMMAND(escape, "s", (char *s), result(escapestring(s)));
ICOMMAND(unescape, "s", (char *s),
{
int len = strlen(s);
char *d = newstring(len);
d[unescapestring(d, s, &s[len])] = '\0';
stringret(d);
});
const char *escapeid(const char *s)
{
const char *end = s + strcspn(s, "\"/;()[]@ \f\t\r\n\0");
@ -2350,11 +2368,13 @@ void concat(tagval *v, int n)
{
commandret->setstr(conc(v, n, true));
}
COMMAND(concat, "V");
void concatword(tagval *v, int n)
{
commandret->setstr(conc(v, n, false));
}
COMMAND(concatword, "V");
void result(tagval &v)
{
@ -2372,6 +2392,12 @@ void result(const char *s)
commandret->setstr(newstring(s));
}
ICOMMAND(result, "t", (tagval *v),
{
*commandret = *v;
v->type = VAL_NULL;
});
void format(tagval *args, int numargs)
{
vector<char> s;
@ -2395,6 +2421,7 @@ void format(tagval *args, int numargs)
s.add('\0');
result(s.getbuf());
}
COMMAND(format, "V");
static const char *liststart = NULL, *listend = NULL, *listquotestart = NULL, *listquoteend = NULL;
@ -2463,6 +2490,7 @@ int listlen(const char *s)
while(parselist(s)) n++;
return n;
}
ICOMMAND(listlen, "s", (char *s), intret(listlen(s)));
void at(tagval *args, int numargs)
{
@ -2477,12 +2505,14 @@ void at(tagval *args, int numargs)
}
commandret->setstr(newstring(start, end-start));
}
COMMAND(at, "si1V");
void substr(char *s, int *start, int *count, int *numargs)
{
int len = strlen(s), offset = clamp(*start, 0, len);
commandret->setstr(newstring(&s[offset], *numargs >= 3 ? clamp(*count, 0, len - offset) : len - offset));
}
COMMAND(substr, "siiN");
void sublist(const char *s, int *skip, int *count, int *numargs)
{
@ -2493,31 +2523,8 @@ void sublist(const char *s, int *skip, int *count, int *numargs)
if(len > 0 && parselist(s, start, end, list, qend)) while(--len > 0 && parselist(s, start, end, qstart, qend));
commandret->setstr(newstring(list, qend - list));
}
COMMAND(sublist, "siiN");
void getalias_(char *s)
{
result(getalias(s));
}
ICOMMAND(exec, "s", (char *file), execfile(file));
ICOMMAND(result, "t", (tagval *v),
{
*commandret = *v;
v->type = VAL_NULL;
});
COMMAND(concat, "V");
COMMAND(concatword, "V");
COMMAND(format, "V");
COMMAND(at, "si1V");
ICOMMAND(escape, "s", (char *s), result(escapestring(s)));
ICOMMAND(unescape, "s", (char *s),
{
int len = strlen(s);
char *d = newstring(len);
d[unescapestring(d, s, &s[len])] = '\0';
stringret(d);
});
ICOMMAND(stripcolors, "s", (char *s),
{
int len = strlen(s);
@ -2525,14 +2532,6 @@ ICOMMAND(stripcolors, "s", (char *s),
filtertext(d, s, true, len);
stringret(d);
});
COMMAND(substr, "siiN");
COMMAND(sublist, "siiN");
ICOMMAND(listlen, "s", (char *s), intret(listlen(s)));
COMMANDN(getalias, getalias_, "s");
ICOMMAND(getvarmin, "s", (char *s), intret(getvarmin(s)));
ICOMMAND(getvarmax, "s", (char *s), intret(getvarmax(s)));
ICOMMAND(getfvarmin, "s", (char *s), floatret(getfvarmin(s)));
ICOMMAND(getfvarmax, "s", (char *s), floatret(getfvarmax(s)));
void looplist(ident *id, const char *list, const uint *body, bool search)
{
@ -2561,6 +2560,48 @@ void looplist(ident *id, const char *list, const uint *body, bool search)
if(search) intret(-1);
if(n) poparg(*id);
}
ICOMMAND(listfind, "rse", (ident *id, char *list, uint *body), looplist(id, list, body, true));
ICOMMAND(looplist, "rse", (ident *id, char *list, uint *body), looplist(id, list, body, false));
void looplistconc(ident *id, const char *list, const uint *body, bool space)
{
if(id->type!=ID_ALIAS) return;
identstack stack;
vector<char> r;
int n = 0;
for(const char *s = list, *start, *end; parselist(s, start, end);)
{
char *val = newstring(start, end-start);
if(n++)
{
if(id->valtype == VAL_STR) delete[] id->val.s;
else id->valtype = VAL_STR;
cleancode(*id);
id->val.s = val;
if(space) r.add(' ');
}
else
{
tagval t;
t.setstr(val);
pusharg(*id, t, stack);
id->flags &= ~IDF_UNKNOWN;
}
tagval v;
executeret(body, v);
const char *vstr = v.getstr();
int len = strlen(vstr);
r.put(vstr, len);
freearg(v);
}
if(n) poparg(*id);
r.add('\0');
commandret->setstr(newstring(r.getbuf(), r.length()-1));
}
ICOMMAND(looplistconcat, "rse", (ident *id, char *list, uint *body), looplistconc(id, list, body, true));
ICOMMAND(looplistconcatword, "rse", (ident *id, char *list, uint *body), looplistconc(id, list, body, false));
void prettylist(const char *s, const char *conj)
{
@ -2596,6 +2637,7 @@ int listincludes(const char *list, const char *needle, int needlelen)
}
return -1;
}
ICOMMAND(indexof, "ss", (char *list, char *elem), intret(listincludes(list, elem, strlen(elem))));
char *listdel(const char *s, const char *del)
{
@ -2611,6 +2653,7 @@ char *listdel(const char *s, const char *del)
p.add('\0');
return newstring(p.getbuf(), p.length()-1);
}
ICOMMAND(listdel, "ss", (char *list, char *del), commandret->setstr(listdel(list, del)));
void listsplice(const char *s, const char *vals, int *skip, int *count, int *numargs)
{
@ -2639,10 +2682,6 @@ void listsplice(const char *s, const char *vals, int *skip, int *count, int *num
}
COMMAND(listsplice, "ssiiN");
ICOMMAND(listdel, "ss", (char *list, char *del), commandret->setstr(listdel(list, del)));
ICOMMAND(indexof, "ss", (char *list, char *elem), intret(listincludes(list, elem, strlen(elem))));
ICOMMAND(listfind, "rse", (ident *id, char *list, uint *body), looplist(id, list, body, true));
ICOMMAND(looplist, "rse", (ident *id, char *list, uint *body), looplist(id, list, body, false));
ICOMMAND(loopfiles, "rsse", (ident *id, char *dir, char *ext, uint *body),
{
if(id->type!=ID_ALIAS) return;