DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / util / strings.c
1 /********************************************************************
3 strings.c - implementation of a string-handling routines
5 Niksa Orlic, 2004-04-19
7 ********************************************************************/
9 #include "error.h"
10 #include "strings.h"
11 #include "memory.h"
13 #include <stdlib.h>
14 #include <string.h>
16 /*
17 Create an empty string
18 */
19 string* string_create()
20 {
21 string *new_string;
22 new_string = (string*) mem_alloc(sizeof(string));
24 if (new_string == NULL)
25 die(1);
27 new_string->cstr = (char*) mem_alloc(sizeof(char) * 16);
29 if (new_string->cstr == NULL)
30 die(1);
32 new_string->cstr[0] = '\0';
34 new_string->length = 0;
35 new_string->allocated = 16;
37 return new_string;
38 }
41 /*
42 Create a copy of the string
43 */
44 string* string_duplicate(string *str)
45 {
46 string *new_string;
47 new_string = (string*) mem_alloc(sizeof(string));
49 if (new_string == NULL)
50 die(1);
52 new_string->cstr = (char*) mem_alloc(sizeof(char) * (str->allocated));
54 if (new_string->cstr == NULL)
55 die(1);
57 memcpy(new_string->cstr, str->cstr, str->length + 1); /* also copy the terminating NULL character */
59 new_string->allocated = str->allocated;
60 new_string->length = str->length;
62 return new_string;
63 }
66 /*
67 Create a string from a C-style string
68 */
69 string* string_from_cstr(char *cstr)
70 {
71 string *new_string;
72 new_string = (string*) mem_alloc(sizeof(string));
74 if (new_string == NULL)
75 die(1);
77 new_string->cstr = (char*) mem_alloc((strlen(cstr)+5) * sizeof(char));
79 if (new_string->cstr == NULL)
80 die(1);
82 strcpy(new_string->cstr, cstr);
84 new_string->allocated = strlen(cstr) + 5;
85 new_string->length = strlen(cstr);
87 return new_string;
88 }
91 /*
92 Free all memory used by the string
93 */
94 void string_destroy(string *str)
95 {
96 mem_free(str->cstr);
97 mem_free(str);
98 }
101 /*
102 Empties the given string
103 */
104 void string_empty(string *str)
106 str->length = 0;
107 str->cstr[0] = '\0';
111 /*
112 Append the second string to the first string
113 */
114 void string_append(string *str1, string *str2)
116 if (str1->length + str2->length + 1 >= str1->allocated)
118 str1->cstr = (char*) mem_realloc(str1->cstr, sizeof(char)*(str1->length + str2->length + 1));
120 if (str1->cstr == NULL)
121 die(1);
123 str1->allocated = str1->length + str2->length + 1;
126 memcpy(str1->cstr + str1->length,
127 str2->cstr,
128 str2->length);
130 str1->length = str1->length + str2->length;
132 str1->cstr[str1->length] = '\0';
136 /*
137 Append the C-style string to the string
138 */
139 void string_append_cstr(string *str1, char *cstr2)
141 short int cstr2_length;
143 if (str1 == NULL)
144 str1 = string_create();
146 cstr2_length = strlen(cstr2);
148 if (str1->length + cstr2_length + 1 >= str1->allocated)
150 str1->cstr = (char*) mem_realloc(str1->cstr, sizeof(char)*(str1->length + cstr2_length + 1));
152 if (str1->cstr == NULL)
153 die(1);
155 str1->allocated = str1->length + cstr2_length + 1;
158 memcpy(str1->cstr + str1->length,
159 cstr2,
160 cstr2_length);
162 str1->length = str1->length + cstr2_length;
164 str1->cstr[str1->length] = '\0';
168 /*
169 Get the C-style string from a given string
170 */
171 char *string_get_cstr(string *str)
173 return str->cstr;
177 /*
178 Get the string length
179 */
180 short int string_length(string *str)
182 return str->length;
185 /*
186 Translate the string into lowercase
187 */
188 void lowercase(char *cstr)
190 int i = 0;
192 while (cstr[i] != 0)
194 if ((cstr[i] >= 'A') && (cstr[i] <= 'Z'))
196 cstr[i] = cstr[i] | (char) 0x20;
199 i++;