From: Michael Vrable Date: Wed, 18 Jul 2007 21:31:32 +0000 (-0700) Subject: Rename format.{cc,h} -> util.{cc,h}. X-Git-Url: http://git.vrable.net/?p=cumulus.git;a=commitdiff_plain;h=4bda3653b0b3a270acdf643cf4918a97dbf147c5 Rename format.{cc,h} -> util.{cc,h}. --- diff --git a/Makefile b/Makefile index e947b47..965d277 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CXXFLAGS=-O -Wall -D_FILE_OFFSET_BITS=64 $(DEBUG) \ `pkg-config --cflags $(PACKAGES)` -DLBS_VERSION=`cat version` LDFLAGS=$(DEBUG) -ltar `pkg-config --libs $(PACKAGES)` -SRCS=format.cc localdb.cc ref.cc scandir.cc sha1.cc statcache.cc store.cc +SRCS=localdb.cc ref.cc scandir.cc sha1.cc statcache.cc store.cc util.cc OBJS=$(SRCS:.cc=.o) lbs : $(OBJS) diff --git a/format.cc b/format.cc deleted file mode 100644 index 9e985af..0000000 --- a/format.cc +++ /dev/null @@ -1,96 +0,0 @@ -/* LBS: An LFS-inspired filesystem backup system - * Copyright (C) 2007 Michael Vrable - * - * Utility functions for converting various datatypes to text format (and - * later, for parsing them back, perhaps). - */ - -#include -#include - -#include -#include -#include -#include - -using std::map; -using std::ostream; -using std::string; - -/* Perform URI-style escaping of a string. Bytes which cannot be represented - * directly are encoded in the form %xx (where "xx" is a string of two - * hexadecimal digits). */ -string uri_encode(const string &in) -{ - string out; - - for (size_t i = 0; i < in.length(); i++) { - unsigned char c = in[i]; - - if (c >= '+' && c < 0x7f) { - out += c; - } else { - char buf[4]; - sprintf(buf, "%%%02x", c); - out += buf; - } - } - - return out; -} - -/* Decoding of strings produced by uri_encode. */ -string uri_decode(const string &in) -{ - char *buf = new char[in.size() + 1]; - - const char *input = in.c_str(); - char *output = buf; - - while (*input != '\0') { - if (*input == '%') { - char hexbuf[4]; - if (isxdigit(input[1]) && isxdigit(input[2])) { - hexbuf[0] = input[1]; - hexbuf[1] = input[2]; - hexbuf[2] = '\0'; - *output++ = strtol(hexbuf, NULL, 16); - input += 3; - } else { - input++; - } - } else { - *output++ = *input++; - } - } - - *output = '\0'; - - string result(buf); - delete buf; - return result; -} - -/* Return the string representation of an integer. */ -string encode_int(long long n) -{ - char buf[64]; - sprintf(buf, "%lld", n); - return buf; -} - -/* Return the string representation of an integer. */ -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"; - } -} diff --git a/format.h b/format.h deleted file mode 100644 index 7cb9bf4..0000000 --- a/format.h +++ /dev/null @@ -1,22 +0,0 @@ -/* LBS: An LFS-inspired filesystem backup system - * Copyright (C) 2006 Michael Vrable - * - * Utility functions for converting various datatypes to text format (and - * later, for parsing them back, perhaps). - */ - -#ifndef _LBS_FORMAT_H -#define _LBS_FORMAT_H - -#include -#include -#include - -std::string uri_encode(const std::string &in); -std::string uri_decode(const std::string &in); -std::string encode_int(long long n); -void dict_output(std::ostream &o, std::map dict); - -long long parse_int(const std::string &s); - -#endif // _LBS_TARSTORE_H diff --git a/scandir.cc b/scandir.cc index 5b3caaa..d398eee 100644 --- a/scandir.cc +++ b/scandir.cc @@ -23,11 +23,11 @@ #include #include -#include "format.h" #include "localdb.h" #include "store.h" #include "sha1.h" #include "statcache.h" +#include "util.h" using std::list; using std::string; diff --git a/statcache.cc b/statcache.cc index 5a8d26c..86886c0 100644 --- a/statcache.cc +++ b/statcache.cc @@ -30,9 +30,9 @@ #include #include -#include "format.h" #include "ref.h" #include "statcache.h" +#include "util.h" using std::list; using std::map; diff --git a/util.cc b/util.cc new file mode 100644 index 0000000..9e985af --- /dev/null +++ b/util.cc @@ -0,0 +1,96 @@ +/* LBS: An LFS-inspired filesystem backup system + * Copyright (C) 2007 Michael Vrable + * + * Utility functions for converting various datatypes to text format (and + * later, for parsing them back, perhaps). + */ + +#include +#include + +#include +#include +#include +#include + +using std::map; +using std::ostream; +using std::string; + +/* Perform URI-style escaping of a string. Bytes which cannot be represented + * directly are encoded in the form %xx (where "xx" is a string of two + * hexadecimal digits). */ +string uri_encode(const string &in) +{ + string out; + + for (size_t i = 0; i < in.length(); i++) { + unsigned char c = in[i]; + + if (c >= '+' && c < 0x7f) { + out += c; + } else { + char buf[4]; + sprintf(buf, "%%%02x", c); + out += buf; + } + } + + return out; +} + +/* Decoding of strings produced by uri_encode. */ +string uri_decode(const string &in) +{ + char *buf = new char[in.size() + 1]; + + const char *input = in.c_str(); + char *output = buf; + + while (*input != '\0') { + if (*input == '%') { + char hexbuf[4]; + if (isxdigit(input[1]) && isxdigit(input[2])) { + hexbuf[0] = input[1]; + hexbuf[1] = input[2]; + hexbuf[2] = '\0'; + *output++ = strtol(hexbuf, NULL, 16); + input += 3; + } else { + input++; + } + } else { + *output++ = *input++; + } + } + + *output = '\0'; + + string result(buf); + delete buf; + return result; +} + +/* Return the string representation of an integer. */ +string encode_int(long long n) +{ + char buf[64]; + sprintf(buf, "%lld", n); + return buf; +} + +/* Return the string representation of an integer. */ +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"; + } +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..7cb9bf4 --- /dev/null +++ b/util.h @@ -0,0 +1,22 @@ +/* LBS: An LFS-inspired filesystem backup system + * Copyright (C) 2006 Michael Vrable + * + * Utility functions for converting various datatypes to text format (and + * later, for parsing them back, perhaps). + */ + +#ifndef _LBS_FORMAT_H +#define _LBS_FORMAT_H + +#include +#include +#include + +std::string uri_encode(const std::string &in); +std::string uri_decode(const std::string &in); +std::string encode_int(long long n); +void dict_output(std::ostream &o, std::map dict); + +long long parse_int(const std::string &s); + +#endif // _LBS_TARSTORE_H