diff options
author | Jedzkie <jedzkie13@rocketmail.com> | 2020-06-23 08:00:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-23 08:00:00 +0800 |
commit | 520863eaeda83da14b88d09f6378cd8abd5a5174 (patch) | |
tree | 2496ea36744dc2f51d264741a78699e719ceeb3b /3rdparty/libconfig/libconfig.c | |
parent | 0b7768837bed2aa521de5b9b76b2348943d7db20 (diff) | |
parent | 9b89425550094f51d633e5b5d6e86af65bffbf39 (diff) | |
download | hercules-520863eaeda83da14b88d09f6378cd8abd5a5174.tar.gz hercules-520863eaeda83da14b88d09f6378cd8abd5a5174.tar.bz2 hercules-520863eaeda83da14b88d09f6378cd8abd5a5174.tar.xz hercules-520863eaeda83da14b88d09f6378cd8abd5a5174.zip |
Merge branch 'master' into jedzkie-pr03
Diffstat (limited to '3rdparty/libconfig/libconfig.c')
-rw-r--r-- | 3rdparty/libconfig/libconfig.c | 58 |
1 files changed, 53 insertions, 5 deletions
diff --git a/3rdparty/libconfig/libconfig.c b/3rdparty/libconfig/libconfig.c index 533145ac4..daf3ba42d 100644 --- a/3rdparty/libconfig/libconfig.c +++ b/3rdparty/libconfig/libconfig.c @@ -174,6 +174,31 @@ static void __config_indent(FILE *stream, int depth, unsigned short w) /* ------------------------------------------------------------------------- */ +/** + * converts an integer to a binary string (because itoa is not in C99) + * skips leading zeros, and does not prepend with a 0b prefix + * + * @param value - the integer to convert to a binary expression + * @param buffer - a pointer to the buffer used to build the string + * @returns the same buffer + */ +char *int_tobin(const unsigned long long value, char *buffer) { + const unsigned long long mask = 1; + unsigned char nonzero = 0; + + for (char bit = 63; bit >= 0; --bit) { + if ((value & (mask << bit)) == 1) { + nonzero = 1; + *buffer++ = '1'; + } else if (nonzero != 0) { + *buffer++ = '0'; + } + } + + *buffer = '\0'; + return buffer; +} + static void __config_write_value(const struct config_t *config, const union config_value_t *value, int type, int format, int depth, FILE *stream) @@ -195,6 +220,17 @@ static void __config_write_value(const struct config_t *config, fprintf(stream, "0x%X", (unsigned int)(value->ival)); break; + case CONFIG_FORMAT_BIN: + { + char buffer[33]; // 32 individual bits (char '0' and '1') represented as 32 bytes + null + fprintf(stream, "0b%s", int_tobin((unsigned int)(value->ival), buffer)); + break; + } + + case CONFIG_FORMAT_OCT: + fprintf(stream, "0o%o", (unsigned int)(value->ival)); + break; + case CONFIG_FORMAT_DEFAULT: default: fprintf(stream, "%d", value->ival); @@ -210,6 +246,17 @@ static void __config_write_value(const struct config_t *config, fprintf(stream, "0x" INT64_HEX_FMT "L", (unsigned long long)(value->llval)); break; + case CONFIG_FORMAT_BIN: + { + char buffer[65]; // 64 individual bits (char '0' and '1') represented as 64 bytes + null + fprintf(stream, "0b%s", int_tobin((unsigned long long)(value->llval), buffer)); + break; + } + + case CONFIG_FORMAT_OCT: + fprintf(stream, "0o" INT64_OCT_FMT "L", (unsigned long long)(value->llval)); + break; + case CONFIG_FORMAT_DEFAULT: default: fprintf(stream, INT64_FMT "L", value->llval); @@ -1174,14 +1221,15 @@ int config_setting_set_string(struct config_setting_t *setting, const char *valu int config_setting_set_format(struct config_setting_t *setting, short format) { - if(((setting->type != CONFIG_TYPE_INT) - && (setting->type != CONFIG_TYPE_INT64)) - || ((format != CONFIG_FORMAT_DEFAULT) && (format != CONFIG_FORMAT_HEX))) - return(CONFIG_FALSE); + if (((setting->type != CONFIG_TYPE_INT) && (setting->type != CONFIG_TYPE_INT64)) + || ((format != CONFIG_FORMAT_DEFAULT) && (format != CONFIG_FORMAT_HEX) + && (format != CONFIG_FORMAT_BIN) && (format != CONFIG_FORMAT_OCT))) { + return CONFIG_FALSE; + } setting->format = format; - return(CONFIG_TRUE); + return CONFIG_TRUE; } /* ------------------------------------------------------------------------- */ |