@end deftypefun
+@deftypefun void config_set_float_precision(@w{config_t *@var{config}}, @w{unsigned short @var{digits}})
+@deftypefunx {unsigned short} config_get_float_precision(@w{config_t *@var{config}})
+
+@b{Since @i{v1.6}}
+
+These functions set and get the number of decimal digits to output when writing
+the configuration to a file or stream.
+
+@code{config_set_float_precision(config,digits)} specifies "the number of
+digits to appear after the radix character". Please refer to printf(3) for details.
+
+Valid float precision range from 0 (no decimals) to about 15 (implementation defined).
+This parameter has no effect on parsing.
+
+@end deftypefun
+
@deftypefun void config_set_options (@w{config_t *@var{config}}, @w{int @var{options}})
@deftypefunx int config_get_options (@w{config_t *@var{config}})
@end deftypemethod
+@deftypemethod Config void setFloatPrecision (@w{unsigned short @var{width}})
+@deftypemethodx Config {unsigned short} getFloatPrecision ()
+
+These methods set and get the float precision for the configuration.
+This parameter influences the formatting of floating point settings in
+the configuration when it is written to a file or stream.
+Float precision has no effect on parsing.
+
+Valid precisions range from 0 to about 15 (implementation dependent),
+though the library will accept and store values up to 255.
+
+@end deftypemethod
+
@deftypemethod Config {Setting &} getRoot ()
This method returns the root setting for the configuration, which is a group.
#define CHUNK_SIZE 16
#define FLOAT_PRECISION DBL_DIG
+#define TAB2DIGITS(x) (((x) & 0xFF00) >> 8)
+#define DIGITS2TAB(x) (((x) & 0xFF) << 8)
+
#define _new(T) (T *)calloc(1, sizeof(T)) /* zeroed */
#define _delete(P) free((void *)(P))
{
char *q;
- snprintf(fbuf, sizeof(fbuf) - 3, "%.*g", FLOAT_PRECISION, value->fval);
+ snprintf(fbuf, sizeof(fbuf) - 3, "%.*f",
+ TAB2DIGITS(config->tab_width), /* XXX: hack; See config_set_float_precision */
+ value->fval
+ );
/* check for exponent */
q = strchr(fbuf, 'e');
}
/* ------------------------------------------------------------------------- */
+void config_set_float_precision(config_t *config,
+ unsigned short digits)
+{
+ config->tab_width |= DIGITS2TAB(digits);
+}
+
+unsigned short config_get_float_precision(config_t *config)
+{
+ return TAB2DIGITS(config->tab_width);
+}
+
+/* ------------------------------------------------------------------------- */
void config_init(config_t *config)
{
config->options = (CONFIG_OPTION_SEMICOLON_SEPARATORS
| CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS
| CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE);
+
+ /* @version 1.6: piggyback float_digits on top of tab_width
+ * (ab)using the existing macros' 0x0F mask in order to preserve
+ * API & ABI compatibility ; changes are fully backwards compatible
+ */
config->tab_width = 2;
+ config->tab_width |= DIGITS2TAB(FLOAT_PRECISION); /* float_digits */
}
/* ------------------------------------------------------------------------- */
extern LIBCONFIG_API void config_set_include_dir(config_t *config,
const char *include_dir);
+extern LIBCONFIG_API void config_set_float_precision(config_t *config,
+ unsigned short digits);
+extern LIBCONFIG_API unsigned short config_get_float_precision(config_t *config);
+
extern LIBCONFIG_API void config_init(config_t *config);
extern LIBCONFIG_API void config_destroy(config_t *config);
(C)->tab_width = ((W) & 0x0F)
#define /* unsigned char */ config_get_tab_width(/* const config_t * */ C) \
- ((C)->tab_width)
+ ((C)->tab_width & 0x0F)
#define /* unsigned short */ config_setting_source_line( \
/* const config_setting_t * */ S) \
void setTabWidth(unsigned short width);
unsigned short getTabWidth() const;
+ void setFloatPrecision(unsigned short digits);
+ unsigned short getFloatPrecision() const;
+
void setIncludeDir(const char *includeDir);
const char *getIncludeDir() const;
// ---------------------------------------------------------------------------
+void Config::setFloatPrecision(unsigned short digits)
+{
+ return (config_set_float_precision(_config,digits));
+}
+
+// ---------------------------------------------------------------------------
+
+unsigned short Config::getFloatPrecision() const
+{
+ return (config_get_float_precision(_config));
+}
+
+// ---------------------------------------------------------------------------
+
void Config::setIncludeDir(const char *includeDir)
{
config_set_include_dir(_config, includeDir);