Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GString.h

Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // GString.h
00004 //
00005 // Simple variable-length string type.
00006 //
00007 // Copyright 1996 Derek B. Noonburg
00008 //
00009 //========================================================================
00010 
00011 #ifndef GSTRING_H
00012 #define GSTRING_H
00013 
00014 #ifdef __GNUC__
00015 #pragma interface
00016 #endif
00017 
00018 #include <string.h>
00019 
00020 class GString {
00021 public:
00022 
00023   // Create an empty string.
00024   GString();
00025 
00026   // Create a string from a C string.
00027   GString(const char *sA);
00028 
00029   // Create a string from <lengthA> chars at <sA>.  This string
00030   // can contain null characters.
00031   GString(const char *sA, int lengthA);
00032 
00033   // Create a string from <lengthA> chars at <idx> in <str>.
00034   GString(GString *str, int idx, int lengthA);
00035 
00036   // Copy a string.
00037   GString(GString *str);
00038   GString *copy() { return new GString(this); }
00039 
00040   // Concatenate two strings.
00041   GString(GString *str1, GString *str2);
00042 
00043   // Convert an integer to a string.
00044   static GString *fromInt(int x);
00045 
00046   // Destructor.
00047   ~GString();
00048 
00049   // Get length.
00050   int getLength() { return length; }
00051 
00052   // Get C string.
00053   char *getCString() { return s; }
00054 
00055   // Get <i>th character.
00056   char getChar(int i) { return s[i]; }
00057 
00058   // Change <i>th character.
00059   void setChar(int i, char c) { s[i] = c; }
00060 
00061   // Clear string to zero length.
00062   GString *clear();
00063 
00064   // Append a character or string.
00065   GString *append(char c);
00066   GString *append(GString *str);
00067   GString *append(const char *str);
00068   GString *append(const char *str, int lengthA);
00069 
00070   // Insert a character or string.
00071   GString *insert(int i, char c);
00072   GString *insert(int i, GString *str);
00073   GString *insert(int i, const char *str);
00074   GString *insert(int i, const char *str, int lengthA);
00075 
00076   // Delete a character or range of characters.
00077   GString *del(int i, int n = 1);
00078 
00079   // Convert string to all-upper/all-lower case.
00080   GString *upperCase();
00081   GString *lowerCase();
00082 
00083   // Compare two strings:  -1:<  0:=  +1:>
00084   // These functions assume the strings do not contain null characters.
00085   int cmp(GString *str) { return strcmp(s, str->getCString()); }
00086   int cmpN(GString *str, int n) { return strncmp(s, str->getCString(), n); }
00087   int cmp(const char *sA) { return strcmp(s, sA); }
00088   int cmpN(const char *sA, int n) { return strncmp(s, sA, n); }
00089 
00090 private:
00091 
00092   int length;
00093   char *s;
00094 
00095   void resize(int length1);
00096 };
00097 
00098 #endif

Generated on Sat Nov 5 16:18:12 2005 for OPIE by  doxygen 1.4.2