/simpletypesystem/trunk

To get this branch, use:
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
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
85
s_string_len (const schar * str) {
86
  return strlen (str);
87
}
88
89
size_t
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
90
s_ustring_len (const suchar * us) {
91
  if (!us) {
92
    return 0;
93
  }
94
  size_t ret_val = 0;
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
95
  suchar * tmp = (suchar *)us;
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
96
  while (*tmp) {
97
    ret_val++;
98
    us++;
99
  }
100
  return ret_val;
101
}
102
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
103
/******************************************************************************/
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
104
schar *
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
105
s_ustring_to_string (const suchar * us) {
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
106
  schar * buffer;
107
  schar * resized;
108
  size_t bufpos;
109
  mbstate_t mbstate;
110
  memset (&mbstate, 0, sizeof (mbstate));
137 by Gustav Hartvigsson
* comment
111
  
112
  /* Thanks to Florian Philipp.
113
   * Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
114
   *
115
   * This is an adaptation of the original code to work with our suchar stuffs.
116
   */
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
117
  
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
118
  /* Save locale */
119
  schar * saved_locale;
120
  {
121
    schar * old_locale;
122
    size_t old_locale_len;
123
    old_locale = setlocale (LC_ALL, NULL);
124
    old_locale_len = strlen (old_locale) + 1;
125
    saved_locale = s_malloc (sizeof (char *) * old_locale_len);
126
    memcpy (saved_locale, old_locale, old_locale_len);
127
    /* set locale */
128
    setlocale (LC_ALL, "C.utf8");
129
  }
130
  
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
131
  buffer = malloc (sizeof (char32_t) * 4);
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
132
  
133
  schar out[MB_CUR_MAX];
134
  for(size_t n = 0, buflen = 4, bufpos = 0;
135
        n < s_ustring_len (us);
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
136
        n++, buflen += MB_CUR_MAX) { /* could be: buflen *= 2 ?*/
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
137
    sint rc = c32rtomb(out, us[n], &mbstate);
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
138
    if(! (resized = s_realloc (buffer, buflen))) {
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
139
      goto err;
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
140
    }
141
    buffer = resized;
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
142
    for (sint i = 0; i < rc; ++i) {
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
143
      buffer[bufpos] = out[i];
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
144
      bufpos++;
145
    }
146
  }
147
  
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
148
    /* reset locale */
149
  setlocale (LC_ALL, saved_locale);
150
  
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
151
  /* shrink buffer to actually required size */
152
  if (! (resized = s_realloc (buffer, bufpos + 1)))
153
    goto err;
154
  
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
155
  
156
  return resized;
157
err:
158
  
159
  s_free(buffer);
160
  return NULL;
161
}
162
163
/******************************************************************************/
164
suchar *
165
s_string_to_ustring (const schar * str) {
166
  suchar * buffer;
167
  suchar * resized;
168
  size_t bufpos;
169
  mbstate_t mbstate;
170
  memset (&mbstate, 0, sizeof (mbstate));
171
  
172
  suint slen = s_string_len (str);
173
  
174
  
175
  /* Addaptation of the code in s_wstring_to_string to make it convert
176
   * mb strings to char32_t strings.
177
   */
178
  
179
  /* Save locale */
180
  schar * saved_locale;
181
  {
182
    schar * old_locale;
183
    size_t old_locale_len;
184
    old_locale = setlocale (LC_ALL, NULL);
185
    old_locale_len = strlen (old_locale) + 1;
186
    saved_locale = s_malloc (sizeof (char *) * old_locale_len);
187
    memcpy (saved_locale, old_locale, old_locale_len);
188
    /* set locale */
189
    setlocale (LC_ALL, "C.utf8");
190
  }
191
  
192
  buffer = malloc (sizeof (char32_t) * 4);
193
  
194
  suchar * out = s_malloc (sizeof (suchar));
195
  for(size_t n = 0, buflen = 4, bufpos = 0;
196
        n < slen;
197
        buflen += 2) {
198
    sint rc = mbrtoc32(out, &str[n], slen ,&mbstate);
199
    n += rc; // Get next mb in string...?
200
    if(! (resized = s_realloc (buffer, buflen * sizeof (suchar)))){
201
      goto err;
202
    }
203
    buffer = resized;
204
    buffer[bufpos] = *out;
205
    bufpos++;
206
  }
207
  
208
  s_free (out);
209
  
210
  /* reset locale */
211
  setlocale (LC_ALL, saved_locale);
212
  
213
  /* shrink buffer to actually required size */
214
  if (! (resized = s_realloc (buffer, (bufpos + 1) * sizeof (suchar)))) {
215
    goto err;
216
  }
217
  
218
  
219
  return resized;
220
err:
221
  
222
  s_free(buffer);
223
  return NULL;
224
  
225
}
226
227
/******************************************************************************/
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
228
/*
229
 * This should not be used. If you need to use this, uncomment it.
230
 * I am conveting to something more sane than wchar_t.
231
 */
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
232
schar *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
233
s_wstring_to_string (const wchar_t * ws) {
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
234
  s_err_print ("Using depricated function: s_wstring_to_string ().\n"
235
               "Do not use s_wstring_to_string, as it is platform "
236
               "specific and may, or may not couse troubble.\n"
237
               "Use uchar strings instead of wchar_t strings, and "
238
               "use s_ustring_to_string instead of this.\n");
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
239
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
240
  size_t buflen;
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
241
  schar * buffer;
242
  schar * resized;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
243
  size_t bufpos;
244
  mbstate_t mbstate;
245
  memset (&mbstate, 0, sizeof (mbstate));
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
246
  
247
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
248
  /* Save locale */
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
249
  schar * saved_locale;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
250
  {
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
251
    schar * old_locale;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
252
    size_t old_locale_len;
253
    old_locale = setlocale (LC_ALL, NULL);
254
    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.
255
    saved_locale = s_malloc (sizeof (char *) * old_locale_len);
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
256
    memcpy (saved_locale, old_locale, old_locale_len);
257
    /* set locale */
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
258
    setlocale (LC_ALL, "C.utf8");
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
259
  }
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
260
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
261
  /* Thanks to Florian Philipp.
262
   * Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
263
   *
264
   * to avoid parsing the string twice, we allocate memory speculatively
265
   * with exponential growth. Then we shrink it at the end.
266
   *
267
   * Alternative:  use
268
   * const wchar_t* tmp = ws;
269
   * size_t buflen = wcsrtombs(NULL, &tmp, 0, mbstate);
270
   * buffer = malloc(buflen);
271
   * wcsrtombs(buffer, &ws, 0, mbstate);
272
   * return buffer;
273
   */
274
  for (buflen = 4, buffer = NULL, bufpos = 0; ws; buflen *= 2) {
275
    size_t converted;
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
276
    if(! (resized = s_realloc (buffer, buflen)))
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
277
      goto err;
278
    buffer = resized;
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
279
    if((converted = wcsrtombs (buffer + bufpos, &ws, buflen - bufpos, &mbstate))
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
280
       == (size_t) -1)
281
      goto err;
282
    bufpos += converted;
283
  }
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
284
  
285
  /* reset locale */
286
  setlocale (LC_ALL, saved_locale);
287
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
288
  /* shrink buffer to actually required size */
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
289
  if (! (resized = s_realloc (buffer, bufpos + 1))) {
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
290
    goto err;
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
291
  }
292
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
293
  return resized;
294
err:
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
295
  
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
296
  s_free(buffer);
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
297
  return NULL;
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
298
}
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
299
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
300