diff options
Diffstat (limited to 'lib/csv/format.c')
-rw-r--r-- | lib/csv/format.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/csv/format.c b/lib/csv/format.c new file mode 100644 index 0000000..31e4bbe --- /dev/null +++ b/lib/csv/format.c @@ -0,0 +1,49 @@ +#include <stdio.h> +#include <locale.h> +#include <caml/memory.h> +#include <caml/alloc.h> + +#ifndef _vscprintf +/* For some reason, MSVC fails to honour this #ifndef. */ +/* Hence function renamed to _vscprintf_so(). */ +int _vscprintf_so(const char * format, va_list pargs) { + int retval; + va_list argcopy; + va_copy(argcopy, pargs); + retval = vsnprintf(NULL, 0, format, argcopy); + va_end(argcopy); + return retval;} +#endif // _vscprintf + +#ifndef vasprintf +int vasprintf(char **strp, const char *fmt, va_list ap) { + int len = _vscprintf_so(fmt, ap); + if (len == -1) return -1; + char *str = malloc((size_t) len + 1); + if (!str) return -1; + int r = vsnprintf(str, len + 1, fmt, ap); /* "secure" version of vsprintf */ + if (r == -1) return free(str), -1; + *strp = str; + return r;} +#endif // vasprintf + +#ifndef asprintf +int asprintf(char *strp[], const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + int r = vasprintf(strp, fmt, ap); + va_end(ap); + return r;} +#endif // asprintf + +CAMLprim value show_float( value float_param ) +{ + CAMLparam1( float_param ); + CAMLlocal1( ml_data ); + double f = Double_val(float_param); + char* raw_data; + int data_len = asprintf(&raw_data, "%f", f); + ml_data = caml_copy_string( raw_data ); + free(raw_data); + CAMLreturn( ml_data ); +} |