X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=util.cc;h=38f1f1dfa6357c43d8c44d8e915ac17528ca89c9;hb=f1f817df56ce99ea544569635698df55d72bd1ee;hp=9e985af2af2b3d23189f30e9d23d46597f872485;hpb=4bda3653b0b3a270acdf643cf4918a97dbf147c5;p=cumulus.git diff --git a/util.cc b/util.cc index 9e985af..38f1f1d 100644 --- a/util.cc +++ b/util.cc @@ -27,7 +27,7 @@ string uri_encode(const string &in) for (size_t i = 0; i < in.length(); i++) { unsigned char c = in[i]; - if (c >= '+' && c < 0x7f) { + if (c >= '+' && c < 0x7f && c != '@') { out += c; } else { char buf[4]; @@ -67,30 +67,34 @@ string uri_decode(const string &in) *output = '\0'; string result(buf); - delete buf; + delete[] buf; return result; } -/* Return the string representation of an integer. */ -string encode_int(long long n) +/* Return the string representation of an integer. Will try to produce output + * in decimal, hexadecimal, or octal according to base, though this is just + * advisory. For negative numbers, will always use decimal. */ +string encode_int(long long n, int base) { char buf[64]; + + if (n >= 0 && base == 16) { + sprintf(buf, "0x%llx", n); + return buf; + } + + if (n > 0 && base == 8) { + sprintf(buf, "0%llo", n); + return buf; + } + sprintf(buf, "%lld", n); return buf; } -/* Return the string representation of an integer. */ +/* Parse the string representation of an integer. Accepts decimal, octal, and + * hexadecimal, just as C would (recognizes the 0 and 0x prefixes). */ long long parse_int(const string &s) { - return strtoll(s.c_str(), NULL, 10); -} - -/* Output a dictionary of string key/value pairs to the given output stream. - * The format is a sequence of lines of the form "key: value". */ -void dict_output(ostream &o, map dict) -{ - for (map::const_iterator i = dict.begin(); - i != dict.end(); ++i) { - o << i->first << ": " << i->second << "\n"; - } + return strtoll(s.c_str(), NULL, 0); }