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