Update copyright years
[m17n/m17n-lib.git] / src / plist.h
1 /* plist.h -- header file for the plist module.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3      National Institute of Advanced Industrial Science and Technology (AIST)
4      Registration Number H15PRO112
5
6    This file is part of the m17n library.
7
8    The m17n library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public License
10    as published by the Free Software Foundation; either version 2.1 of
11    the License, or (at your option) any later version.
12
13    The m17n library 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 GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with the m17n library; if not, write to the Free
20    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    02111-1307, USA.  */
22
23 #ifndef _M17N_PLIST_H_
24 #define _M17N_PLIST_H_
25
26 struct MPlist
27 {
28   /** Header for a managed object.  */
29   M17NObject control;
30
31   /**en Key of the first element of the plist.  If the value is Mnil,
32      this is the tail of the plist.  In that case, <val> and <next> is
33      NULL.  If the value is a managing key, <val> is a managed
34      object.  */
35   MSymbol key;
36
37   /**en Value of the first element of the plist.  */
38   union {
39     void *pointer;
40     M17NFunc func;
41   } val;
42
43   /**en Plist for the next element. */
44   MPlist *next;
45 };
46
47 /** Macros to access each member of PLIST.  */
48
49 #define MPLIST_KEY(plist) ((plist)->key)
50 #define MPLIST_VAL(plist) ((plist)->val.pointer)
51 #define MPLIST_FUNC(plist) ((plist)->val.func)
52 #define MPLIST_NEXT(plist) ((plist)->next)
53 #define MPLIST_TAIL_P(plist) ((plist)->key == Mnil)
54
55 #define MPLIST_SYMBOL_P(plist) (MPLIST_KEY (plist) == Msymbol)
56 #define MPLIST_STRING_P(plist) (MPLIST_KEY (plist) == Mstring)
57 #define MPLIST_MTEXT_P(plist) (MPLIST_KEY (plist) == Mtext)
58 #define MPLIST_INTEGER_P(plist) (MPLIST_KEY (plist) == Minteger)
59 #define MPLIST_PLIST_P(plist) (MPLIST_KEY (plist) == Mplist)
60
61 #define MPLIST_NESTED_P(plist)  \
62   ((plist)->control.flag & 1)
63 #define MPLIST_SET_NESTED_P(plist)      \
64   ((plist)->control.flag |= 1)
65
66 #define MPLIST_VAL_FUNC_P(plist)        \
67   ((plist)->control.flag & 2)
68 #define MPLIST_SET_VAL_FUNC_P(plist)    \
69   ((plist)->control.flag |= 2)
70
71 #define MPLIST_SYMBOL(plist) ((MSymbol) MPLIST_VAL (plist))
72 #define MPLIST_STRING(plist) ((char *) MPLIST_VAL (plist))
73 #define MPLIST_MTEXT(plist) ((MText *) MPLIST_VAL (plist))
74 #define MPLIST_INTEGER(plist) ((int) MPLIST_VAL (plist))
75 #define MPLIST_PLIST(plist) ((MPlist *) MPLIST_VAL (plist))
76
77 #define MPLIST_FIND(plist, key)                                         \
78   do {                                                                  \
79     while (! MPLIST_TAIL_P (plist) && MPLIST_KEY (plist) != (key))      \
80       (plist) = (plist)->next;                                          \
81   } while (0)
82
83
84 #define MPLIST_DO(elt, plist)   \
85   for ((elt) = (plist); ! MPLIST_TAIL_P (elt); (elt) = MPLIST_NEXT (elt))
86
87 #define MPLIST_LENGTH(plist)                    \
88   (MPLIST_TAIL_P (plist) ? 0                    \
89    : MPLIST_TAIL_P ((plist)->next) ? 1          \
90    : MPLIST_TAIL_P ((plist)->next->next) ? 2    \
91    : mplist_length (plist))
92
93 #define MPLIST_ADD_PLIST(PLIST, KEY, VAL) \
94   MPLIST_SET_NESTED_P (mplist_add ((PLIST), (KEY), (VAL)))
95 #define MPLIST_PUSH_PLIST(PLIST, KEY, VAL) \
96   MPLIST_SET_NESTED_P (mplist_push ((PLIST), (KEY), (VAL)))
97 #define MPLIST_PUT_PLIST(PLIST, KEY, VAL) \
98   MPLIST_SET_NESTED_P (mplist_put ((PLIST), (KEY), (VAL)))
99
100
101 extern unsigned char hex_mnemonic[256];
102 extern unsigned char escape_mnemonic[256];
103
104 extern MPlist *mplist__from_file (FILE *fp, MPlist *keys);
105
106 extern MPlist *mplist__from_plist (MPlist *plist);
107
108 extern MPlist *mplist__from_alist (MPlist *plist);
109
110 extern MPlist *mplist__from_string (unsigned char *str, int n);
111
112 extern int mplist__serialize (MText *mt, MPlist *plist, int pretty);
113
114 extern MPlist *mplist__conc (MPlist *plist, MPlist *tail);
115
116 extern void mplist__pop_unref (MPlist *plist);
117
118 extern MPlist *mplist__assq (MPlist *plist, MSymbol key);
119
120 #endif  /* _M17N_PLIST_H_ */