mirror of
https://github.com/NixOS/nix
synced 2025-06-27 08:31:16 +02:00
* Use a proper namespace.
* Optimise header file usage a bit. * Compile the parser as C++.
This commit is contained in:
parent
aab8812732
commit
75068e7d75
61 changed files with 650 additions and 268 deletions
|
@ -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 += \
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
73
src/libutil/types.hh
Normal file
73
src/libutil/types.hh
Normal file
|
@ -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 */
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue