Previous Page
Next Page

wmemcpy

Copies the contents of a block of wide characters

#include <wchar.h>
wchar_t *wmemcpy ( wchar_t * restrict dest , const wchar_t * restrict src ,
                  size_t n  );

The wmemcpy( ) function copies n successive wide characters beginning at the address in src to the location beginning at the address in dest. The return value is the same as the first argument, dest. The two pointer values must be at least n wide characters apart, so that the source and destination blocks do not overlap; otherwise, the function's behavior is undefined. For overlapping blocks, use wmemmove( ).

Example

#define BUFFERSIZE 2048             // Size as a number of wchar_t elements.

wchar_t inputbuffer[BUFFERSIZE] = { L'\0' },
        *writeptr = inputbuffer;

struct block { wchar_t *text;
  struct block *next;
  struct block *prev;
} firstblock = { NULL }, *tmp = NULL;  // The first block is the list head.

struct block *newblock( struct block *lastblock );
                                       // Creates a linked-list member.
wchar_t *storetext( struct block *listhead,
                    wchar_t *buffer,
                    size_t bufsize );
                    // Copies input buffer to a new linked-list member.

int main( )
{

  while ( fgetws( writeptr, BUFFERSIZE - (writeptr - inputbuffer), stdin )
          != NULL )
    {
      // Set writeptr to end of the input string:
      writeptr = wmemchr( inputbuffer, L'\0',
                          sizeof(inputbuffer) / sizeof(wchar_t) );

      if ( BUFFERSIZE - (writeptr - inputbuffer) < 80 )
                                            // If block full, or nearly so:
        {                                   // copy buffer to a data block.
          writeptr = storetext( &firstblock, inputbuffer, BUFFERSIZE );
          if ( writeptr == NULL )        // Out of memory!
            abort( );
        }
    }         // Here if fgetws( ) returns NULL.
  writeptr = storetext( &firstblock, inputbuffer, BUFFERSIZE );
  if ( writeptr == NULL )        // Out of memory!
    abort( );
}

wchar_t *storetext(struct block *listhead, wchar_t *buffer, size_t bufsize)
     // Copies input buffer to a new chained-list member;
     // returns pointer to input buffer, or NULL on failure.
{
  struct block *tmp = listhead;     // create new block on end of list ...
  while ( tmp->next != NULL )
    tmp = tmp->next;
  if (( tmp = newblock( tmp )) != NULL )
    wmemcpy( tmp->text, buffer, bufsize ); // ... and copy the text.
  else  // Out of memory!
    return NULL;

#ifdef DEBUG
  fwprintf( stderr, L"\nStored a block with this text:\n%ls\n", tmp->text );
#endif

  return buffer;          // Return pointer to buffer, now ready for re-use.
}

struct block *newblock( struct block *lastblock )
     // Allocates a new block and appends it to the chained list;
     // returns pointer to new block, or NULL on failure.
{
  if (( lastblock->next = malloc( sizeof(struct block) )) != NULL
      && ( lastblock->next->text = malloc( BUFFERSIZE * sizeof(wchar_t) ))
      != NULL)
    {
      lastblock->next->prev = lastblock;
      lastblock->next->next = NULL;
      return lastblock->next;
    }
  else                            // Out of memory!
    return NULL;
}

See Also

wmemmove( ), wcscpy( ), wcsncpy( ), memcpy( ), strcpy( ), strncpy( ), memove( )


Previous Page
Next Page