home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / toolkt21 / c / samples / tp / word.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-12  |  4.0 KB  |  155 lines

  1. #ifndef lint
  2. static char *sccsid = "@(#)word.c 1.3 1/22/92 16:12:04 [1/26/92] (c)IBM Corp. 1992";
  3. #endif
  4.  
  5. /*
  6.  * This class is adapted from the book
  7.  *   Class Construction in C and C++, Object Oriented Fundamentals
  8.  *   by Roger Sessions, Copyright (c) 1992 Prentice Hall.
  9.  * Reprinted with permission.
  10.  */
  11.  
  12. #define word_Class_Source
  13. #include "word.ih"
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. static int nNews = 0;
  18. static int nDeletes = 0;
  19. #define PARRAY_SIZE        1000
  20. static char *parray[PARRAY_SIZE];
  21.  
  22.  
  23. /* ************************************************************ */
  24. SOM_Scope word *SOMLINK wordInit1(word * somSelf,
  25.                    char *string)
  26. {
  27.     wordData *somThis = wordGetData(somSelf);
  28.     wordMethodDebug("word", "wordInit1");
  29.  
  30.     if (_storage)
  31.     free(_storage);
  32.     _storage = malloc(strlen(string) + 1);
  33.     strcpy(_storage, string);
  34.     _length = strlen(string);
  35.     parray[nNews] = _storage;
  36.     nNews++;
  37.     return somSelf;
  38. }
  39.  
  40. /* ************************************************************ */
  41. SOM_Scope word *SOMLINK wordInit2(word * somSelf,
  42.                    char newChar,
  43.                    int nChars)
  44. {
  45.     wordData *somThis = wordGetData(somSelf);
  46.     int n;
  47.  
  48.     wordMethodDebug("word", "wordInit2");
  49.  
  50.     if (_storage)
  51.     free(_storage);
  52.     _storage = malloc(nChars + 1);
  53.     for (n = 0; n < nChars; n++) {
  54.     _storage[n] = newChar;
  55.     }
  56.     _storage[n] = '\0';
  57.     _length = strlen(_storage);
  58.     parray[nNews] = _storage;
  59.     nNews++;
  60.     return somSelf;
  61. }
  62.  
  63. /* ************************************************************ */
  64. SOM_Scope int SOMLINK getLength(word * somSelf)
  65. {
  66.     wordData *somThis = wordGetData(somSelf);
  67.     wordMethodDebug("word", "getLength");
  68.     return _length;
  69. }
  70.  
  71. /* ************************************************************ */
  72. SOM_Scope int SOMLINK wordToInt(word * somSelf)
  73. {
  74.     wordData *somThis = wordGetData(somSelf);
  75.     wordMethodDebug("word", "wordToInt");
  76.     return atoi(_storage);
  77. }
  78.  
  79. /* ************************************************************ */
  80. SOM_Scope int SOMLINK match(word * somSelf,
  81.                  void *target)
  82. {
  83.     wordData *somThis = wordGetData(somSelf);
  84.     int targetLength = strlen(target);
  85.     wordMethodDebug("word", "match");
  86.     return (!strncmp(_storage, target, targetLength));
  87. }
  88.  
  89. /* ************************************************************ */
  90. SOM_Scope void SOMLINK print(word * somSelf,
  91.                   FILE * outputFile)
  92. {
  93.     wordData *somThis = wordGetData(somSelf);
  94.     wordMethodDebug("word", "print");
  95.  
  96.     fprintf(outputFile, "%s", _storage);
  97.     fflush(outputFile);
  98. }
  99.  
  100. /* ************************************************************ */
  101. SOM_Scope void SOMLINK somInit(word * somSelf)
  102. {
  103.     wordData *somThis = wordGetData(somSelf);
  104.     wordMethodDebug("word", "somInit");
  105.     parent_somInit(somSelf);
  106.     _storage = 0;
  107. }
  108.  
  109. /* ************************************************************ */
  110. SOM_Scope void SOMLINK somUninit(word * somSelf)
  111. {
  112.     wordData *somThis = wordGetData(somSelf);
  113.     wordMethodDebug("word", "somUninit");
  114.     if (_storage)
  115.     free(_storage);
  116.     parent_somUninit(somSelf);
  117. }
  118.  
  119. /* ************************************************************ */
  120. SOM_Scope void SOMLINK somDumpSelfInt(word * somSelf,
  121.                        int level)
  122. {
  123.     wordData *somThis = wordGetData(somSelf);
  124.     wordMethodDebug("word", "somDumpSelfInt");
  125.     parent_somDumpSelfInt(somSelf, level);
  126. }
  127.  
  128. void showWordStats()
  129. {
  130.     int n;
  131.     printf("\n");
  132.     printf("   nNews: %d\n", nNews);
  133.     printf("nDeletes: %d\n", nDeletes);
  134.     for (n = 0; n < PARRAY_SIZE; n++) {
  135.     if (parray[n]) {
  136.         printf("n: %d storage: %s\n", n, parray[n]);
  137.     }
  138.     }
  139. }
  140.  
  141.  
  142. /* ************************************************************ */
  143. SOM_Scope void SOMLINK wrdReplace(word * somSelf,
  144.                    char *newWord)
  145. {
  146.     wordData *somThis = wordGetData(somSelf);
  147.     wordMethodDebug("word", "wrdReplace");
  148.  
  149.     _length = strlen(newWord);
  150.     if (_storage)
  151.     free(_storage);
  152.     _storage = malloc(_length + 1);
  153.     strcpy(_storage, newWord);
  154. }
  155.