Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
%macro expander

 
Post new topic   Reply to topic Printable version
 View previous topic  C help Post :: Post hs_listnewb  View next topic  
Author Message
-Smong-
Guest


Offline

PostPosted: Mon Aug 02, 2004 4:26 pm    Post subject: %macro expander Reply to topic Reply with quote

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.
Back to top
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:40
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3614
Location: Las Vegas
Offline

PostPosted: Tue Aug 03, 2004 12:08 am    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List Send email
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> ASSS Custom Projects All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 655 page(s) served in previous 5 minutes.

phpBB Created this page in 0.511682 seconds : 27 queries executed (83.1%): GZIP compression disabled