mirror of
https://github.com/NixOS/nix
synced 2025-06-29 10:31:15 +02:00
Merge branch 'master' of github.com:NixOS/nix into hash-always-has-type
This commit is contained in:
commit
d090562348
36 changed files with 394 additions and 354 deletions
|
@ -11,7 +11,7 @@ namespace nix {
|
|||
#define ANSI_GREEN "\e[32;1m"
|
||||
#define ANSI_YELLOW "\e[33;1m"
|
||||
#define ANSI_BLUE "\e[34;1m"
|
||||
#define ANSI_MAGENTA "\e[35m;1m"
|
||||
#define ANSI_CYAN "\e[36m;1m"
|
||||
#define ANSI_MAGENTA "\e[35;1m"
|
||||
#define ANSI_CYAN "\e[36;1m"
|
||||
|
||||
}
|
||||
|
|
|
@ -63,11 +63,12 @@ struct ParseSink
|
|||
virtual void createSymlink(const Path & path, const string & target) { };
|
||||
};
|
||||
|
||||
struct TeeSink : ParseSink
|
||||
struct TeeParseSink : ParseSink
|
||||
{
|
||||
StringSink saved;
|
||||
TeeSource source;
|
||||
|
||||
TeeSink(Source & source) : source(source) { }
|
||||
TeeParseSink(Source & source) : source(source, saved) { }
|
||||
};
|
||||
|
||||
void parseDump(ParseSink & sink, Source & source);
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
#include "hash.hh"
|
||||
#include "archive.hh"
|
||||
#include "util.hh"
|
||||
#include "istringstream_nocopy.hh"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
|
|
@ -1,92 +0,0 @@
|
|||
/* This file provides a variant of std::istringstream that doesn't
|
||||
copy its string argument. This is useful for large strings. The
|
||||
caller must ensure that the string object is not destroyed while
|
||||
it's referenced by this object. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
template <class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT>>
|
||||
class basic_istringbuf_nocopy : public std::basic_streambuf<CharT, Traits>
|
||||
{
|
||||
public:
|
||||
typedef std::basic_string<CharT, Traits, Allocator> string_type;
|
||||
|
||||
typedef typename std::basic_streambuf<CharT, Traits>::off_type off_type;
|
||||
|
||||
typedef typename std::basic_streambuf<CharT, Traits>::pos_type pos_type;
|
||||
|
||||
typedef typename std::basic_streambuf<CharT, Traits>::int_type int_type;
|
||||
|
||||
typedef typename std::basic_streambuf<CharT, Traits>::traits_type traits_type;
|
||||
|
||||
private:
|
||||
const string_type & s;
|
||||
|
||||
off_type off;
|
||||
|
||||
public:
|
||||
basic_istringbuf_nocopy(const string_type & s) : s{s}, off{0}
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which)
|
||||
{
|
||||
if (which & std::ios_base::in) {
|
||||
this->off = dir == std::ios_base::beg
|
||||
? off
|
||||
: (dir == std::ios_base::end
|
||||
? s.size() + off
|
||||
: this->off + off);
|
||||
}
|
||||
return pos_type(this->off);
|
||||
}
|
||||
|
||||
pos_type seekpos(pos_type pos, std::ios_base::openmode which)
|
||||
{
|
||||
return seekoff(pos, std::ios_base::beg, which);
|
||||
}
|
||||
|
||||
std::streamsize showmanyc()
|
||||
{
|
||||
return s.size() - off;
|
||||
}
|
||||
|
||||
int_type underflow()
|
||||
{
|
||||
if (typename string_type::size_type(off) == s.size())
|
||||
return traits_type::eof();
|
||||
return traits_type::to_int_type(s[off]);
|
||||
}
|
||||
|
||||
int_type uflow()
|
||||
{
|
||||
if (typename string_type::size_type(off) == s.size())
|
||||
return traits_type::eof();
|
||||
return traits_type::to_int_type(s[off++]);
|
||||
}
|
||||
|
||||
int_type pbackfail(int_type ch)
|
||||
{
|
||||
if (off == 0 || (ch != traits_type::eof() && ch != s[off - 1]))
|
||||
return traits_type::eof();
|
||||
|
||||
return traits_type::to_int_type(s[--off]);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template <class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT>>
|
||||
class basic_istringstream_nocopy : public std::basic_iostream<CharT, Traits>
|
||||
{
|
||||
typedef basic_istringbuf_nocopy<CharT, Traits, Allocator> buf_type;
|
||||
buf_type buf;
|
||||
public:
|
||||
basic_istringstream_nocopy(const typename buf_type::string_type & s) :
|
||||
std::basic_iostream<CharT, Traits>(&buf), buf(s) {};
|
||||
};
|
||||
|
||||
typedef basic_istringstream_nocopy<char> istringstream_nocopy;
|
|
@ -166,17 +166,30 @@ struct StringSource : Source
|
|||
};
|
||||
|
||||
|
||||
/* Adapter class of a Source that saves all data read to `s'. */
|
||||
/* A sink that writes all incoming data to two other sinks. */
|
||||
struct TeeSink : Sink
|
||||
{
|
||||
Sink & sink1, & sink2;
|
||||
TeeSink(Sink & sink1, Sink & sink2) : sink1(sink1), sink2(sink2) { }
|
||||
virtual void operator () (const unsigned char * data, size_t len)
|
||||
{
|
||||
sink1(data, len);
|
||||
sink2(data, len);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Adapter class of a Source that saves all data read to a sink. */
|
||||
struct TeeSource : Source
|
||||
{
|
||||
Source & orig;
|
||||
ref<std::string> data;
|
||||
TeeSource(Source & orig)
|
||||
: orig(orig), data(make_ref<std::string>()) { }
|
||||
Sink & sink;
|
||||
TeeSource(Source & orig, Sink & sink)
|
||||
: orig(orig), sink(sink) { }
|
||||
size_t read(unsigned char * data, size_t len)
|
||||
{
|
||||
size_t n = orig.read(data, len);
|
||||
this->data->append((const char *) data, n);
|
||||
sink(data, len);
|
||||
return n;
|
||||
}
|
||||
};
|
||||
|
@ -336,4 +349,27 @@ Source & operator >> (Source & in, bool & b)
|
|||
}
|
||||
|
||||
|
||||
/* An adapter that converts a std::basic_istream into a source. */
|
||||
struct StreamToSourceAdapter : Source
|
||||
{
|
||||
std::shared_ptr<std::basic_istream<char>> istream;
|
||||
|
||||
StreamToSourceAdapter(std::shared_ptr<std::basic_istream<char>> istream)
|
||||
: istream(istream)
|
||||
{ }
|
||||
|
||||
size_t read(unsigned char * data, size_t len) override
|
||||
{
|
||||
if (!istream->read((char *) data, len)) {
|
||||
if (istream->eof()) {
|
||||
if (istream->gcount() == 0)
|
||||
throw EndOfFile("end of file");
|
||||
} else
|
||||
throw Error("I/O error in StreamToSourceAdapter");
|
||||
}
|
||||
return istream->gcount();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue