1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#ifndef COMPONENTS_FILES_ESCAPE_HPP
#define COMPONENTS_FILES_ESCAPE_HPP
#include <queue>
#include <components/files/multidircollection.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/program_options.hpp>
/**
* \namespace Files
*/
namespace Files
{
/**
* \struct escape_hash_filter
*/
struct escape_hash_filter : public boost::iostreams::input_filter
{
static const int sEscape;
static const int sHashIdentifier;
static const int sEscapeIdentifier;
escape_hash_filter();
virtual ~escape_hash_filter();
template <typename Source> int get(Source & src);
private:
std::queue<int> mNext;
bool mSeenNonWhitespace;
bool mFinishLine;
};
template <typename Source>
int escape_hash_filter::get(Source & src)
{
if (mNext.empty())
{
int character = boost::iostreams::get(src);
if (character == boost::iostreams::WOULD_BLOCK)
{
mNext.push(character);
}
else if (character == EOF)
{
mSeenNonWhitespace = false;
mFinishLine = false;
mNext.push(character);
}
else if (character == '\n')
{
mSeenNonWhitespace = false;
mFinishLine = false;
mNext.push(character);
}
else if (mFinishLine)
{
mNext.push(character);
}
else if (character == '#')
{
if (mSeenNonWhitespace)
{
mNext.push(sEscape);
mNext.push(sHashIdentifier);
}
else
{
//it's fine being interpreted by Boost as a comment, and so is anything afterwards
mNext.push(character);
mFinishLine = true;
}
}
else if (character == sEscape)
{
mNext.push(sEscape);
mNext.push(sEscapeIdentifier);
}
else
{
mNext.push(character);
}
if (!mSeenNonWhitespace && !isspace(character))
mSeenNonWhitespace = true;
}
int retval = mNext.front();
mNext.pop();
return retval;
}
struct unescape_hash_filter : public boost::iostreams::input_filter
{
unescape_hash_filter();
virtual ~unescape_hash_filter();
template <typename Source> int get(Source & src);
private:
bool expectingIdentifier;
};
template <typename Source>
int unescape_hash_filter::get(Source & src)
{
int character;
if (!expectingIdentifier)
character = boost::iostreams::get(src);
else
{
character = escape_hash_filter::sEscape;
expectingIdentifier = false;
}
if (character == escape_hash_filter::sEscape)
{
int nextChar = boost::iostreams::get(src);
int intended;
if (nextChar == escape_hash_filter::sEscapeIdentifier)
intended = escape_hash_filter::sEscape;
else if (nextChar == escape_hash_filter::sHashIdentifier)
intended = '#';
else if (nextChar == boost::iostreams::WOULD_BLOCK)
{
expectingIdentifier = true;
intended = nextChar;
}
else
intended = '?';
return intended;
}
else
return character;
}
/**
* \class EscapeHashString
*/
class EscapeHashString
{
private:
std::string mData;
public:
static std::string processString(const std::string & str);
EscapeHashString();
EscapeHashString(const std::string & str);
EscapeHashString(const std::string & str, size_t pos, size_t len = std::string::npos);
EscapeHashString(const char * s);
EscapeHashString(const char * s, size_t n);
EscapeHashString(size_t n, char c);
template <class InputIterator>
EscapeHashString(InputIterator first, InputIterator last);
std::string toStdString() const;
friend std::ostream & operator<< (std::ostream & os, const EscapeHashString & eHS);
};
std::istream & operator>> (std::istream & is, EscapeHashString & eHS);
struct EscapeStringVector
{
std::vector<Files::EscapeHashString> mVector;
EscapeStringVector();
virtual ~EscapeStringVector();
std::vector<std::string> toStdStringVector() const;
};
//boost program options validation
void validate(boost::any &v, const std::vector<std::string> &tokens, Files::EscapeHashString * eHS, int a);
void validate(boost::any &v, const std::vector<std::string> &tokens, EscapeStringVector *, int);
struct EscapePath
{
boost::filesystem::path mPath;
static PathContainer toPathContainer(const std::vector<EscapePath> & escapePathContainer);
};
typedef std::vector<EscapePath> EscapePathContainer;
std::istream & operator>> (std::istream & istream, EscapePath & escapePath);
} /* namespace Files */
#endif /* COMPONENTS_FILES_ESCAPE_HPP */
|