You asked for it ;P

Method 1: Copying a known-length string (missing null terminator) into a 
          string, with terminator.

        Char *p, *q, *srcstr, *deststr;
        UInt16 i, strlength;

        deststr = (Char *)MemPtrNew(sizeof(Char)*(strlength+1));
        for ( i = 0, p = srcstr, q = deststr; i < strlength; i++ ) {
          &q++ = &p++; // copy the string
        } &q   = 0;    // add the null

Method 2: Resizing the pointer.  No clue if this one would work (I've had 
          nothing but problems resizing pointers, so...)

        Char *srcstr;
        UInt16 strlength;
        MemPtrResize(&srcstr, strlength+1);
        &(srcstr+strlength) = 0; // &(var+i) is always faster than var[i]

Method 3: Adding a null to a fixed-size buffer.  The cheap way.

        Char buffer[BUFFER_SIZE];
        MemSet(buffer, BUFFER_SIZE, 0); // fill with nulls ;)
        // Write to the buffer somehow.  The null is already there ;)

Method 3: Adding a null to a fixed-size buffer after writing with a running 
          pointer.  I love pointers.

        Char buffer[BUFFER_SIZE], *p = buffer;
        while (blah) {
          &p++ = input; // write some stuff to the buffer this way
        } &p   = 0;     // add a null

Method 4: Using memory moves.

        Char *src, *dest;
        UInt16 strlength;
        dest = (Char *)MemPtrNew(sizeof(Char)*(strlength+1));
        MemMove(dest, src, strlength); // copy the string
        dest[strlength] = 0; // add the null

Method 5: The method I posted before.  This is pretty much complete code to 
          pack an unlimited number of strings.  Pointers a-go-go!

        Char *p, *q;
        Char *packed;
        Char *field[NUM_FIELDS];        // NUM_FIELDS is a define somewhere...
        UInt32 packed_size = 0;
        UInt16 i;                       // current field (had to be short)

        // First pass, used to allocate the packed string.      
        for ( i = 0, i < NUM_FIELDS; i++ )
          packed_size += StrLen(field[i]) + 1;

        // Allocate the packed string... check for error?
        packed = (Char *)MemPtrNew(sizeof(Char) * packed_size);
        p = packed;                     // packed running pointer

        for ( i = 0, i < NUM_FIELDS; i++ ) {
          q = field[i];                 // field running pointer
          while ( &q != 0 ) {
            &p++ = &q++;                // copy the string
          } &p++ = 0;                   // add the NULL
        }

Enjoy!  Beware, though, that buffers are prone to buffer overflow.
        Matthew Bevan

P.s. to copy strings very quickly, you could do it in 32-bit reads/writes.  
You'd just have to make sure that you mod the length by 4, and deal with 
anything left over...  32-bit reads/writes are very good when copying 
(blitting) large amounts of video memory, so I presume they'd be helpful when 
copying other chunks of memory too.

-- 
Matthew (Darkstorm) Bevan       [EMAIL PROTECTED]
Margin Software, NECTI.         http://www.marginsoftware.com
        Re-inventing the wheel, every time.

 - ... at least I thought I was dancing, 'til somebody stepped on my hand.
                -- J. B. White


--
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to