* UCW Library -- Basic Extended Types
*
* (c) 2014 Martin Mares <mj@ucw.cz>
+ * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
#include <ucw/xtypes.h>
#include <errno.h>
#include <stdlib.h>
+#include <inttypes.h>
#define XTYPE_NUM_FORMAT(_type, _fmt, _typename) static const char *xt_##_typename##_format(void *src, u32 fmt UNUSED, struct mempool *pool) \
{\
XTYPE_NUM_STRUCT(_type, _typename)
XTYPE_NUM_DEF(int, "%d", int)
-XTYPE_NUM_DEF(s64, "%ld", s64)
+XTYPE_NUM_DEF(s64, "%" PRId64, s64)
XTYPE_NUM_DEF(intmax_t, "%jd", intmax)
XTYPE_NUM_DEF(uint, "%u", uint)
-XTYPE_NUM_DEF(u64, "%lu", u64)
+XTYPE_NUM_DEF(u64, "%" PRIu64, u64)
XTYPE_NUM_DEF(uintmax_t, "%ju", uintmax)
/* double */
{
switch(fmt) {
case XTYPE_FMT_RAW:
- return mp_printf(pool, "%.2lf", *(double *)src);
+ return mp_printf(pool, "%.10lf", *(double *)src);
case XTYPE_FMT_PRETTY:
return mp_printf(pool, "%.2lf", *(double *)src);
-
case XTYPE_FMT_DEFAULT:
default:
- return mp_printf(pool, "%.2lf", *(double *)src);
+ return mp_printf(pool, "%.5lf", *(double *)src);
}
}
static const char *xt_double_parse(const char *str, void *dest, struct mempool *pool UNUSED)
{
char *endptr = NULL;
+ size_t sz = strlen(str);
errno = 0;
double result = strtod(str, &endptr);
- size_t sz = strlen(str);
- if(endptr != str + sz) return "Could not parse double.";
+ if(endptr != str + sz) return "Could not parse double";
if(errno == ERANGE) return "Could not parse double: overflow happend during parsing";
*((double *) dest) = result;
/* bool */
-static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *pool) // (struct table *tbl, int col, enum xtype_fmt fmt, bool val)
+static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *pool)
{
switch(fmt) {
case XTYPE_FMT_DEFAULT:
static const char *xt_bool_parse(const char *str, void *dest, struct mempool *pool UNUSED)
{
- if(str[1] == 0) {
+ if(str[1] == 0) { // FIXME: Possible segfault
if(str[0] == '1') {
*((bool *)dest) = false;
return NULL;
return NULL;
}
- return "Could not parse bool.";
+ return "Could not parse bool";
}
const struct xtype xt_bool = {
.format = xt_bool_format,
};
-
/* str */
-static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool) // (struct table *tbl, int col, enum xtype_fmt fmt, bool val)
+
+static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool)
{
- return mp_printf(pool, "%s", *((char **) src));
+ return mp_strdup(pool, (const char *) src);
}
static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool UNUSED)