bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
|
21
by Gustav Hartvigsson
Woops! |
1 |
/*
|
2 |
Copyright (c) 2013-2014 Gustav Hartvigsson
|
|
3 |
||
4 |
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 |
of this software and associated documentation files (the "Software"), to deal
|
|
6 |
in the Software without restriction, including without limitation the rights
|
|
7 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 |
copies of the Software, and to permit persons to whom the Software is
|
|
9 |
furnished to do so, subject to the following conditions:
|
|
10 |
||
11 |
The above copyright notice and this permission notice shall be included in
|
|
12 |
all copies or substantial portions of the Software.
|
|
13 |
||
14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 |
THE SOFTWARE.
|
|
21 |
*/
|
|
22 |
||
23 |
#include "utils.h" |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
24 |
|
|
23
by Gustav Hartvigsson
* Fixed some of the build warnings. |
25 |
#include <string.h> |
26 |
#include <stdlib.h> |
|
27 |
#include <stdio.h> |
|
28 |
#include <stdarg.h> |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
29 |
#include <time.h> |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
30 |
#include <wchar.h> |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
31 |
#include <locale.h> |
|
21
by Gustav Hartvigsson
Woops! |
32 |
|
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
33 |
schar * |
34 |
s_string_new (const schar * s) { |
|
|
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
35 |
if (s == NULL) { |
36 |
return NULL; |
|
37 |
} |
|
38 |
size_t s_len = strlen (s); |
|
|
105
by Gustav Hartvigsson
* Derp.. |
39 |
assert (s_len > 0); |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
40 |
schar * ret_val = s_malloc (s_len + 1); |
|
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
41 |
strcpy (ret_val, s); |
|
106
by Gustav Hartvigsson
* Made impremented s_linked_list_free () |
42 |
ret_val[s_len] = '\0'; |
|
23
by Gustav Hartvigsson
* Fixed some of the build warnings. |
43 |
return ret_val; |
44 |
}
|
|
45 |
||
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
46 |
schar * |
47 |
s_string_new_fmt (const schar * format, ...) { |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
48 |
schar * buffer = s_malloc (strlen (format) + 512); |
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
49 |
schar * ret_val = NULL; |
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
50 |
va_list args; |
51 |
va_start(args, format); |
|
52 |
vsprintf (buffer, format, args); |
|
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
53 |
ret_val = s_string_new (buffer); |
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
54 |
va_end(args); |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
55 |
s_free (buffer); |
|
21
by Gustav Hartvigsson
Woops! |
56 |
return ret_val; |
57 |
}
|
|
58 |
||
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
59 |
schar * |
60 |
s_string_new_with_len (const schar * s, size_t len) { |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
61 |
schar * ret_val = s_malloc (len + 1); |
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
62 |
ret_val[len + 1] = 0x0; |
|
21
by Gustav Hartvigsson
Woops! |
63 |
strncpy (ret_val, s, len); |
64 |
return ret_val; |
|
65 |
}
|
|
66 |
||
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
67 |
schar * |
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
68 |
s_current_time (void) { |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
69 |
schar * ret_val = s_malloc (21); |
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
70 |
time_t t = time (NULL); |
71 |
strftime (ret_val, 21, "%F %T", localtime (&t)); |
|
72 |
return ret_val; |
|
73 |
}
|
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
74 |
|
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
75 |
sboolean
|
76 |
s_string_is_equal (const schar * a, const schar * b) { |
|
77 |
if (strcmp (a, b)){ |
|
78 |
return FALSE; |
|
79 |
} else { |
|
80 |
return TRUE; |
|
81 |
} |
|
82 |
}
|
|
83 |
||
|
136
by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing |
84 |
size_t
|
85 |
s_ustring_len (const suchar * us) { |
|
86 |
if (!us) { |
|
87 |
return 0; |
|
88 |
} |
|
89 |
size_t ret_val = 0; |
|
90 |
suchar * tmp = us; |
|
91 |
while (*tmp) { |
|
92 |
ret_val++; |
|
93 |
us++; |
|
94 |
} |
|
95 |
return ret_val; |
|
96 |
}
|
|
97 |
||
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
98 |
schar * |
|
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
99 |
s_ustring_to_string (const suchar * us) { |
|
136
by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing |
100 |
size_t buflen; |
101 |
schar * buffer; |
|
102 |
schar * resized; |
|
103 |
size_t bufpos; |
|
104 |
mbstate_t mbstate; |
|
105 |
memset (&mbstate, 0, sizeof (mbstate)); |
|
106 |
||
107 |
||
108 |
/* Save locale */ |
|
109 |
schar * saved_locale; |
|
110 |
{ |
|
111 |
schar * old_locale; |
|
112 |
size_t old_locale_len; |
|
113 |
old_locale = setlocale (LC_ALL, NULL); |
|
114 |
old_locale_len = strlen (old_locale) + 1; |
|
115 |
saved_locale = s_malloc (sizeof (char *) * old_locale_len); |
|
116 |
memcpy (saved_locale, old_locale, old_locale_len); |
|
117 |
/* set locale */ |
|
118 |
setlocale (LC_ALL, "C.utf8"); |
|
119 |
} |
|
120 |
|
|
121 |
|
|
122 |
schar out[MB_CUR_MAX]; |
|
123 |
for(size_t n = 0, buflen = 4, bufpos = 0; |
|
124 |
n < s_ustring_len (us); |
|
125 |
n++, buflen *= 2) { |
|
126 |
sint rc = c32rtomb(out, us[n], &mbstate); |
|
127 |
if(! (resized = s_realloc (buffer, buflen))) |
|
128 |
goto err; |
|
129 |
for (sint i = 0; i < rc; ++i) { |
|
130 |
buffer[bufpos]; |
|
131 |
bufpos++; |
|
132 |
} |
|
133 |
} |
|
134 |
|
|
135 |
/* shrink buffer to actually required size */ |
|
136 |
if (! (resized = s_realloc (buffer, bufpos + 1))) |
|
137 |
goto err; |
|
138 |
|
|
139 |
/* reset locale */ |
|
140 |
setlocale (LC_ALL, saved_locale); |
|
141 |
||
142 |
return resized; |
|
143 |
err: |
|
144 |
/* reset locale */ |
|
145 |
setlocale (LC_ALL, saved_locale); |
|
146 |
||
147 |
s_free(buffer); |
|
|
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
148 |
return NULL; |
149 |
}
|
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
150 |
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
151 |
|
|
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
152 |
/*
|
153 |
* This should not be used. If you need to use this, uncomment it.
|
|
154 |
* I am conveting to something more sane than wchar_t.
|
|
155 |
*/
|
|
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
156 |
schar * |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
157 |
s_wstring_to_string (const wchar_t * ws) { |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
158 |
s_err_print ("Using depricated function: s_wstring_to_string ().\n" |
159 |
"Do not use s_wstring_to_string, as it is platform " |
|
160 |
"specific and may, or may not couse troubble.\n" |
|
161 |
"Use uchar strings instead of wchar_t strings, and " |
|
162 |
"use s_ustring_to_string instead of this.\n"); |
|
|
104
by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function... |
163 |
|
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
164 |
size_t buflen; |
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
165 |
schar * buffer; |
166 |
schar * resized; |
|
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
167 |
size_t bufpos; |
168 |
mbstate_t mbstate; |
|
169 |
memset (&mbstate, 0, sizeof (mbstate)); |
|
|
104
by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function... |
170 |
|
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
171 |
|
172 |
/* Save locale */ |
|
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
173 |
schar * saved_locale; |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
174 |
{ |
|
103
by Gustav Hartvigsson
* General cleanup/make it pritty. |
175 |
schar * old_locale; |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
176 |
size_t old_locale_len; |
177 |
old_locale = setlocale (LC_ALL, NULL); |
|
178 |
old_locale_len = strlen (old_locale) + 1; |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
179 |
saved_locale = s_malloc (sizeof (char *) * old_locale_len); |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
180 |
memcpy (saved_locale, old_locale, old_locale_len); |
181 |
/* set locale */ |
|
|
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
182 |
setlocale (LC_ALL, "C.utf8"); |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
183 |
} |
|
104
by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function... |
184 |
|
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
185 |
/* Thanks to Florian Philipp. |
186 |
* Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
|
|
187 |
*
|
|
188 |
* to avoid parsing the string twice, we allocate memory speculatively
|
|
189 |
* with exponential growth. Then we shrink it at the end.
|
|
190 |
*
|
|
191 |
* Alternative: use
|
|
192 |
* const wchar_t* tmp = ws;
|
|
193 |
* size_t buflen = wcsrtombs(NULL, &tmp, 0, mbstate);
|
|
194 |
* buffer = malloc(buflen);
|
|
195 |
* wcsrtombs(buffer, &ws, 0, mbstate);
|
|
196 |
* return buffer;
|
|
197 |
*/
|
|
198 |
for (buflen = 4, buffer = NULL, bufpos = 0; ws; buflen *= 2) { |
|
199 |
size_t converted; |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
200 |
if(! (resized = s_realloc (buffer, buflen))) |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
201 |
goto err; |
202 |
buffer = resized; |
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
203 |
if((converted = wcsrtombs (buffer + bufpos, &ws, buflen - bufpos, &mbstate)) |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
204 |
== (size_t) -1) |
205 |
goto err; |
|
206 |
bufpos += converted; |
|
207 |
} |
|
|
104
by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function... |
208 |
|
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
209 |
/* shrink buffer to actually required size */ |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
210 |
if (! (resized = s_realloc (buffer, bufpos + 1))) |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
211 |
goto err; |
|
104
by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function... |
212 |
|
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
213 |
/* reset locale */ |
214 |
setlocale (LC_ALL, saved_locale); |
|
|
104
by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function... |
215 |
|
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
216 |
return resized; |
217 |
err: |
|
218 |
/* reset locale */ |
|
219 |
setlocale (LC_ALL, saved_locale); |
|
|
104
by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function... |
220 |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
221 |
s_free(buffer); |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
222 |
return NULL; |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
223 |
}
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
224 |
|
|
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
225 |