/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to libssts/utils.c

  • Committer: Gustav Hartvigsson
  • Date: 2015-10-24 19:55:57 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20151024195557-lpmkc68r52x4t63h
* General cleanup/make it pritty.
* Added s_dynamic_array_append.
* Implimented the new Error System w/ error domains.
* Added s_string_is_equal
* changed char to schar here and there.
TODO:
  Change the tests to match the new error system.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include <wchar.h>
30
30
#include <locale.h>
31
31
 
32
 
char *
33
 
s_string_new (const char * s) {
 
32
schar *
 
33
s_string_new (const schar * s) {
34
34
  if (s == NULL) {
35
35
    return NULL;
36
36
  }
37
37
  size_t s_len = strlen (s);
38
 
  char * ret_val = malloc (s_len + 1);
 
38
  schar * ret_val = malloc (s_len + 1);
39
39
  strcpy (ret_val, s);
40
40
  return ret_val;
41
41
}
42
42
 
43
 
char *
44
 
s_string_new_fmt (const char * format, ...) {
45
 
  char * buffer = malloc (strlen (format) + 512);
46
 
  char * ret_val = NULL;
 
43
schar *
 
44
s_string_new_fmt (const schar * format, ...) {
 
45
  schar * buffer = malloc (strlen (format) + 512);
 
46
  schar * ret_val = NULL;
47
47
  va_list args;
48
48
  va_start(args, format);
49
49
  vsprintf (buffer, format, args);
50
 
  ret_val = strdup (buffer);
 
50
  ret_val = s_string_new (buffer);
51
51
  va_end(args);
52
52
  free (buffer);
53
53
  return ret_val;
54
54
}
55
55
 
56
 
char *
57
 
s_string_new_with_len (const char * s, size_t len) {
58
 
  char * ret_val = malloc (len + 1);
 
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;
59
60
  strncpy (ret_val, s, len);
60
61
  return ret_val;
61
62
}
62
63
 
63
 
char *
 
64
schar *
64
65
s_current_time (void) {
65
 
  char * ret_val = malloc (21);
 
66
  schar * ret_val = malloc (21);
66
67
  time_t t = time (NULL);
67
68
  strftime (ret_val, 21, "%F %T", localtime (&t));
68
69
  return ret_val;
69
70
}
70
71
 
71
 
char *
 
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
82
s_ustring_to_string (const suchar * us) {
73
83
  
74
84
  return NULL;
79
89
 * This should not be used. If you need to use this, uncomment it.
80
90
 * I am conveting to something more sane than wchar_t.
81
91
 */
82
 
char *
 
92
schar *
83
93
s_wstring_to_string (const wchar_t * ws) {
84
94
  s_err_print ("Using depricated function: s_wstring_to_string ().\n"
85
95
               "Do not use s_wstring_to_string, as it is platform "
88
98
               "use s_ustring_to_string instead of this.\n");
89
99
  
90
100
  size_t buflen;
91
 
  char * buffer;
92
 
  char * resized;
 
101
  schar * buffer;
 
102
  schar * resized;
93
103
  size_t bufpos;
94
104
  mbstate_t mbstate;
95
105
  memset (&mbstate, 0, sizeof (mbstate));
96
106
  
97
107
 
98
108
  /* Save locale */
99
 
  char * saved_locale;
 
109
  schar * saved_locale;
100
110
  {
101
 
    char * old_locale;
 
111
    schar * old_locale;
102
112
    size_t old_locale_len;
103
113
    old_locale = setlocale (LC_ALL, NULL);
104
114
    old_locale_len = strlen (old_locale) + 1;