From 87c6a5ed4ec515c2e154f98529c1d2f077d2a7e4 Mon Sep 17 00:00:00 2001 From: Michael Vrable Date: Fri, 20 Jul 2007 10:05:06 -0700 Subject: [PATCH] Add support for octal and hexadecimal output in dumps. Switch to using octal for file modesin metadata listings, since this is more readable. --- scandir.cc | 2 +- util.cc | 22 ++++++++++++++++++---- util.h | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/scandir.cc b/scandir.cc index 4ffd078..f5ab2b6 100644 --- a/scandir.cc +++ b/scandir.cc @@ -280,7 +280,7 @@ void dump_inode(const string& path, // Path within snapshot printf("%s\n", path.c_str()); - file_info["mode"] = encode_int(stat_buf.st_mode & 07777); + file_info["mode"] = encode_int(stat_buf.st_mode & 07777, 8); file_info["mtime"] = encode_int(stat_buf.st_mtime); file_info["user"] = encode_int(stat_buf.st_uid); file_info["group"] = encode_int(stat_buf.st_gid); diff --git a/util.cc b/util.cc index 9e985af..c1b5f41 100644 --- a/util.cc +++ b/util.cc @@ -71,18 +71,32 @@ string uri_decode(const string &in) 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); + return strtoll(s.c_str(), NULL, 0); } /* Output a dictionary of string key/value pairs to the given output stream. diff --git a/util.h b/util.h index 7cb9bf4..89ceac7 100644 --- a/util.h +++ b/util.h @@ -14,7 +14,7 @@ std::string uri_encode(const std::string &in); std::string uri_decode(const std::string &in); -std::string encode_int(long long n); +std::string encode_int(long long n, int base=10); void dict_output(std::ostream &o, std::map dict); long long parse_int(const std::string &s); -- 2.20.1