Thanks for the FYI on the memory leak. I hate "static" assignments though,
because the stay there too. Any other suggestions??

-V

----- Original Message ----- 
From: "Hiddukel" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, February 12, 2004 5:36 PM
Subject: RE: format number function


> This is the best solution I've seen thus far.  I got to digging and I
> actually have a strip_color function that works in exactly the same way as
> this.  BTW valnir ... the str_dup in your version of the function does
work,
> but it will allocate memory each time it is called and will never free it
> ... huge memory leak.
>
> Thanks for all the help on this *bows to the gurus*.
>
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Ryan
> Jennings
> Sent: Thursday, February 12, 2004 5:03 PM
> To: ROM Mailing List
> Subject: RE: format number function
>
> You can use a static multi-buffer output.
> new function would be like:
>
> char *fnum(long number)
> {
>      int index,index_new,rest;
>      char buf[16];
>      static char buf_new[5][16]; // allows for 5 consecutive
> calls
>      static int I; // increment counter
>      char *result;
>
>      sprintf(buf, "%ld", number);
>      rest = strlen(buf)%3;
>
>      ++I, I %= 5; // increment the buffer
>      result = buf_new[I];
>
>      for (index = index_new = 0;index < strlen(buf); index++,
> index_new++)
>      {
>           if (index != 0 && (index - rest) %3 == 0 )
>           {
>    result[index_new]=',';
>                index_new++;
>    result[index_new] = buf[index];
>           }
>           else
>    result[index_new] = buf[index];
>      }
>      result[index_new] = '\0';
>      return result;
> }
>
> You could also make it more fun by passing a CHAR_DATA pointer and
> making it plr configurable.
>
> Markanth <[EMAIL PROTECTED]>
>
> > I have a function that I got from a snippet that I need a
> > little help with. It seems to work fine the first time it is
> > called but not the second time. Here is the code I was using
> > to test it with:
> >
> > printf_to_ch(ch, "Your current balance is: %s Steel and %s
> > Gold.\n\r", fnum(1000), fnum(2000));
> >
> > What this outputs is:
> >
> > Your current balance is: 1,000 Steel and 1,000 Gold.
> >
> > When it should be:
> >
> > Your current balance is: 1,000 Steel and 2,000 Gold.
> >
> > Here is the fnum function in question:
> >
> > char *fnum(long number)
> > {
> >      int index,index_new,rest;
> >      char buf[16];
> >      static char buf_new[16];
> >
> >      sprintf(buf, "%ld", number);
> >      rest = strlen(buf)%3;
> >
> >      for (index = index_new = 0;index < strlen(buf); index++,
> > index_new++)
> >      {
> >           if (index != 0 && (index - rest) %3 == 0 )
> >           {
> >                buf_new[index_new]=',';
> >                index_new++;
> >                buf_new[index_new] = buf[index];
> >           }
> >           else
> >                buf_new[index_new] = buf[index];
> >      }
> >      buf_new[index_new] = '\0';
> >      return buf_new;
> > }
> >
> > Any help on this would be very much appreciated.
>
>
> -- 
> ROM mailing list
> [email protected]
> http://www.rom.org/cgi-bin/mailman/listinfo/rom
>
>
>
> -- 
> ROM mailing list
> [email protected]
> http://www.rom.org/cgi-bin/mailman/listinfo/rom
>


Reply via email to