/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"
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
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
32
schar *
33
s_string_new (const schar * s) {
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
34
  if (s == NULL) {
35
    return NULL;
36
  }
37
  size_t s_len = strlen (s);
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
38
  schar * ret_val = malloc (s_len + 1);
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
39
  strcpy (ret_val, s);
23 by Gustav Hartvigsson
* Fixed some of the build warnings.
40
  return ret_val;
41
}
42
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
43
schar *
44
s_string_new_fmt (const schar * format, ...) {
45
  schar * buffer = malloc (strlen (format) + 512);
46
  schar * ret_val = NULL;
39 by Gustav Hartvigsson
* Added "check" target for testing.
47
  va_list args;
48
  va_start(args, format);
49
  vsprintf (buffer, format, args);
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
50
  ret_val = s_string_new (buffer);
39 by Gustav Hartvigsson
* Added "check" target for testing.
51
  va_end(args);
52
  free (buffer);
21 by Gustav Hartvigsson
Woops!
53
  return ret_val;
54
}
55
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
56
schar *
57
s_string_new_with_len (const schar * s, size_t len) {
58
  schar * ret_val = malloc (len + 1);
59
  ret_val[len + 1] = 0x0;
21 by Gustav Hartvigsson
Woops!
60
  strncpy (ret_val, s, len);
61
  return ret_val;
62
}
63
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
64
schar *
61 by Gustav Hartvigsson
* Made the code more easy to read.
65
s_current_time (void) {
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
66
  schar * ret_val = malloc (21);
39 by Gustav Hartvigsson
* Added "check" target for testing.
67
  time_t t = time (NULL);
68
  strftime (ret_val, 21, "%F %T", localtime (&t));
69
  return ret_val;
70
}
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
71
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
72
sboolean
73
s_string_is_equal (const schar * a, const schar * b) {
74
  if (strcmp (a, b)){
75
    return FALSE;
76
  } else {
77
    return TRUE;
78
  }
79
}
80
81
schar *
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
82
s_ustring_to_string (const suchar * us) {
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
83
  
84
  return NULL;
85
}
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
86
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
87
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
88
/*
89
 * This should not be used. If you need to use this, uncomment it.
90
 * I am conveting to something more sane than wchar_t.
91
 */
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
92
schar *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
93
s_wstring_to_string (const wchar_t * ws) {
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
94
  s_err_print ("Using depricated function: s_wstring_to_string ().\n"
95
               "Do not use s_wstring_to_string, as it is platform "
96
               "specific and may, or may not couse troubble.\n"
97
               "Use uchar strings instead of wchar_t strings, and "
98
               "use s_ustring_to_string instead of this.\n");
99
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
100
  size_t buflen;
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
101
  schar * buffer;
102
  schar * resized;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
103
  size_t bufpos;
104
  mbstate_t mbstate;
105
  memset (&mbstate, 0, sizeof (mbstate));
106
  
107
108
  /* Save locale */
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
109
  schar * saved_locale;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
110
  {
103 by Gustav Hartvigsson
* General cleanup/make it pritty.
111
    schar * old_locale;
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
112
    size_t old_locale_len;
113
    old_locale = setlocale (LC_ALL, NULL);
114
    old_locale_len = strlen (old_locale) + 1;
115
    saved_locale = malloc (sizeof (char *) * old_locale_len);
116
    memcpy (saved_locale, old_locale, old_locale_len);
117
    /* set locale */
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
118
    setlocale (LC_ALL, "C.utf8");
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
119
  }
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
120
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
121
  /* Thanks to Florian Philipp.
122
   * Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
123
   *
124
   * to avoid parsing the string twice, we allocate memory speculatively
125
   * with exponential growth. Then we shrink it at the end.
126
   *
127
   * Alternative:  use
128
   * const wchar_t* tmp = ws;
129
   * size_t buflen = wcsrtombs(NULL, &tmp, 0, mbstate);
130
   * buffer = malloc(buflen);
131
   * wcsrtombs(buffer, &ws, 0, mbstate);
132
   * return buffer;
133
   */
134
  for (buflen = 4, buffer = NULL, bufpos = 0; ws; buflen *= 2) {
135
    size_t converted;
136
    if(! (resized = realloc (buffer, buflen)))
137
      goto err;
138
    buffer = resized;
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
139
    if((converted = wcsrtombs (buffer + bufpos, &ws, buflen - bufpos, &mbstate))
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
140
       == (size_t) -1)
141
      goto err;
142
    bufpos += converted;
143
  }
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
144
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
145
  /* shrink buffer to actually required size */
146
  if (! (resized = realloc (buffer, bufpos + 1)))
147
    goto err;
148
  
149
  /* reset locale */
150
  setlocale (LC_ALL, saved_locale);
151
  
152
  return resized;
153
err:
154
  /* reset locale */
155
  setlocale (LC_ALL, saved_locale);
156
  
157
  free(buffer);
158
  return NULL;
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
159
}
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
160
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
161