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> |
|
21
by Gustav Hartvigsson
Woops! |
30 |
|
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
31 |
char * |
32 |
s_string_new (const char * s) { |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
33 |
char * ret_val = strdup (s); |
|
23
by Gustav Hartvigsson
* Fixed some of the build warnings. |
34 |
return ret_val; |
35 |
}
|
|
36 |
||
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
37 |
char * |
38 |
s_string_new_fmt (const char * format, ...) { |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
39 |
char * buffer = malloc (strlen (format) + 512); |
40 |
char * ret_val = NULL; |
|
41 |
va_list args; |
|
42 |
va_start(args, format); |
|
43 |
vsprintf (buffer, format, args); |
|
44 |
ret_val = (char *) strdup (buffer); |
|
45 |
va_end(args); |
|
46 |
free (buffer); |
|
|
21
by Gustav Hartvigsson
Woops! |
47 |
return ret_val; |
48 |
}
|
|
49 |
||
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
50 |
char * |
51 |
s_string_new_with_len (const char * s, size_t len) { |
|
|
21
by Gustav Hartvigsson
Woops! |
52 |
char * ret_val = malloc (len + 1); |
53 |
strncpy (ret_val, s, len); |
|
54 |
return ret_val; |
|
55 |
}
|
|
56 |
||
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
57 |
char * |
58 |
s_current_time (void) { |
|
|
39
by Gustav Hartvigsson
* Added "check" target for testing. |
59 |
char * ret_val = malloc (21); |
60 |
time_t t = time (NULL); |
|
61 |
strftime (ret_val, 21, "%F %T", localtime (&t)); |
|
62 |
return ret_val; |
|
63 |
}
|
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
64 |
|
65 |
||
66 |
char * |
|
67 |
s_wstring_to_string (const wchar_t * ws) { |
|
68 |
size_t new_len = ((sizeof (wchar_t *) * wcslen (ws)) + 1); |
|
69 |
char * buffer = malloc (new_len); |
|
70 |
char * ret_val = NULL; |
|
71 |
mbstate_t state; |
|
72 |
|
|
73 |
int ret = wcsrtombs (buffer, &ws, new_len, &state); |
|
74 |
if (ret) { |
|
75 |
ret_val = (char *) strdup (buffer); |
|
76 |
} else { |
|
77 |
s_warn_print ("Could not copy wide c string into a c string.\n"); |
|
78 |
} |
|
79 |
free (buffer); |
|
80 |
return ret_val; |
|
81 |
}
|