base: use reference for ascii_to output argument

Issue #1477
This commit is contained in:
Norman Feske 2015-04-10 17:45:35 +02:00 committed by Christian Helmuth
parent d884cf1a9a
commit d9d65aa86b
22 changed files with 86 additions and 51 deletions

View File

@ -175,7 +175,7 @@ static int lookup_tid_by_client_socket(int sd)
static Prefix_len prefix_len(name.sun_path); static Prefix_len prefix_len(name.sun_path);
unsigned tid = 0; 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"); PRAW("Error: could not parse tid number");
return -1; return -1;
} }

View File

@ -83,7 +83,7 @@ class Genode::Arg
/* read numeric value and skip the corresponding tokens */ /* read numeric value and skip the corresponding tokens */
Number_of_bytes value; Number_of_bytes value;
size_t n = ascii_to(t.start(), &value); size_t n = ascii_to(t.start(), value);
if (n == 0) if (n == 0)
return false; return false;

View File

@ -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 class Genode::Number_of_bytes
{ {
@ -266,8 +266,18 @@ namespace Genode {
/** /**
* Read unsigned long value from string * 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; unsigned long i = 0, value = 0;
@ -294,27 +304,42 @@ namespace Genode {
value = value*base + d; value = value*base + d;
} }
*result = value; result = value;
return i; 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; unsigned long result_long = 0;
size_t ret = ascii_to(s, &result_long, base); size_t ret = ascii_to_unsigned_long(s, result_long, 0);
*result = result_long; result = result_long;
return ret; return ret;
} }
/** /**
* Read signed long value from string * 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; int i = 0;
@ -326,11 +351,11 @@ namespace Genode {
int j = 0; int j = 0;
unsigned long value = 0; unsigned long value = 0;
j = ascii_to(s, &value, base); j = ascii_to_unsigned_long(s, value, 10);
if (!j) return i; if (!j) return i;
*result = sign*value; result = sign*value;
return i + j; return i + j;
} }
@ -340,13 +365,15 @@ namespace Genode {
* *
* This function scales the resulting size value according to the suffixes * This function scales the resulting size value according to the suffixes
* for G (2^30), M (2^20), and K (2^10) if present. * 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; unsigned long res = 0;
/* convert numeric part of string */ /* convert numeric part of string */
int i = ascii_to(s, &res, 0); int i = ascii_to_unsigned_long(s, res, 0);
/* handle suffixes */ /* handle suffixes */
if (i > 0) if (i > 0)
@ -357,15 +384,17 @@ namespace Genode {
default: break; default: break;
} }
*result = res; result = res;
return i; return i;
} }
/** /**
* Read double float value from string * 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 v = 0.0; /* decimal part */
double d = 0.1; /* power of fractional digit */ double d = 0.1; /* power of fractional digit */
@ -383,7 +412,7 @@ namespace Genode {
/* if no fractional part exists, return current value */ /* if no fractional part exists, return current value */
if (s[i] != '.') { if (s[i] != '.') {
*result = neg ? -v : v; result = neg ? -v : v;
return i; return i;
} }
@ -394,7 +423,7 @@ namespace Genode {
for (; s[i] && is_digit(s[i]); i++, d *= 0.1) for (; s[i] && is_digit(s[i]); i++, d *= 0.1)
v += d*digit(s[i], false); v += d*digit(s[i], false);
*result = neg ? -v : v; result = neg ? -v : v;
return i; return i;
} }

View File

@ -142,7 +142,11 @@ char *getenv(const char *name)
long int strtol(const char *nptr, char **endptr, int base) long int strtol(const char *nptr, char **endptr, int base)
{ {
long res = 0; long res = 0;
Genode::ascii_to<long>(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; return res;
} }
@ -150,7 +154,7 @@ long int strtol(const char *nptr, char **endptr, int base)
double strtod(const char *nptr, char **endptr) double strtod(const char *nptr, char **endptr)
{ {
double res = 0; double res = 0;
Genode::ascii_to<double>(nptr, &res, 0); Genode::ascii_to(nptr, res);
return res; return res;
} }

View File

@ -15,9 +15,9 @@
#include <base/snprintf.h> #include <base/snprintf.h>
#include <nic_session/nic_session.h> #include <nic_session/nic_session.h>
#include <cap_session/connection.h> #include <cap_session/connection.h>
#include <os/config.h>
#include <nic/xml_node.h> #include <nic/xml_node.h>
#include <util/xml_node.h> #include <util/xml_node.h>
#include <os/config.h>
#include <extern_c_begin.h> #include <extern_c_begin.h>
#include <lx_emul.h> #include <lx_emul.h>

View File

@ -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) int strict_strtoul(const char *s, unsigned int base, unsigned long *res)
{ {
unsigned long r = -EINVAL; unsigned long r = -EINVAL;
Genode::ascii_to<unsigned long>(s, &r, base); Genode::ascii_to_unsigned_long(s, r, base);
*res = r; *res = r;
return r; return r;

View File

@ -16,6 +16,6 @@
extern "C" long atol(const char *nptr) extern "C" long atol(const char *nptr)
{ {
long result = 0; long result = 0;
Genode::ascii_to(nptr, &result); Genode::ascii_to(nptr, result);
return result; return result;
} }

View File

@ -17,7 +17,7 @@ extern "C" double strtod(const char *nptr, char **endptr)
{ {
double value = 0; double value = 0;
int num_chars = Genode::ascii_to(nptr, &value); int num_chars = Genode::ascii_to(nptr, value);
if (endptr) if (endptr)
*endptr = (char *)(nptr + num_chars); *endptr = (char *)(nptr + num_chars);

View File

@ -12,17 +12,21 @@
*/ */
#include <util/string.h> #include <util/string.h>
#include <base/printf.h>
using namespace Genode;
extern "C" long int strtol(const char *nptr, char **endptr, int base) extern "C" long int strtol(const char *nptr, char **endptr, int base)
{ {
long num_chars, result = 0; using namespace Genode;
if (base == 0) long result = 0;
num_chars = ascii_to(nptr, &result);
else if (base != 0 && base != 10) {
num_chars = ascii_to(nptr, &result, base); PERR("strtol: base of %d not supported", base);
return 0;
}
long const num_chars = ascii_to(nptr, result);
if (endptr) if (endptr)
*endptr = (char *)(nptr + num_chars); *endptr = (char *)(nptr + num_chars);

View File

@ -127,7 +127,7 @@ Genode::size_t Http::read_header()
continue; continue;
if (count) { if (count) {
ascii_to(t.start(), &_http_ret); ascii_to(t.start(), _http_ret);
break; break;
} }
@ -154,7 +154,7 @@ void Http::get_capacity()
} }
if (key) { if (key) {
ascii_to(t.start(), &_size); ascii_to(t.start(), _size);
if (verbose) if (verbose)
PDBG("File size: %zu bytes", _size); PDBG("File size: %zu bytes", _size);

View File

@ -242,7 +242,7 @@ struct Wm::Decorator_nitpicker_session : Genode::Rpc_object<Nitpicker::Session>
case Command::OP_TITLE: case Command::OP_TITLE:
{ {
unsigned long id = 0; unsigned long id = 0;
Genode::ascii_to(cmd.title.title.string(), &id); Genode::ascii_to(cmd.title.title.string(), id);
if (id > 0) if (id > 0)
_content_registry.insert(cmd.title.view, _content_registry.insert(cmd.title.view,

View File

@ -23,9 +23,7 @@ namespace Genode {
/** /**
* Convert ASCII string to mac address * Convert ASCII string to mac address
*/ */
template <> inline size_t ascii_to(char const *s, Nic::Mac_address &mac)
inline size_t ascii_to<Nic::Mac_address>(char const *s,
Nic::Mac_address* mac, unsigned)
{ {
enum { enum {
HEX = true, HEX = true,
@ -47,7 +45,7 @@ namespace Genode {
mac_str[i] = (digit(s[hi], HEX) << 4) | digit(s[lo], HEX); 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; return MAC_CHAR_LEN;
} }

View File

@ -19,7 +19,7 @@
namespace Genode { namespace Genode {
struct Color; 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 * \return number of consumed characters, or 0 if the string contains
* no valid color * 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 */ /* validate string */
if (strlen(s) < 7 || *s != '#') return 0; 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), green = 16*digit(s[3], HEX) + digit(s[4], HEX),
blue = 16*digit(s[5], HEX) + digit(s[6], HEX); blue = 16*digit(s[5], HEX) + digit(s[6], HEX);
*result = Color(red, green, blue); result = Color(red, green, blue);
return 7; return 7;
} }

View File

@ -161,7 +161,7 @@ class Genode::Xml_attribute
* the length, we have to consider both the starting * the length, we have to consider both the starting
* and the trailing quote character. * 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 <typename T> template <typename T>
bool value(T *out) const { 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 * Return begin of node including the start tag

View File

@ -69,7 +69,7 @@ class Vfs::Tar_file_system : public File_system
strncpy(buf, field, sizeof(buf)); strncpy(buf, field, sizeof(buf));
unsigned long value = 0; unsigned long value = 0;
ascii_to(buf, &value, 8); Genode::ascii_to_unsigned_long(buf, value, 8);
return value; return value;
} }

View File

@ -76,7 +76,7 @@ class Command_line
bool parameter(char const *tag, T &result) bool parameter(char const *tag, T &result)
{ {
Token value = _value_token(tag); Token value = _value_token(tag);
return value && Genode::ascii_to(value.start(), &result) != 0; return value && Genode::ascii_to(value.start(), result) != 0;
} }
/** /**

View File

@ -38,7 +38,7 @@ struct Cpufreq_command : Command
} }
unsigned long f = 0; unsigned long f = 0;
Genode::ascii_to(freq, &f, 10); Genode::ascii_to(freq, f);
tprintf(terminal, "set frequency to %ld Hz\n", f); tprintf(terminal, "set frequency to %ld Hz\n", f);
regulator.level(f); regulator.level(f);
} }

View File

@ -26,8 +26,8 @@
#include <base/sleep.h> #include <base/sleep.h>
#include <cap_session/connection.h> #include <cap_session/connection.h>
#include <nic/component.h> #include <nic/component.h>
#include <os/config.h>
#include <nic/xml_node.h> #include <nic/xml_node.h>
#include <os/config.h>
/* Linux */ /* Linux */
#include <errno.h> #include <errno.h>

View File

@ -43,7 +43,7 @@ Ipv4_packet::Ipv4_address Ipv4_packet::ip_from_string(const char *ip)
t.string(tmpstr, sizeof(tmpstr)); t.string(tmpstr, sizeof(tmpstr));
unsigned long tmpc = 0; unsigned long tmpc = 0;
Genode::ascii_to(tmpstr, &tmpc, 10); Genode::ascii_to(tmpstr, tmpc);
ipb[cnt] = tmpc & 0xFF; ipb[cnt] = tmpc & 0xFF;
t = t.next(); t = t.next();

View File

@ -47,7 +47,7 @@ namespace File_system {
strncpy(buf, field, sizeof(buf)); strncpy(buf, field, sizeof(buf));
unsigned long value = 0; unsigned long value = 0;
ascii_to(buf, &value, 8); ascii_to_unsigned_long(buf, value, 8);
return value; return value;
} }

View File

@ -79,8 +79,8 @@ class Rom_session_component : public Genode::Rpc_object<Genode::Rom_session>
while (block_id < block_cnt) { while (block_id < block_cnt) {
unsigned long file_size = 0; unsigned long file_size = 0;
Genode::ascii_to(_tar_addr + block_id*_BLOCK_LEN + _FIELD_SIZE_LEN, Genode::ascii_to_unsigned_long(_tar_addr + block_id*_BLOCK_LEN + _FIELD_SIZE_LEN,
&file_size, 8); file_size, 8);
/* get name of tar record */ /* get name of tar record */
char const *record_filename = _tar_addr + block_id*_BLOCK_LEN; char const *record_filename = _tar_addr + block_id*_BLOCK_LEN;

View File

@ -270,7 +270,7 @@ namespace File_system {
/* account for \n when reading from the file */ /* account for \n when reading from the file */
_length += 1; _length += 1;
ascii_to(_content, &tmp, 10); ascii_to(_content, tmp);
_size = _check_size_limit(tmp); _size = _check_size_limit(tmp);
} }