1 /*
2  * Copyright (C) 1997-2004, Michael Jennings
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef _LIBAST_STR_H_
25 #define _LIBAST_STR_H_
26 
27 #define SPIF_STR(obj)                    ((spif_str_t) (obj))
28 #define SPIF_STR_CLASS(o)                (SPIF_CAST(strclass) SPIF_OBJ_CLASS(o))
29 #define SPIF_OBJ_IS_STR(o)               (SPIF_OBJ_IS_TYPE(o, str))
30 #define SPIF_STR_ISNULL(s)               SPIF_OBJ_ISNULL(SPIF_OBJ(s))
31 #define SPIF_STRCLASS_VAR(type)          spif_ ## type ## _strclass
32 #define SPIF_STR_CALL_METHOD(o, meth)    SPIF_STR_CLASS(o)->meth
33 
34 #define SPIF_STR_NEW(type)           SPIF_STR((SPIF_CLASS(SPIF_CLASS_VAR(type)))->noo())
35 #define SPIF_STR_INIT(obj)           SPIF_OBJ_INIT(obj)
36 #define SPIF_STR_DONE(obj)           SPIF_OBJ_DONE(obj)
37 #define SPIF_STR_DEL(obj)            SPIF_OBJ_DEL(obj)
38 #define SPIF_STR_SHOW(obj, b, i)     SPIF_OBJ_SHOW(obj, b, i)
39 #define SPIF_STR_COMP(o1, o2)        SPIF_OBJ_COMP(o1, o2)
40 #define SPIF_STR_DUP(obj)            SPIF_OBJ_DUP(obj)
41 #define SPIF_STR_TYPE(obj)           SPIF_OBJ_TYPE(obj)
42 
43 #define SPIF_STR_NEW_FROM_PTR(type, p)          SPIF_STR((SPIF_CLASS(SPIF_CLASS_VAR(type)))->new_from_ptr)(p))
44 #define SPIF_STR_NEW_FROM_BUFF(type, b, s)      SPIF_STR((SPIF_CLASS(SPIF_CLASS_VAR(type)))->new_from_buff)(b, s))
45 #define SPIF_STR_NEW_FROM_FP(type, fp)          SPIF_STR((SPIF_CLASS(SPIF_CLASS_VAR(type)))->new_from_fp)(fp))
46 #define SPIF_STR_NEW_FROM_FD(type, fd)          SPIF_STR((SPIF_CLASS(SPIF_CLASS_VAR(type)))->new_from_fd)(fd))
47 #define SPIF_STR_NEW_FROM_NUM(type, num)        SPIF_STR((SPIF_CLASS(SPIF_CLASS_VAR(type)))->new_from_num)(num))
48 #define SPIF_STR_INIT_FROM_PTR(o, p)            SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), init_from_ptr)(o, p))
49 #define SPIF_STR_INIT_FROM_BUFF(o, b, s)        SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), init_from_buff)(o, b, s))
50 #define SPIF_STR_INIT_FROM_FP(o, fp)            SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), init_from_fp)(o, fp))
51 #define SPIF_STR_INIT_FROM_FD(o, fd)            SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), init_from_fd)(o, fd))
52 #define SPIF_STR_INIT_FROM_NUM(o, num)          SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), init_from_num)(o, num))
53 #define SPIF_STR_APPEND(o, x)                   SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), append)(o, x))
54 #define SPIF_STR_APPEND_CHAR(o, x)              SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), append_char)(o, x))
55 #define SPIF_STR_APPEND_FROM_PTR(o, x)          SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), append_from_ptr)(o, x))
56 #define SPIF_STR_CASECMP(o, x)                  SPIF_CAST(cmp) (SPIF_STR_CALL_METHOD((o), casecmp)(o, x))
57 #define SPIF_STR_CASECMP_WITH_PTR(o, x)         SPIF_CAST(cmp) (SPIF_STR_CALL_METHOD((o), casecmp_with_ptr)(o, x))
58 #define SPIF_STR_CLEAR(o, x)                    SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), clear)(o, x))
59 #define SPIF_STR_CMP(o, x)                      SPIF_CAST(cmp) (SPIF_STR_CALL_METHOD((o), cmp)(o, x))
60 #define SPIF_STR_CMP_WITH_PTR(o, x)             SPIF_CAST(cmp) (SPIF_STR_CALL_METHOD((o), cmp_with_ptr)(o, x))
61 #define SPIF_STR_DOWNCASE(o)                    SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), downcase)(o))
62 #define SPIF_STR_FIND(o, x)                     SPIF_CAST(stridx) (SPIF_STR_CALL_METHOD((o), find)(o, x))
63 #define SPIF_STR_FIND_FROM_PTR(o, x)            SPIF_CAST(stridx) (SPIF_STR_CALL_METHOD((o), find_from_ptr)(o, x))
64 #define SPIF_STR_INDEX(o, x)                    SPIF_CAST(stridx) (SPIF_STR_CALL_METHOD((o), index)(o, x))
65 #define SPIF_STR_NCASECMP(o, x, n)              SPIF_CAST(cmp) (SPIF_STR_CALL_METHOD((o), ncasecmp)(o, x, n))
66 #define SPIF_STR_NCASECMP_WITH_PTR(o, x, n)     SPIF_CAST(cmp) (SPIF_STR_CALL_METHOD((o), ncasecmp_with_ptr)(o, x, n))
67 #define SPIF_STR_NCMP(o, x, n)                  SPIF_CAST(cmp) (SPIF_STR_CALL_METHOD((o), ncmp)(o, x, n))
68 #define SPIF_STR_NCMP_WITH_PTR(o, x, n)         SPIF_CAST(cmp) (SPIF_STR_CALL_METHOD((o), ncmp_with_ptr)(o, x, n))
69 #define SPIF_STR_PREPEND(o, x)                  SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), prepend)(o, x))
70 #define SPIF_STR_PREPEND_CHAR(o, x)             SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), prepend_char)(o, x))
71 #define SPIF_STR_PREPEND_FROM_PTR(o, x)         SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), prepend_from_ptr)(o, x))
72 #define SPIF_STR_REVERSE(o)                     SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), reverse)(o))
73 #define SPIF_STR_RINDEX(o, x)                   SPIF_CAST(stridx) (SPIF_STR_CALL_METHOD((o), rindex)(o, x))
74 #define SPIF_STR_SPLICE(o, n1, n2, x)           SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), splice)(o, n1, n2, x))
75 #define SPIF_STR_SPLICE_FROM_PTR(o, n1, n2, x)  SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), splice_from_ptr)(o, n1, n2, x))
76 #define SPIF_STR_SUBSTR(o, n1, n2)              SPIF_CAST(str) (SPIF_STR_CALL_METHOD((o), substr)(o, n1, n2))
77 #define SPIF_STR_SUBSTR_TO_PTR(o, n1, n2)       SPIF_CAST(charptr) (SPIF_STR_CALL_METHOD((o), substr_to_ptr)(o, n1, n2))
78 #define SPIF_STR_TO_FLOAT(o)                    SPIF_CAST_C(double) (SPIF_STR_CALL_METHOD((o), to_float)(o))
79 #define SPIF_STR_TO_NUM(o, x)                   SPIF_CAST_C(size_t) (SPIF_STR_CALL_METHOD((o), to_num)(o, x))
80 #define SPIF_STR_TRIM(o)                        SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), trim)(o))
81 #define SPIF_STR_UPCASE(o)                      SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), upcase)(o))
82 
83 #define SPIF_STR_STR(obj)  (SPIF_CONST_CAST(charptr) ((SPIF_STR_ISNULL(obj)) \
84                                                       ? (SPIF_CAST(charptr) "") \
85                                                       : (SPIF_STR(obj)->s)))
86 typedef spif_int64_t spif_stridx_t;
87 
SPIF_DECL_OBJ(str)88 SPIF_DECL_OBJ(str) {
89     SPIF_DECL_PARENT_TYPE(obj);
90     spif_charptr_t s;
91     SPIF_DECL_PROPERTY_C(spif_stridx_t, size);
92     SPIF_DECL_PROPERTY_C(spif_stridx_t, len);
93 };
94 
SPIF_DECL_OBJ(strclass)95 SPIF_DECL_OBJ(strclass) {
96     SPIF_DECL_PARENT_TYPE(class);
97 
98     spif_func_t new_from_ptr;
99     spif_func_t new_from_buff;
100     spif_func_t new_from_fp;
101     spif_func_t new_from_fd;
102     spif_func_t new_from_num;
103     spif_func_t init_from_ptr;
104     spif_func_t init_from_buff;
105     spif_func_t init_from_fp;
106     spif_func_t init_from_fd;
107     spif_func_t init_from_num;
108     spif_func_t append;
109     spif_func_t append_char;
110     spif_func_t append_from_ptr;
111     spif_func_t casecmp;
112     spif_func_t casecmp_with_ptr;
113     spif_func_t clear;
114     spif_func_t cmp;
115     spif_func_t cmp_with_ptr;
116     spif_func_t downcase;
117     spif_func_t find;
118     spif_func_t find_from_ptr;
119     spif_func_t index;
120     spif_func_t ncasecmp;
121     spif_func_t ncasecmp_with_ptr;
122     spif_func_t ncmp;
123     spif_func_t ncmp_with_ptr;
124     spif_func_t prepend;
125     spif_func_t prepend_char;
126     spif_func_t prepend_from_ptr;
127     spif_func_t reverse;
128     spif_func_t rindex;
129     spif_func_t splice;
130     spif_func_t splice_from_ptr;
131     spif_func_t substr;
132     spif_func_t substr_to_ptr;
133     spif_func_t to_float;
134     spif_func_t to_num;
135     spif_func_t trim;
136     spif_func_t upcase;
137 };
138 
139 extern spif_class_t SPIF_CLASS_VAR(str);
140 extern spif_strclass_t SPIF_STRCLASS_VAR(str);
141 extern spif_str_t spif_str_new(void);
142 extern spif_str_t spif_str_new_from_ptr(spif_charptr_t);
143 extern spif_str_t spif_str_new_from_buff(spif_charptr_t, spif_stridx_t);
144 extern spif_str_t spif_str_new_from_fp(FILE *);
145 extern spif_str_t spif_str_new_from_fd(int);
146 extern spif_str_t spif_str_new_from_num(long);
147 extern spif_bool_t spif_str_del(spif_str_t);
148 extern spif_bool_t spif_str_init(spif_str_t);
149 extern spif_bool_t spif_str_init_from_ptr(spif_str_t, spif_charptr_t);
150 extern spif_bool_t spif_str_init_from_buff(spif_str_t, spif_charptr_t, spif_stridx_t);
151 extern spif_bool_t spif_str_init_from_fp(spif_str_t, FILE *);
152 extern spif_bool_t spif_str_init_from_fd(spif_str_t, int);
153 extern spif_bool_t spif_str_init_from_num(spif_str_t, long);
154 extern spif_bool_t spif_str_done(spif_str_t);
155 extern spif_str_t spif_str_show(spif_str_t, spif_charptr_t, spif_str_t, size_t);
156 extern spif_cmp_t spif_str_comp(spif_str_t, spif_str_t);
157 extern spif_str_t spif_str_dup(spif_str_t);
158 extern spif_classname_t spif_str_type(spif_str_t);
159 
160 extern spif_bool_t spif_str_append(spif_str_t, spif_str_t);
161 extern spif_bool_t spif_str_append_char(spif_str_t, spif_char_t);
162 extern spif_bool_t spif_str_append_from_ptr(spif_str_t, spif_charptr_t);
163 extern spif_cmp_t spif_str_casecmp(spif_str_t, spif_str_t);
164 extern spif_cmp_t spif_str_casecmp_with_ptr(spif_str_t, spif_charptr_t);
165 extern spif_bool_t spif_str_clear(spif_str_t, spif_char_t);
166 extern spif_cmp_t spif_str_cmp(spif_str_t, spif_str_t);
167 extern spif_cmp_t spif_str_cmp_with_ptr(spif_str_t, spif_charptr_t);
168 extern spif_bool_t spif_str_downcase(spif_str_t);
169 extern spif_stridx_t spif_str_find(spif_str_t, spif_str_t);
170 extern spif_stridx_t spif_str_find_from_ptr(spif_str_t, spif_charptr_t);
171 extern spif_stridx_t spif_str_index(spif_str_t, spif_char_t);
172 extern spif_cmp_t spif_str_ncasecmp(spif_str_t, spif_str_t, spif_stridx_t);
173 extern spif_cmp_t spif_str_ncasecmp_with_ptr(spif_str_t, spif_charptr_t, spif_stridx_t);
174 extern spif_cmp_t spif_str_ncmp(spif_str_t, spif_str_t, spif_stridx_t);
175 extern spif_cmp_t spif_str_ncmp_with_ptr(spif_str_t, spif_charptr_t, spif_stridx_t);
176 extern spif_bool_t spif_str_prepend(spif_str_t, spif_str_t);
177 extern spif_bool_t spif_str_prepend_char(spif_str_t, spif_char_t);
178 extern spif_bool_t spif_str_prepend_from_ptr(spif_str_t, spif_charptr_t);
179 extern spif_bool_t spif_str_reverse(spif_str_t);
180 extern spif_stridx_t spif_str_rindex(spif_str_t, spif_char_t);
181 extern spif_bool_t spif_str_splice(spif_str_t, spif_stridx_t, spif_stridx_t, spif_str_t);
182 extern spif_bool_t spif_str_splice_from_ptr(spif_str_t, spif_stridx_t, spif_stridx_t, spif_charptr_t);
183 extern spif_str_t spif_str_substr(spif_str_t, spif_stridx_t, spif_stridx_t);
184 extern spif_charptr_t spif_str_substr_to_ptr(spif_str_t, spif_stridx_t, spif_stridx_t);
185 extern double spif_str_to_float(spif_str_t);
186 extern size_t spif_str_to_num(spif_str_t, int);
187 extern spif_bool_t spif_str_trim(spif_str_t);
188 extern spif_bool_t spif_str_upcase(spif_str_t);
189 SPIF_DECL_PROPERTY_FUNC_C(str, spif_stridx_t, size);
190 SPIF_DECL_PROPERTY_FUNC_C(str, spif_stridx_t, len);
191 
192 #endif /* _LIBAST_STR_H_ */
193