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