3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2011 Angel Ortega <angel@triptico.com>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 http://www.triptico.com
50 MPDM_STRING
= (1<<_MPDM_STRING
), /* data can be string-compared */
51 MPDM_MULTIPLE
= (1<<_MPDM_MULTIPLE
), /* data is multiple */
52 MPDM_FREE
= (1<<_MPDM_FREE
), /* free data at destroy */
53 MPDM_IVAL
= (1<<_MPDM_IVAL
), /* integer value cached in .ival */
54 MPDM_RVAL
= (1<<_MPDM_RVAL
), /* real value cached in .rval */
55 MPDM_HASH
= (1<<_MPDM_HASH
), /* data is a hash */
56 MPDM_FILE
= (1<<_MPDM_FILE
), /* data is a FILE * */
57 MPDM_EXEC
= (1<<_MPDM_EXEC
), /* data is 'executable' */
58 MPDM_REGEX
= (1<<_MPDM_REGEX
), /* data is a compiled regex */
59 MPDM_MUTEX
= (1<<_MPDM_MUTEX
), /* data is a mutex */
60 MPDM_SEMAPHORE
= (1<<_MPDM_SEMAPHORE
), /* data is a semaphore */
61 MPDM_THREAD
= (1<<_MPDM_THREAD
), /* data is a thread handle */
62 MPDM_NONDYN
= (1<<_MPDM_NONDYN
) /* data is non-dynamic (do not free()) */
66 typedef struct mpdm_val
*mpdm_t
;
70 int flags
; /* value flags */
71 int ref
; /* reference count */
72 int size
; /* data size */
73 int ival
; /* cached integer value */
74 double rval
; /* cache real value */
75 const void *data
; /* the real data */
79 /* the main control structure */
81 mpdm_t root
; /* the root hash */
82 int count
; /* total count of values */
83 int hash_buckets
; /* default hash buckets */
86 extern struct mpdm_control
*mpdm
;
88 mpdm_t
mpdm_init(mpdm_t v
, int flags
, const void *data
, int size
);
89 mpdm_t
mpdm_new(int flags
, const void *data
, int size
);
90 mpdm_t
mpdm_ref(mpdm_t v
);
91 mpdm_t
mpdm_unref(mpdm_t v
);
92 mpdm_t
mpdm_unrefnd(mpdm_t v
);
94 int mpdm_size(const mpdm_t v
);
95 mpdm_t
mpdm_clone(const mpdm_t v
);
96 mpdm_t
mpdm_root(void);
98 void mpdm_void(mpdm_t v
);
99 int mpdm_is_null(mpdm_t v
);
101 mpdm_t
mpdm_exec(mpdm_t c
, mpdm_t args
, mpdm_t ctxt
);
102 mpdm_t
mpdm_exec_1(mpdm_t c
, mpdm_t a1
, mpdm_t ctxt
);
103 mpdm_t
mpdm_exec_2(mpdm_t c
, mpdm_t a1
, mpdm_t a2
, mpdm_t ctxt
);
104 mpdm_t
mpdm_exec_3(mpdm_t c
, mpdm_t a1
, mpdm_t a2
, mpdm_t a3
, mpdm_t ctxt
);
106 mpdm_t
mpdm_new_a(int flags
, int size
);
107 mpdm_t
mpdm_aclone(const mpdm_t v
);
109 mpdm_t
mpdm_expand(mpdm_t a
, int offset
, int num
);
110 mpdm_t
mpdm_collapse(mpdm_t a
, int offset
, int num
);
111 mpdm_t
mpdm_aset(mpdm_t a
, mpdm_t e
, int offset
);
112 mpdm_t
mpdm_aget(const mpdm_t a
, int offset
);
113 mpdm_t
mpdm_ins(mpdm_t a
, mpdm_t e
, int offset
);
114 mpdm_t
mpdm_adel(mpdm_t a
, int offset
);
115 mpdm_t
mpdm_shift(mpdm_t a
);
116 mpdm_t
mpdm_push(mpdm_t a
, mpdm_t e
);
117 mpdm_t
mpdm_pop(mpdm_t a
);
118 mpdm_t
mpdm_queue(mpdm_t a
, mpdm_t e
, int size
);
119 int mpdm_seek(const mpdm_t a
, const mpdm_t k
, int step
);
120 int mpdm_seek_s(const mpdm_t a
, const wchar_t * k
, int step
);
121 int mpdm_bseek(const mpdm_t a
, const mpdm_t k
, int step
, int *pos
);
122 int mpdm_bseek_s(const mpdm_t a
, const wchar_t * k
, int step
, int *pos
);
123 mpdm_t
mpdm_sort(mpdm_t a
, int step
);
124 mpdm_t
mpdm_sort_cb(mpdm_t a
, int step
, mpdm_t asort_cb
);
126 mpdm_t
mpdm_split_s(const mpdm_t v
, const wchar_t *s
);
127 mpdm_t
mpdm_split(const mpdm_t a
, const mpdm_t s
);
128 mpdm_t
mpdm_join_s(const mpdm_t a
, const wchar_t *s
);
129 mpdm_t
mpdm_join(const mpdm_t a
, const mpdm_t s
);
131 void *mpdm_poke_o(void *dst
, int *dsize
, int *offset
, const void *org
, int osize
, int esize
);
132 void *mpdm_poke(void *dst
, int *dsize
, const void *org
, int osize
, int esize
);
133 wchar_t *mpdm_pokewsn(wchar_t *dst
, int *dsize
, const wchar_t *str
, int slen
);
134 wchar_t *mpdm_pokews(wchar_t *dst
, int *dsize
, const wchar_t *str
);
135 wchar_t *mpdm_pokev(wchar_t *dst
, int *dsize
, const mpdm_t v
);
136 wchar_t *mpdm_mbstowcs(const char *str
, int *s
, int l
);
137 char *mpdm_wcstombs(const wchar_t * str
, int *s
);
138 mpdm_t
mpdm_new_wcs(int flags
, const wchar_t * str
, int size
, int cpy
);
139 mpdm_t
mpdm_new_mbstowcs(int flags
, const char *str
, int l
);
140 mpdm_t
mpdm_new_wcstombs(int flags
, const wchar_t * str
);
141 mpdm_t
mpdm_new_i(int ival
);
142 mpdm_t
mpdm_new_r(double rval
);
143 int mpdm_wcwidth(wchar_t c
);
144 mpdm_t
mpdm_sprintf(const mpdm_t fmt
, const mpdm_t args
);
145 mpdm_t
mpdm_ulc(const mpdm_t s
, int u
);
146 mpdm_t
mpdm_sscanf(const mpdm_t str
, const mpdm_t fmt
, int offset
);
147 mpdm_t
mpdm_tr(mpdm_t str
, mpdm_t s1
, mpdm_t s2
);
149 wchar_t *mpdm_string(const mpdm_t v
);
150 wchar_t *mpdm_string2(const mpdm_t v
, wchar_t *wtmp
);
151 mpdm_t
mpdm_splice(const mpdm_t v
, const mpdm_t i
, int offset
, int del
);
152 mpdm_t
mpdm_strcat_sn(const mpdm_t s1
, const wchar_t *s2
, int size
);
153 mpdm_t
mpdm_strcat_s(const mpdm_t s1
, const wchar_t *s2
);
154 mpdm_t
mpdm_strcat(const mpdm_t s1
, const mpdm_t s2
);
155 int mpdm_cmp(const mpdm_t v1
, const mpdm_t v2
);
156 int mpdm_cmp_s(const mpdm_t v1
, const wchar_t *v2
);
157 int mpdm_ival(mpdm_t v
);
158 double mpdm_rval(mpdm_t v
);
159 mpdm_t
mpdm_set_ival(mpdm_t v
, int ival
);
160 mpdm_t
mpdm_set_rval(mpdm_t v
, double rval
);
162 mpdm_t
mpdm_xnew(mpdm_t(*a1
) (mpdm_t
, mpdm_t
, mpdm_t
), mpdm_t a2
);
163 mpdm_t
mpdm_new_copy(int flags
, void *ptr
, int size
);
165 int mpdm_hsize(const mpdm_t h
);
166 mpdm_t
mpdm_hget(const mpdm_t h
, const mpdm_t k
);
167 mpdm_t
mpdm_hget_s(const mpdm_t h
, const wchar_t *k
);
168 int mpdm_exists(const mpdm_t h
, const mpdm_t k
);
169 mpdm_t
mpdm_hset(mpdm_t h
, mpdm_t k
, mpdm_t v
);
170 mpdm_t
mpdm_hset_s(mpdm_t h
, const wchar_t *k
, mpdm_t v
);
171 mpdm_t
mpdm_hdel(mpdm_t h
, const mpdm_t k
);
172 mpdm_t
mpdm_keys(const mpdm_t h
);
173 int mpdm_iterator(mpdm_t h
, int *context
, mpdm_t
*v1
, mpdm_t
*v2
);
175 extern wchar_t * (*mpdm_dump_1
) (const mpdm_t v
, int l
, wchar_t *ptr
, int *size
);
176 mpdm_t
mpdm_dumper(const mpdm_t v
);
177 void mpdm_dump(const mpdm_t v
);
179 #define MPDM_SGET(r, k) mpdm_sget((r), MPDM_LS((k)))
180 #define MPDM_SSET(r, k, v) mpdm_sset((r), MPDM_LS((k)), (v))
182 mpdm_t
mpdm_sget(mpdm_t r
, mpdm_t k
);
183 mpdm_t
mpdm_sget_i(mpdm_t r
, mpdm_t k
, int i
);
184 mpdm_t
mpdm_sset(mpdm_t r
, mpdm_t k
, mpdm_t v
);
186 int mpdm_write_wcs(FILE * f
, const wchar_t * str
);
187 mpdm_t
mpdm_new_f(FILE * f
);
188 mpdm_t
mpdm_open(const mpdm_t filename
, const mpdm_t mode
);
189 mpdm_t
mpdm_close(mpdm_t fd
);
190 mpdm_t
mpdm_read(const mpdm_t fd
);
191 int mpdm_write(const mpdm_t fd
, const mpdm_t v
);
192 mpdm_t
mpdm_getchar(const mpdm_t fd
);
193 int mpdm_putchar(const mpdm_t fd
, const mpdm_t c
);
194 int mpdm_fseek(const mpdm_t fd
, long offset
, int whence
);
195 long mpdm_ftell(const mpdm_t fd
);
196 FILE * mpdm_get_filehandle(const mpdm_t fd
);
197 int mpdm_encoding(mpdm_t charset
);
198 int mpdm_unlink(const mpdm_t filename
);
199 mpdm_t
mpdm_stat(const mpdm_t filename
);
200 int mpdm_chmod(const mpdm_t filename
, mpdm_t perms
);
201 int mpdm_chdir(const mpdm_t dir
);
202 int mpdm_chown(const mpdm_t filename
, mpdm_t uid
, mpdm_t gid
);
203 mpdm_t
mpdm_glob(mpdm_t spec
, mpdm_t base
);
205 mpdm_t
mpdm_popen(const mpdm_t prg
, const mpdm_t mode
);
206 mpdm_t
mpdm_popen2(const mpdm_t prg
);
207 mpdm_t
mpdm_pclose(mpdm_t fd
);
209 extern int mpdm_regex_offset
;
210 extern int mpdm_regex_size
;
211 extern int mpdm_sregex_count
;
213 mpdm_t
mpdm_regex(const mpdm_t v
, const mpdm_t r
, int offset
);
214 mpdm_t
mpdm_sregex(const mpdm_t v
, const mpdm_t r
, const mpdm_t s
, int offset
);
216 mpdm_t
mpdm_gettext(const mpdm_t str
);
217 int mpdm_gettext_domain(const mpdm_t dom
, const mpdm_t data
);
219 mpdm_t
mpdm_home_dir(void);
220 mpdm_t
mpdm_app_dir(void);
222 /* value type testing macros */
224 #define MPDM_IS_ARRAY(v) ((v != NULL) && ((v)->flags) & MPDM_MULTIPLE)
225 #define MPDM_IS_HASH(v) ((v != NULL) && ((v)->flags) & MPDM_HASH)
226 #define MPDM_IS_EXEC(v) ((v != NULL) && ((v)->flags) & MPDM_EXEC)
227 #define MPDM_IS_STRING(v) ((v != NULL) && ((v)->flags) & MPDM_STRING)
229 /* value creation utility macros */
231 #define MPDM_A(n) mpdm_new_a(0,n)
232 #define MPDM_H(n) mpdm_new_a(MPDM_HASH|MPDM_IVAL,n)
233 #define MPDM_LS(s) mpdm_new_wcs(0, s, -1, 0)
234 #define MPDM_S(s) mpdm_new_wcs(0, s, -1, 1)
235 #define MPDM_NS(s,n) mpdm_new_wcs(0, s, n, 1)
236 #define MPDM_ENS(s,n) mpdm_new(MPDM_STRING|MPDM_FREE, s, n)
237 #define MPDM_C(f,p,s) mpdm_new_copy(f,p,s)
239 #define MPDM_I(i) mpdm_new_i((i))
240 #define MPDM_R(r) mpdm_new_r((r))
241 #define MPDM_P(p) mpdm_new(0,(void *)p, 0, NULL)
242 #define MPDM_MBS(s) mpdm_new_mbstowcs(0, s, -1)
243 #define MPDM_NMBS(s,n) mpdm_new_mbstowcs(0, s, n)
244 #define MPDM_2MBS(s) mpdm_new_wcstombs(0, s)
246 #define MPDM_X(f) mpdm_new(MPDM_EXEC, (const void *)f, 0)
247 #define MPDM_X2(f,b) mpdm_xnew(f,b)
249 #define MPDM_F(f) mpdm_new_f(f)
251 #define MPDM_AA(n) mpdm_init(alloca(sizeof(struct mpdm_val)), \
252 MPDM_NONDYN | MPDM_MULTIPLE, \
253 memset(alloca(sizeof(mpdm_t) * n), '\0', sizeof(mpdm_t) * n), \
256 #define MPDM_AS(s) mpdm_init(alloca(sizeof(struct mpdm_val)), \
257 MPDM_NONDYN | MPDM_STRING, \
260 int mpdm_startup(void);
261 void mpdm_shutdown(void);
263 void mpdm_sleep(int msecs
);
265 mpdm_t
mpdm_new_mutex(void);
266 void mpdm_mutex_lock(mpdm_t mutex
);
267 void mpdm_mutex_unlock(mpdm_t mutex
);
269 mpdm_t
mpdm_new_semaphore(int init_value
);
270 void mpdm_semaphore_wait(mpdm_t sem
);
271 void mpdm_semaphore_post(mpdm_t sem
);
273 mpdm_t
mpdm_exec_thread(mpdm_t c
, mpdm_t args
, mpdm_t ctxt
);