]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxSWIG/SWIG/sstring.cxx
1 /*******************************************************************************
2 * Simplified Wrapper and Interface Generator (SWIG)
4 * Author : David Beazley
6 * Department of Computer Science
7 * University of Chicago
10 * beazley@cs.uchicago.edu
12 * Please read the file LICENSE for the copyright and terms by which SWIG
13 * can be used and distributed.
14 *******************************************************************************/
19 //-----------------------------------------------------------------------
20 // char *copy_string(char *str)
22 // Makes a copy of string str. Returns a pointer to it.
23 //-----------------------------------------------------------------------
25 char *copy_string(char *str
) {
28 res
= new char[strlen(str
)+1];
34 //-----------------------------------------------------------------------
35 // void format_string(char *str)
37 // Replace all of the escape sequences in the string str. It is
38 // assumed that the new string is smaller than the original!
39 //-----------------------------------------------------------------------
41 void format_string(char *str
) {
45 newstr
= copy_string(str
);
60 // We're in a simple escape sequence figure out what to do
99 // ---------------------------------------------------------------------------
103 // SWIG String class.
104 // This class is used to construct long strings when writing
105 // wrapper functions. It also "mimicks" the C++ streams I/O
106 // library for creating strings. For example :
108 // str << "hi there" << 3 << "\n";
110 // Will append the given strings to str.
112 // The idea here is to provide a mechanism for writing wrapper
113 // functions as strings before writing them out to a file.
115 // ---------------------------------------------------------------------------
116 #define INIT_MAXSIZE 64
118 // ---------------------------------------------------------------
119 // Pools. This is a list of available strings for memory allocation
121 // ---------------------------------------------------------------
130 static StrMem pool
[POOLSIZE
];
131 static int pool_index
= 0;
133 // Returns an item from the pool that can accomodate len
134 static char *get_pool(int len
, int &newlen
) {
137 if (pool_index
< 1) {
139 return new char[len
];
144 if ((pool
[i
].len
>= len
) && (pool
[i
].len
<= 4*len
)) {
146 newlen
= pool
[i
].len
;
147 pool
[i
].str
= pool
[pool_index
-1].str
;
148 pool
[i
].len
= pool
[pool_index
-1].len
;
156 return new char[len
];
159 // Puts an item onto the pool
161 static void put_pool(char *str
, int len
) {
162 if (len
< INIT_MAXSIZE
) {
166 if (pool_index
== POOLSIZE
) {
167 delete [] pool
[pool_index
-1].str
;
170 pool
[pool_index
].str
= str
;
171 pool
[pool_index
].len
= len
;
172 if (pool_index
!= POOLSIZE
)
176 // ---------------------------------------------------------------
179 // Create a new string with nothing in it
180 // ---------------------------------------------------------------
183 maxsize
= INIT_MAXSIZE
;
184 str
= get_pool(maxsize
,maxsize
); // May return a pool that is larger
189 // ---------------------------------------------------------------
190 // String::String(const char *s)
192 // Create a new string copied from a normal C-style string
193 // ---------------------------------------------------------------
195 String::String(const char *s
) {
196 int max
= INIT_MAXSIZE
;
200 if ((l
+1) > max
) max
= l
+1;
202 str
= get_pool(max
,maxsize
);
212 // ---------------------------------------------------------------
213 // String::~String(const char *s)
216 // ---------------------------------------------------------------
219 put_pool(str
,maxsize
);
222 // ---------------------------------------------------------------
223 // String::add(const char *newstr)
225 // Concatenate newstr onto the current string
226 // ---------------------------------------------------------------
228 void String::add(const char *newstr
) {
234 l
= (int) strlen(newstr
);
236 if (newlen
>= maxsize
-1) {
237 newmaxsize
= 2*maxsize
;
238 if (newlen
>= newmaxsize
-1) newmaxsize
= newlen
+ 1;
239 nstr
= get_pool(newmaxsize
,newmaxsize
);
241 put_pool(str
,maxsize
);
242 maxsize
= newmaxsize
;
245 strcpy(str
+len
,newstr
);
249 // ---------------------------------------------------------------
252 // Adds a single character to the current string
253 // ---------------------------------------------------------------
255 void String::add(char nc
) {
261 if (newlen
>= maxsize
-1) {
262 newmaxsize
= 2*maxsize
;
263 if (newlen
>= newmaxsize
-1) newmaxsize
= newlen
+ 1;
264 nstr
= get_pool(newmaxsize
,newmaxsize
);
266 put_pool(str
,maxsize
);
267 maxsize
= newmaxsize
;
274 // -----------------------------------------------------------------
275 // String::insert(const char *newstr)
277 // Inserts a string into the front of a string. (Primarily used
278 // for documentation generation)
279 // -----------------------------------------------------------------
281 void String::insert(const char *newstr
) {
288 newlen
= len
+ l
+ 1;
289 if (newlen
>= maxsize
-1) {
290 newmaxsize
= 2*maxsize
;
291 if (newlen
>= newmaxsize
-1) newmaxsize
= newlen
+ 1;
292 nstr
= get_pool(newmaxsize
,newmaxsize
);
294 put_pool(str
,maxsize
);
295 maxsize
= newmaxsize
;
299 /* Shift all of the characters over */
301 for (i
= len
-1; i
>= 0; i
--) {
305 /* Now insert the new string */
307 strncpy(str
,newstr
,l
);
313 // -----------------------------------------------------------------
314 // char *String::get()
316 // Get the current value of the string
317 // -----------------------------------------------------------------
319 char *String::get() const {
323 // -----------------------------------------------------------------
324 // String &operator<<(...)
326 // Shorthand for appending to the end of a string
327 // -----------------------------------------------------------------
329 String
&operator<<(String
&t
,const char *s
) {
335 String
&operator<<(String
&t
,const char s
) {
340 String
&operator<<(String
&t
,const int a
) {
342 sprintf(temp
,"%d",a
);
347 String
&operator<<(String
&t
, String
&s
) {
352 String
&String::operator=(const char *s
) {
357 if ((newlen
>= maxsize
) && (str
)) {
358 put_pool(str
,maxsize
);
359 str
= get_pool(newlen
+1,maxsize
);
371 // -----------------------------------------------------------------
372 // String &operator>>(...)
374 // Shorthand for inserting into the beginning of a string
375 // -----------------------------------------------------------------
377 String
&operator>>(const char *s
, String
&t
) {
382 String
&operator>>(String
&s
, String
&t
) {
387 // -----------------------------------------------------------------
388 // void String::untabify()
390 // Expand all tabs into spaces. This is useful for producing
391 // documentation and other things.
392 // -----------------------------------------------------------------
394 void String::untabify() {
400 // Copy the current string representation
403 oldmaxsize
= maxsize
;
405 // Reset the internal state of this string
408 str
= get_pool(maxsize
,maxsize
);
411 // Now walk down the old string and expand tabs. Tabs are usually place
412 // every 8 characters.
421 // Expand the character
422 for (i
= 0; i
< (8 - (pos
% 8)); i
++) {
433 // Blow away the old string
434 put_pool(s
,oldmaxsize
);
438 // -----------------------------------------------------------------
439 // void String::replace(const char *token, const char *rep)
441 // Search for tokens in a string and replace them with rep.
442 // This probably isn't the fastest implementation, but fortunately
443 // SWIG rarely calls this function.
444 // -----------------------------------------------------------------
446 void String::replace(const char *token
, const char *rep
) {
448 int oldmaxsize
= maxsize
;
449 // Copy the current string representation
453 // Now walk down the old string and search for tokens
459 str
= get_pool(maxsize
,maxsize
);
461 // Found a token in string s
462 // Dump characters into our string
470 // Now dump the replacement string into place
474 // Jump over the token
479 // Attach rest of the string
482 put_pool(s
,oldmaxsize
);
487 // -----------------------------------------------------------------
488 // void String::replaceid(char *token, char *rep)
490 // Searches for valid identifiers matching token and replaces
491 // them with rep. Unlike replace() tokens must be a valid C
492 // identifier (surrounded by whitespace).
493 // -----------------------------------------------------------------
495 void String::replaceid(const char *token
, const char *rep
) {
497 int whitespace
, tokenlen
;
498 int oldmaxsize
= maxsize
;
499 // Copy the current string representation
503 // Reset the internal state of this string
505 tokenlen
= strlen(token
);
507 // Now walk down the old string and search for tokens
513 str
= get_pool(maxsize
,maxsize
);
515 // Found a token in string s
516 // Dump characters into our string
521 if (!(isalpha(*c
) || (*c
== '_') || (*c
== '$'))) whitespace
= 1;
527 // Check to see if there is whitespace afterwards
528 if ((!c
[tokenlen
]) || (!(isalnum(c
[tokenlen
]) || (c
[tokenlen
] == '_') || (c
[tokenlen
] == '$')))) {
541 // Attach rest of the string
545 // Delete the old string
546 put_pool(s
,oldmaxsize
);
551 // -----------------------------------------------------------------
552 // void String::strip()
554 // Intelligently strips whitespace from a string. Will not strip
555 // whitespace if it is between two characters that are part of a
556 // legal C identifier. For example 'unsigned int'.
557 // -----------------------------------------------------------------
559 void String::strip() {
560 char *s
= str
; // Old pointer value
561 char *c
, lastchar
= 0;
563 int oldmaxsize
= maxsize
;
565 str
= get_pool(maxsize
,maxsize
); // Get a new string.
571 // See if this character doesn't violate our whitespace rules
573 if (isalnum(lastchar
) || (lastchar
== '_') || (lastchar
== '$')) {
574 if (isalnum(*c
) || (*c
== '_') || (*c
== '$'))
586 put_pool(s
,oldmaxsize
);