/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
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
72
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
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) {
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
79
  s_err_print ("Using depricated function: s_wstring_to_string ().\n"
80
               "Do not use s_wstring_to_string, as it is platform "
81
               "specific and may, or may not couse troubble.\n"
82
               "Use uchar strings instead of wchar_t strings, and "
83
               "use s_ustring_to_string instead of this.\n");
84
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
85
  size_t buflen;
86
  char * buffer;
87
  char * resized;
88
  size_t bufpos;
89
  mbstate_t mbstate;
90
  memset (&mbstate, 0, sizeof (mbstate));
91
  
92
93
  /* Save locale */
94
    char * saved_locale;
95
  {
96
    char * old_locale;
97
    size_t old_locale_len;
98
    old_locale = setlocale (LC_ALL, NULL);
99
    old_locale_len = strlen (old_locale) + 1;
100
    saved_locale = malloc (sizeof (char *) * old_locale_len);
101
    memcpy (saved_locale, old_locale, old_locale_len);
102
    /* set locale */
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
103
    setlocale (LC_ALL, "C.utf8");
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
104
  }
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
105
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
106
  /* Thanks to Florian Philipp.
107
   * Thread: https://plus.google.com/u/0/+GustavHartvigsson/posts/4Wk7La1kWPP
108
   *
109
   * to avoid parsing the string twice, we allocate memory speculatively
110
   * with exponential growth. Then we shrink it at the end.
111
   *
112
   * Alternative:  use
113
   * const wchar_t* tmp = ws;
114
   * size_t buflen = wcsrtombs(NULL, &tmp, 0, mbstate);
115
   * buffer = malloc(buflen);
116
   * wcsrtombs(buffer, &ws, 0, mbstate);
117
   * return buffer;
118
   */
119
  for (buflen = 4, buffer = NULL, bufpos = 0; ws; buflen *= 2) {
120
    size_t converted;
121
    if(! (resized = realloc (buffer, buflen)))
122
      goto err;
123
    buffer = resized;
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
124
    if((converted = wcsrtombs (buffer + bufpos, &ws, buflen - bufpos, &mbstate))
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
125
       == (size_t) -1)
126
      goto err;
127
    bufpos += converted;
128
  }
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
129
  
64 by Gustav Hartvigsson
* Fixed s_wstring_to_string (), involves setlocale :-)
130
  /* shrink buffer to actually required size */
131
  if (! (resized = realloc (buffer, bufpos + 1)))
132
    goto err;
133
  
134
  /* reset locale */
135
  setlocale (LC_ALL, saved_locale);
136
  
137
  return resized;
138
err:
139
  /* reset locale */
140
  setlocale (LC_ALL, saved_locale);
141
  
142
  free(buffer);
143
  return NULL;
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
144
}
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
145
66 by Gustav Hartvigsson
* Removed moved s_wstring_to_string.
146