/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
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
75
schar *
76
s_current_time_full (void) {
77
   schar * ret_val = s_malloc (32);
78
   schar fmt[32];
79
   time_t t = time (NULL);
80
   struct timeval tv;
81
   gettimeofday (&tv, NULL);
146 by Gustav Hartvigsson
* Reorderd includes in SimpleTypeSystem.h
82
   strftime (fmt, 50,"%Y-%m-%dT%H:%M:%S.%%03u%z", localtime (&t));
142 by Gustav Hartvigsson
* fiddeled with the test macros to make them easyer to read
83
   snprintf(ret_val, 50, fmt, tv.tv_usec);
84
   return ret_val;
85
}
86
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
87
sboolean
88
s_string_is_equal (const schar * a, const schar * b) {
89
  if (strcmp (a, b)){
90
    return FALSE;
91
  } else {
92
    return TRUE;
93
  }
94
}
95
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
96
size_t
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
97
s_string_len (const schar * str) {
98
  return strlen (str);
99
}
100
101
size_t
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
102
s_ustring_len (const suchar * us) {
103
  if (!us) {
104
    return 0;
105
  }
106
  size_t ret_val = 0;
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
107
  suchar * tmp = (suchar *)us;
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
108
  while (*tmp) {
109
    ret_val++;
110
    us++;
111
  }
112
  return ret_val;
113
}
114
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
115
/******************************************************************************/
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
116
schar *
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
117
s_ustring_to_string (const suchar * us) {
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
118
  schar * buffer;
119
  schar * resized;
120
  size_t bufpos;
121
  mbstate_t mbstate;
122
  memset (&mbstate, 0, sizeof (mbstate));
137 by Gustav Hartvigsson
* comment
123
  
124
  /* Thanks to Florian Philipp.
125
   * Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
126
   *
127
   * This is an adaptation of the original code to work with our suchar stuffs.
128
   */
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
129
  
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
130
  /* Save locale */
131
  schar * saved_locale;
132
  {
133
    schar * old_locale;
134
    size_t old_locale_len;
135
    old_locale = setlocale (LC_ALL, NULL);
136
    old_locale_len = strlen (old_locale) + 1;
137
    saved_locale = s_malloc (sizeof (char *) * old_locale_len);
138
    memcpy (saved_locale, old_locale, old_locale_len);
139
    /* set locale */
140
    setlocale (LC_ALL, "C.utf8");
141
  }
142
  
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
143
  buffer = malloc (sizeof (char32_t) * 4);
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
144
  
145
  schar out[MB_CUR_MAX];
146
  for(size_t n = 0, buflen = 4, bufpos = 0;
147
        n < s_ustring_len (us);
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
148
        n++, buflen += MB_CUR_MAX) { /* could be: buflen *= 2 ?*/
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
149
    sint rc = c32rtomb(out, us[n], &mbstate);
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
150
    if(! (resized = s_realloc (buffer, buflen))) {
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
151
      goto err;
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
152
    }
153
    buffer = resized;
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
154
    for (sint i = 0; i < rc; ++i) {
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
155
      buffer[bufpos] = out[i];
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
156
      bufpos++;
157
    }
158
  }
159
  
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
160
    /* reset locale */
161
  setlocale (LC_ALL, saved_locale);
162
  
136 by Gustav Hartvigsson
* implemented suchar string to cstring converter, needs testing
163
  /* shrink buffer to actually required size */
164
  if (! (resized = s_realloc (buffer, bufpos + 1)))
165
    goto err;
166
  
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
167
  
168
  return resized;
169
err:
170
  
171
  s_free(buffer);
172
  return NULL;
173
}
174
175
/******************************************************************************/
176
suchar *
177
s_string_to_ustring (const schar * str) {
178
  suchar * buffer;
179
  suchar * resized;
180
  size_t bufpos;
181
  mbstate_t mbstate;
182
  memset (&mbstate, 0, sizeof (mbstate));
183
  
184
  suint slen = s_string_len (str);
185
  
186
  
187
  /* Addaptation of the code in s_wstring_to_string to make it convert
188
   * mb strings to char32_t strings.
189
   */
190
  
191
  /* Save locale */
192
  schar * saved_locale;
193
  {
194
    schar * old_locale;
195
    size_t old_locale_len;
196
    old_locale = setlocale (LC_ALL, NULL);
197
    old_locale_len = strlen (old_locale) + 1;
198
    saved_locale = s_malloc (sizeof (char *) * old_locale_len);
199
    memcpy (saved_locale, old_locale, old_locale_len);
200
    /* set locale */
201
    setlocale (LC_ALL, "C.utf8");
202
  }
203
  
204
  buffer = malloc (sizeof (char32_t) * 4);
205
  
206
  suchar * out = s_malloc (sizeof (suchar));
207
  for(size_t n = 0, buflen = 4, bufpos = 0;
208
        n < slen;
209
        buflen += 2) {
210
    sint rc = mbrtoc32(out, &str[n], slen ,&mbstate);
211
    n += rc; // Get next mb in string...?
212
    if(! (resized = s_realloc (buffer, buflen * sizeof (suchar)))){
213
      goto err;
214
    }
215
    buffer = resized;
216
    buffer[bufpos] = *out;
217
    bufpos++;
218
  }
219
  
220
  s_free (out);
221
  
222
  /* reset locale */
223
  setlocale (LC_ALL, saved_locale);
224
  
225
  /* shrink buffer to actually required size */
226
  if (! (resized = s_realloc (buffer, (bufpos + 1) * sizeof (suchar)))) {
227
    goto err;
228
  }
229
  
230
  
231
  return resized;
232
err:
233
  
234
  s_free(buffer);
235
  return NULL;
236
  
237
}
238
239
/******************************************************************************/
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
240
/*
241
 * This should not be used. If you need to use this, uncomment it.
242
 * I am conveting to something more sane than wchar_t.
243
 */
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
244
schar *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
245
s_wstring_to_string (const wchar_t * ws) {
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
246
  s_err_print ("Using depricated function: s_wstring_to_string ().\n"
247
               "Do not use s_wstring_to_string, as it is platform "
248
               "specific and may, or may not couse troubble.\n"
249
               "Use uchar strings instead of wchar_t strings, and "
250
               "use s_ustring_to_string instead of this.\n");
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
251
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
252
  size_t buflen;
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
253
  schar * buffer;
254
  schar * resized;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
255
  size_t bufpos;
256
  mbstate_t mbstate;
257
  memset (&mbstate, 0, sizeof (mbstate));
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
258
  
259
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
260
  /* Save locale */
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
261
  schar * saved_locale;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
262
  {
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
263
    schar * old_locale;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
264
    size_t old_locale_len;
265
    old_locale = setlocale (LC_ALL, NULL);
266
    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.
267
    saved_locale = s_malloc (sizeof (char *) * old_locale_len);
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
268
    memcpy (saved_locale, old_locale, old_locale_len);
269
    /* set locale */
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
270
    setlocale (LC_ALL, "C.utf8");
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
271
  }
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
272
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
273
  /* Thanks to Florian Philipp.
274
   * Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
275
   *
276
   * to avoid parsing the string twice, we allocate memory speculatively
277
   * with exponential growth. Then we shrink it at the end.
278
   *
279
   * Alternative:  use
280
   * const wchar_t* tmp = ws;
281
   * size_t buflen = wcsrtombs(NULL, &tmp, 0, mbstate);
282
   * buffer = malloc(buflen);
283
   * wcsrtombs(buffer, &ws, 0, mbstate);
284
   * return buffer;
285
   */
286
  for (buflen = 4, buffer = NULL, bufpos = 0; ws; buflen *= 2) {
287
    size_t converted;
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
288
    if(! (resized = s_realloc (buffer, buflen)))
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
289
      goto err;
290
    buffer = resized;
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
291
    if((converted = wcsrtombs (buffer + bufpos, &ws, buflen - bufpos, &mbstate))
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
292
       == (size_t) -1)
293
      goto err;
294
    bufpos += converted;
295
  }
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
296
  
297
  /* reset locale */
298
  setlocale (LC_ALL, saved_locale);
299
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
300
  /* shrink buffer to actually required size */
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
301
  if (! (resized = s_realloc (buffer, bufpos + 1))) {
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
302
    goto err;
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
303
  }
304
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
305
  return resized;
306
err:
138 by Gustav Hartvigsson
* Fixed s_base_16_enc
307
  
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
308
  s_free(buffer);
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
309
  return NULL;
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
310
}
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
311
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
312
/*
313
 * See: http://www.programmingsimplified.com/c/source-code/c-program-binary-search
314
 */
315
#define _INTERNAL_MAKE_BIN_SEARCH_FUNC(T) \
316
sboolean \
317
s_binary_search_##T ( T list[], size_t f, size_t l, T n) {\
318
  size_t first = f;\
319
  size_t last = l;\
320
  size_t middle = (first + last)/2;\
321
  \
322
  while (first <= last) {\
323
    if (SPrimeListLong[middle] < n) {\
324
     first = middle + 1;\
325
    } else if (list[middle == n]) {\
326
      return TRUE;\
327
    } else {\
328
      last = middle - 1;\
329
    }\
330
    middle = (first + last) / 2;\
331
  }\
332
  return FALSE;\
333
}
334
335
_INTERNAL_MAKE_BIN_SEARCH_FUNC(sbyte)
336
337
_INTERNAL_MAKE_BIN_SEARCH_FUNC(subyte)
338
339
_INTERNAL_MAKE_BIN_SEARCH_FUNC(sshort)
340
341
_INTERNAL_MAKE_BIN_SEARCH_FUNC(sushort)
342
343
_INTERNAL_MAKE_BIN_SEARCH_FUNC(sint)
344
345
_INTERNAL_MAKE_BIN_SEARCH_FUNC(suint)
346
347
_INTERNAL_MAKE_BIN_SEARCH_FUNC(slong)
348
349
_INTERNAL_MAKE_BIN_SEARCH_FUNC(sulong)
350
351
_INTERNAL_MAKE_BIN_SEARCH_FUNC(sfloat)
352
353
_INTERNAL_MAKE_BIN_SEARCH_FUNC(sdouble)
354
355
_INTERNAL_MAKE_BIN_SEARCH_FUNC(squadruple)
356
357
358
/* ************************************************************************** 
359
 * Signal handlers
360
 * ************************************************************************** */
361
void
362
s_sig_segfault (int code) {
363
  if (code == SIGSEGV) {
364
    print_backtrace();
365
  }
366
}
367
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
368