Slide 5.22: Clear.h
Slide 6.1: Record access
Home

Unpack.h


The file Unpack.h is a method implementation of the class TextBuffer. This method is used by the method Unpack( ) of the class Book. It performs the following two tasks:
~wenchen/public_html/cgi-bin/351/week5/Unpack.h

#include  <cstring>
  using namespace  std;

int  TextBuffer::Unpack( char* str, char* begin, char* end ) {
  int  len = -1, len1 = strlen( begin ), len2 = len1+1;
  int  i, j, start = nextByte;

  for ( i = start;  i < bufferSize-len1-len2;  i++ )
    if ( strncmp( buffer+i, begin, len1 ) == 0 ) {
      i += len1;
      break;
    }
  for ( j = i;  j < bufferSize-len2;  j++ )
    if ( strncmp( buffer+j, end, len2 ) == 0 ) {
      len = j - i;
      break;
    }
  if ( len == -1 )  return( false );
  nextByte = j + len2;
  if ( nextByte > bufferSize )  return( false );
  strncpy ( str, buffer+i, len );
  str[len] = '\0';

  i = 0; 
  while ( ( str[i]==' ' ) || ( str[i]=='\n' ) || 
          ( str[i]=='\t' ) )   i++;
  strcpy( str, str+i );
  i = len - i - 1; 
  while ( ( str[i]==' ' ) || ( str[i]=='\n' ) || 
          ( str[i]=='\t' ) )   i--;
  str[i+1] = '\0';
  return( true );
}

strncpy ( char * dest, const char * src, sizet_t num ) copies the first num characters of src to dest.