1 //======================================================================== 2 // 3 // GString.h 4 // 5 // Simple variable-length string type. 6 // 7 // Copyright 1996-2003 Glyph & Cog, LLC 8 // 9 //======================================================================== 10 11 #ifndef GSTRING_H 12 #define GSTRING_H 13 14 #include <aconf.h> 15 16 #ifdef USE_GCC_PRAGMAS 17 #pragma interface 18 #endif 19 20 #include <stdarg.h> 21 #include "gtypes.h" 22 23 class GString { 24 public: 25 26 // Create an empty string. 27 GString(); 28 29 // Create a string from a C string. 30 GString(const char *sA); 31 32 // Create a string from <lengthA> chars at <sA>. This string 33 // can contain null characters. 34 GString(const char *sA, int lengthA); 35 36 // Create a string from <lengthA> chars at <idx> in <str>. 37 GString(GString *str, int idx, int lengthA); 38 39 // Copy a string. 40 GString(GString *str); copy()41 GString *copy() { return new GString(this); } 42 43 // Concatenate two strings. 44 GString(GString *str1, GString *str2); 45 46 // Convert an integer to a string. 47 static GString *fromInt(int x); 48 49 // Create a formatted string. Similar to printf, but without the 50 // string overflow issues. Formatting elements consist of: 51 // {<arg>:[<width>][.<precision>]<type>} 52 // where: 53 // - <arg> is the argument number (arg 0 is the first argument 54 // following the format string) -- NB: args must be first used in 55 // order; they can be reused in any order 56 // - <width> is the field width -- negative to reverse the alignment; 57 // starting with a leading zero to zero-fill (for integers) 58 // - <precision> is the number of digits to the right of the decimal 59 // point (for floating point numbers) 60 // - <type> is one of: 61 // d, x, o, b -- int in decimal, hex, octal, binary 62 // ud, ux, uo, ub -- unsigned int 63 // ld, lx, lo, lb, uld, ulx, ulo, ulb -- long, unsigned long 64 // f, g -- double 65 // c -- char 66 // s -- string (char *) 67 // t -- GString * 68 // w -- blank space; arg determines width 69 // To get literal curly braces, use {{ or }}. 70 static GString *format(char *fmt, ...); 71 static GString *formatv(char *fmt, va_list argList); 72 73 // Destructor. 74 ~GString(); 75 76 // Get length. getLength()77 int getLength() { return length; } 78 79 // Get C string. getCString()80 char *getCString() { return s; } 81 82 // Get <i>th character. getChar(int i)83 char getChar(int i) { return s[i]; } 84 85 // Change <i>th character. setChar(int i,char c)86 void setChar(int i, char c) { s[i] = c; } 87 88 // Clear string to zero length. 89 GString *clear(); 90 91 // Append a character or string. 92 GString *append(char c); 93 GString *append(GString *str); 94 GString *append(const char *str); 95 GString *append(const char *str, int lengthA); 96 97 // Append a formatted string. 98 GString *appendf(char *fmt, ...); 99 GString *appendfv(char *fmt, va_list argList); 100 101 // Insert a character or string. 102 GString *insert(int i, char c); 103 GString *insert(int i, GString *str); 104 GString *insert(int i, const char *str); 105 GString *insert(int i, const char *str, int lengthA); 106 107 // Delete a character or range of characters. 108 GString *del(int i, int n = 1); 109 110 // Convert string to all-upper/all-lower case. 111 GString *upperCase(); 112 GString *lowerCase(); 113 114 // Compare two strings: -1:< 0:= +1:> 115 int cmp(GString *str); 116 int cmpN(GString *str, int n); 117 int cmp(const char *sA); 118 int cmpN(const char *sA, int n); 119 120 private: 121 122 int length; 123 char *s; 124 125 void resize(int length1); 126 static void formatInt(long x, char *buf, int bufSize, 127 GBool zeroFill, int width, int base, 128 char **p, int *len); 129 static void formatUInt(Gulong x, char *buf, int bufSize, 130 GBool zeroFill, int width, int base, 131 char **p, int *len); 132 static void formatDouble(double x, char *buf, int bufSize, int prec, 133 GBool trim, char **p, int *len); 134 }; 135 136 #endif 137