/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Copyright (c) 2013-2014 Gustav Hartvigsson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "utils.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <wchar.h>
#include <locale.h>

char *
s_string_new (const char * s) {
  if (s == NULL) {
    return NULL;
  }
  size_t s_len = strlen (s);
  char * ret_val = malloc (s_len + 1);
  strcpy (ret_val, s);
  return ret_val;
}

char *
s_string_new_fmt (const char * format, ...) {
  char * buffer = malloc (strlen (format) + 512);
  char * ret_val = NULL;
  va_list args;
  va_start(args, format);
  vsprintf (buffer, format, args);
  ret_val = strdup (buffer);
  va_end(args);
  free (buffer);
  return ret_val;
}

char *
s_string_new_with_len (const char * s, size_t len) {
  char * ret_val = malloc (len + 1);
  strncpy (ret_val, s, len);
  return ret_val;
}

char *
s_current_time (void) {
  char * ret_val = malloc (21);
  time_t t = time (NULL);
  strftime (ret_val, 21, "%F %T", localtime (&t));
  return ret_val;
}

char *
s_ustring_to_string (const suchar * us) {
  
  return NULL;
}


/*
 * This should not be used. If you need to use this, uncomment it.
 * I am conveting to something more sane than wchar_t.
 */
char *
s_wstring_to_string (const wchar_t * ws) {
  s_err_print ("Using depricated function: s_wstring_to_string ().\n"
               "Do not use s_wstring_to_string, as it is platform "
               "specific and may, or may not couse troubble.\n"
               "Use uchar strings instead of wchar_t strings, and "
               "use s_ustring_to_string instead of this.\n");
  
  size_t buflen;
  char * buffer;
  char * resized;
  size_t bufpos;
  mbstate_t mbstate;
  memset (&mbstate, 0, sizeof (mbstate));
  

  /* Save locale */
  char * saved_locale;
  {
    char * old_locale;
    size_t old_locale_len;
    old_locale = setlocale (LC_ALL, NULL);
    old_locale_len = strlen (old_locale) + 1;
    saved_locale = malloc (sizeof (char *) * old_locale_len);
    memcpy (saved_locale, old_locale, old_locale_len);
    /* set locale */
    setlocale (LC_ALL, "C.utf8");
  }
  
  /* Thanks to Florian Philipp.
   * Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
   *
   * to avoid parsing the string twice, we allocate memory speculatively
   * with exponential growth. Then we shrink it at the end.
   *
   * Alternative:  use
   * const wchar_t* tmp = ws;
   * size_t buflen = wcsrtombs(NULL, &tmp, 0, mbstate);
   * buffer = malloc(buflen);
   * wcsrtombs(buffer, &ws, 0, mbstate);
   * return buffer;
   */
  for (buflen = 4, buffer = NULL, bufpos = 0; ws; buflen *= 2) {
    size_t converted;
    if(! (resized = realloc (buffer, buflen)))
      goto err;
    buffer = resized;
    if((converted = wcsrtombs (buffer + bufpos, &ws, buflen - bufpos, &mbstate))
       == (size_t) -1)
      goto err;
    bufpos += converted;
  }
  
  /* shrink buffer to actually required size */
  if (! (resized = realloc (buffer, bufpos + 1)))
    goto err;
  
  /* reset locale */
  setlocale (LC_ALL, saved_locale);
  
  return resized;
err:
  /* reset locale */
  setlocale (LC_ALL, saved_locale);
  
  free(buffer);
  return NULL;
}