Um, but this code doesn't do anything. Since "text" is allocated as the
same size as the input buffer (s), this doesn't provide any protection.
It just does an unnecessary StrNCopy. You want to call MemPtrNew with a
LARGER size, a size that's large enough to handle any formatStr.
Unfortunately, there is no such size, so the best you can do is estimate
the largest size your app will need and use that. It's unfortunate that
Palm didn't provide a printf-like variant that calls a callback function
to output each char into the buffer. Then you could do a real
StrNPrintF without an extra buffer. Of course, you could write your own
from scratch by duplicating all the logic in StrNPrintF.
--Mark
Ariel Barreiro wrote:
>
> alloc using the memory manager, there is a talk about allocking (correctly
> spelled? I dont think so), I will give you a complete example:
>
> thats better of defining static becouse when you free them, you are really
> liberating the memory as you don't with statics.
>
> #include <stdarg.h>
> void StrNPrintF(CharPtr s,UInt size, CharPtr formatStr, ...)
> {
> va_list args;
> // Char text[size];
> // Instead of using a local array, use a pointer
> char *text;
>
> // Alloc
> if ( ( text = MemPtrNew(size) ) == NULL) {
> // MALLOC_ERROR
> return; // You may change the void result for an int and return a
> custom errorNumber
> }
>
> va_start(args, formatStr);
> StrVPrintF(text, formatStr, args);
> va_end(args);
> StrNCopy(s, text, size);
>
> // Ready, Free the pointer
> MemPtrFree(text);
> }
>
> Hope it helps, Ariel
>
> ----- Original Message -----
> From: Charles Rezsonya <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Saturday, January 01, 2000 10:55 AM
> Subject: Re: StrPrintF
>
> > alloc using what? if this is using some of the dmMemPtr__ api call's,
> can
> > you provide an example? the way i prevent stack problems at the moment is
> > by declaring some static.
> >
> > -----Original Message-----
> > From: Ariel Barreiro <[EMAIL PROTECTED]>
> > To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
> > Date: Tuesday, January 04, 2000 2:46 AM
> > Subject: RE: StrPrintF
> >
> >
> > >you can build our own StrNPrintF fuction, with the StrVPrintF, the code
> > >would be something like this:
> > >
> > >#include <stdarg.h>
> > >void StrNPrintF(CharPtr s,UInt size, CharPtr formatStr, ...)
> > >{
> > >va_list args;
> > >Char text[size];
> > >va_start(args, formatStr);
> > >StrVPrintF(text, formatStr, args);
> > >va_end(args);
> > >StrNCopy(s, text, size);
> > >}
> > >
> > >hope this might give you and idea, you also shoud alloc the memory
> instead
> > >of creating a local buffer in order not to overflow the stack.
> > >
> > >Ariel.
> > >
> > >
> > >----- Original Message -----
> > >From: Charles Rezsonya <[EMAIL PROTECTED]>
> > >To: <[EMAIL PROTECTED]>
> > >Sent: Friday, December 31, 1999 11:18 AM
> > >Subject: StrPrintF
> > >
> > >
> > >> the use of StrPrintF is quite nice, i have some message construction
> > used
> > >> in a protocol and stuff, but i find it sorrowing that i can't utilize
> > the
> > >> N. as an example, if i was to do strCat's or StrCopy's i can protect
> > >> myself more with StrNCat's and StrNCopy's, but there are no
> > StrNPrintF's.
> > >> has anyone or does anyone have a good idea to use StrPrintF but have a
> N
> > >> equivilent (or any related call's?)
> > >>
> > >>
> > >>
> > >>
> > >
> > >
> >
> >
> >
> >
> >