Line Breaker Class
#include <string> #include <cstdlib> using namespace std; /* * This is a wrapper for the return of subscript in LineBreaker that handles * certain annoying conversion issues. */ class SorC { public: SorC(const char *cp): m_val(cp) { } const char * val() const { return m_val; } operator const char *() const { return m_val; } private: const char *m_val; }; inline bool operator==(const SorC &soc, const char *cp) { return string(soc.val()) == string(cp); } inline bool operator==(const char *cp, const SorC &soc) { return soc == cp; }; inline bool operator!=(const char *cp, const SorC &soc) { return !(cp == soc); } inline bool operator!=(const SorC &soc, const char *cp) { return !(soc == cp); } /* * This class takes a C++ string and provides an array of plain C strings * (array of pointers to char) representing the words in the line. */ class LineBreaker { public: // Construct the object, providing the line of text to be // broken up. LineBreaker(const string &s); // Clean up. ~LineBreaker() { delete [] m_data; delete [] m_parmlist; } // Return a pointer to the first slot of an array of pointers // containing the words in the string sent to the constructor. // The list is terminated with a NULL pointer. This is suitable // for sending to the execv Unix system call, and friends. operator char * const * () const { return m_parmlist; } // Get one of the items out of the list. If the subscript is out // of range, the empty string is returned. The return value is // mainly a character pointer, but it will be treated as a string // for equality and inequality comparison. SorC operator[] (int sub) { return SorC(0 <= sub && sub < m_count ? m_parmlist[sub] : ""); } // Get one of the items in the list converted from string to // integer. int intat(int sub) { return atoi((*this)[sub]); } // Size of the list (excluding terminator). int size() { return m_count; } // Remove the last item from the list and return it. If the // list is initially empty, return the empty string. SorC pop(); private: char *m_data; // Dyn array of characters from the string. char **m_parmlist; // Array of words int m_count; // Count of the number of words. };