1 /*
2    Copyright (c) 2001-2002 Perry Rapp
3    "The MIT license"
4    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 */
8 /*=============================================================
9  * stralloc.c -- heap allocation & deallocation for strings
10  *==============================================================*/
11 
12 #include "llstdlib.h"
13 /* llstdlib.h pulls in standard.h, config.h, sys_inc.h */
14 
15 /*===============================
16  * strsave -- Save copy of string
17  * returns stdalloc'd memory
18  *=============================*/
19 STRING
strsave(CNSTRING str)20 strsave (CNSTRING str)
21 {
22 	/* some OSs (made in Redmond) do not handle strdup(0) very well */
23 	ASSERT(str);
24 	return strcpy(stdalloc(strlen(str) + 1), str);
25 }
26 /*===============================
27  * strfree -- Free & clear a STRING by ref
28  *  (STRING may be NULL)
29  *=============================*/
30 void
strfree(STRING * str)31 strfree (STRING * str)
32 {
33 	if (*str) {
34 		stdfree(*str);
35 		*str = NULL;
36 	}
37 }
38 /*===============================
39  * strcfree -- Free & clear a CNSTRING by ref
40  *  (STRING may be NULL)
41  *=============================*/
42 void
strcfree(CNSTRING * str)43 strcfree (CNSTRING * str)
44 {
45 	if (*str) {
46 		stdfree(*str);
47 		*str = NULL;
48 	}
49 }
50 /*===============================
51  * strupdate -- strfree followed by strsave
52  *  (STRING may be NULL)
53  *=============================*/
54 void
strupdate(STRING * str,CNSTRING value)55 strupdate (STRING * str, CNSTRING value)
56 {
57 	strfree(str);
58 	if (value)
59 		*str = strsave(value);
60 }
61 /*==================================
62  * strconcat -- Catenate two strings
63  * Either (but not both) args may be null
64  * returns stdalloc'd memory
65  *================================*/
66 STRING
strconcat(STRING s1,STRING s2)67 strconcat (STRING s1, STRING s2)
68 {
69 	INT len;
70 	STRING s3;
71 	s1 = s1 ? s1 : "";
72 	s2 = s2 ? s2 : "";
73 	if (!s1[0]) return strsave(s2);
74 	if (!s2[0]) return strsave(s1);
75 	len = strlen(s1)+strlen(s2);
76 	s3 = (STRING) stdalloc(len+1);
77 	strcpy(s3, s1);
78 	strcat(s3, s2);
79 	return s3;
80 }
81 /*=============================================+
82  * free_array_strings -- Free all strings in an array
83  *  n:   [IN]  size of array
84  *  arr: [I/O] array
85  *============================================*/
86 void
free_array_strings(INT n,STRING * arr)87 free_array_strings (INT n, STRING * arr)
88 {
89 	INT i;
90 	for (i=0; i<n; ++i)
91 	{
92 		strfree(&arr[i]); /* frees & zeros pointer */
93 	}
94 }
95 /*==============================
96  * allocsubbytes -- Return substring (by byte counts)
97  *  assumes valid inputs
98  *  returns alloc'd memory
99  * start is 0-based start byte, len is # bytes
100  * strictly at the byte level
101  * client is responsible for codeset
102  * Created: 2001/08/02 (Perry Rapp)
103  *============================*/
104 STRING
allocsubbytes(STRING s,INT start,INT num)105 allocsubbytes (STRING s, INT start, INT num)
106 {
107 	STRING substr;
108 	substr = stdalloc(num+1);
109 	strncpy(substr, &s[start], num);
110 	substr[num] = 0;
111 	return substr;
112 }
113