about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-09-04T21·06+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-09-04T21·06+0000
commit75068e7d753cf6cbe45a4bf294000dca9bd41d8b (patch)
treec6274cc10caab08349b5585206034f41ca4a575f /src/libutil
parentaab88127321344d5818d823bff515d127108d058 (diff)
* Use a proper namespace.
* Optimise header file usage a bit.
* Compile the parser as C++.

Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/Makefile.am3
-rw-r--r--src/libutil/archive.cc5
-rw-r--r--src/libutil/archive.hh14
-rw-r--r--src/libutil/aterm-map.cc13
-rw-r--r--src/libutil/aterm-map.hh6
-rw-r--r--src/libutil/aterm.cc14
-rw-r--r--src/libutil/aterm.hh15
-rw-r--r--src/libutil/hash.cc11
-rw-r--r--src/libutil/hash.hh8
-rw-r--r--src/libutil/types.hh73
-rw-r--r--src/libutil/util.cc26
-rw-r--r--src/libutil/util.hh59
-rw-r--r--src/libutil/xml-writer.cc8
-rw-r--r--src/libutil/xml-writer.hh14
14 files changed, 178 insertions, 91 deletions
diff --git a/src/libutil/Makefile.am b/src/libutil/Makefile.am
index 7798ac2bb1ca..dc514d55fa1b 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 25deccc09c15..32c75fee5d40 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 b498c95c1436..f85d589c6b2c 100644
--- a/src/libutil/archive.hh
+++ b/src/libutil/archive.hh
@@ -1,6 +1,10 @@
-#include <string>
+#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 33388e148a65..6c53e07c4605 100644
--- a/src/libutil/aterm-map.cc
+++ b/src/libutil/aterm-map.cc
@@ -1,9 +1,14 @@
 #include "aterm-map.hh"
 
+#include <iostream>
+
 #include <assert.h>
 #include <stdlib.h>
 
 
+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 <iostream>
-
 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 115ed36cd53a..293df1eb596a 100644
--- a/src/libutil/aterm-map.hh
+++ b/src/libutil/aterm-map.hh
@@ -4,7 +4,8 @@
 #include <aterm2.h>
 #include <assert.h>
 
-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 6ca4521909ea..bb6e33ce92c0 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 22364ba0118f..431c67d130ec 100644
--- a/src/libutil/aterm.hh
+++ b/src/libutil/aterm.hh
@@ -5,15 +5,15 @@ extern "C" {
 #include <aterm2.h>
 }
 
-#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 b69c14832877..16597fd47cbf 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 <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
 
+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 97466cf9ef1a..95629bc9e5ba 100644
--- a/src/libutil/hash.hh
+++ b/src/libutil/hash.hh
@@ -1,11 +1,10 @@
 #ifndef __HASH_H
 #define __HASH_H
 
-#include <string>
+#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 000000000000..1de378961e46
--- /dev/null
+++ b/src/libutil/types.hh
@@ -0,0 +1,73 @@
+#ifndef __TYPES_H
+#define __TYPES_H
+
+#include <string>
+#include <list>
+#include <set>
+
+#include <boost/format.hpp>
+
+
+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<string> Strings;
+typedef set<string> StringSet;
+
+
+/* Paths are just strings. */
+typedef string Path;
+typedef list<Path> Paths;
+typedef set<Path> 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 54cfc6c7fded..44b39f8c623a 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -9,17 +9,16 @@
 #include <cstdio>
 #include <sstream>
 
-#include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
-#include <unistd.h>
-#include <dirent.h>
 #include <fcntl.h>
-#include <signal.h>
 
 #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 125ba2695c2c..b4a61ae04a0d 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -1,58 +1,15 @@
 #ifndef __UTIL_H
 #define __UTIL_H
 
-#include <string>
-#include <list>
-#include <set>
-#include <sstream>
+#include "types.hh"
 
 #include <sys/types.h>
 #include <dirent.h>
 #include <unistd.h>
 #include <signal.h>
 
-#include <boost/format.hpp>
 
-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<string> Strings;
-typedef set<string> StringSet;
-
-
-/* Paths are just strings. */
-typedef string Path;
-typedef list<Path> Paths;
-typedef set<Path> 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 27235933e7fb..20351e2c30ac 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 << "<?xml version='1.0' encoding='utf-8'?>\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 8c203a3486f0..e5cc5f8c5417 100644
--- a/src/libutil/xml-writer.hh
+++ b/src/libutil/xml-writer.hh
@@ -6,7 +6,12 @@
 #include <list>
 #include <map>
 
-using namespace std;
+
+namespace nix {
+
+using std::string;
+using std::map;
+using std::list;
 
 
 typedef map<string, string> 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 */