Include malloc.h instead of doing ifdef magic in mpdm.h.
[mpdm.git] / mpdm_v.c
blob4b8f8f55f2e25ab63ee71e73df8125fc189de763
1 /*
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
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <locale.h>
33 #include "mpdm.h"
36 /** data **/
38 /* control structure */
40 struct mpdm_control *mpdm = NULL;
43 /** code **/
45 static void cleanup_value(mpdm_t v)
46 /* cleans a value */
48 /* collapse multiple values */
49 if (v->flags & MPDM_MULTIPLE) {
50 int n;
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);
59 v->data = NULL;
62 mpdm->count--;
64 if (!(v->flags & MPDM_NONDYN))
65 free(v);
69 static void destroy_value(mpdm_t v)
70 /* destroys a value */
72 cleanup_value(v);
76 /**
77 * mpdm_init - Initializes a value.
78 * @v: the value to initialize
79 * @flags: flags
80 * @data: pointer to real data
81 * @size: size of data
83 * Initializes a value.
85 * This function is normally not directly used; use any of the type
86 * creation macros instead.
87 * [Value Creation]
89 mpdm_t mpdm_init(mpdm_t v, int flags, const void *data, int size)
91 /* if v is NULL crash JUST NOW */
92 v->flags = flags;
93 v->ref = 0;
94 v->data = data;
95 v->size = size;
97 mpdm->count++;
99 return v;
104 * mpdm_new - Creates a new value.
105 * @flags: flags
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.
116 * [Value Creation]
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.
126 * @v: the value
128 * Increments the reference count of a value.
129 * [Value Management]
131 mpdm_t mpdm_ref(mpdm_t v)
133 if (v != NULL)
134 v->ref++;
136 return v;
141 * mpdm_unref - Decrements the reference count of a value.
142 * @v: the value
144 * Decrements the reference count of a value. If the reference
145 * count of the value reaches 0, it's destroyed.
146 * [Value Management]
148 mpdm_t mpdm_unref(mpdm_t v)
150 if (v != NULL) {
151 v->ref--;
153 if (v->ref <= 0) {
154 destroy_value(v);
155 v = NULL;
159 return v;
164 * mpdm_unrefnd - Decrements the reference count of a value, without destroy.
165 * @v: the value
167 * Decrements the reference count of a value, without destroying
168 * the value if it's unreferenced.
169 * [Value Management]
171 mpdm_t mpdm_unrefnd(mpdm_t v)
173 if (v != NULL)
174 v->ref--;
176 return v;
181 * mpdm_size - Returns the size of an element.
182 * @v: the element
184 * Returns the size of an element. It does not change the
185 * reference count of the value.
186 * [Value Management]
188 int mpdm_size(const mpdm_t v)
190 int r = 0;
192 /* NULL values have no size */
193 if (v != NULL)
194 r = v->size;
196 return r;
201 * mpdm_clone - Creates a clone of a value.
202 * @v: the 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.
207 * [Value Management]
209 mpdm_t mpdm_clone(const mpdm_t v)
211 mpdm_t r;
213 if (MPDM_IS_ARRAY(v) && !MPDM_IS_EXEC(v))
214 r = mpdm_aclone(v);
215 else
216 r = v;
218 return r;
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.
227 * [Value Management]
229 mpdm_t mpdm_root(void)
231 if (mpdm->root == NULL)
232 mpdm->root = mpdm_ref(MPDM_H(0));
234 return mpdm->root;
239 * mpdm_set_ival - Sets the integer value.
240 * @v: the value
241 * @ival: the integer
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;
250 v->ival = ival;
252 return v;
257 * mpdm_set_rval - Sets the real value.
258 * @v: the value
259 * @rval: the real
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;
268 v->rval = rval;
270 return v;
275 * mpdm_void - Refs then unrefs a value.
276 * @v: the 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)
284 mpdm_ref(v);
285 mpdm_unref(v);
290 * mpdm_is_null - Returns 1 if a value is NULL.
291 * @v: the value
293 * Returns 1 if a value is NULL. The reference count is touched.
295 int mpdm_is_null(mpdm_t v)
297 int r;
299 mpdm_ref(v);
300 r = v == NULL ? 1 : 0;
301 mpdm_unref(v);
303 return r;
308 * mpdm_exec - Executes an executable value.
309 * @c: the code value
310 * @args: the arguments
311 * @ctxt: the context
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,
325 * returns NULL.
326 * [Value Management]
328 mpdm_t mpdm_exec(mpdm_t c, mpdm_t args, mpdm_t ctxt)
330 mpdm_t r = NULL;
332 mpdm_ref(c);
333 mpdm_ref(args);
334 mpdm_ref(ctxt);
336 if (c != NULL && (c->flags & MPDM_EXEC)) {
338 if (c->flags & MPDM_MULTIPLE) {
339 mpdm_t x;
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 */
346 x = mpdm_aget(c, 0);
348 if ((func =
349 (mpdm_t(*)(mpdm_t, mpdm_t, mpdm_t)) (x->data)) != NULL)
350 r = func(mpdm_aget(c, 1), args, ctxt);
352 else {
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);
362 mpdm_unref(ctxt);
363 mpdm_unref(args);
364 mpdm_unref(c);
366 return r;
370 mpdm_t mpdm_exec_1(mpdm_t c, mpdm_t a1, mpdm_t ctxt)
372 mpdm_t r;
373 mpdm_t a = MPDM_LA(1);
375 mpdm_ref(a);
376 mpdm_aset(a, a1, 0);
378 r = mpdm_exec(c, a, ctxt);
380 mpdm_unref(a);
382 return r;
386 mpdm_t mpdm_exec_2(mpdm_t c, mpdm_t a1, mpdm_t a2, mpdm_t ctxt)
388 mpdm_t r;
389 mpdm_t a = MPDM_LA(2);
391 mpdm_ref(a);
392 mpdm_aset(a, a1, 0);
393 mpdm_aset(a, a2, 1);
395 r = mpdm_exec(c, a, ctxt);
397 mpdm_unref(a);
399 return r;
403 mpdm_t mpdm_exec_3(mpdm_t c, mpdm_t a1, mpdm_t a2, mpdm_t a3, mpdm_t ctxt)
405 mpdm_t r;
406 mpdm_t a = MPDM_LA(3);
408 mpdm_ref(a);
409 mpdm_aset(a, a1, 0);
410 mpdm_aset(a, a2, 1);
411 mpdm_aset(a, a3, 2);
413 r = mpdm_exec(c, a, ctxt);
415 mpdm_unref(a);
417 return r;
421 mpdm_t mpdm_xnew(mpdm_t(*a1) (mpdm_t, mpdm_t, mpdm_t), mpdm_t a2)
423 mpdm_t x;
425 x = MPDM_A(2);
426 x->flags |= MPDM_EXEC;
428 mpdm_ref(x);
430 mpdm_aset(x, MPDM_X(a1), 0);
431 mpdm_aset(x, a2, 1);
433 mpdm_unrefnd(x);
435 return x;
439 mpdm_t mpdm_new_copy(int flags, void *ptr, int size)
441 mpdm_t r = NULL;
443 if (ptr != NULL) {
444 char *ptr2 = malloc(size);
445 memcpy(ptr2, ptr, size);
447 r = mpdm_new(MPDM_FREE | flags, ptr2, size);
450 return r;
454 static mpdm_t MPDM(const mpdm_t args, mpdm_t ctxt)
455 /* accesor / mutator for MPDM internal data */
457 mpdm_t v;
459 mpdm_ref(args);
461 v = mpdm_aget(args, 0);
463 if (v != NULL) {
464 mpdm_t w;
466 /* do changes */
467 if ((w = mpdm_hget_s(v, L"hash_buckets")) != NULL)
468 mpdm->hash_buckets = mpdm_ival(w);
471 /* now collect all information */
472 v = MPDM_H(0);
474 mpdm_ref(v);
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));
480 mpdm_unref(args);
482 mpdm_unrefnd(v);
484 return v;
487 extern char **environ;
489 static mpdm_t build_env(void)
490 /* builds a hash with the environment */
492 char **ptr;
493 mpdm_t e = MPDM_H(0);
495 mpdm_ref(e);
497 for (ptr = environ; *ptr != NULL; ptr++) {
498 char *eq = strchr(*ptr, '=');
500 if (eq != NULL) {
501 mpdm_t k, v;
503 k = MPDM_NMBS((*ptr), eq - (*ptr));
504 v = MPDM_MBS(eq + 1);
506 mpdm_hset(e, k, v);
510 mpdm_unrefnd(e);
512 return e;
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 */
525 if (mpdm == NULL) {
526 /* alloc space */
527 if ((mpdm = malloc(sizeof(struct mpdm_control))) == NULL)
528 return -1;
530 /* cleans it */
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");
540 mpdm_encoding(NULL);
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 */
550 return 0;
555 * mpdm_shutdown - Shuts down MPDM.
557 * Shuts down MPDM. No MPDM functions should be used from now on.
559 void mpdm_shutdown(void)
561 /* dummy, by now */
565 * MPDM_A - Creates an array value.
566 * @n: Number of elements
568 * Creates a new array value with @n elements.
569 * [Value Creation]
571 /** mpdm_t MPDM_A(int n); */
572 /* ; */
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.
581 * [Value Creation]
583 /** mpdm_t MPDM_H(int n); */
584 /* ; */
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).
592 * [Value Creation]
594 /** mpdm_t MPDM_LS(wchar_t * wcs); */
595 /* ; */
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.
603 * [Value Creation]
605 /** mpdm_t MPDM_S(wchar_t * wcs); */
606 /* ; */
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.
615 * [Value Creation]
617 /** mpdm_t MPDM_NS(wchar_t * wcs, int s); */
618 /* ; */
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
627 * destruction.
628 * [Value Creation]
630 /** mpdm_t MPDM_ENS(wchar_t * wcs, int s); */
631 /* ; */
634 * MPDM_I - Creates an integer value.
635 * @i: the integer
637 * Creates a new integer value. MPDM integers are strings.
638 * [Value Creation]
640 /** mpdm_t MPDM_I(int i); */
641 /* ; */
644 * MPDM_R - Creates a real value.
645 * @r: the real number
647 * Creates a new real value. MPDM integers are strings.
648 * [Value Creation]
650 /** mpdm_t MPDM_R(double r); */
651 /* ; */
654 * MPDM_F - Creates a file value.
655 * @f: the file descriptor
657 * Creates a new file value.
658 * [Value Creation]
660 /** mpdm_t MPDM_F(FILE * f); */
661 /* ; */
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().
669 * [Value Creation]
671 /** mpdm_t MPDM_MBS(char * mbs); */
672 /* ; */
675 * MPDM_NMBS - Creates a string value from a multibyte string, with size.
676 * @mbs: the multibyte string
677 * @s: the size
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().
681 * [Value Creation]
683 /** mpdm_t MPDM_NMBS(char * mbs, int s); */
684 /* ; */
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
693 * and such.
694 * [Value Creation]
696 /** mpdm_t MPDM_2MBS(wchar_t * wcs); */
697 /* ; */
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.
706 * [Value Creation]
708 /** mpdm_t MPDM_X(mpdm_t (* func)(mpdm_t args)); */
709 /* ; */
712 * MPDM_C - Creates a new value with a copy of a buffer.
713 * @flags: additional flags
714 * @ptr: pointer to data
715 * @size: data size
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.
719 * [Value Creation]
721 /** mpdm_t MPDM_C(int flags, void *ptr, int size); */
722 /* ; */
725 * MPDM_IS_ARRAY - Tests if a value is an array.
726 * @v: the value
728 * Returns non-zero if @v is an array.
730 /** int MPDM_IS_ARRAY(mpdm_t v); */
731 /* ; */
734 * MPDM_IS_HASH - Tests if a value is a hash.
735 * @v: the value
737 * Returns non-zero if @v is a hash.
739 /** int MPDM_IS_HASH(mpdm_t v); */
740 /* ; */
743 * MPDM_IS_EXEC - Tests if a value is executable.
744 * @v: the value
746 * Returns non-zero if @v is executable.
748 /** int MPDM_IS_EXEC(mpdm_t v); */
749 /* ; */
752 * MPDM_IS_STRING - Tests if a value is a string.
753 * @v: the value
755 * Returns non-zero if @v is a string.
757 /** int MPDM_IS_STRING(mpdm_t v); */
758 /* ; */