Fri, 12 Jul 2013 20:50:18 +0200
new properties parser
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #ifndef _SSTRING_H
30 #define _SSTRING_H
32 #include "ucx.h"
33 #include <stddef.h>
35 /* use macros for literals only */
36 #define S(s) { (char*)s, sizeof(s)-1 }
37 #define ST(s) sstrn((char*)s, sizeof(s)-1)
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
43 typedef struct sstring {
44 char *ptr;
45 size_t length;
46 } sstr_t;
48 /*
49 * creates a new sstr_t from a null terminated string
50 *
51 * s null terminated string
52 */
53 sstr_t sstr(char *s);
55 /*
56 * creates a new sstr_t from a string and length
57 *
58 * s string
59 * n length of string
60 */
61 sstr_t sstrn(char *s, size_t n);
64 /*
65 * gets the length of n sstr_t strings
66 *
67 * n number of strings
68 * s string
69 * ... strings
70 */
71 size_t sstrnlen(size_t n, sstr_t s, ...);
74 /*
75 * concatenates n strings
76 *
77 * n number of strings
78 * s new string with enough memory allocated
79 * ... strings
80 */
81 sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...);
84 /*
85 *
86 */
87 sstr_t sstrsubs(sstr_t s, size_t start);
89 /*
90 *
91 */
92 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length);
94 /*
95 *
96 */
97 sstr_t sstrchr(sstr_t s, int c);
99 /*
100 * splits s into n parts
101 *
102 * s the string to split
103 * d the delimiter string
104 * n the maximum size of the resulting list
105 * a size of 0 indicates an unbounded list size
106 * the actual size of the list will be stored here
107 *
108 * Hint: use this value to avoid dynamic reallocation of the result list
109 *
110 * Returns a list of the split strings
111 * NOTE: this list needs to be freed manually after usage
112 *
113 * Returns NULL on error
114 */
115 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n);
117 int sstrcmp(sstr_t s1, sstr_t s2);
119 sstr_t sstrdup(sstr_t s);
121 sstr_t sstrtrim(sstr_t string);
123 #ifdef __cplusplus
124 }
125 #endif
127 #endif /* _SSTRING_H */