Debian Codegraph @

src/widgets/flnfc_common.cxx

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