]> mj.ucw.cz Git - libucw.git/blob - ucw/xtypes.c
Build: Forced linker to find the currently compiled libraries instead of possibly...
[libucw.git] / ucw / xtypes.c
1 /*
2  *      UCW Library -- Extended Types -- Generic Operations
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/xtypes.h>
12
13 #include <string.h>
14
15 static const char * const fmt_names[] = {
16   [XTYPE_FMT_DEFAULT] = "default",
17   [XTYPE_FMT_RAW] = "raw",
18   [XTYPE_FMT_PRETTY] = "pretty",
19 };
20
21 const char *xtype_parse_fmt(const struct xtype *xt, const char *str, u32 *dest, struct mempool *pool)
22 {
23   for (uint i=0; i < ARRAY_SIZE(fmt_names); i++)
24     if (!strcmp(str, fmt_names[i]))
25       {
26         *dest = i;
27         return NULL;
28       }
29
30   if (xt && xt->parse_fmt)
31     return (xt->parse_fmt)(str, dest, pool);
32   else
33     return "Unknown mode";
34 }
35
36 const char *xtype_format_fmt(struct xtype *xt, u32 fmt, struct mempool *pool)
37 {
38   if (fmt & XTYPE_FMT_CUSTOM)
39     {
40       if (xt->format_fmt)
41         return (xt->format_fmt)(fmt, pool);
42     }
43   else if (fmt < ARRAY_SIZE(fmt_names))
44     return fmt_names[fmt];
45
46   return "";
47 }
48
49 int xtype_unit_parser(const char *str, const struct unit_definition *units)
50 {
51   for (int i=0; units[i].unit; i++)
52     if (!strcmp(str, units[i].unit))
53       return i;
54   return -1;
55 }