2 * UCW Library -- Extended Types -- Generic Operations
4 * (c) 2014 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/xtypes.h>
15 static const char * const fmt_names[] = {
16 [XTYPE_FMT_DEFAULT] = "default",
17 [XTYPE_FMT_RAW] = "raw",
18 [XTYPE_FMT_PRETTY] = "pretty",
21 const char *xtype_parse_fmt(const struct xtype *xt, const char *str, u32 *dest, struct mempool *pool)
23 for (uint i=0; i < ARRAY_SIZE(fmt_names); i++)
24 if (!strcmp(str, fmt_names[i]))
30 if (xt && xt->parse_fmt)
31 return (xt->parse_fmt)(str, dest, pool);
33 return "Unknown mode";
36 const char *xtype_format_fmt(struct xtype *xt, u32 fmt, struct mempool *pool)
38 if (fmt & XTYPE_FMT_CUSTOM)
41 return (xt->format_fmt)(fmt, pool);
43 else if (fmt < ARRAY_SIZE(fmt_names))
44 return fmt_names[fmt];
49 int xtype_unit_parser(const char *str, const struct unit_definition *units)
51 for (int i=0; units[i].unit; i++)
52 if (!strcmp(str, units[i].unit))