home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / SimpleParseUtils / SimpleParseUtils.cp next >
Encoding:
Text File  |  1995-10-20  |  4.8 KB  |  234 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SimpleParseUtils.cp
  3.     
  4.     Contains:    Utilities for useful for very simple
  5.                 parsing tasks.
  6.     
  7.                 NOTE:  These procs are totally inefficient,
  8.                 especially GetToken.  Don't try to write the
  9.                 next C++ compiler with them.  You could write
  10.                 a simple class browser, or possibly a compiler
  11.                 for a better language, but these could be
  12.                 written more efficiently still for such a
  13.                 purpose.
  14.     
  15.     Version:    1.0 (for System 7.x)
  16.     
  17.     Copyright:    ©1995 Chris K. Thomas.  All Rights Reserved.
  18. */
  19.  
  20. #include "SimpleParseUtils.h"
  21.  
  22. const unsigned char    *whiteSpaces = "\p \t\r\n";
  23. const unsigned char    *wordBreaks = "\p \t\r\n~$%/+:;{}[]~`'\"\0";
  24. const unsigned char    *endLines = "\p\r\n";
  25.  
  26. // * if inMem points to the same string as inString, true
  27. Boolean StrEquals(const unsigned char *inString, char *inMem)
  28. {
  29.     Boolean out = true;
  30.     
  31.     for(long i = 1; i <= inString[0]; i++)
  32.     {
  33.         if(*(inString + i) != *(unsigned char *)(inMem + i))
  34.         {
  35.             out = false;
  36.             break;
  37.         }
  38.     }
  39.     
  40.     return out;
  41. }
  42.  
  43. // * if inMem points to a the same string as inString followed by whitespace, true
  44. Boolean StrEqualsToken(const unsigned char *inString, char *inMem)
  45. {
  46.     Boolean out = true;
  47.     
  48.     for(long i = 1; i <= inString[0]; i++)
  49.     {
  50.         if(*(inString + i) != *(unsigned char *)(inMem + i))
  51.         {
  52.             out = false;
  53.             break;
  54.         }
  55.     }
  56.     
  57.     // * if the character after the string isn’t whitespace, false
  58.     if(out)
  59.         out = StrEqualsChars(whiteSpaces, (char *)&inMem[inString[0] + 1]);
  60.     
  61.     return out;
  62. }
  63.  
  64. // * if inMem points to a the same string as inString followed by whitespace, true
  65. Boolean PtrEqualsToken(const Ptr inString, char *inMem)
  66. {
  67.     Boolean out = true;
  68.     
  69.     long strSize = GetPtrSize(inString);
  70.     
  71.     for(long i = 0; i < strSize; i++)
  72.     {
  73.         if(*(inString + i) != *(unsigned char *)(inMem + i))
  74.         {
  75.             out = false;
  76.             break;
  77.         }
  78.     }
  79.     
  80.     // * if the character after the string isn’t whitespace, false
  81.     if(out)
  82.         out = StrEqualsChars(whiteSpaces, (char *)&inMem[strSize + 1]);
  83.     
  84.     return out;
  85. }
  86.  
  87. // * if inMem points to a char which is in inTryChars, true
  88. Boolean StrEqualsChars(const unsigned char *inTryChars, char *inMem)
  89. {
  90.     Assert_(inTryChars);
  91.     Assert_(inMem);
  92.     
  93.     Boolean out = false;
  94.     
  95.     for(long i = 1; i <= inTryChars[0];i++)
  96.     {
  97.         if(*(inTryChars + i) == *(unsigned char *)inMem)
  98.         {
  99.             out = true;
  100.             break;
  101.         }
  102.     }
  103.     
  104.     return out;
  105. }
  106.  
  107. // * count whitespace chars
  108. long CountWhiteSpace(char *inMem, long inLength)
  109. {
  110.     Assert_(inMem);
  111.     
  112.     long out = 0;
  113.     
  114.     while(StrEqualsChars(whiteSpaces, inMem + out)
  115.             && (out < inLength))
  116.     {
  117.         out++;
  118.     }
  119.     
  120.     return out;
  121. }
  122.  
  123. // * return a string containing the next token
  124. StringPtr GetTokenString(char *inMem, long inLength)
  125. {
  126.     Assert_(inMem);
  127.     
  128.     StringPtr        curString;
  129.     long            wordLength = 0;
  130.     long            startIndex;
  131.     
  132.     startIndex = CountWhiteSpace(inMem, inLength);
  133.     
  134.     while(!StrEqualsChars(wordBreaks, (inMem + startIndex + wordLength)))
  135.         wordLength++;
  136.     
  137.     curString = (StringPtr)NewPtr(wordLength + 1);
  138.     ThrowIfNULL_(curString);
  139.     
  140.     BlockMoveData(inMem + startIndex, &curString[1], wordLength);
  141.     curString[0] = wordLength;
  142.     
  143.     return curString;
  144. }
  145.  
  146. // * return a length-unlimited string containing the next token
  147. Ptr GetTokenPtr(char *inMem, long inLength)
  148. {
  149.     Assert_(inMem);
  150.     
  151.     Ptr                curString;
  152.     long            wordLength = 0;
  153.     long            startIndex;
  154.     
  155.     startIndex = CountWhiteSpace(inMem, inLength);
  156.     
  157.     while(!StrEqualsChars(wordBreaks, (inMem + startIndex + wordLength)))
  158.         wordLength++;
  159.     
  160.     curString = NewPtr(wordLength);
  161.     ThrowIfMemFail_(curString);
  162.     
  163.     BlockMoveData(inMem + startIndex, &curString[0], wordLength);
  164.     
  165.     return curString;
  166. }
  167.  
  168. Ptr GetDelimitedToken(const unsigned char *inDelimiter, char *inMem, long inLength)
  169. {
  170.     Assert_(inMem);
  171.     
  172.     Ptr                curString;
  173.     long            wordLength = 0;
  174.     long            startIndex;
  175.     
  176.     startIndex = CountWhiteSpace(inMem, inLength);
  177.     
  178.     while(!StrEqualsChars(inDelimiter, (inMem + startIndex + wordLength)))
  179.         wordLength++;
  180.     
  181.     curString = NewPtr(wordLength);
  182.     ThrowIfMemFail_(curString);
  183.     
  184.     BlockMoveData(inMem + startIndex, &curString[0], wordLength);
  185.     
  186.     return curString;
  187. }
  188.  
  189. // * return the char in inString actually found
  190. short FindSelectChar(const unsigned char *inString, char *inMem, long inLength)
  191. {
  192.     long    i = 0;
  193.     short    out = 0;
  194.     
  195.     while(i < inLength)
  196.     {
  197.         
  198.         // * compare the current char in inMem with each char in inString
  199.         for(long charIndex = 1; charIndex <= inString[0]; charIndex++)
  200.         {
  201.             if(inMem[i] == inString[charIndex])
  202.             {
  203.                 out = inString[charIndex];
  204.                 
  205.                 // * break out of the outer loop
  206.                 // * without requiring a doneFlag
  207.                 i = inLength + 5;    // * could be any value
  208.                 break;
  209.             }
  210.         }
  211.         
  212.         i++;
  213.     }
  214.     
  215.     return out;
  216. }
  217.  
  218. // * get all text from inMem to end of line
  219. Ptr    GetEntireLine(char *inMem, long inLength)
  220. {
  221.     long    lineLength = 0;
  222.     Ptr        out = NULL;
  223.     
  224.     while(!StrEqualsChars(endLines, &inMem[lineLength]) && lineLength < inLength)
  225.         lineLength++;
  226.     
  227.     out = NewPtr(lineLength);
  228.     ThrowIfMemFail_(out);
  229.     
  230.     BlockMoveData(&inMem[0], out, lineLength);
  231.     
  232.     return out;
  233. }
  234.