/** * Copyright Sören Molander, 2000-2004 * The code maybe copied and modified at will * * Description: util.g contains procedures for flexible string handling * * author: Lars Olsson, CR&T, Sören Molander */ /** * \file * String utilities. * * **/ #ifndef INCLUDE_UTIL_H #define INCLUDE_UTIL_H #include #include #include // #include /** Utilities for string processing */ namespace Strutil { using namespace std; /** String delimiter class */ class Delimiter { public: Delimiter(const char* whites); virtual ~Delimiter() {} public: virtual bool isDelimiter(char c); public: char delimChar(void) const; private: string whites; }; /** * String delimitation with support for quotes * This Delimiter subclass works as its superclass * expect that it first checks to see if the character * is inside quotes. */ class QuoteDelimiter : public Delimiter { public: QuoteDelimiter(const char* whites, const char* quotechars); public: bool isDelimiter(char c); private: bool markQuote(char c); bool insideQuote(void); private: class QuoteInfo { public: QuoteInfo():quotechar('"'),inside(false) {} QuoteInfo(char c):quotechar(c),inside(false) {} QuoteInfo(const QuoteInfo& qi) {copy(qi);} QuoteInfo& operator=(const QuoteInfo& qi) {copy(qi); return *this;} private: void copy(const QuoteInfo& qi) {quotechar = qi.quotechar; inside = qi.inside;} public: char quotechar; bool inside; }; private: vector aqinfo; }; /** Chop a string into "blank" separated words */ void chopstring(const string& s,Delimiter& delim,vector& words); /** Flatten a string list */ void flatten(const vector& words,size_t start,const char* pre,const char* sep,const char* post,string& s); /* convert string to uppercase */ /* extern void toupper(string& s); */ /* convert string to uppercase */ /* extern void tolower(string& s); */ /** check if s1 is a prefix of s2 */ bool isprefix(const string& s1,const string& s2); /** check if s1 is a suffix of s2 */ bool issuffix(const string& s1,const string& s2); /* part of s1 that comes after s2 */ void afterstring(const string& s1, const string& s2, string& result); /** Remove extraenous white space */ void collapse(string& s,Delimiter& delim); /** Remove extraenous white space */ void collapse(const string& in,Delimiter& delim,string& out); /** String to integer helper function */ void reverse(char []); /** String to integer helper function */ void itoa(int n, char []); /** modern version of itoa */ string itoa(int n); /** stream base version of itof */ // string ftoa(double); /** Parse a filename into basename and extension */ void fileBaseExtension(string& filename, string& basename, string& extension); /** String uppercase */ string Upcase(const string&); /** String lowercase */ string Lowcase(const string&); } #endif