|
1 /* |
|
2 * botc source code |
|
3 * Copyright (C) 2012 Santeri `Dusk` Piippo |
|
4 * All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright notice, |
|
10 * this list of conditions and the following disclaimer. |
|
11 * 2. Redistributions in binary form must reproduce the above copyright notice, |
|
12 * this list of conditions and the following disclaimer in the documentation |
|
13 * and/or other materials provided with the distribution. |
|
14 * 3. Neither the name of the Skulltag Development Team nor the names of its |
|
15 * contributors may be used to endorse or promote products derived from this |
|
16 * software without specific prior written permission. |
|
17 * 4. Redistributions in any form must be accompanied by information on how to |
|
18 * obtain complete source code for the software and any accompanying |
|
19 * software that uses the software. The source code must either be included |
|
20 * in the distribution or be available for no more than the cost of |
|
21 * distribution plus a nominal fee, and must be freely redistributable |
|
22 * under reasonable conditions. For an executable file, complete source |
|
23 * code means the source code for all modules it contains. It does not |
|
24 * include source code for modules or files that typically accompany the |
|
25 * major components of the operating system on which the executable file |
|
26 * runs. |
|
27 * |
|
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
38 * POSSIBILITY OF SUCH DAMAGE. |
|
39 */ |
|
40 |
|
41 #ifndef __STR_H__ |
|
42 #define __STR_H__ |
|
43 |
|
44 char* vdynformat (const char* c, va_list v, unsigned int size); |
|
45 |
|
46 #define SCCF_NUMBER 1<<0 |
|
47 #define SCCF_WORD 1<<1 |
|
48 |
|
49 // Dynamic string object, allocates memory when needed and |
|
50 // features a good bunch of manipulation methods |
|
51 class str { |
|
52 private: |
|
53 // The actual message |
|
54 char* text; |
|
55 |
|
56 // Where will append() place new characters? |
|
57 unsigned int curs; |
|
58 |
|
59 // Allocated length |
|
60 unsigned int alloclen; |
|
61 |
|
62 // Resize the text buffer to len characters |
|
63 void resize (unsigned int len); |
|
64 public: |
|
65 // ====================================================================== |
|
66 // CONSTRUCTORS |
|
67 str (); |
|
68 str (const char* c); |
|
69 str (char c); |
|
70 |
|
71 // ====================================================================== |
|
72 // METHODS |
|
73 |
|
74 // Empty the string |
|
75 void clear (); |
|
76 |
|
77 // Length of the string |
|
78 unsigned int len (); |
|
79 |
|
80 // The char* form of the string |
|
81 char* chars (); |
|
82 |
|
83 // Dumps the character table of the string |
|
84 void dump (); |
|
85 |
|
86 // Appends text to the string |
|
87 void append (char c); |
|
88 void append (const char* c); |
|
89 void append (str c); |
|
90 |
|
91 // Appends formatted text to the string. |
|
92 void appendformat (const char* c, ...); |
|
93 void appendformat (str c, ...); |
|
94 |
|
95 // Returns the first occurrence of c in the string, optionally starting |
|
96 // from a certain position rather than the start. |
|
97 unsigned int first (const char* c, unsigned int a = 0); |
|
98 |
|
99 // Returns the last occurrence of c in the string, optionally starting |
|
100 // from a certain position rather than the end. |
|
101 unsigned int last (const char* c, int a = -1); |
|
102 |
|
103 // Returns a substring of the string, from a to b. |
|
104 str substr (unsigned int a, unsigned int b); |
|
105 |
|
106 // Replace a substring with another substring. |
|
107 void replace (const char* o, const char* n, unsigned int a = 0); |
|
108 |
|
109 // Removes a given index from the string, optionally more characters than just 1. |
|
110 void remove (unsigned int idx, unsigned int dellen=1); |
|
111 |
|
112 void trim (int dellen); |
|
113 |
|
114 // Inserts a substring into a certain position. |
|
115 void insert (char* c, unsigned int pos); |
|
116 |
|
117 // Reverses the string. |
|
118 void reverse (); |
|
119 |
|
120 // Repeats the string a given amount of times. |
|
121 void repeat (unsigned int n); |
|
122 |
|
123 // Is the string a number? |
|
124 bool isnumber (); |
|
125 |
|
126 // Is the string a word, i.e consists only of alphabetic letters? |
|
127 bool isword (); |
|
128 |
|
129 bool contentcheck (int flags); |
|
130 |
|
131 int compare (const char* c); |
|
132 int compare (str c); |
|
133 |
|
134 // ====================================================================== |
|
135 // OPERATORS |
|
136 str operator + (str& c); |
|
137 str& operator += (char c); |
|
138 str& operator += (const char* c); |
|
139 str& operator += (const str c); |
|
140 char operator [] (unsigned int pos); |
|
141 operator char* () const; |
|
142 operator int () const; |
|
143 operator unsigned int () const; |
|
144 }; |
|
145 |
|
146 #endif // __STR_H__ |