3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2011 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
) {
52 for (n
= 0; n
< mpdm_size(v
); n
++)
53 mpdm_unref(mpdm_aget(v
, n
));
56 /* free data if needed */
57 if (v
->data
!= NULL
&& v
->flags
& MPDM_FREE
) {
58 free((void *) v
->data
);
64 if (!(v
->flags
& MPDM_NONDYN
))
69 static void destroy_value(mpdm_t v
)
70 /* destroys a value */
77 * mpdm_init - Initializes a value.
78 * @v: the value to initialize
80 * @data: pointer to real data
83 * Initializes a value.
85 * This function is normally not directly used; use any of the type
86 * creation macros instead.
89 mpdm_t
mpdm_init(mpdm_t v
, int flags
, const void *data
, int size
)
91 /* if v is NULL crash JUST NOW */
104 * mpdm_new - Creates a new value.
106 * @data: pointer to real data
107 * @size: size of data
109 * Creates a new value. @flags is an or-ed set of flags, @data is a
110 * pointer to the data the value will store and @size the size of these
111 * data (if value is to be a multiple one, @size is a number of elements,
112 * or a number of bytes otherwise).
114 * This function is normally not directly used; use any of the type
115 * creation macros instead.
118 mpdm_t
mpdm_new(int flags
, const void *data
, int size
)
120 return mpdm_init(malloc(sizeof(struct mpdm_val
)), flags
, data
, size
);
125 * mpdm_ref - Increments the reference count of a value.
128 * Increments the reference count of a value.
131 mpdm_t
mpdm_ref(mpdm_t v
)
141 * mpdm_unref - Decrements the reference count of a value.
144 * Decrements the reference count of a value. If the reference
145 * count of the value reaches 0, it's destroyed.
148 mpdm_t
mpdm_unref(mpdm_t v
)
164 * mpdm_unrefnd - Decrements the reference count of a value, without destroy.
167 * Decrements the reference count of a value, without destroying
168 * the value if it's unreferenced.
171 mpdm_t
mpdm_unrefnd(mpdm_t v
)
181 * mpdm_size - Returns the size of an element.
184 * Returns the size of an element. It does not change the
185 * reference count of the value.
188 int mpdm_size(const mpdm_t v
)
192 /* NULL values have no size */
201 * mpdm_clone - Creates a clone of a value.
204 * Creates a clone of a value. If the value is multiple, a new value will
205 * be created containing clones of all its elements; otherwise,
206 * the same unchanged value is returned.
209 mpdm_t
mpdm_clone(const mpdm_t v
)
213 if (MPDM_IS_ARRAY(v
) && !MPDM_IS_EXEC(v
))
223 * mpdm_root - Returns the root hash.
225 * Returns the root hash. This hash is stored internally and can be used
226 * as a kind of global symbol table.
229 mpdm_t
mpdm_root(void)
231 if (mpdm
->root
== NULL
)
232 mpdm
->root
= mpdm_ref(MPDM_H(0));
239 * mpdm_set_ival - Sets the integer value.
243 * Sets the integer value for @v. It does not change
244 * the reference count of @v.
246 mpdm_t
mpdm_set_ival(mpdm_t v
, int ival
)
247 /* sets an integer value to a value */
249 v
->flags
|= MPDM_IVAL
;
257 * mpdm_set_rval - Sets the real value.
261 * Sets the real value for @v. It does not change
262 * the reference count of @v.
264 mpdm_t
mpdm_set_rval(mpdm_t v
, double rval
)
265 /* sets a real value to a value */
267 v
->flags
|= MPDM_RVAL
;
275 * mpdm_void - Refs then unrefs a value.
278 * References and unreferences a value. To be used to receive
279 * the output of mpdm_exec() in case of it being void (i.e.
280 * its return value ignored).
282 void mpdm_void(mpdm_t v
)
290 * mpdm_is_null - Returns 1 if a value is NULL.
293 * Returns 1 if a value is NULL. The reference count is touched.
295 int mpdm_is_null(mpdm_t v
)
300 r
= v
== NULL
? 1 : 0;
308 * mpdm_exec - Executes an executable value.
310 * @args: the arguments
313 * Executes an executable value. If @c is a scalar value, its data
314 * should be a pointer to a directly executable C function with a
315 * prototype of mpdm_t func(mpdm_t args, mpdm_t ctxt); if it's a multiple
316 * one, the first value's data should be a pointer to a directly executable
317 * C function with a prototype of
318 * mpdm_t func(mpdm_t b, mpdm_t args, mpdm_t ctxt) and
319 * the second value will be passed as the @b argument. This value is used
320 * to store bytecode or so when implementing virtual machines or compilers.
321 * The @ctxt is meant to be used as a special context to implement local
322 * symbol tables and such. Its meaning is free and can be NULL.
324 * Returns the return value of the code. If @c is NULL or not executable,
328 mpdm_t
mpdm_exec(mpdm_t c
, mpdm_t args
, mpdm_t ctxt
)
336 if (c
!= NULL
&& (c
->flags
& MPDM_EXEC
)) {
338 if (c
->flags
& MPDM_MULTIPLE
) {
340 mpdm_t(*func
) (mpdm_t
, mpdm_t
, mpdm_t
);
342 /* value is multiple; first element is the
343 3 argument version of the executable function,
344 next its optional additional information,
345 the arguments and the context */
349 (mpdm_t(*)(mpdm_t
, mpdm_t
, mpdm_t
)) (x
->data
)) != NULL
)
350 r
= func(mpdm_aget(c
, 1), args
, ctxt
);
353 mpdm_t(*func
) (mpdm_t
, mpdm_t
);
355 /* value is scalar; c is the 2 argument
356 version of the executable function */
357 if ((func
= (mpdm_t(*)(mpdm_t
, mpdm_t
)) (c
->data
)) != NULL
)
358 r
= func(args
, ctxt
);
370 mpdm_t
mpdm_exec_1(mpdm_t c
, mpdm_t a1
, mpdm_t ctxt
)
373 mpdm_t a
= MPDM_LA(1);
378 r
= mpdm_exec(c
, a
, ctxt
);
386 mpdm_t
mpdm_exec_2(mpdm_t c
, mpdm_t a1
, mpdm_t a2
, mpdm_t ctxt
)
389 mpdm_t a
= MPDM_LA(2);
395 r
= mpdm_exec(c
, a
, ctxt
);
403 mpdm_t
mpdm_exec_3(mpdm_t c
, mpdm_t a1
, mpdm_t a2
, mpdm_t a3
, mpdm_t ctxt
)
406 mpdm_t a
= MPDM_LA(3);
413 r
= mpdm_exec(c
, a
, ctxt
);
421 mpdm_t
mpdm_xnew(mpdm_t(*a1
) (mpdm_t
, mpdm_t
, mpdm_t
), mpdm_t a2
)
426 x
->flags
|= MPDM_EXEC
;
430 mpdm_aset(x
, MPDM_X(a1
), 0);
439 mpdm_t
mpdm_new_copy(int flags
, void *ptr
, int size
)
444 char *ptr2
= malloc(size
);
445 memcpy(ptr2
, ptr
, size
);
447 r
= mpdm_new(MPDM_FREE
| flags
, ptr2
, size
);
454 static mpdm_t
MPDM(const mpdm_t args
, mpdm_t ctxt
)
455 /* accesor / mutator for MPDM internal data */
461 v
= mpdm_aget(args
, 0);
467 if ((w
= mpdm_hget_s(v
, L
"hash_buckets")) != NULL
)
468 mpdm
->hash_buckets
= mpdm_ival(w
);
471 /* now collect all information */
476 mpdm_hset_s(v
, L
"version", MPDM_MBS(VERSION
));
477 mpdm_hset_s(v
, L
"count", MPDM_I(mpdm
->count
));
478 mpdm_hset_s(v
, L
"hash_buckets", MPDM_I(mpdm
->hash_buckets
));
487 extern char **environ
;
489 static mpdm_t
build_env(void)
490 /* builds a hash with the environment */
493 mpdm_t e
= MPDM_H(0);
497 for (ptr
= environ
; *ptr
!= NULL
; ptr
++) {
498 char *eq
= strchr(*ptr
, '=');
503 k
= MPDM_NMBS((*ptr
), eq
- (*ptr
));
504 v
= MPDM_MBS(eq
+ 1);
517 * mpdm_startup - Initializes MPDM.
519 * Initializes the Minimum Profit Data Manager. Returns 0 if
520 * everything went OK.
522 int mpdm_startup(void)
524 /* do the startup only unless done beforehand */
527 if ((mpdm
= malloc(sizeof(struct mpdm_control
))) == NULL
)
531 memset(mpdm
, '\0', sizeof(struct mpdm_control
));
533 /* sets the defaults */
534 mpdm
->hash_buckets
= 31;
536 /* sets the locale */
537 if (setlocale(LC_ALL
, "") == NULL
)
538 setlocale(LC_ALL
, "C");
542 /* store the MPDM() function */
543 mpdm_hset_s(mpdm_root(), L
"MPDM", MPDM_X(MPDM
));
545 /* store the ENV hash */
546 mpdm_hset_s(mpdm_root(), L
"ENV", build_env());
549 /* everything went OK */
555 * mpdm_shutdown - Shuts down MPDM.
557 * Shuts down MPDM. No MPDM functions should be used from now on.
559 void mpdm_shutdown(void)
565 * MPDM_A - Creates an array value.
566 * @n: Number of elements
568 * Creates a new array value with @n elements.
571 /** mpdm_t MPDM_A(int n); */
575 * MPDM_H - Creates a hash value.
576 * @n: Number of buckets in the hash (0: use default)
578 * Creates a new hash value with @n buckets. The number
579 * of buckets must be a prime number. If @n is 0, an
580 * optimal number of buckets will be used.
583 /** mpdm_t MPDM_H(int n); */
587 * MPDM_LS - Creates a string value from a literal string.
588 * @wcs: the wide character string
590 * Creates a new string value from a literal, wide character string.
591 * A pointer to the string will be stored in the value (not a copy).
594 /** mpdm_t MPDM_LS(wchar_t * wcs); */
598 * MPDM_S - Creates a string value from a string.
599 * @wcs: the wide character string
601 * Creates a new string value from a wide character string. The value
602 * will store a copy of the string that will be freed on destruction.
605 /** mpdm_t MPDM_S(wchar_t * wcs); */
609 * MPDM_NS - Creates a string value from a string, with size.
610 * @wcs: the wide character string
611 * @s: the size in chars the string will hold
613 * Creates a new string value with a copy of the first @s characters
614 * from the @wcs string.
617 /** mpdm_t MPDM_NS(wchar_t * wcs, int s); */
621 * MPDM_ENS - Creates a string value from an external string, with size.
622 * @wcs: the external wide character string
623 * @s: the size in chars the string will hold
625 * Creates a new string value with size @s. The @wcs string must be
626 * a dynamic value (i.e. allocated by malloc()) that will be freed on
630 /** mpdm_t MPDM_ENS(wchar_t * wcs, int s); */
634 * MPDM_I - Creates an integer value.
637 * Creates a new integer value. MPDM integers are strings.
640 /** mpdm_t MPDM_I(int i); */
644 * MPDM_R - Creates a real value.
645 * @r: the real number
647 * Creates a new real value. MPDM integers are strings.
650 /** mpdm_t MPDM_R(double r); */
654 * MPDM_F - Creates a file value.
655 * @f: the file descriptor
657 * Creates a new file value.
660 /** mpdm_t MPDM_F(FILE * f); */
664 * MPDM_MBS - Creates a string value from a multibyte string.
665 * @mbs: the multibyte string
667 * Creates a new string value from a multibyte string, that will be
668 * converted to wcs by mpdm_mbstowcs().
671 /** mpdm_t MPDM_MBS(char * mbs); */
675 * MPDM_NMBS - Creates a string value from a multibyte string, with size.
676 * @mbs: the multibyte string
679 * Creates a new string value with the first @s characters from the @mbs
680 * multibyte string, that will be converted to wcs by mpdm_mbstowcs().
683 /** mpdm_t MPDM_NMBS(char * mbs, int s); */
687 * MPDM_2MBS - Creates a multibyte string value from a wide char string.
688 * @wcs: the wide char string
690 * Creates a multibyte string value from the @wcs wide char string,
691 * converting it by mpdm_wcstombs(). Take note that multibyte string values
692 * are not properly strings, so they cannot be used for string comparison
696 /** mpdm_t MPDM_2MBS(wchar_t * wcs); */
700 * MPDM_X - Creates a new executable value.
701 * @func: the C code function
703 * Creates a new executable value given a pointer to the @func C code function.
704 * The function must receive an mpdm_t array value (that will hold their
705 * arguments) and return another one.
708 /** mpdm_t MPDM_X(mpdm_t (* func)(mpdm_t args)); */
712 * MPDM_C - Creates a new value with a copy of a buffer.
713 * @flags: additional flags
714 * @ptr: pointer to data
717 * Create a new value with a copy of a buffer. The value will store a copy
718 * of @ptr and have the additional @flags.
721 /** mpdm_t MPDM_C(int flags, void *ptr, int size); */
725 * MPDM_IS_ARRAY - Tests if a value is an array.
728 * Returns non-zero if @v is an array.
730 /** int MPDM_IS_ARRAY(mpdm_t v); */
734 * MPDM_IS_HASH - Tests if a value is a hash.
737 * Returns non-zero if @v is a hash.
739 /** int MPDM_IS_HASH(mpdm_t v); */
743 * MPDM_IS_EXEC - Tests if a value is executable.
746 * Returns non-zero if @v is executable.
748 /** int MPDM_IS_EXEC(mpdm_t v); */
752 * MPDM_IS_STRING - Tests if a value is a string.
755 * Returns non-zero if @v is a string.
757 /** int MPDM_IS_STRING(mpdm_t v); */