/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
84
schar *
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
85
s_ustring_to_string (const suchar * us) {
104 by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function...
86
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
87
  return NULL;
88
}
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
89
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
90
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
91
/*
92
 * This should not be used. If you need to use this, uncomment it.
93
 * I am conveting to something more sane than wchar_t.
94
 */
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
95
schar *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
96
s_wstring_to_string (const wchar_t * ws) {
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
97
  s_err_print ("Using depricated function: s_wstring_to_string ().\n"
98
               "Do not use s_wstring_to_string, as it is platform "
99
               "specific and may, or may not couse troubble.\n"
100
               "Use uchar strings instead of wchar_t strings, and "
101
               "use s_ustring_to_string instead of this.\n");
104 by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function...
102
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
103
  size_t buflen;
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
104
  schar * buffer;
105
  schar * resized;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
106
  size_t bufpos;
107
  mbstate_t mbstate;
108
  memset (&mbstate, 0, sizeof (mbstate));
104 by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function...
109
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
110
111
  /* Save locale */
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
112
  schar * saved_locale;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
113
  {
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
114
    schar * old_locale;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
115
    size_t old_locale_len;
116
    old_locale = setlocale (LC_ALL, NULL);
117
    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.
118
    saved_locale = s_malloc (sizeof (char *) * old_locale_len);
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
119
    memcpy (saved_locale, old_locale, old_locale_len);
120
    /* set locale */
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
121
    setlocale (LC_ALL, "C.utf8");
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
122
  }
104 by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function...
123
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
124
  /* Thanks to Florian Philipp.
125
   * Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
126
   *
127
   * to avoid parsing the string twice, we allocate memory speculatively
128
   * with exponential growth. Then we shrink it at the end.
129
   *
130
   * Alternative:  use
131
   * const wchar_t* tmp = ws;
132
   * size_t buflen = wcsrtombs(NULL, &tmp, 0, mbstate);
133
   * buffer = malloc(buflen);
134
   * wcsrtombs(buffer, &ws, 0, mbstate);
135
   * return buffer;
136
   */
137
  for (buflen = 4, buffer = NULL, bufpos = 0; ws; buflen *= 2) {
138
    size_t converted;
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
139
    if(! (resized = s_realloc (buffer, buflen)))
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
140
      goto err;
141
    buffer = resized;
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
142
    if((converted = wcsrtombs (buffer + bufpos, &ws, buflen - bufpos, &mbstate))
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
143
       == (size_t) -1)
144
      goto err;
145
    bufpos += converted;
146
  }
104 by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function...
147
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
148
  /* shrink buffer to actually required size */
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
149
  if (! (resized = s_realloc (buffer, bufpos + 1)))
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
150
    goto err;
104 by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function...
151
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
152
  /* reset locale */
153
  setlocale (LC_ALL, saved_locale);
104 by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function...
154
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
155
  return resized;
156
err:
157
  /* reset locale */
158
  setlocale (LC_ALL, saved_locale);
104 by Gustav Hartvigsson
* Passing arguments can not be initialised from within a function...
159
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
160
  s_free(buffer);
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
161
  return NULL;
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
162
}
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
163
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
164