ILE C/C++ Programmer's Guide

Creating the Source Files

The SEARCH program is implemented as a class object Search. The class Search contains:

The service program is composed of the following files:

User Header File

The class and function declarations are placed into a separate header file, search.h, as shown in the following figure:

Figure 5. Example of Header File (search.h)


// header file search.h
 // contains declarations for class Search, and inlined function
  // definitions
 
  #include <iostream.h>
 
  class Search  {
    private:
      char skippat[256];
      char * needle_p;
      int  needle_size;
    public:
 
  // Constructors
      Search( unsigned char * needle, int size);
      Search ( unsigned char * needle);
      Search ( char * needle);
 
  //Destructor
     ~Search ()  { delete needle_p;}
 
  //Overloaded member functions
      unsigned int where ( char * haystack)  {
         return where (haystack, strlen(haystack));
      }
      unsigned int where ( unsigned char * haystack)  {
         return where (haystack, strlen((const char *)haystack));
      }
      unsigned int where ( char * haystack, int size)  {
          return where ( (unsigned char *) haystack, size);
      }
      unsigned int where ( unsigned char * haystack, int size);
  };

Source Code Files

If the definitions for the member functions of class Search are not inlined in the class declaration, they are contained in two separate files:

These files are shown in the following figures:

Figure 6. Source File that Contains Constructor Definitions for the Search Class


// source file search.cpp
// contains the definitions for the constructors for class Search
 
 #include "search.h"
 
    Search::Search( unsigned char * needle, int size)
       : needle_size(size) , needle_p ( new char [size])
    {
       memset (skippat, needle_size, 256);
       for (unsigned int i=0; i<size; ++i) {
          skippat [needle [i]] = size -i-1;
       }
       memcpy (needle_p, needle, needle_size);
    }
    Search::Search ( unsigned char * needle) {
       needle_size = strlen( (const char *)needle) ;
       needle_p = new char [needle_size];
       memset (skippat, needle_size, 256);
       for (unsigned int i=0; i<needle_size; ++i) {
          skippat [needle [i]] = needle_size -i-1;
       }
       memcpy(needle_p, needle, needle_size);
    }
    Search::Search ( char * needle) {
       needle_size = strlen( needle) ;
       needle_p = new char [needle_size];
       memset (skippat,needle_size, 256);
       for (unsigned int i=0; i<needle_size; ++i) {
          skippat [needle [i]] = needle_size -i-1;
       }
       memcpy(needle_p, needle, needle_size);
    }

Figure 7. File that Contains the Member Function Definition for the Search Class


// where.cpp
// contains definition of overloaded member function for class Search
 
    #include "search.h"
 
    unsigned int Search:: where ( unsigned char * haystack, int size)
    {  unsigned int i, t;
       int j;
       for ( i= needle_size-1, j = needle_size-1; j >= 0; --i, --j ){
           while ( haystack[i] != needle_p[j]) {
               t = skippat [ haystack [i]] ;
               i += (needle_size - j > t) ? needle_size - j : t ;
               if (i >= size)
                  return size;
               j = needle_size -  1;
           }
       }
       return ++i;
    }

The modules that result from the compilation of these source files, SEARCH and WHERE, are bound into a service program, SERVICE1.


[ Top of Page | Previous Page | Next Page | Table of Contents ]