libreis  0.1.0
A simple header-based drop-in library
types.h
1 #ifndef REIS_TYPES_H
2 #define REIS_TYPES_H
3 
4 #include <reis/base.h>
5 
6 /* A nifty, while a little hacky, generic string type. */
7 typedef struct string {
8  enum str_type {
9  E_STRING,
10  E_WCSTRING
11  } __attribute__ ((__packed__)) type;
12  union data {
13  char *str;
14  wchar_t *wcs;
15  } val;
16  size_t len;
17 } string_t;
18 
19 
20 static inline string_t* strs( const char *lit ) {
21  size_t len = strlen( lit );
22  string_t *s = (string_t*)MALLOC( sizeof(string_t) );
23  s->val.str = (char*)MALLOC( len + 1 );
24  strcpy( s->val.str, lit );
25  s->len = len;
26  s->type = E_STRING;
27  return s;
28 }
29 
30 static inline string_t* strwcs( const wchar_t *lit ) {
31  size_t len = wcslen( lit );
32  string_t *s = (string_t*)MALLOC( sizeof(string_t) );
33  s->val.wcs = (wchar_t*)MALLOC( sizeof(wchar_t) * (len + 1) );
34  wcscpy( s->val.wcs, lit );
35  s->len = len;
36  s->type = E_WCSTRING;
37  return s;
38 }
39 
40 /* The constructor for `string_t`. */
41 #define STR(s) _Generic((s), \
42  char *: strs, \
43  const char *: strs, \
44  wchar_t *: strwcs, \
45  const wchar_t *:strwcs \
46 )(s)
47 
48 typedef string_t* string;
49 
50 #endif /* REIS_TYPES_H */
Shared types and functions, accounts for GNU Linux and MacOS specifications.
Definition: types.h:7
Definition: types.h:12