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) { |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
34 |
char * ret_val = strdup (s); |
|
23
by Gustav Hartvigsson
* Fixed some of the build warnings. |
35 |
return ret_val; |
36 |
}
|
|
37 |
||
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
38 |
char * |
39 |
s_string_new_fmt (const char * format, ...) { |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
40 |
char * buffer = malloc (strlen (format) + 512); |
41 |
char * ret_val = NULL; |
|
42 |
va_list args; |
|
43 |
va_start(args, format); |
|
44 |
vsprintf (buffer, format, args); |
|
45 |
ret_val = (char *) strdup (buffer); |
|
46 |
va_end(args); |
|
47 |
free (buffer); |
|
|
21
by Gustav Hartvigsson
Woops! |
48 |
return ret_val; |
49 |
}
|
|
50 |
||
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
51 |
char * |
52 |
s_string_new_with_len (const char * s, size_t len) { |
|
|
21
by Gustav Hartvigsson
Woops! |
53 |
char * ret_val = malloc (len + 1); |
54 |
strncpy (ret_val, s, len); |
|
55 |
return ret_val; |
|
56 |
}
|
|
57 |
||
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
58 |
char * |
59 |
s_current_time (void) { |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
60 |
char * ret_val = malloc (21); |
61 |
time_t t = time (NULL); |
|
62 |
strftime (ret_val, 21, "%F %T", localtime (&t)); |
|
63 |
return ret_val; |
|
64 |
}
|
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
65 |
|
66 |
||
67 |
char * |
|
68 |
s_wstring_to_string (const wchar_t * ws) { |
|
|
64
by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-) |
69 |
size_t buflen; |
70 |
char * buffer; |
|
71 |
char * resized; |
|
72 |
size_t bufpos; |
|
73 |
mbstate_t mbstate; |
|
74 |
memset (&mbstate, 0, sizeof (mbstate)); |
|
75 |
|
|
76 |
||
77 |
/* Save locale */ |
|
78 |
char * saved_locale; |
|
79 |
{ |
|
80 |
char * old_locale; |
|
81 |
size_t old_locale_len; |
|
82 |
old_locale = setlocale (LC_ALL, NULL); |
|
83 |
old_locale_len = strlen (old_locale) + 1; |
|
84 |
saved_locale = malloc (sizeof (char *) * old_locale_len); |
|
85 |
memcpy (saved_locale, old_locale, old_locale_len); |
|
86 |
/* set locale */ |
|
87 |
setlocale (LC_ALL, ""); |
|
88 |
} |
|
89 |
/* Thanks to Florian Philipp. |
|
90 |
* Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
|
|
91 |
*
|
|
92 |
* to avoid parsing the string twice, we allocate memory speculatively
|
|
93 |
* with exponential growth. Then we shrink it at the end.
|
|
94 |
*
|
|
95 |
* Alternative: use
|
|
96 |
* const wchar_t* tmp = ws;
|
|
97 |
* size_t buflen = wcsrtombs(NULL, &tmp, 0, mbstate);
|
|
98 |
* buffer = malloc(buflen);
|
|
99 |
* wcsrtombs(buffer, &ws, 0, mbstate);
|
|
100 |
* return buffer;
|
|
101 |
*/
|
|
102 |
for (buflen = 4, buffer = NULL, bufpos = 0; ws; buflen *= 2) { |
|
103 |
size_t converted; |
|
104 |
if(! (resized = realloc (buffer, buflen))) |
|
105 |
goto err; |
|
106 |
buffer = resized; |
|
107 |
if((converted = wcsrtombs (buffer + bufpos, &ws, buflen - bufpos, &mbstate)) |
|
108 |
== (size_t) -1) |
|
109 |
goto err; |
|
110 |
bufpos += converted; |
|
111 |
} |
|
112 |
/* shrink buffer to actually required size */ |
|
113 |
if (! (resized = realloc (buffer, bufpos + 1))) |
|
114 |
goto err; |
|
115 |
|
|
116 |
/* reset locale */ |
|
117 |
setlocale (LC_ALL, saved_locale); |
|
118 |
|
|
119 |
return resized; |
|
120 |
err: |
|
121 |
/* reset locale */ |
|
122 |
setlocale (LC_ALL, saved_locale); |
|
123 |
|
|
124 |
free(buffer); |
|
125 |
return NULL; |
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
126 |
}
|