/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 src/utils.c

  • Committer: Gustav Hartvigsson
  • Date: 2015-04-08 18:25:07 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20150408182507-094jw13yhmi3bhfu
* Added "check" target for testing.
* Changed SRC to SSTS_SRC for future proofing.
* Removed a few "_" from before some struct names. It is not needed.
* Re-wrote how s_object_to_string () works on SError's. Should be faster now, but less safe...
  * To make this work I added SErrorTypeName string array. (Need to surpress the errer it creats).
* Re-orderd SErrorType enum.
* Use s_string_new_fmt () the default method for s_object_to_string ().
* Changed to strdup in some functions in utils.c.
* Added s_current_time () function to get an ISO time string.
* Added some more documentation to utils.h
* !DEBUG -> DEBUG in utils.h
* Added more tests to the test suite.
* Re-Wrote test_macros.h to display time, making it a lil' bit nicer to look at.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include <stdlib.h>
26
26
#include <stdio.h>
27
27
#include <stdarg.h>
 
28
#include <time.h>
 
29
 
28
30
 
29
31
char * s_string_new (const char * s) {
30
 
  char * ret_val = malloc (strlen (s) + 1);
31
 
  strcpy (ret_val, s);
32
 
  free ((char *) s);
 
32
  char * ret_val = strdup (s);
33
33
  return ret_val;
34
34
}
35
35
 
36
36
char * s_string_new_fmt (const char * format, ...) {
37
 
  char * ret_val = malloc (strlen (format) + 1);
38
 
  strcpy (ret_val, format);
39
 
  free ((char *) format);
 
37
  char * buffer = malloc (strlen (format) + 512);
 
38
  char * ret_val = NULL;
 
39
  va_list args;
 
40
  va_start(args, format);
 
41
  vsprintf (buffer, format, args);
 
42
  ret_val = (char *) strdup (buffer);
 
43
  va_end(args);
 
44
  free (buffer);
40
45
  return ret_val;
41
46
}
42
47
 
43
48
char * s_string_new_with_len (const char * s, size_t len) {
44
49
  char * ret_val = malloc (len + 1);
45
50
  strncpy (ret_val, s, len);
46
 
  free ((char *) s);
47
51
  return ret_val;
48
52
}
49
53
 
 
54
char * s_current_time (void) {
 
55
  char * ret_val = malloc (21);
 
56
  time_t t = time (NULL);
 
57
  strftime (ret_val, 21, "%F %T", localtime (&t));
 
58
  return ret_val;
 
59
}