[1572] in Coldmud discussion meeting

root meeting help first previous next last

explode_quoted native code

daemon@ATHENA.MIT.EDU (Mon May 1 12:06:21 2000 )

Date: Mon, 1 May 2000 08:45:43 -0700 (PDT)
From: Jeremy Weatherford <xidus@xidus.net>
To: coldstuff@cold.org
Reply-To: coldstuff@cold.org


Here's the code for an explode_quoted in C.  I'll put together a patchfile
later for those who aren't familiar with adding native methods.

It's a little idiosyncratic, but it works the way -I- want it to work.  ;)

;explode_quoted("\"bork bork\" bork\"froon \"froon\"bork\" bork bork");
=> ["bork bork", "bork\"froon", "froon\"bork", "bork", "bork"]

Your mileage may vary, let me know if you feel strongly that it should
work differently.

The code:

(in src/ops/string.c)
COLDC_FUNC(explode_quoted) {
    cData * args;
    cList * exploded;

    /* Accept a string to explode and an optional string for the word
     * separator. */
    if (!func_init_1(&args, STRING))
	return;

    exploded = strexplodequoted(args[0].u.str);

    /* Pop the arguments and push the list onto the stack. */
    pop(1);
    push_list(exploded);
    list_discard(exploded);
}

(in src/strutil.c)
cList * strexplodequoted(cStr * str) {
    char     * s = string_chars(str),
             * p = s,
             * q;
    Int        len = string_length(str);
    cList   * list = list_new(0);
    cStr * word;
    cData     d;

    d.type = STRING;

    while (*p) {
      while (*p == ' ')
	p++;
      if (*p == '"') {
	p++;
	q = p;
	while (*q && !(*q == '"' && (*(q+1) == ' ' || !*(q+1))))
	  q++;
	ADD_WORD((p, q-p));
	p = q+1; /* after end quote */
      } else if (*p) {
	q = p;
	while (*q && (*q != ' ')) /* stop on space */
	  q++;
	ADD_WORD((p, q-p));
	p = q;
      }
    }

    return list;
}

(in src/include/strutil.h)
cList   * strexplodequoted(cStr * str);

(in src/include/functions.h)
COLDC_FUNC(explode_quoted);

Also add it to the array in src/opcodes.c, and add a F_EXPLODE_QUOTED
token in src/include/parse.h

Speaking of which, is it reasonable to just pick the highest unused token
value and bump LAST_TOKEN up by one?  If not, how should I add new
functions?

Patchfile forthcoming, probably around 2pmPST today.

Oh, and... I'm working on a new minimal core of my own, just to figure out
how things work.  It's structured quite a bit differently from ColdCore,
and I think it'll turn out to be quite useful.  Let me know by e-mail if
you're interested in a copy once I get something useful.  It's 814 lines
and 15k right now, and I sort of have a working login interface and
parser.  :)

Jeremy Weatherford
xidus@xidus.net
http://xidus.net