1 //
2 // flnfc_common.cxx -- common string subs for Fl_Native_File_Chooser
3 //
4 // Copyright 2004 by Greg Ercolano.
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Library General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 #include <string.h>
20
21 // COPY A STRING WITH 'new'
22 // Value can be NULL
23 //
strnew(const char * val)24 static char *strnew(const char *val) {
25 if ( val == NULL ) return(NULL);
26 char *s = new char[strlen(val)+1];
27 strcpy(s, val);
28 return(s);
29 }
30
31 // FREE STRING CREATED WITH strnew(), NULLS OUT STRING
32 // Value can be NULL
33 //
strfree(char * val)34 static char *strfree(char *val) {
35 if ( val ) delete [] val;
36 return(NULL);
37 }
38
39 #ifndef __WOE32__
40 // 'DYNAMICALLY' APPEND ONE STRING TO ANOTHER
41 // Returns newly allocated string, or NULL
42 // if s && val == NULL.
43 // 's' can be NULL; returns a strnew(val).
44 // 'val' can be NULL; s is returned unmodified.
45 //
46 // Usage:
47 // char *s = strnew("foo"); // s = "foo"
48 // s = strapp(s, "bar"); // s = "foobar"
49 //
strapp(char * s,const char * val)50 static char *strapp(char *s, const char *val) {
51 if ( ! val ) {
52 return(s); // Nothing to append? return s
53 }
54 if ( ! s ) {
55 return(strnew(val)); // New string? return copy of val
56 }
57 char *news = new char[strlen(s)+strlen(val)+1];
58 strcpy(news, s);
59 strcat(news, val);
60 delete [] s; // delete old string
61 return(news); // return new copy
62 }
63 #endif
64
65 // APPEND A CHARACTER TO A STRING
66 // This does NOT allocate space for the new character.
67 //
chrcat(char * s,char c)68 static void chrcat(char *s, char c) {
69 char tmp[2] = { c, '\0' };
70 strcat(s, tmp);
71 }
72