Re: More arguments?

From: Daniel A. Koepke (dkoepke@circlemud.org)
Date: 01/08/01


On Mon, 8 Jan 2001, Shane P. Lee wrote:

> I want to make a command that accepts four different arguments,
> but since I have been unable to find another command that does
> this, I'm left in the dark...

The same way you make commands of one, two, three, etc. arguments.  And
there is a command that takes four arguments:

  set file <player> <field> <value>

But that's not the best code to look at.  This

  char arg1[MAX_INPUT_LENGTH];
  char arg2[MAX_INPUT_LENGTH];
  char arg3[MAX_INPUT_LENGTH];
  char arg4[MAX_INPUT_LENGTH];

  argument = one_argument(argument, arg1);
  argument = one_argument(argument, arg2);
  argument = one_argument(argument, arg3);
  argument = one_argument(argument, arg4);

will do it, without error checking, of course.  You could use
two_arguments() to tighten that up length-wise:

  argument = two_arguments(argument, arg1, arg2);
  argument = two_arguments(argument, arg3, arg4);

or get really unreadable:

  argument = two_arguments(two_arguments(argument,arg1,arg2),arg3,arg4);

Anyway, point is, getting any number of arguments is really quite simple.
After storing the first space-delimited token from its first argument in
the char array you specify, one_argument() returns a pointer to the rest
of the string.  For example, let argument equal "one two three":

  one_argument(argument, arg);
  ==> arg: "one"
  ==> argument: "one two three"
  ==> return value: " two three"

  argument = one_argument(argument, arg);
  ==> arg: "one"
  ==> argument: " two three"
  ==> return value: " two three"

  argument = one_argument(argument, arg);
  ==> arg: "two"
  ==> argument: " three"
  ==> return value: " three"

  one_argument(argument, arg);
  ==> arg: "three"
  ==> argument: " three"
  ==> return value: ""

Note that the first and second calls above both store 'one' in arg.  This
is because argument is not changed by one_argument(), unless we store the
return value in it.  This can also be seen by argument remaining " three"
after the last one_argument() call.

Another function that works similarly is half_chop(), except instead of
returning a pointer to the remaining string, it stores the remainder in
its third argument.  For example, assuming argument is "one two three":

  half_chop(argument, arg, rest);
  ==> arg: "one"
  ==> rest: " two three"
  ==> argument: "one two three"

Not so difficult after all, eh?  :)

-dak

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/03/01 PST