Qsmtp  0.37
sstring.h
Go to the documentation of this file.
1 
4 #ifndef SSTRING_H
5 #define SSTRING_H
6 
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 
15 typedef struct string {
16  char *s;
17  size_t len;
18 } string;
19 
23 typedef struct cstring {
24  const char *s;
25  size_t len;
26 } cstring;
27 
28 #define STREMPTY(x) do { (x).s = NULL; (x).len = 0; } while (0)
29 
30 #define STREMPTY_INIT { .s = NULL, .len = 0 }
31 
32 static inline const cstring *
33 to_cstring(const string *s)
34 {
35  return (const cstring *)s;
36 }
37 
48 static inline int __attribute__ ((nonnull (1)))
49 newstr(string *s, const size_t len)
50 {
51  if (len == 0) {
52  STREMPTY(*s);
53  return 0;
54  }
55 
56  s->len = len;
57  s->s = malloc(len);
58  return (len && !s->s) ? -1 : 0;
59 }
60 
75 static inline int __attribute__ ((nonnull (1,2)))
76 dupstr(string *s, const char *t)
77 {
78  s->len = strlen(t);
79  if (s->len == 0) {
80  STREMPTY(*s);
81  return 0;
82  }
83  s->s = strdup(t);
84  return (s->s == NULL) ? -1 : 0;
85 }
86 
87 #endif
dupstr
static int dupstr(string *s, const char *t)
duplicate a character string to a string buffer
Definition: sstring.h:76
string
record of a string
Definition: sstring.h:15
cstring::len
size_t len
Definition: sstring.h:25
cstring
record of a constant string
Definition: sstring.h:23
newstr
static int newstr(string *s, const size_t len)
allocate a new string buffer of the given length
Definition: sstring.h:49
string::s
char * s
Definition: sstring.h:16
string::len
size_t len
Definition: sstring.h:17
cstring::s
const char * s
Definition: sstring.h:24