From 5e15341857e57671a3c617579e3d5dcc89040936 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Fri, 10 Jan 2025 22:06:06 +0100 Subject: Print the float numbers using the user locale --- lib/csv/dataType.ml | 4 +++- lib/csv/dune | 1 + lib/csv/format.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 lib/csv/format.c (limited to 'lib/csv') diff --git a/lib/csv/dataType.ml b/lib/csv/dataType.ml index c582b9c..1d2c7f9 100644 --- a/lib/csv/dataType.ml +++ b/lib/csv/dataType.ml @@ -1,3 +1,5 @@ +external show_float : float -> string = "show_float" + let match_date = Re.Str.regexp {|[0-9]+/[0-9]+/[0-9]+|} type t = @@ -11,7 +13,7 @@ let to_string = function | Null -> "" | Error s -> s | Integer i -> string_of_int i - | Float f -> string_of_float f + | Float f -> show_float f | Content c -> ( match String.starts_with ~prefix:"0" c with | false -> c diff --git a/lib/csv/dune b/lib/csv/dune index b0f4a72..2cdc868 100755 --- a/lib/csv/dune +++ b/lib/csv/dune @@ -3,4 +3,5 @@ (libraries re ) + (foreign_stubs (language c) (names format)) ) 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 +#include +#include +#include + +#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 ); +} -- cgit v1.2.3