From d9d65aa86be607bb2cbe83c7734bce558bc6744b Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Fri, 10 Apr 2015 17:45:35 +0200 Subject: [PATCH] base: use reference for ascii_to output argument Issue #1477 --- repos/base-linux/src/base/ipc/ipc.cc | 2 +- repos/base/include/util/arg_string.h | 2 +- repos/base/include/util/string.h | 61 ++++++++++++++----- repos/dde_linux/src/lib/libnl/lxcc_emul.cc | 8 ++- repos/dde_linux/src/lib/usb/nic/nic.cc | 2 +- repos/dde_linux/src/lib/wifi/lxcc_emul.cc | 2 +- repos/demo/src/lib/mini_c/atol.cc | 2 +- repos/demo/src/lib/mini_c/strtod.cc | 2 +- repos/demo/src/lib/mini_c/strtol.cc | 16 +++-- repos/gems/src/server/http_blk/http.cc | 4 +- .../gems/src/server/wm/decorator_nitpicker.h | 2 +- repos/os/include/nic/xml_node.h | 6 +- repos/os/include/util/color.h | 6 +- repos/os/include/util/xml_node.h | 4 +- repos/os/include/vfs/tar_file_system.h | 2 +- repos/os/src/app/cli_monitor/command_line.h | 2 +- .../app/cli_monitor/foc/arndale/extension.cc | 2 +- repos/os/src/drivers/nic/linux/main.cc | 2 +- repos/os/src/lib/net/ipv4.cc | 2 +- repos/os/src/server/tar_fs/record.h | 2 +- repos/os/src/server/tar_rom/main.cc | 4 +- repos/os/src/server/trace_fs/trace_files.h | 2 +- 22 files changed, 86 insertions(+), 51 deletions(-) diff --git a/repos/base-linux/src/base/ipc/ipc.cc b/repos/base-linux/src/base/ipc/ipc.cc index befe9f859f..7aba89e1bf 100644 --- a/repos/base-linux/src/base/ipc/ipc.cc +++ b/repos/base-linux/src/base/ipc/ipc.cc @@ -175,7 +175,7 @@ static int lookup_tid_by_client_socket(int sd) static Prefix_len prefix_len(name.sun_path); unsigned tid = 0; - if (Genode::ascii_to(name.sun_path + prefix_len.len, &tid) == 0) { + if (Genode::ascii_to(name.sun_path + prefix_len.len, tid) == 0) { PRAW("Error: could not parse tid number"); return -1; } diff --git a/repos/base/include/util/arg_string.h b/repos/base/include/util/arg_string.h index 8b70ca02c2..7def211d11 100644 --- a/repos/base/include/util/arg_string.h +++ b/repos/base/include/util/arg_string.h @@ -83,7 +83,7 @@ class Genode::Arg /* read numeric value and skip the corresponding tokens */ Number_of_bytes value; - size_t n = ascii_to(t.start(), &value); + size_t n = ascii_to(t.start(), value); if (n == 0) return false; diff --git a/repos/base/include/util/string.h b/repos/base/include/util/string.h index e6b749d8d1..472c4aac3e 100644 --- a/repos/base/include/util/string.h +++ b/repos/base/include/util/string.h @@ -27,7 +27,7 @@ namespace Genode { /** - * Wrapper of 'size_t' for selecting 'ascii_to' specialization + * Wrapper of 'size_t' for selecting the 'ascii_to' function to parse byte values */ class Genode::Number_of_bytes { @@ -266,8 +266,18 @@ namespace Genode { /** * Read unsigned long value from string + * + * \param s source string + * \param result destination variable + * \param base integer base + * \return number of consumed characters + * + * If the base argument is 0, the integer base is detected based on the + * characters in front of the number. If the number is prefixed with "0x", + * a base of 16 is used, otherwise a base of 10. */ - inline size_t ascii_to(const char *s, unsigned long *result, unsigned base = 0) + inline size_t ascii_to_unsigned_long(const char *s, unsigned long &result, + unsigned base) { unsigned long i = 0, value = 0; @@ -294,27 +304,42 @@ namespace Genode { value = value*base + d; } - *result = value; + result = value; return i; } /** - * Read unsigned int value from string + * Read unsigned long value from string + * + * \return number of consumed characters */ - inline size_t ascii_to(const char *s, unsigned int *result, unsigned base = 10) + inline size_t ascii_to(const char *s, unsigned long &result) + { + return ascii_to_unsigned_long(s, result, 0); + } + + + /** + * Read unsigned int value from string + * + * \return number of consumed characters + */ + inline size_t ascii_to(const char *s, unsigned int &result) { unsigned long result_long = 0; - size_t ret = ascii_to(s, &result_long, base); - *result = result_long; + size_t ret = ascii_to_unsigned_long(s, result_long, 0); + result = result_long; return ret; } /** * Read signed long value from string + * + * \return number of consumed characters */ - inline size_t ascii_to(const char *s, long *result, unsigned base = 10) + inline size_t ascii_to(const char *s, long &result) { int i = 0; @@ -326,11 +351,11 @@ namespace Genode { int j = 0; unsigned long value = 0; - j = ascii_to(s, &value, base); + j = ascii_to_unsigned_long(s, value, 10); if (!j) return i; - *result = sign*value; + result = sign*value; return i + j; } @@ -340,13 +365,15 @@ namespace Genode { * * This function scales the resulting size value according to the suffixes * for G (2^30), M (2^20), and K (2^10) if present. + * + * \return number of consumed characters */ - inline size_t ascii_to(const char *s, Number_of_bytes *result, unsigned base = 0) + inline size_t ascii_to(const char *s, Number_of_bytes &result) { unsigned long res = 0; /* convert numeric part of string */ - int i = ascii_to(s, &res, 0); + int i = ascii_to_unsigned_long(s, res, 0); /* handle suffixes */ if (i > 0) @@ -357,15 +384,17 @@ namespace Genode { default: break; } - *result = res; + result = res; return i; } /** * Read double float value from string + * + * \return number of consumed characters */ - inline size_t ascii_to(const char *s, double *result, unsigned base = 0) + inline size_t ascii_to(const char *s, double &result) { double v = 0.0; /* decimal part */ double d = 0.1; /* power of fractional digit */ @@ -383,7 +412,7 @@ namespace Genode { /* if no fractional part exists, return current value */ if (s[i] != '.') { - *result = neg ? -v : v; + result = neg ? -v : v; return i; } @@ -394,7 +423,7 @@ namespace Genode { for (; s[i] && is_digit(s[i]); i++, d *= 0.1) v += d*digit(s[i], false); - *result = neg ? -v : v; + result = neg ? -v : v; return i; } diff --git a/repos/dde_linux/src/lib/libnl/lxcc_emul.cc b/repos/dde_linux/src/lib/libnl/lxcc_emul.cc index 19bfc5b7e8..26e67de37a 100644 --- a/repos/dde_linux/src/lib/libnl/lxcc_emul.cc +++ b/repos/dde_linux/src/lib/libnl/lxcc_emul.cc @@ -142,7 +142,11 @@ char *getenv(const char *name) long int strtol(const char *nptr, char **endptr, int base) { long res = 0; - Genode::ascii_to(nptr, &res, base); + if (base != 0 && base != 10) { + PERR("strtol: base of %d is not supported", base); + return 0; + } + Genode::ascii_to(nptr, res); return res; } @@ -150,7 +154,7 @@ long int strtol(const char *nptr, char **endptr, int base) double strtod(const char *nptr, char **endptr) { double res = 0; - Genode::ascii_to(nptr, &res, 0); + Genode::ascii_to(nptr, res); return res; } diff --git a/repos/dde_linux/src/lib/usb/nic/nic.cc b/repos/dde_linux/src/lib/usb/nic/nic.cc index a39548e733..559a814bf2 100644 --- a/repos/dde_linux/src/lib/usb/nic/nic.cc +++ b/repos/dde_linux/src/lib/usb/nic/nic.cc @@ -15,9 +15,9 @@ #include #include #include -#include #include #include +#include #include #include diff --git a/repos/dde_linux/src/lib/wifi/lxcc_emul.cc b/repos/dde_linux/src/lib/wifi/lxcc_emul.cc index 51c6803935..8564c00500 100644 --- a/repos/dde_linux/src/lib/wifi/lxcc_emul.cc +++ b/repos/dde_linux/src/lib/wifi/lxcc_emul.cc @@ -1034,7 +1034,7 @@ int dev_set_name(struct device *dev, const char *fmt, ...) int strict_strtoul(const char *s, unsigned int base, unsigned long *res) { unsigned long r = -EINVAL; - Genode::ascii_to(s, &r, base); + Genode::ascii_to_unsigned_long(s, r, base); *res = r; return r; diff --git a/repos/demo/src/lib/mini_c/atol.cc b/repos/demo/src/lib/mini_c/atol.cc index 178baeae62..4e41b4ad0b 100644 --- a/repos/demo/src/lib/mini_c/atol.cc +++ b/repos/demo/src/lib/mini_c/atol.cc @@ -16,6 +16,6 @@ extern "C" long atol(const char *nptr) { long result = 0; - Genode::ascii_to(nptr, &result); + Genode::ascii_to(nptr, result); return result; } diff --git a/repos/demo/src/lib/mini_c/strtod.cc b/repos/demo/src/lib/mini_c/strtod.cc index de4fe8cb02..e50b72dad1 100644 --- a/repos/demo/src/lib/mini_c/strtod.cc +++ b/repos/demo/src/lib/mini_c/strtod.cc @@ -17,7 +17,7 @@ extern "C" double strtod(const char *nptr, char **endptr) { double value = 0; - int num_chars = Genode::ascii_to(nptr, &value); + int num_chars = Genode::ascii_to(nptr, value); if (endptr) *endptr = (char *)(nptr + num_chars); diff --git a/repos/demo/src/lib/mini_c/strtol.cc b/repos/demo/src/lib/mini_c/strtol.cc index 9b46d53e3c..41e4708e68 100644 --- a/repos/demo/src/lib/mini_c/strtol.cc +++ b/repos/demo/src/lib/mini_c/strtol.cc @@ -12,17 +12,21 @@ */ #include +#include -using namespace Genode; extern "C" long int strtol(const char *nptr, char **endptr, int base) { - long num_chars, result = 0; + using namespace Genode; - if (base == 0) - num_chars = ascii_to(nptr, &result); - else - num_chars = ascii_to(nptr, &result, base); + long result = 0; + + if (base != 0 && base != 10) { + PERR("strtol: base of %d not supported", base); + return 0; + } + + long const num_chars = ascii_to(nptr, result); if (endptr) *endptr = (char *)(nptr + num_chars); diff --git a/repos/gems/src/server/http_blk/http.cc b/repos/gems/src/server/http_blk/http.cc index 4328063fe5..b4419f2fea 100644 --- a/repos/gems/src/server/http_blk/http.cc +++ b/repos/gems/src/server/http_blk/http.cc @@ -127,7 +127,7 @@ Genode::size_t Http::read_header() continue; if (count) { - ascii_to(t.start(), &_http_ret); + ascii_to(t.start(), _http_ret); break; } @@ -154,7 +154,7 @@ void Http::get_capacity() } if (key) { - ascii_to(t.start(), &_size); + ascii_to(t.start(), _size); if (verbose) PDBG("File size: %zu bytes", _size); diff --git a/repos/gems/src/server/wm/decorator_nitpicker.h b/repos/gems/src/server/wm/decorator_nitpicker.h index 03b9d2a879..45b4977209 100644 --- a/repos/gems/src/server/wm/decorator_nitpicker.h +++ b/repos/gems/src/server/wm/decorator_nitpicker.h @@ -242,7 +242,7 @@ struct Wm::Decorator_nitpicker_session : Genode::Rpc_object case Command::OP_TITLE: { unsigned long id = 0; - Genode::ascii_to(cmd.title.title.string(), &id); + Genode::ascii_to(cmd.title.title.string(), id); if (id > 0) _content_registry.insert(cmd.title.view, diff --git a/repos/os/include/nic/xml_node.h b/repos/os/include/nic/xml_node.h index 95403e7a5f..ef07602970 100644 --- a/repos/os/include/nic/xml_node.h +++ b/repos/os/include/nic/xml_node.h @@ -23,9 +23,7 @@ namespace Genode { /** * Convert ASCII string to mac address */ - template <> - inline size_t ascii_to(char const *s, - Nic::Mac_address* mac, unsigned) + inline size_t ascii_to(char const *s, Nic::Mac_address &mac) { enum { HEX = true, @@ -47,7 +45,7 @@ namespace Genode { mac_str[i] = (digit(s[hi], HEX) << 4) | digit(s[lo], HEX); } - Genode::memcpy(mac->addr, mac_str, MAC_SIZE); + Genode::memcpy(mac.addr, mac_str, MAC_SIZE); return MAC_CHAR_LEN; } diff --git a/repos/os/include/util/color.h b/repos/os/include/util/color.h index 18a1c0afd3..82923e8d94 100644 --- a/repos/os/include/util/color.h +++ b/repos/os/include/util/color.h @@ -19,7 +19,7 @@ namespace Genode { struct Color; - inline size_t ascii_to(const char *, Color *, unsigned base = 0); + inline size_t ascii_to(const char *, Color &); } @@ -45,7 +45,7 @@ struct Genode::Color * \return number of consumed characters, or 0 if the string contains * no valid color */ -inline Genode::size_t Genode::ascii_to(const char *s, Genode::Color *result, unsigned) +inline Genode::size_t Genode::ascii_to(const char *s, Genode::Color &result) { /* validate string */ if (strlen(s) < 7 || *s != '#') return 0; @@ -59,7 +59,7 @@ inline Genode::size_t Genode::ascii_to(const char *s, Genode::Color *result, uns green = 16*digit(s[3], HEX) + digit(s[4], HEX), blue = 16*digit(s[5], HEX) + digit(s[6], HEX); - *result = Color(red, green, blue); + result = Color(red, green, blue); return 7; } diff --git a/repos/os/include/util/xml_node.h b/repos/os/include/util/xml_node.h index 1439a4df6b..a93ec9a42b 100644 --- a/repos/os/include/util/xml_node.h +++ b/repos/os/include/util/xml_node.h @@ -161,7 +161,7 @@ class Genode::Xml_attribute * the length, we have to consider both the starting * and the trailing quote character. */ - return ascii_to(_value.start() + 1, out) == _value.len() - 2; + return ascii_to(_value.start() + 1, *out) == _value.len() - 2; } /** @@ -557,7 +557,7 @@ class Genode::Xml_node */ template bool value(T *out) const { - return ascii_to(content_addr(), out) == content_size(); } + return ascii_to(content_addr(), *out) == content_size(); } /** * Return begin of node including the start tag diff --git a/repos/os/include/vfs/tar_file_system.h b/repos/os/include/vfs/tar_file_system.h index 8ecb4013d8..47ca6423e8 100644 --- a/repos/os/include/vfs/tar_file_system.h +++ b/repos/os/include/vfs/tar_file_system.h @@ -69,7 +69,7 @@ class Vfs::Tar_file_system : public File_system strncpy(buf, field, sizeof(buf)); unsigned long value = 0; - ascii_to(buf, &value, 8); + Genode::ascii_to_unsigned_long(buf, value, 8); return value; } diff --git a/repos/os/src/app/cli_monitor/command_line.h b/repos/os/src/app/cli_monitor/command_line.h index 53f3c761d8..e185202fe6 100644 --- a/repos/os/src/app/cli_monitor/command_line.h +++ b/repos/os/src/app/cli_monitor/command_line.h @@ -76,7 +76,7 @@ class Command_line bool parameter(char const *tag, T &result) { Token value = _value_token(tag); - return value && Genode::ascii_to(value.start(), &result) != 0; + return value && Genode::ascii_to(value.start(), result) != 0; } /** diff --git a/repos/os/src/app/cli_monitor/foc/arndale/extension.cc b/repos/os/src/app/cli_monitor/foc/arndale/extension.cc index 93bd4d8223..e3ff91dc92 100644 --- a/repos/os/src/app/cli_monitor/foc/arndale/extension.cc +++ b/repos/os/src/app/cli_monitor/foc/arndale/extension.cc @@ -38,7 +38,7 @@ struct Cpufreq_command : Command } unsigned long f = 0; - Genode::ascii_to(freq, &f, 10); + Genode::ascii_to(freq, f); tprintf(terminal, "set frequency to %ld Hz\n", f); regulator.level(f); } diff --git a/repos/os/src/drivers/nic/linux/main.cc b/repos/os/src/drivers/nic/linux/main.cc index 0db6e601b0..b7213de2f6 100644 --- a/repos/os/src/drivers/nic/linux/main.cc +++ b/repos/os/src/drivers/nic/linux/main.cc @@ -26,8 +26,8 @@ #include #include #include -#include #include +#include /* Linux */ #include diff --git a/repos/os/src/lib/net/ipv4.cc b/repos/os/src/lib/net/ipv4.cc index bf8982f03c..092ace537f 100644 --- a/repos/os/src/lib/net/ipv4.cc +++ b/repos/os/src/lib/net/ipv4.cc @@ -43,7 +43,7 @@ Ipv4_packet::Ipv4_address Ipv4_packet::ip_from_string(const char *ip) t.string(tmpstr, sizeof(tmpstr)); unsigned long tmpc = 0; - Genode::ascii_to(tmpstr, &tmpc, 10); + Genode::ascii_to(tmpstr, tmpc); ipb[cnt] = tmpc & 0xFF; t = t.next(); diff --git a/repos/os/src/server/tar_fs/record.h b/repos/os/src/server/tar_fs/record.h index b2b1556dc3..4384b70b36 100644 --- a/repos/os/src/server/tar_fs/record.h +++ b/repos/os/src/server/tar_fs/record.h @@ -47,7 +47,7 @@ namespace File_system { strncpy(buf, field, sizeof(buf)); unsigned long value = 0; - ascii_to(buf, &value, 8); + ascii_to_unsigned_long(buf, value, 8); return value; } diff --git a/repos/os/src/server/tar_rom/main.cc b/repos/os/src/server/tar_rom/main.cc index 045d6551f8..a81131037b 100755 --- a/repos/os/src/server/tar_rom/main.cc +++ b/repos/os/src/server/tar_rom/main.cc @@ -79,8 +79,8 @@ class Rom_session_component : public Genode::Rpc_object while (block_id < block_cnt) { unsigned long file_size = 0; - Genode::ascii_to(_tar_addr + block_id*_BLOCK_LEN + _FIELD_SIZE_LEN, - &file_size, 8); + Genode::ascii_to_unsigned_long(_tar_addr + block_id*_BLOCK_LEN + _FIELD_SIZE_LEN, + file_size, 8); /* get name of tar record */ char const *record_filename = _tar_addr + block_id*_BLOCK_LEN; diff --git a/repos/os/src/server/trace_fs/trace_files.h b/repos/os/src/server/trace_fs/trace_files.h index a23e544fd5..2397e06e4f 100644 --- a/repos/os/src/server/trace_fs/trace_files.h +++ b/repos/os/src/server/trace_fs/trace_files.h @@ -270,7 +270,7 @@ namespace File_system { /* account for \n when reading from the file */ _length += 1; - ascii_to(_content, &tmp, 10); + ascii_to(_content, tmp); _size = _check_size_limit(tmp); }