Qsmtp  0.30dev
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) { (x).s = NULL; (x).len = 0; }
29 
40 static inline int __attribute__ ((nonnull (1)))
41 newstr(string *s, const size_t len)
42 {
43  if (len == 0) {
44  STREMPTY(*s);
45  return 0;
46  }
47 
48  s->len = len;
49  s->s = malloc(len);
50  return (len && !s->s) ? -1 : 0;
51 }
52 
67 static inline int __attribute__ ((nonnull (1,2)))
68 dupstr(string *s, const char *t)
69 {
70  s->len = strlen(t);
71  if (s->len == 0) {
72  STREMPTY(*s);
73  return 0;
74  }
75  s->s = strdup(t);
76  return (s->s == NULL) ? -1 : 0;
77 }
78 
79 #endif
size_t len
Definition: sstring.h:17
const char * s
Definition: sstring.h:24
char * s
Definition: sstring.h:16
static int newstr(string *s, const size_t len)
allocate a new string buffer of the given length
Definition: sstring.h:41
size_t len
Definition: sstring.h:25
record of a string
Definition: sstring.h:15
static int dupstr(string *s, const char *t)
duplicate a character string to a string buffer
Definition: sstring.h:68
record of a constant string
Definition: sstring.h:23