home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-20 | 4.8 KB | 234 lines | [TEXT/CWIE] |
- /*
- File: SimpleParseUtils.cp
-
- Contains: Utilities for useful for very simple
- parsing tasks.
-
- NOTE: These procs are totally inefficient,
- especially GetToken. Don't try to write the
- next C++ compiler with them. You could write
- a simple class browser, or possibly a compiler
- for a better language, but these could be
- written more efficiently still for such a
- purpose.
-
- Version: 1.0 (for System 7.x)
-
- Copyright: ©1995 Chris K. Thomas. All Rights Reserved.
- */
-
- #include "SimpleParseUtils.h"
-
- const unsigned char *whiteSpaces = "\p \t\r\n";
- const unsigned char *wordBreaks = "\p \t\r\n~$%/+:;{}[]~`'\"\0";
- const unsigned char *endLines = "\p\r\n";
-
- // * if inMem points to the same string as inString, true
- Boolean StrEquals(const unsigned char *inString, char *inMem)
- {
- Boolean out = true;
-
- for(long i = 1; i <= inString[0]; i++)
- {
- if(*(inString + i) != *(unsigned char *)(inMem + i))
- {
- out = false;
- break;
- }
- }
-
- return out;
- }
-
- // * if inMem points to a the same string as inString followed by whitespace, true
- Boolean StrEqualsToken(const unsigned char *inString, char *inMem)
- {
- Boolean out = true;
-
- for(long i = 1; i <= inString[0]; i++)
- {
- if(*(inString + i) != *(unsigned char *)(inMem + i))
- {
- out = false;
- break;
- }
- }
-
- // * if the character after the string isn’t whitespace, false
- if(out)
- out = StrEqualsChars(whiteSpaces, (char *)&inMem[inString[0] + 1]);
-
- return out;
- }
-
- // * if inMem points to a the same string as inString followed by whitespace, true
- Boolean PtrEqualsToken(const Ptr inString, char *inMem)
- {
- Boolean out = true;
-
- long strSize = GetPtrSize(inString);
-
- for(long i = 0; i < strSize; i++)
- {
- if(*(inString + i) != *(unsigned char *)(inMem + i))
- {
- out = false;
- break;
- }
- }
-
- // * if the character after the string isn’t whitespace, false
- if(out)
- out = StrEqualsChars(whiteSpaces, (char *)&inMem[strSize + 1]);
-
- return out;
- }
-
- // * if inMem points to a char which is in inTryChars, true
- Boolean StrEqualsChars(const unsigned char *inTryChars, char *inMem)
- {
- Assert_(inTryChars);
- Assert_(inMem);
-
- Boolean out = false;
-
- for(long i = 1; i <= inTryChars[0];i++)
- {
- if(*(inTryChars + i) == *(unsigned char *)inMem)
- {
- out = true;
- break;
- }
- }
-
- return out;
- }
-
- // * count whitespace chars
- long CountWhiteSpace(char *inMem, long inLength)
- {
- Assert_(inMem);
-
- long out = 0;
-
- while(StrEqualsChars(whiteSpaces, inMem + out)
- && (out < inLength))
- {
- out++;
- }
-
- return out;
- }
-
- // * return a string containing the next token
- StringPtr GetTokenString(char *inMem, long inLength)
- {
- Assert_(inMem);
-
- StringPtr curString;
- long wordLength = 0;
- long startIndex;
-
- startIndex = CountWhiteSpace(inMem, inLength);
-
- while(!StrEqualsChars(wordBreaks, (inMem + startIndex + wordLength)))
- wordLength++;
-
- curString = (StringPtr)NewPtr(wordLength + 1);
- ThrowIfNULL_(curString);
-
- BlockMoveData(inMem + startIndex, &curString[1], wordLength);
- curString[0] = wordLength;
-
- return curString;
- }
-
- // * return a length-unlimited string containing the next token
- Ptr GetTokenPtr(char *inMem, long inLength)
- {
- Assert_(inMem);
-
- Ptr curString;
- long wordLength = 0;
- long startIndex;
-
- startIndex = CountWhiteSpace(inMem, inLength);
-
- while(!StrEqualsChars(wordBreaks, (inMem + startIndex + wordLength)))
- wordLength++;
-
- curString = NewPtr(wordLength);
- ThrowIfMemFail_(curString);
-
- BlockMoveData(inMem + startIndex, &curString[0], wordLength);
-
- return curString;
- }
-
- Ptr GetDelimitedToken(const unsigned char *inDelimiter, char *inMem, long inLength)
- {
- Assert_(inMem);
-
- Ptr curString;
- long wordLength = 0;
- long startIndex;
-
- startIndex = CountWhiteSpace(inMem, inLength);
-
- while(!StrEqualsChars(inDelimiter, (inMem + startIndex + wordLength)))
- wordLength++;
-
- curString = NewPtr(wordLength);
- ThrowIfMemFail_(curString);
-
- BlockMoveData(inMem + startIndex, &curString[0], wordLength);
-
- return curString;
- }
-
- // * return the char in inString actually found
- short FindSelectChar(const unsigned char *inString, char *inMem, long inLength)
- {
- long i = 0;
- short out = 0;
-
- while(i < inLength)
- {
-
- // * compare the current char in inMem with each char in inString
- for(long charIndex = 1; charIndex <= inString[0]; charIndex++)
- {
- if(inMem[i] == inString[charIndex])
- {
- out = inString[charIndex];
-
- // * break out of the outer loop
- // * without requiring a doneFlag
- i = inLength + 5; // * could be any value
- break;
- }
- }
-
- i++;
- }
-
- return out;
- }
-
- // * get all text from inMem to end of line
- Ptr GetEntireLine(char *inMem, long inLength)
- {
- long lineLength = 0;
- Ptr out = NULL;
-
- while(!StrEqualsChars(endLines, &inMem[lineLength]) && lineLength < inLength)
- lineLength++;
-
- out = NewPtr(lineLength);
- ThrowIfMemFail_(out);
-
- BlockMoveData(&inMem[0], out, lineLength);
-
- return out;
- }
-