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