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" |
|
23
by Gustav Hartvigsson
* Fixed some of the build warnings. |
24 |
#include <string.h> |
25 |
#include <stdlib.h> |
|
26 |
#include <stdio.h> |
|
27 |
#include <stdarg.h> |
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
28 |
#include <time.h> |
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
29 |
#include <wchar.h> |
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
30 |
#include <locale.h> |
21
by Gustav Hartvigsson
Woops! |
31 |
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
32 |
char * |
33 |
s_string_new (const char * s) { |
|
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
34 |
if (s == NULL) { |
35 |
return NULL; |
|
36 |
} |
|
37 |
size_t s_len = strlen (s); |
|
38 |
char * ret_val = malloc (s_len + 1); |
|
39 |
strcpy (ret_val, s); |
|
23
by Gustav Hartvigsson
* Fixed some of the build warnings. |
40 |
return ret_val; |
41 |
}
|
|
42 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
43 |
char * |
44 |
s_string_new_fmt (const char * format, ...) { |
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
45 |
char * buffer = malloc (strlen (format) + 512); |
46 |
char * ret_val = NULL; |
|
47 |
va_list args; |
|
48 |
va_start(args, format); |
|
49 |
vsprintf (buffer, format, args); |
|
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
50 |
ret_val = strdup (buffer); |
39
by Gustav Hartvigsson
* Added "check" target for testing. |
51 |
va_end(args); |
52 |
free (buffer); |
|
21
by Gustav Hartvigsson
Woops! |
53 |
return ret_val; |
54 |
}
|
|
55 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
56 |
char * |
57 |
s_string_new_with_len (const char * s, size_t len) { |
|
21
by Gustav Hartvigsson
Woops! |
58 |
char * ret_val = malloc (len + 1); |
59 |
strncpy (ret_val, s, len); |
|
60 |
return ret_val; |
|
61 |
}
|
|
62 |
||
61
by Gustav Hartvigsson
* Made the code more easy to read. |
63 |
char * |
64 |
s_current_time (void) { |
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
65 |
char * ret_val = malloc (21); |
66 |
time_t t = time (NULL); |
|
67 |
strftime (ret_val, 21, "%F %T", localtime (&t)); |
|
68 |
return ret_val; |
|
69 |
}
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
70 |
|
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
71 |
char * |
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
72 |
s_ustring_to_string (const suchar * us) { |
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
73 |
|
74 |
return NULL; |
|
75 |
}
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
76 |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
77 |
|
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
78 |
/*
|
79 |
* This should not be used. If you need to use this, uncomment it.
|
|
80 |
* I am conveting to something more sane than wchar_t.
|
|
81 |
*/
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
82 |
char * |
83 |
s_wstring_to_string (const wchar_t * ws) { |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
84 |
s_err_print ("Using depricated function: s_wstring_to_string ().\n" |
85 |
"Do not use s_wstring_to_string, as it is platform " |
|
86 |
"specific and may, or may not couse troubble.\n" |
|
87 |
"Use uchar strings instead of wchar_t strings, and " |
|
88 |
"use s_ustring_to_string instead of this.\n"); |
|
89 |
|
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
90 |
size_t buflen; |
91 |
char * buffer; |
|
92 |
char * resized; |
|
93 |
size_t bufpos; |
|
94 |
mbstate_t mbstate; |
|
95 |
memset (&mbstate, 0, sizeof (mbstate)); |
|
96 |
|
|
97 |
||
98 |
/* Save locale */ |
|
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
99 |
char * saved_locale; |
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
100 |
{ |
101 |
char * old_locale; |
|
102 |
size_t old_locale_len; |
|
103 |
old_locale = setlocale (LC_ALL, NULL); |
|
104 |
old_locale_len = strlen (old_locale) + 1; |
|
105 |
saved_locale = malloc (sizeof (char *) * old_locale_len); |
|
106 |
memcpy (saved_locale, old_locale, old_locale_len); |
|
107 |
/* set locale */ |
|
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
108 |
setlocale (LC_ALL, "C.utf8"); |
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
109 |
} |
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
110 |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
111 |
/* Thanks to Florian Philipp. |
112 |
* Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
|
|
113 |
*
|
|
114 |
* to avoid parsing the string twice, we allocate memory speculatively
|
|
115 |
* with exponential growth. Then we shrink it at the end.
|
|
116 |
*
|
|
117 |
* Alternative: use
|
|
118 |
* const wchar_t* tmp = ws;
|
|
119 |
* size_t buflen = wcsrtombs(NULL, &tmp, 0, mbstate);
|
|
120 |
* buffer = malloc(buflen);
|
|
121 |
* wcsrtombs(buffer, &ws, 0, mbstate);
|
|
122 |
* return buffer;
|
|
123 |
*/
|
|
124 |
for (buflen = 4, buffer = NULL, bufpos = 0; ws; buflen *= 2) { |
|
125 |
size_t converted; |
|
126 |
if(! (resized = realloc (buffer, buflen))) |
|
127 |
goto err; |
|
128 |
buffer = resized; |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
129 |
if((converted = wcsrtombs (buffer + bufpos, &ws, buflen - bufpos, &mbstate)) |
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
130 |
== (size_t) -1) |
131 |
goto err; |
|
132 |
bufpos += converted; |
|
133 |
} |
|
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
134 |
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
135 |
/* shrink buffer to actually required size */ |
136 |
if (! (resized = 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 |
free(buffer); |
|
148 |
return NULL; |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
149 |
}
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
150 |
|
66
by Gustav Hartvigsson
* Removed moved s_wstring_to_string. |
151 |