3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
6 mpdm_v.c - Basic value management
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
38 /* control structure */
40 struct mpdm_control
*mpdm
= NULL
;
45 static void cleanup_value(mpdm_t v
)
48 /* collapse multiple values */
49 if (v
->flags
& MPDM_MULTIPLE
)
50 mpdm_collapse(v
, 0, v
->size
);
52 /* free data if needed */
53 if (v
->data
!= NULL
&& v
->flags
& MPDM_FREE
) {
54 free((void *)v
->data
);
65 * mpdm_new - Creates a new value.
67 * @data: pointer to real data
70 * Creates a new value. @flags is an or-ed set of flags, @data is a
71 * pointer to the data the value will store and @size the size of these
72 * data (if value is to be a multiple one, @size is a number of elements,
73 * or a number of bytes otherwise).
75 * This function is normally not directly used; use any of the type
76 * creation macros instead.
79 mpdm_t
mpdm_new(int flags
, const void *data
, int size
)
84 if ((v
= malloc(sizeof(struct mpdm_val
))) == NULL
)
87 /* account one value more */
100 * mpdm_ref - Increments the reference count of a value.
103 * Increments the reference count of a value.
106 mpdm_t
mpdm_ref(mpdm_t v
)
115 * mpdm_unref - Decrements the reference count of a value.
118 * Decrements the reference count of a value.
121 mpdm_t
mpdm_unref(mpdm_t v
)
137 * mpdm_unrefnd - Decrements the reference count of a value, without destroy.
140 * Decrements the reference count of a value, without destroying
141 * the value if it's unreferenced.
144 mpdm_t
mpdm_unrefnd(mpdm_t v
)
153 * mpdm_size - Returns the size of an element.
156 * Returns the size of an element.
159 int mpdm_size(const mpdm_t v
)
163 /* NULL values have no size */
172 * mpdm_clone - Creates a clone of a value.
175 * Creates a clone of a value. If the value is multiple, a new value will
176 * be created containing clones of all its elements; otherwise,
177 * the same unchanged value is returned.
180 mpdm_t
mpdm_clone(const mpdm_t v
)
186 if (MPDM_IS_ARRAY(v
))
198 * mpdm_root - Returns the root hash.
200 * Returns the root hash. This hash is stored internally and can be used
201 * as a kind of global symbol table.
204 mpdm_t
mpdm_root(void)
206 if (mpdm
->root
== NULL
)
207 mpdm
->root
= mpdm_ref(MPDM_H(0));
213 mpdm_t
mpdm_set_ival(mpdm_t v
, int ival
)
214 /* sets an integer value to a value */
216 v
->flags
|= MPDM_IVAL
;
223 mpdm_t
mpdm_set_rval(mpdm_t v
, double rval
)
224 /* sets a real value to a value */
226 v
->flags
|= MPDM_RVAL
;
234 * mpdm_exec - Executes an executable value.
236 * @args: the arguments
239 * Executes an executable value. If @c is a scalar value, its data
240 * should be a pointer to a directly executable C function with a
241 * prototype of mpdm_t func(mpdm_t args, mpdm_t ctxt); if it's a multiple
242 * one, the first value's data should be a pointer to a directly executable
243 * C function with a prototype of
244 * mpdm_t func(mpdm_t b, mpdm_t args, mpdm_t ctxt) and
245 * the second value will be passed as the @b argument. This value is used
246 * to store bytecode or so when implementing virtual machines or compilers.
247 * The @ctxt is meant to be used as a special context to implement local
248 * symbol tables and such. Its meaning is free and can be NULL.
250 * Returns the return value of the code. If @c is NULL or not executable,
254 mpdm_t
mpdm_exec(mpdm_t c
, mpdm_t args
, mpdm_t ctxt
)
262 if (c
!= NULL
&& (c
->flags
& MPDM_EXEC
)) {
264 if (c
->flags
& MPDM_MULTIPLE
) {
266 mpdm_t(*func
) (mpdm_t
, mpdm_t
, mpdm_t
);
268 /* value is multiple; first element is the
269 3 argument version of the executable function,
270 next its optional additional information,
271 the arguments and the context */
274 if ((func
= (mpdm_t(*)(mpdm_t
, mpdm_t
, mpdm_t
)) (x
->data
)) != NULL
)
275 r
= func(mpdm_aget(c
, 1), args
, ctxt
);
278 mpdm_t(*func
) (mpdm_t
, mpdm_t
);
280 /* value is scalar; c is the 2 argument
281 version of the executable function */
282 if ((func
= (mpdm_t(*)(mpdm_t
, mpdm_t
)) (c
->data
)) != NULL
)
283 r
= func(args
, ctxt
);
295 mpdm_t
mpdm_exec_1(mpdm_t c
, mpdm_t a1
, mpdm_t ctxt
)
298 mpdm_t a
= MPDM_A(1);
303 r
= mpdm_exec(c
, a
, ctxt
);
311 mpdm_t
mpdm_exec_2(mpdm_t c
, mpdm_t a1
, mpdm_t a2
, mpdm_t ctxt
)
314 mpdm_t a
= MPDM_A(2);
320 r
= mpdm_exec(c
, a
, ctxt
);
328 mpdm_t
mpdm_exec_3(mpdm_t c
, mpdm_t a1
, mpdm_t a2
, mpdm_t a3
, mpdm_t ctxt
)
331 mpdm_t a
= MPDM_A(3);
338 r
= mpdm_exec(c
, a
, ctxt
);
346 mpdm_t
mpdm_xnew(mpdm_t(*a1
) (mpdm_t
, mpdm_t
, mpdm_t
), mpdm_t a2
)
351 x
->flags
|= MPDM_EXEC
;
355 mpdm_aset(x
, MPDM_X(a1
), 0);
364 mpdm_t
mpdm_new_copy(int flags
, void *ptr
, int size
)
369 char *ptr2
= malloc(size
);
370 memcpy(ptr2
, ptr
, size
);
372 r
= mpdm_new(MPDM_FREE
| flags
, ptr2
, size
);
379 static mpdm_t
MPDM(const mpdm_t args
, mpdm_t ctxt
)
380 /* accesor / mutator for MPDM internal data */
386 v
= mpdm_aget(args
, 0);
392 if ((w
= mpdm_hget_s(v
, L
"hash_buckets")) != NULL
)
393 mpdm
->hash_buckets
= mpdm_ival(w
);
396 /* now collect all information */
401 mpdm_hset_s(v
, L
"version", MPDM_MBS(VERSION
));
402 mpdm_hset_s(v
, L
"count", MPDM_I(mpdm
->count
));
403 mpdm_hset_s(v
, L
"hash_buckets", MPDM_I(mpdm
->hash_buckets
));
404 mpdm_hset_s(v
, L
"destroy_on_unref", MPDM_I(1));
413 extern char **environ
;
415 static mpdm_t
build_env(void)
416 /* builds a hash with the environment */
419 mpdm_t e
= MPDM_H(0);
423 for (ptr
= environ
; *ptr
!= NULL
; ptr
++) {
424 char *eq
= strchr(*ptr
, '=');
429 k
= MPDM_NMBS((*ptr
), eq
- (*ptr
));
430 v
= MPDM_MBS(eq
+ 1);
443 * mpdm_startup - Initializes MPDM.
445 * Initializes the Minimum Profit Data Manager. Returns 0 if
446 * everything went OK.
448 int mpdm_startup(void)
450 /* do the startup only unless done beforehand */
453 if ((mpdm
= malloc(sizeof(struct mpdm_control
))) == NULL
)
457 memset(mpdm
, '\0', sizeof(struct mpdm_control
));
459 /* sets the defaults */
460 mpdm
->hash_buckets
= 31;
462 /* sets the locale */
463 if (setlocale(LC_ALL
, "") == NULL
)
464 setlocale(LC_ALL
, "C");
468 /* store the MPDM() function */
469 mpdm_hset_s(mpdm_root(), L
"MPDM", MPDM_X(MPDM
));
471 /* store the ENV hash */
472 mpdm_hset_s(mpdm_root(), L
"ENV", build_env());
475 /* everything went OK */
481 * mpdm_shutdown - Shuts down MPDM.
483 * Shuts down MPDM. No MPDM functions should be used from now on.
485 void mpdm_shutdown(void)
491 * MPDM_A - Creates an array value.
492 * @n: Number of elements
494 * Creates a new array value with @n elements.
497 /** mpdm_t MPDM_A(int n); */
501 * MPDM_H - Creates a hash value.
502 * @n: Number of buckets in the hash (0: use default)
504 * Creates a new hash value with @n buckets. The number
505 * of buckets must be a prime number. If @n is 0, an
506 * optimal number of buckets will be used.
509 /** mpdm_t MPDM_H(int n); */
513 * MPDM_LS - Creates a string value from a literal string.
514 * @wcs: the wide character string
516 * Creates a new string value from a literal, wide character string.
517 * A pointer to the string will be stored in the value (not a copy).
520 /** mpdm_t MPDM_LS(wchar_t * wcs); */
524 * MPDM_S - Creates a string value from a string.
525 * @wcs: the wide character string
527 * Creates a new string value from a wide character string. The value
528 * will store a copy of the string that will be freed on destruction.
531 /** mpdm_t MPDM_S(wchar_t * wcs); */
535 * MPDM_NS - Creates a string value from a string, with size.
536 * @wcs: the wide character string
537 * @s: the size in chars the string will hold
539 * Creates a new string value with a copy of the first @s characters
540 * from the @wcs string.
543 /** mpdm_t MPDM_NS(wchar_t * wcs, int s); */
547 * MPDM_ENS - Creates a string value from an external string, with size.
548 * @wcs: the external wide character string
549 * @s: the size in chars the string will hold
551 * Creates a new string value with size @s. The @wcs string must be
552 * a dynamic value (i.e. allocated by malloc()) that will be freed on
556 /** mpdm_t MPDM_ENS(wchar_t * wcs, int s); */
560 * MPDM_I - Creates an integer value.
563 * Creates a new integer value. MPDM integers are strings.
566 /** mpdm_t MPDM_I(int i); */
570 * MPDM_R - Creates a real value.
571 * @r: the real number
573 * Creates a new real value. MPDM integers are strings.
576 /** mpdm_t MPDM_R(double r); */
580 * MPDM_F - Creates a file value.
581 * @f: the file descriptor
583 * Creates a new file value.
586 /** mpdm_t MPDM_F(FILE * f); */
590 * MPDM_MBS - Creates a string value from a multibyte string.
591 * @mbs: the multibyte string
593 * Creates a new string value from a multibyte string, that will be
594 * converted to wcs by mpdm_mbstowcs().
597 /** mpdm_t MPDM_MBS(char * mbs); */
601 * MPDM_NMBS - Creates a string value from a multibyte string, with size.
602 * @mbs: the multibyte string
605 * Creates a new string value with the first @s characters from the @mbs
606 * multibyte string, that will be converted to wcs by mpdm_mbstowcs().
609 /** mpdm_t MPDM_NMBS(char * mbs, int s); */
613 * MPDM_2MBS - Creates a multibyte string value from a wide char string.
614 * @wcs: the wide char string
616 * Creates a multibyte string value from the @wcs wide char string,
617 * converting it by mpdm_wcstombs(). Take note that multibyte string values
618 * are not properly strings, so they cannot be used for string comparison
622 /** mpdm_t MPDM_2MBS(wchar_t * wcs); */
626 * MPDM_X - Creates a new executable value.
627 * @func: the C code function
629 * Creates a new executable value given a pointer to the @func C code function.
630 * The function must receive an mpdm_t array value (that will hold their
631 * arguments) and return another one.
634 /** mpdm_t MPDM_X(mpdm_t (* func)(mpdm_t args)); */
638 * MPDM_C - Creates a new value with a copy of a buffer.
639 * @flags: additional flags
640 * @ptr: pointer to data
643 * Create a new value with a copy of a buffer. The value will store a copy
644 * of @ptr and have the additional @flags.
647 /** mpdm_t MPDM_C(int flags, void *ptr, int size); */
651 * MPDM_IS_ARRAY - Tests if a value is an array.
654 * Returns non-zero if @v is an array.
656 /** int MPDM_IS_ARRAY(mpdm_t v); */
660 * MPDM_IS_HASH - Tests if a value is a hash.
663 * Returns non-zero if @v is a hash.
665 /** int MPDM_IS_HASH(mpdm_t v); */
669 * MPDM_IS_EXEC - Tests if a value is executable.
672 * Returns non-zero if @v is executable.
674 /** int MPDM_IS_EXEC(mpdm_t v); */
678 * MPDM_IS_STRING - Tests if a value is a string.
681 * Returns non-zero if @v is a string.
683 /** int MPDM_IS_STRING(mpdm_t v); */