1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// $Id: Dstr.hh,v 1.3 2002/12/19 18:40:45 flaterco Exp $
// Dstr: Dave's String class.
// This source is shared among several of my projects and is public domain.
// Operations are added as needed and there is not necessarily a canonical
// version.
/* Slackful strcmp; 0 = match. It's case-insensitive and accepts a
prefix instead of the entire string. The second argument is the
one that can be shorter. */
int slackcmp (char *a, char *b);
class Dstr {
public:
Dstr();
Dstr(const char *val);
Dstr(char val);
Dstr(const Dstr &val);
~Dstr();
unsigned length() const;
int isNull() const;
// Read a line. The trailing newline is stripped. DOS/VMS two-character
// line discipline is not supported. On EOF, Dstr becomes null.
Dstr& getline (FILE *fp);
// Scan a string like fscanf (fp, "%s")
Dstr& scan (FILE *fp);
// Scan a line from a Dstr, stripping newline.
void getline (Dstr &line_out);
// Assign
Dstr& operator= (const char *val);
Dstr& operator= (char val);
Dstr& operator= (const Dstr &val);
// Append
Dstr& operator+= (const char *val);
Dstr& operator+= (char val);
Dstr& operator+= (const Dstr &val);
Dstr& operator+= (int val);
Dstr& operator+= (unsigned int val);
Dstr& operator+= (long int val);
Dstr& operator+= (long unsigned int val);
Dstr& operator+= (long long int val);
Dstr& operator+= (double val);
// Prepend
Dstr& operator*= (const char *val);
Dstr& operator*= (char val);
Dstr& operator*= (const Dstr &val);
// Truncate
Dstr& operator-= (unsigned at_index);
// Break off the first substring delimited by whitespace or double quotes
// (no escaping) and assign it to val. The double quotes are NOT removed,
// and if the argument is terminated by the end-of-line rather than a
// matching quote, you'll get the unbalanced quotes back.
Dstr& operator/= (Dstr &val);
// Remove all text before the specified index
Dstr& operator/= (unsigned at_index);
// Convert contents to a double.
double asdouble() const;
// Get index; returns -1 if not found
int strchr (char val) const;
int strrchr (char val) const;
int strstr (const Dstr &val) const;
int strstr (const char *val) const;
// Case-insensitive.
int strcasestr (const Dstr &val) const;
// Get character at index
char operator[] (unsigned at_index) const;
// Pad to length with spaces.
Dstr &pad (unsigned to_length);
// Strip leading and trailing whitespace.
Dstr &trim ();
// Retrieve value as character string. This will actually be theBuffer
// unless it's NULL -- in which case an empty string will be
// substituted.
char *aschar() const;
// Same thing, but strdup'd
char *asdupchar() const;
// Same thing, but starting at index.
char *ascharfrom(unsigned from_index) const;
// Retrieve value as a character string, no NULL masking.
char *asrawchar() const;
// Replace all instances of character X with character Y; returns number
// of reps.
unsigned repchar (char X, char Y);
// Smash case
Dstr &lowercase();
protected:
char *theBuffer;
unsigned max; // Total max buffer size including \0
unsigned used; // Length not including \0
};
// Compare
int operator== (const Dstr &val1, const char *val2);
int operator== (const char *val1, const Dstr &val2);
int operator== (const Dstr &val1, const Dstr &val2);
int operator!= (const char *val1, const Dstr &val2);
int operator!= (const Dstr &val1, const char *val2);
int operator!= (const Dstr &val1, const Dstr &val2);
// "Is kinda like" comparison operator (opposite of slackcmp)
/* It's case-insensitive and accepts a prefix instead of the entire
string. The second argument is the one that can be shorter. */
int operator%= (const Dstr &a, const Dstr &b);
// This is case insensitive and sorts by Latin1 collating sequence.
int dstrcasecmp (const Dstr &val1, const Dstr &val2);
int dstrcasecmp (const Dstr &val1, char *val2);
|