From 75068e7d753cf6cbe45a4bf294000dca9bd41d8b Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 4 Sep 2006 21:06:23 +0000 Subject: * Use a proper namespace. * Optimise header file usage a bit. * Compile the parser as C++. --- src/libutil/Makefile.am | 3 +- src/libutil/archive.cc | 5 ++++ src/libutil/archive.hh | 14 +++++++-- src/libutil/aterm-map.cc | 13 +++++++-- src/libutil/aterm-map.hh | 6 +++- src/libutil/aterm.cc | 14 +++++---- src/libutil/aterm.hh | 15 +++++++--- src/libutil/hash.cc | 11 +++++-- src/libutil/hash.hh | 8 ++++-- src/libutil/types.hh | 73 +++++++++++++++++++++++++++++++++++++++++++++++ src/libutil/util.cc | 26 +++++++++-------- src/libutil/util.hh | 59 ++++---------------------------------- src/libutil/xml-writer.cc | 8 +++++- src/libutil/xml-writer.hh | 14 +++++++-- 14 files changed, 178 insertions(+), 91 deletions(-) create mode 100644 src/libutil/types.hh (limited to 'src/libutil') diff --git a/src/libutil/Makefile.am b/src/libutil/Makefile.am index 7798ac2bb1..dc514d55fa 100644 --- a/src/libutil/Makefile.am +++ b/src/libutil/Makefile.am @@ -3,7 +3,8 @@ pkglib_LTLIBRARIES = libutil.la libutil_la_SOURCES = util.cc util.hh hash.cc hash.hh \ archive.cc archive.hh aterm.cc aterm.hh \ aterm-map.cc aterm-map.hh \ - xml-writer.cc xml-writer.hh + xml-writer.cc xml-writer.hh \ + types.hh if !HAVE_OPENSSL libutil_la_SOURCES += \ diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index 25deccc09c..32c75fee5d 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -12,6 +12,9 @@ #include "util.hh" +namespace nix { + + static string archiveVersion1 = "nix-archive-1"; @@ -319,3 +322,5 @@ void restorePath(const Path & path, RestoreSource & source) restore(path, source); } + +} diff --git a/src/libutil/archive.hh b/src/libutil/archive.hh index b498c95c14..f85d589c6b 100644 --- a/src/libutil/archive.hh +++ b/src/libutil/archive.hh @@ -1,6 +1,10 @@ -#include +#ifndef __ARCHIVE_H +#define __ARCHIVE_H -#include "util.hh" +#include "types.hh" + + +namespace nix { /* dumpPath creates a Nix archive of the specified path. The format @@ -61,3 +65,9 @@ struct RestoreSource }; void restorePath(const Path & path, RestoreSource & source); + + +} + + +#endif /* !__ARCHIVE_H */ diff --git a/src/libutil/aterm-map.cc b/src/libutil/aterm-map.cc index 33388e148a..6c53e07c46 100644 --- a/src/libutil/aterm-map.cc +++ b/src/libutil/aterm-map.cc @@ -1,9 +1,14 @@ #include "aterm-map.hh" +#include + #include #include +namespace nix { + + static const unsigned int maxLoadFactor = /* 1 / */ 3; static unsigned int nrResizes = 0; static unsigned int sizeTotalAlloc = 0; @@ -214,10 +219,11 @@ unsigned int ATermMap::size() } -#include - void printATermMapStats() { + using std::cerr; + using std::endl; + cerr << "RESIZES: " << nrResizes << " " << sizeTotalAlloc << " " << sizeCurAlloc << " " @@ -319,3 +325,6 @@ int main(int argc, char * * argv) printATermMapStats(); } #endif + + +} diff --git a/src/libutil/aterm-map.hh b/src/libutil/aterm-map.hh index 115ed36cd5..293df1eb59 100644 --- a/src/libutil/aterm-map.hh +++ b/src/libutil/aterm-map.hh @@ -4,7 +4,8 @@ #include #include -using namespace std; + +namespace nix { class ATermMap @@ -121,5 +122,8 @@ private: /* Hack. */ void printATermMapStats(); + +} + #endif /* !__ATERM_MAP_H */ diff --git a/src/libutil/aterm.cc b/src/libutil/aterm.cc index 6ca4521909..bb6e33ce92 100644 --- a/src/libutil/aterm.cc +++ b/src/libutil/aterm.cc @@ -1,7 +1,9 @@ #include "aterm.hh" +using std::string; -string atPrint(ATerm t) + +string nix::atPrint(ATerm t) { if (!t) throw Error("attempt to print null aterm"); char * s = ATwriteToString(t); @@ -10,13 +12,13 @@ string atPrint(ATerm t) } -ostream & operator << (ostream & stream, ATerm e) +std::ostream & operator << (std::ostream & stream, ATerm e) { - return stream << atPrint(e); + return stream << nix::atPrint(e); } -Error badTerm(const format & f, ATerm t) +nix::Error nix::badTerm(const format & f, ATerm t) { char * s = ATwriteToString(t); if (!s) throw Error("cannot print term"); @@ -29,13 +31,13 @@ Error badTerm(const format & f, ATerm t) } -ATerm toATerm(const char * s) +ATerm nix::toATerm(const char * s) { return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue)); } -ATerm toATerm(const string & s) +ATerm nix::toATerm(const string & s) { return toATerm(s.c_str()); } diff --git a/src/libutil/aterm.hh b/src/libutil/aterm.hh index 22364ba011..431c67d130 100644 --- a/src/libutil/aterm.hh +++ b/src/libutil/aterm.hh @@ -5,15 +5,15 @@ extern "C" { #include } -#include "util.hh" +#include "types.hh" + + +namespace nix { /* Print an ATerm. */ string atPrint(ATerm t); -/* Write an ATerm to an output stream. */ -ostream & operator << (ostream & stream, ATerm e); - class ATermIterator { ATermList t; @@ -45,5 +45,12 @@ Error badTerm(const format & f, ATerm t); ATerm toATerm(const char * s); ATerm toATerm(const string & s); + +} + + +/* Write an ATerm to an output stream. */ +std::ostream & operator << (std::ostream & stream, ATerm e); + #endif /* !__ATERM_H */ diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index b69c148328..16597fd47c 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -15,12 +15,16 @@ extern "C" { #include "hash.hh" #include "archive.hh" +#include "util.hh" #include #include #include +namespace nix { + + Hash::Hash() { type = htUnknown; @@ -89,9 +93,9 @@ Hash parseHash(HashType ht, const string & s) string s2(s, i * 2, 2); if (!isxdigit(s2[0]) || !isxdigit(s2[1])) throw Error(format("invalid hash `%1%'") % s); - istringstream str(s2); + std::istringstream str(s2); int n; - str >> hex >> n; + str >> std::hex >> n; hash.hash[i] = n; } return hash; @@ -313,3 +317,6 @@ HashType parseHashType(const string & s) else if (s == "sha256") return htSHA256; else return htUnknown; } + + +} diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index 97466cf9ef..95629bc9e5 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -1,11 +1,10 @@ #ifndef __HASH_H #define __HASH_H -#include +#include "types.hh" -#include "util.hh" -using namespace std; +namespace nix { typedef enum { htUnknown, htMD5, htSHA1, htSHA256 } HashType; @@ -76,5 +75,8 @@ Hash compressHash(const Hash & hash, unsigned int newSize); /* Parse a string representing a hash type. */ HashType parseHashType(const string & s); + +} + #endif /* !__HASH_H */ diff --git a/src/libutil/types.hh b/src/libutil/types.hh new file mode 100644 index 0000000000..1de378961e --- /dev/null +++ b/src/libutil/types.hh @@ -0,0 +1,73 @@ +#ifndef __TYPES_H +#define __TYPES_H + +#include +#include +#include + +#include + + +namespace nix { + + +/* Inherit some names from other namespaces for convenience. */ +using std::string; +using std::list; +using std::set; +using std::vector; +using boost::format; + + +class Error : public std::exception +{ +protected: + string err; +public: + Error(const format & f); + ~Error() throw () { }; + const char * what() const throw () { return err.c_str(); } + const string & msg() const throw () { return err; } + Error & addPrefix(const format & f); +}; + +class SysError : public Error +{ +public: + SysError(const format & f); +}; + +#define MakeError(newClass, superClass) \ + class newClass : public superClass \ + { \ + public: \ + newClass(const format & f) : superClass(f) { }; \ + }; + +MakeError(UsageError, Error) + + +typedef list Strings; +typedef set StringSet; + + +/* Paths are just strings. */ +typedef string Path; +typedef list Paths; +typedef set PathSet; + + +typedef enum { + lvlError, + lvlInfo, + lvlTalkative, + lvlChatty, + lvlDebug, + lvlVomit +} Verbosity; + + +} + + +#endif /* !__TYPES_H */ diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 54cfc6c7fd..44b39f8c62 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -9,17 +9,16 @@ #include #include -#include #include #include -#include -#include #include -#include #include "util.hh" +namespace nix { + + Error::Error(const format & f) { err = f.str(); @@ -368,8 +367,8 @@ void Nest::open(Verbosity level, const format & f) { if (level <= verbosity) { if (logType == ltEscapes) - cerr << "\033[" << escVerbosity(level) << "p" - << f.str() << "\n"; + std::cerr << "\033[" << escVerbosity(level) << "p" + << f.str() << "\n"; else printMsg_(level, f); nest = true; @@ -383,7 +382,7 @@ void Nest::close() if (nest) { nestingLevel--; if (logType == ltEscapes) - cerr << "\033[q"; + std::cerr << "\033[q"; nest = false; } } @@ -697,8 +696,8 @@ string runProgram(Path program) execl(program.c_str(), program.c_str(), (char *) 0); throw SysError(format("executing `%1%'") % program); - } catch (exception & e) { - cerr << "error: " << e.what() << endl; + } catch (std::exception & e) { + std::cerr << "error: " << e.what() << std::endl; } quickExit(1); } @@ -743,7 +742,7 @@ void _interrupted() /* Block user interrupts while an exception is being handled. Throwing an exception while another exception is being handled kills the program! */ - if (!uncaught_exception()) { + if (!std::uncaught_exception()) { _isInterrupted = 0; throw Error("interrupted by the user"); } @@ -837,7 +836,7 @@ bool statusOk(int status) string int2String(int n) { - ostringstream str; + std::ostringstream str; str << n; return str.str(); } @@ -845,7 +844,10 @@ string int2String(int n) bool string2Int(const string & s, int & n) { - istringstream str(s); + std::istringstream str(s); str >> n; return str && str.get() == EOF; } + + +} diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 125ba2695c..b4a61ae04a 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -1,58 +1,15 @@ #ifndef __UTIL_H #define __UTIL_H -#include -#include -#include -#include +#include "types.hh" #include #include #include #include -#include -using namespace std; -using namespace boost; - - -class Error : public exception -{ -protected: - string err; -public: - Error(const format & f); - ~Error() throw () { }; - const char * what() const throw () { return err.c_str(); } - const string & msg() const throw () { return err; } - Error & addPrefix(const format & f); -}; - -class SysError : public Error -{ -public: - SysError(const format & f); -}; - -#define MakeError(newClass, superClass) \ - class newClass : public superClass \ - { \ - public: \ - newClass(const format & f) : superClass(f) { }; \ - }; - -MakeError(UsageError, Error) - - -typedef list Strings; -typedef set StringSet; - - -/* Paths are just strings. */ -typedef string Path; -typedef list Paths; -typedef set PathSet; +namespace nix { /* Return an environment variable. */ @@ -138,15 +95,6 @@ typedef enum { ltFlat /* no nesting */ } LogType; -typedef enum { - lvlError, - lvlInfo, - lvlTalkative, - lvlChatty, - lvlDebug, - lvlVomit -} Verbosity; - extern LogType logType; extern Verbosity verbosity; /* suppress msgs > this */ @@ -307,5 +255,8 @@ struct SwitchToOriginalUser ~SwitchToOriginalUser(); }; + +} + #endif /* !__UTIL_H */ diff --git a/src/libutil/xml-writer.cc b/src/libutil/xml-writer.cc index 27235933e7..20351e2c30 100644 --- a/src/libutil/xml-writer.cc +++ b/src/libutil/xml-writer.cc @@ -3,7 +3,10 @@ #include "xml-writer.hh" -XMLWriter::XMLWriter(bool indent, ostream & output) +namespace nix { + + +XMLWriter::XMLWriter(bool indent, std::ostream & output) : output(output), indent(indent) { output << "\n"; @@ -122,3 +125,6 @@ int main(int argc, char * * argv) return 0; } #endif + + +} diff --git a/src/libutil/xml-writer.hh b/src/libutil/xml-writer.hh index 8c203a3486..e5cc5f8c54 100644 --- a/src/libutil/xml-writer.hh +++ b/src/libutil/xml-writer.hh @@ -6,7 +6,12 @@ #include #include -using namespace std; + +namespace nix { + +using std::string; +using std::map; +using std::list; typedef map XMLAttrs; @@ -16,7 +21,7 @@ class XMLWriter { private: - ostream & output; + std::ostream & output; bool indent; bool closed; @@ -25,7 +30,7 @@ private: public: - XMLWriter(bool indent, ostream & output); + XMLWriter(bool indent, std::ostream & output); ~XMLWriter(); void close(); @@ -63,5 +68,8 @@ public: } }; + +} + #endif /* !__XML_WRITER_H */ -- cgit 1.4.1