/* Filename: stringhelp.cpp Author: Br. David Carlson Date: January 25, 2001 This header file, together with the matching stringhelp.h file, provides StrMax, StringType, and these operations on StringType data: Capitalize, MyGetline, SubstringPresent, GetSubstring and GetValue. */ #include "stringhelp.h" /* Given: String A string to capitalize. Task: To capitalize all letters in String. Return: NewString The capitalized verson of the string. */ void Capitalize(StringType String, StringType NewString) { int k, Len; Len = strlen(String); for (k = 0; k < Len; k++) NewString[k] = toupper(String[k]); NewString[Len] = '\0'; } /* Given: String A string to search. Substring The substring to search for. Task: To see if Substring occurs within String. A case-insenstive check is made. (For example, "aBc" is present in "dabCq".) Return: True if Substring is present within String, false otherwise. */ bool SubstringPresent(StringType String, StringType Substring) { StringType CapString, CapSubstring; Capitalize(String, CapString); Capitalize(Substring, CapSubstring); if (strstr(CapString, CapSubstring) == '\0') return false; else return true; } /* Given: String The string to search through. StartChar The character preceding the sought for substring. EndChar The character immediately following the substring. Task: To extract the substring of String found between the first occurance of StartChar and the first occurance of EndChar after StartChar. Return: Substring This substring. True in the function name if such a substring was found. False in the function name if no such substring was found (in which case Substring may contain garbage). */ bool GetSubstring(StringType String, char StartChar, char EndChar, StringType Substring) { int k, m, Length; bool Flag; Length = strlen(String); Flag = false; for (k = 0; k < Length; k++) if (String[k] == StartChar) { Flag = true; break; } if (! Flag) return false; k++; m = 0; Flag = false; while (k < Length) { if (String[k] == EndChar) { Flag = true; Substring[m] = '\0'; break; } else Substring[m++] = String[k++]; } return Flag; } /* Given: InStream An input file stream already open for reading. StringMax The maximum number of characters that can be put into String, including the NULL that marks the end of the string. Task: To read up to StringMax - 1 characters from InStream, storing them in String, but stopping if a newline is read or end of file or an error condition is reached. Return: String The string just read, with a NULL appended to mark the end of the string. If a newline was read, it is not stored in String. Also returns in the function name the count of the number of characters placed in String (not including the NULL). */ int MyGetLine(istream & InStream, char * String, int StringMax) { char Ch; int Count, Last; Count = 0; Last = StringMax - 1; Ch = InStream.get(); while ((Ch != '\n') && (! InStream.fail())) { if (Count < Last) String[Count++] = Ch; Ch = InStream.get(); } String[Count] = '\0'; return Count; } /* Given: EnvVariable The name (as a string) of a WWW environment variable. Task: To retrieve the value of this variable, being sure not to copy more characters than will fit into Result and rejecting any dangerous characters (anything other than alphanumeric characters, NULL, space, plus sign, minus sign, and period). Return: Result The value of this WWW environment variable, truncated if need be. If a dangerous character is found, the empty string is returned in Result. */ void GetValue(StringType EnvVariable, StringType Result) { char * Match; char ch; register int k; Match = getenv(EnvVariable); for (k = 0; k < StrMax - 1; k++) { ch = *Match; if (ch == '\0') { Result[k] = '\0'; break; } else if (isalnum(ch) || (ch == ' ') || (ch == '+') || (ch == '-') || (ch == '.')) { Result[k] = ch; Match++; } else // bad data, return empty string { Result[0] = '\0'; break; } } // just to be sure there's a NULL at the end of the string: Result[StrMax - 1] = '\0'; }