/usr/include/scribus/desaxe/digester.h is in scribus-dev 1.4.6+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | /*
* digester.h
*
*
* Created by Andreas Vox on 02.06.06.
* Copyright 2006 under GPL2. All rights reserved.
*
*/
#ifndef DIGESTER_H
#define DIGESTER_H
#include <cassert>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <typeinfo>
#include "desaxe_conf.h"
#include "saxhandler.h"
//#define DESAXE_DEBUG 1
namespace desaxe {
class Action;
class RuleState;
namespace PRIVATE {
class VarPtr
{
public:
void* ptr;
std::string type;
};
template<class ObjType>
inline
VarPtr mkcell(ObjType* obj)
{
VarPtr result;
result.ptr = obj;// result.ptr = const_cast<void*>(static_cast<const void*>(obj)); //??
result.type = typeid(obj).name();
return result;
}
template<class ObjType>
inline
void chkcell(const VarPtr& cell, std::vector<VarPtr>* stack = NULL)
{
ObjType* dummy = NULL;
if( cell.type != typeid(dummy).name() )
{
std::cerr << "requested type '" << typeid(dummy).name() << "' doesnt match cell type '" << cell.type << "'\n";
if (stack)
{
int i=0;
std::vector<VarPtr>::iterator it;
for (it = stack->begin(); it != stack->end(); ++it)
{
std::cerr << i++ << "\t" << (*it).type << "\t" << (*it).ptr << "\n";
}
}
assert (false);
}
}
class Patch {
public:
// internal linked list
Patch* next;
Patch(Patch* nxt) : next(nxt) {}
virtual void run(VarPtr lnk) = 0;
virtual ~Patch() {}
};
} // namespace PRIVATE
/**
Digester helps you to create C++ objects from a SAX stream.
All you have to do is register actions for patterns of element tags
you expect in your XML stream. These actions can create new objects, set
attributes, or call methods. Actions operate on intermediate objects which
are hold on a stack maintained by Digester.
*/
class Digester : public SaxHandler {
public:
Digester();
Digester& operator=(const Digester& other);
virtual ~Digester();
void addRule(const Xml_string& pattern, Action action);
void parseFile(const Xml_string& filename);
void parseMemory(const char* data, unsigned int length);
template<class ObjType>
ObjType* result();
int nrOfErrors() const;
const Xml_string getError(int i) const;
// called by SAX parser:
void beginDoc();
void endDoc();
void begin(const Xml_string& tag, Xml_attr attr);
void end(const Xml_string& tag);
void chars(const Xml_string& text);
// used by actions:
void fail();
void error(const Xml_string& msg);
template<class ObjType>
ObjType* top(unsigned int offset = 0);
template<class ObjType>
ObjType* bottom(unsigned int offset = 0);
template<class ObjType>
void setResult( ObjType* res );
void pop();
void popn(unsigned int number);
template<class ObjType>
void push(ObjType* obj);
// used to resolve idrefs and for general storage
template<class ObjType>
ObjType* lookup(const Xml_string& idref);
template<class ObjType>
void store(const Xml_string& idref, ObjType* res );
// used to resolve idrefs if use is before definition
template<class LinkType>
void patchCall(const Xml_string& idref, void (*fun)(LinkType*) );
template<class ObjType, class LinkType>
void patchInvoke(const Xml_string& idref, ObjType* obj, void (ObjType::*fun)(LinkType*) );
// used to insert "/" where necessary
static Xml_string concat(const Xml_string& pattern1, const Xml_string& pattern2);
private:
RuleState*
state;
std::vector<PRIVATE::VarPtr>
objects;
std::map<Xml_string, PRIVATE::VarPtr>
storage;
std::map<Xml_string, PRIVATE::Patch*>
patches;
PRIVATE::VarPtr
result_;
std::vector<Xml_string>
errors;
};
template<class ObjType>
inline
ObjType* Digester::top(unsigned int offset)
{
#ifdef DESAXE_DEBUG
std::cerr << "top(" << offset << ") of " << objects.size() << "\n";
#endif
unsigned int count = objects.size();
assert (offset < count);
PRIVATE::chkcell<ObjType>(objects[count - offset - 1], &objects);
#ifdef DESAXE_DEBUG
std::cerr << "stack-> " << static_cast<ObjType*>(objects[count - offset - 1].ptr) << "\n";
#endif
return static_cast<ObjType*>(objects[count - offset - 1].ptr);
}
template<class ObjType>
inline
ObjType* Digester::bottom(unsigned int offset)
{
#ifdef DESAXE_DEBUG
std::cerr << "bottom(" << offset << ") of " << objects.size() << "\n";
#endif
//unsigned int count = objects.size();
assert (offset < objects.size());
PRIVATE::chkcell<ObjType> (objects[offset]);
return static_cast<ObjType*>(objects[offset].ptr);
}
template<class ObjType>
inline
ObjType* Digester::result()
{
ObjType* dummy = NULL;
if (result_.type != typeid(dummy).name())
return NULL;
#ifdef DESAXE_DEBUG
std::cerr << "result-> " << static_cast<ObjType*>(result_.ptr) << "\n";
#endif
return static_cast<ObjType*>(result_.ptr);
}
template<class ObjType>
inline
void Digester::setResult(ObjType* res)
{
#ifdef DESAXE_DEBUG
std::cerr << res << " ->result\n";
#endif
result_ = PRIVATE::mkcell(res);
}
inline
void Digester::pop()
{
assert (1 <= (unsigned int) objects.size());
objects.pop_back();
}
inline
void Digester::popn(unsigned int number)
{
unsigned int count = (unsigned int) objects.size();
assert (number <= count);
objects.resize(count - number);
}
template<class ObjType>
inline
void Digester::push(ObjType* obj)
{
#ifdef DESAXE_DEBUG
std::cerr << "stack<- " << obj << "\n";
#endif
objects.push_back(PRIVATE::mkcell(obj));
}
// now lookup / store / patch business
namespace PRIVATE {
template <class LinkType>
struct Patch1 : public Patch
{
typedef void (*FunType1)(LinkType*);
FunType1 fun;
Patch1(FunType1 fn, Patch* nxt = NULL) : Patch(nxt), fun(fn) {}
void run(VarPtr link)
{
fun( static_cast<LinkType*>(link.ptr) );
}
};
template <class ObjType, class LinkType>
struct Patch2 : public Patch
{
typedef void (ObjType::*FunType2)(LinkType*);
ObjType* obj;
FunType2 fun;
Patch2(ObjType* ob, FunType2 fn, Patch* nxt = NULL) : Patch(nxt), obj(ob), fun(fn) {}
void run(VarPtr link)
{
(obj->*fun)( static_cast<LinkType*>(link.ptr) );
}
};
inline
void runPatches(Patch*& list, VarPtr link)
{
while (list)
{
Patch* nxt = list->next;
list->run(link);
delete list;
list = nxt;
}
}
inline
void deletePatches(std::map<Xml_string, Patch*>& patches)
{
std::map<Xml_string, Patch*>::iterator it;
for (it = patches.begin(); it != patches.end(); ++it)
{
Patch* list = it->second;
while (list)
{
Patch* nxt = list->next;
delete list;
list = nxt;
}
}
patches.clear();
}
// template<> class Patch1<void>;
// template<> class Patch2<VarPtr,void>;
} //namespace PRIVATE
template<class ObjType>
inline
ObjType* Digester::lookup(const Xml_string& idref)
{
using namespace PRIVATE;
std::map<Xml_string, VarPtr>::iterator cell = storage.find(idref);
if (cell == storage.end())
{
#ifdef DESAXE_DEBUG
std::cerr << "lookup[" << idref << "]-> NULL\n";
#endif
return NULL;
}
else
{
chkcell<ObjType> (cell->second);
#ifdef DESAXE_DEBUG
std::cerr << "lookup[" << idref << "]-> " << static_cast<ObjType*>(cell->second.ptr) << "\n";
#endif
return static_cast<ObjType*>(cell->second.ptr);
}
}
template<class ObjType>
inline
void Digester::store(const Xml_string& idref, ObjType* obj)
{
using namespace PRIVATE;
#ifdef DESAXE_DEBUG
std::cerr << "store[" << idref << "] <- " << obj << "\n";
#endif
storage[idref] = mkcell(obj);
runPatches(patches[idref], storage[idref]);
}
template<class LinkType>
void Digester::patchCall(const Xml_string& idref, void (*fun)(LinkType*) )
{
using namespace PRIVATE;
std::map<Xml_string, VarPtr>::iterator cell = storage.find(idref);
if (cell == storage.end())
{
patches[idref] = new Patch1<LinkType>(fun, patches[idref] );
}
else
{
Patch1<LinkType>(fun).run(cell->second);
}
}
template<class ObjType, class LinkType>
void Digester::patchInvoke(const Xml_string& idref, ObjType* obj, void (ObjType::*fun)(LinkType*) )
{
using namespace PRIVATE;
std::map<Xml_string, VarPtr>::iterator cell = storage.find(idref);
if (cell == storage.end())
{
patches[idref] = new Patch2<ObjType,LinkType>(obj, fun, patches[idref] );
}
else
{
Patch2<ObjType,LinkType>(obj, fun).run(cell->second);
}
}
} // namespace desaxe
#endif
|