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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Computational Engine *
* *
* Copyright (c) 1999-2011, Ben Burton *
* For further details contact Ben Burton (bab@debian.org). *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this program; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
* MA 02110-1301, USA. *
* *
**************************************************************************/
/* end stub */
#include <cctype>
#include <cstring>
#include <fstream>
#include "engine.h"
#include "file/nfile.h"
#include "file/nfileinfo.h"
#include "file/nxmlcallback.h"
#include "file/nxmlfile.h"
#include "packet/ncontainer.h"
#include "packet/nxmlpacketreader.h"
#include "utilities/zstream.h"
namespace regina {
namespace {
/**
* Reads the outermost \<reginadata ...\> XML element.
*/
class ReginaDataReader : public regina::NXMLPacketReader {
private:
NContainer container;
/**< Sits above the entire packet tree read from file. */
bool isReginaData;
/**< Are we actually reading a \<reginadata ...\> element? */
public:
/**
* Create a new top-level reader.
*/
ReginaDataReader() : isReginaData(false) {
}
virtual NPacket* getPacket() {
if (isReginaData)
return &container;
else
return 0;
}
virtual void startElement(const std::string& n,
const regina::xml::XMLPropertyDict&, NXMLElementReader*) {
if (n == "reginadata")
isReginaData = true;
}
virtual void abort(NXMLElementReader*) {
// Delete all children of the top-level container.
while (NPacket* child = container.getFirstTreeChild()) {
child->makeOrphan();
delete child;
}
}
};
/**
* The number of characters by which we expect to have finished the
* <reginadata ...> opening tag in an old pre-utf8 XML data stream.
*/
const int regDataOpenBy = 200;
/**
* The size of the XML chunks that are sent through to libxml2 for parsing.
*
* Since we plan to perform surgery on pre-utf8 data streams, the chunk
* size must be large enough to catch the regina engine version in the
* first bite. In particular, we require regChunkSize > regDataOpenBy.
*/
const int regChunkSize = 1024;
}
bool writeXMLFile(const char* fileName, NPacket* subtree, bool compressed) {
if (compressed) {
CompressionStream out(fileName);
if (! out)
return false;
subtree->writeXMLFile(out);
} else {
std::ofstream out(fileName);
if (! out)
return false;
subtree->writeXMLFile(out);
}
return true;
}
NPacket* readXMLFile(const char* fileName) {
DecompressionStream in(fileName);
if (! in)
return 0;
ReginaDataReader reader;
regina::NXMLCallback callback(reader, std::cerr);
// Instead of using the ready-made regina::xml::XMLParser::parse_stream(),
// we split the stream into parseable chunks manually. This allows us to
// perform surgery on pre-utf8 streams and tell the parser that old files
// actually store their data in latin1 (which the old file format
// neglected to specify in the XML prologue).
{
regina::xml::XMLParser parser(callback);
char* buf = new char[regChunkSize];
int chunkRead;
bool seenFirstChunk = false;
while (true) {
// Read in the next chunk.
for (chunkRead = 0; chunkRead < regChunkSize; chunkRead++) {
buf[chunkRead] = static_cast<char>(in.get());
if (in.eof())
break;
}
if (chunkRead == 0)
break;
// Parse the chunk that has just been read.
// Interesting things (i.e., surgery) can only happen in the
// first chunk.
if (seenFirstChunk) {
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
seenFirstChunk = true;
// This is the first chunk in the data stream. See if we
// need to perform surgery upon it.
//
// We will be fairly rigid in looking for substrings, since
// we are only trying to match old pre-utf8 data files. If
// future versions of the calculation engine change the
// opening "signature" then this does not matter, since they
// will not need surgery anyway.
//
// If anything looks out of place, just abort the surgery
// and send everything straight through to the XML parser,
// in the hope that either the file is new enough to not
// need surgery (i.e., it uses utf8 already) or it's old but
// only uses plain ASCII.
char tmp = buf[regDataOpenBy];
buf[regDataOpenBy] = 0;
char* start = ::strstr(buf, "<reginadata engine=\"");
if (! start) {
buf[regDataOpenBy] = tmp;
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
start += 20; /* Length of "<reginadata engine=\"" */
char* finish = ::strchr(start, '"');
if (finish == 0 || finish == start) {
// Never found a closing quote, or else the engine version
// string is empty.
buf[regDataOpenBy] = tmp;
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
// We've found the engine version.
buf[regDataOpenBy] = tmp;
tmp = *finish;
*finish = 0;
if (versionUsesUTF8(start)) {
// No need for surgery.
*finish = tmp;
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
// Surgery is required.
// Make sure there is somewhere to squeeze in an encoding.
start = ::strstr(buf, "encoding=");
if (start) {
// Someone else seems to have done it already.. *shrug*
*finish = tmp;
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
start = ::strstr(buf, "?>");
if (! start) {
// Can't find the end of the <?xml ...?> opening sequence!
*finish = tmp;
parser.parse_chunk(std::string(buf, chunkRead));
continue;
}
// Good to go. Parse the chunk with a new encoding squeezed in
// just before the "?>".
*finish = tmp;
parser.parse_chunk(std::string(buf, start - buf));
parser.parse_chunk(" encoding=\"ISO-8859-1\"");
parser.parse_chunk(std::string(start, chunkRead - (start - buf)));
}
parser.finish();
delete[] buf;
}
// See if we read anything.
// If so, break it away from the top-level container and return it.
NPacket* p = reader.getPacket();
if (p) {
p = p->getFirstTreeChild();
if (p)
p->makeOrphan();
return p;
} else
return 0;
}
NPacket* readFileMagic(const std::string& fileName) {
NFileInfo* info = NFileInfo::identify(fileName);
if (! info)
return 0;
NPacket* ans;
if (info->getType() == NFileInfo::TYPE_XML)
ans = readXMLFile(fileName.c_str());
else if (info->getType() == NFileInfo::TYPE_BINARY)
ans = readFromFile(fileName.c_str());
else
ans = 0;
delete info;
return ans;
}
} // namespace regina
|