Server Help

ASSS Custom Projects - %macro expander

Anonymous - Mon Aug 02, 2004 4:26 pm
Post subject: %macro expander
I spent a bit of time on this and though it might be useful to other people too. The code is straight from race 1.2.
Code: Show/Hide
/* valid macros:
* name  - player's name
* lap   - laps completed so far
* chkpt - last checkpoint crossed
* rank  - position in the race, 0 if still racing
* time  - time in seconds since the race started (#.##) */

/* returns a pointer to the end of dest */
local inline char *append_string(char *dest, const char *src)
{
   while(*src) *dest++ = *src++;
   return dest;
}

/* returns a pointer to the end of dest */
local inline char *append_integer(char *dest, int num)
{
   char buf[11], *p = buf;
   snprintf(buf, 11, "%d", num);
   while(*p) *dest++ = *p++;
   return dest;
}

/* returns a pointer to the end of dest */
local inline char *append_double(char *dest, double num)
{
   char buf[11], *p = buf;
   snprintf(buf, 11, "%.2f", num);
   while(*p) *dest++ = *p++;
   return dest;
}

/* p can be NULL. returns a %sound. */
local int expand_message(Arena *arena, Player *p, const char *format,
   char *out, int outsize)
{
   pdata *d = p ? PPDATA(p, pdkey) : NULL;
   char *t = out;
   int sound = 0;

   /* keep looping until we run out of space or
    * hit the end of the format string */
   while(t - out < outsize && *format)
   {
      /* keep copying until we hit a % delimeter */
      if (*format != '%')
         *t++ = *format++;
      else
      {
         const char *macro = ++format;
         if (!strncmp(macro, "name", 4))
         {
            if (p)
            {
               t = append_string(t, p->name);
               format += 4;
            }
         }
         else if (!strncmp(macro, "lap", 3))
         {
            if (d)
            {
               t = append_integer(t, d->completedlaps);
               format += 3;
            }
         }
         else if (!strncmp(macro, "chkpt", 5))
         {
            if (d)
            {
               t = append_integer(t, d->nextchkpt);
               format += 5;
            }
         }
         else if (!strncmp(macro, "rank", 4))
         {
            if (d)
            {
               t = append_integer(t, d->rank);
               format += 4;
            }
         }
         else if (!strncmp(macro, "time", 4))
         {
            if (d)
            {
               t = append_double(t, (double)
                  (current_ticks() - d->start)
                  / 100);
               format += 4;
            }
         }
         else
         {
            /* maybe it is a number (%bong) */
            char *next;
            int num = strtol(macro, &next, 0);
            if (next != macro)
            {
               sound = num;
               format = next;
            }
         }
      }
   }

   *t = 0;
   return sound;
}
Hrm, I don't think expand_message() needs Arena *arena somehow. There could be problems if the size of out is too small.
Mine GO BOOM - Tue Aug 03, 2004 12:08 am
Post subject:
Isn't there a standard C library for this? I believe the *printf functions all use it when they want to expand %s, %d, etc. Then again, I maybe just making this up, only taking a guess since making a %macro expander seems like something that someone has already made into a standard library.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group