Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. // XMLReader.cpp
  2. //
  3. // (C) 2006 - 2009 MicroNeil Research Corporation.
  4. //
  5. // This program is part of the MicroNeil Research Open Library Project. For
  6. // more information go to http://www.microneil.com/OpenLibrary/index.html
  7. //
  8. // This program is free software; you can redistribute it and/or modify it
  9. // under the terms of the GNU General Public License as published by the
  10. // Free Software Foundation; either version 2 of the License, or (at your
  11. // option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful, but WITHOUT
  14. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. // more details.
  17. //
  18. // You should have received a copy of the GNU General Public License along with
  19. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. // Place, Suite 330, Boston, MA 02111-1307 USA
  21. // See XMLReader.hpp for details
  22. #include <vector>
  23. #include "CodeDweller/XMLReader.hpp"
  24. using namespace std;
  25. namespace CodeDweller {
  26. //// Helper functions //////////////////////////////////////////////////////////
  27. // isNameChar(const char x)
  28. // true if the character can be used in a name.
  29. bool isNameChar(const char x) {
  30. return (
  31. isalnum(x) ||
  32. ('.' == x) ||
  33. ('-' == x) ||
  34. ('_' == x) ||
  35. (':' == x)
  36. );
  37. }
  38. // Eat Spaces and Count Lines
  39. // While we're parsing the configuration file there are times we will need to
  40. // skip some amount of whitespace. While doing that we need to keep track of
  41. // any new-lines we cross so that we always know what line number we are on.
  42. // This function makes that work a one-liner in our parsing routines.
  43. int eatSpacesCountLines(XMLReaderData& Data, int& Index) { // Eat spaces and count lines.
  44. int LineCount = 0; // Keep the line count here.
  45. char C = 0; // Keep the current character here.
  46. for(;;) { // We'll be looping like this...
  47. C = Data.Data(Index); // Grab the character at the Index.
  48. if(0 == C) { // If we have run out of data
  49. break; // then we are certainly done.
  50. }
  51. if(isspace(C)) { // If it is a whitespace
  52. if('\n' == C) { // check to see if it's a new line
  53. ++LineCount; // and count it if it is.
  54. } // Since it was a space in any case
  55. ++Index; // move the index past it.
  56. } else { // As soon as we hit something not
  57. break; // a whitespace we are done looping.
  58. }
  59. }
  60. return LineCount; // In the end return the line count.
  61. }
  62. // Eat NonTagText Count Lines
  63. // This is a variation on the Eat Spaces theme except that it is used in an
  64. // element to bypass any floating text or spaces that might be in the file. In
  65. // a perfect world such a thing would not exist -- but just in case it does we
  66. // want to handle it gracefully. This function will get us to the first < that
  67. // we can find - presumably the opening tag of an element.
  68. int eatNonTagTextCountLines(XMLReaderData& Data, int& Index) { // Eat "stuff" and count lines.
  69. int LineCount = 0; // Keep the line count here.
  70. char C = 0; // Keep the current character here.
  71. for(;;) { // We'll be looping like this...
  72. C = Data.Data(Index); // Grab the character at the Index.
  73. if(0 == C) { // If we have run out of data
  74. break; // then we are certainly done.
  75. }
  76. if('\n' == C) { // check to see if it's a new line
  77. ++LineCount; // and count it if it is.
  78. } else
  79. if('<' == C) { // When we find our < we're done!
  80. break;
  81. } // If C wasn't what we're after
  82. ++Index; // move the index past this byte.
  83. }
  84. return LineCount; // In the end return the line count.
  85. }
  86. // Eat Comments Count Lines
  87. // This is another variant of Eat Spaces. In this if we are on a <!-- tag
  88. // opening, then we will eat the rest of it through -->
  89. int eatCommentsCountLines(XMLReaderData& Data, int& Index) { // Eat any <!-- -->
  90. int LineCount = 0; // Keep the line count here.
  91. char C = 0; // Keep the current character here.
  92. // First - are we on a comment?
  93. if( // If the text at Index doesn't
  94. Data.Data(Index) != '<' || // look like the start of a
  95. Data.Data(Index + 1) != '!' || // comment then we are done.
  96. Data.Data(Index + 2) != '-' ||
  97. Data.Data(Index + 3) != '-'
  98. ) {
  99. return 0; // Return after no changes.
  100. }
  101. // Since we are on a comment, let's eat
  102. Index += 4; // Move past the comment start.
  103. for(;;) { // We'll be looping like this...
  104. C = Data.Data(Index); // Grab the character at the Index.
  105. if(0 == C) { // If we have run out of data
  106. break; // then we are certainly done.
  107. }
  108. if('\n' == C) { // check to see if it's a new line
  109. ++LineCount; // and count it if it is.
  110. } else
  111. if('-' == C) { // When we find a - we check for -->
  112. if(
  113. '-' == Data.Data(Index + 1) && // If we have found the end of our
  114. '>' == Data.Data(Index + 2) // comment then we are ready to
  115. ) { // stop.
  116. Index += 3; // Move the Index past the end
  117. break; // and break out of the loop.
  118. }
  119. } // If C wasn't what we're after
  120. ++Index; // move the index past this byte.
  121. }
  122. return LineCount; // In the end return the line count.
  123. }
  124. // Eat DocSpecs Count Lines
  125. // Another variation of Eat Spaces - this time to eat <? doc specs ?>
  126. int eatDocSpecsCountLines(XMLReaderData& Data, int& Index) { // Eat any <? ?>
  127. int LineCount = 0; // Keep the line count here.
  128. char C = 0; // Keep the current character here.
  129. // First - are we on a doc spec?
  130. if( // If the text at Index doesn't
  131. Data.Data(Index) != '<' || // look like the start of a
  132. Data.Data(Index + 1) != '?' // doc spec then we are done.
  133. ) {
  134. return 0; // Return after no changes.
  135. }
  136. // Since we are on a doc spec, let's eat
  137. for(;;) { // We'll be looping like this...
  138. C = Data.Data(Index); // Grab the character at the Index.
  139. if(0 == C) { // If we have run out of data
  140. break; // then we are certainly done.
  141. }
  142. if('\n' == C) { // check to see if it's a new line
  143. ++LineCount; // and count it if it is.
  144. } else
  145. if('?' == C) { // When we find a - we check for ?>
  146. if('>' == Data.Data(Index + 1)) { // If we foudn the end we're done!
  147. Index += 2; // Move the Index past the end
  148. break; // and break out of the loop.
  149. }
  150. } // If C wasn't what we're after
  151. ++Index; // move the index past this byte.
  152. }
  153. return LineCount; // In the end return the line count.
  154. }
  155. // Eat Attribute Count Lines
  156. // Another variation of Eat Spaces - this time to eat unknown attributes.
  157. int eatAttributeCountLines(XMLReaderData& Data, int& Index) { // Eat Attribute ( name='data' )
  158. int LineCount = 0; // Keep the line count here.
  159. char C = 0; // Keep the current character here.
  160. while(isNameChar(Data.Data(Index))) ++Index; // Eat through the name.
  161. LineCount += eatSpacesCountLines(Data, Index); // Eat any spaces.
  162. if('=' != Data.Data(Index)) { // We should have found our = sign.
  163. return LineCount; // If we did NOT then we're done.
  164. } else { // If we did, then we're still
  165. ++Index; // going - so move past it.
  166. }
  167. LineCount += eatSpacesCountLines(Data, Index); // Eat any extra spaces.
  168. C = Data.Data(Index); // Grab the next byte.
  169. if( // It should be either a
  170. '\'' != Data.Data(Index) && // single quote or a
  171. '\"' != Data.Data(Index) // double quote.
  172. ) { // If it is neither of these
  173. return LineCount; // then we are done.
  174. } else { // If it was a quote then
  175. ++Index; // get ready to go.
  176. }
  177. while(Data.Data(Index) != C) { // Carefully eat the data.
  178. if(0 == Data.Data(Index)) { // If we run out of Data
  179. return LineCount; // we are done.
  180. } else
  181. if('\n' == Data.Data(Index)) { // If we find a newline then
  182. ++LineCount; // we count it.
  183. }
  184. ++Index; // Whatever it is move past it.
  185. } // Once we've found our ending
  186. ++Index; // quote, we move past it and
  187. return LineCount; // return our Line count.
  188. }
  189. // Eat DocSpecs Count Lines
  190. // Another variation of Eat Spaces - this time to eat unknown elements.
  191. int eatElementCountLines(XMLReaderData& Data, int& Index) { // Eat Element ( <name>..</name> )
  192. int LineCount = 0; // Keep the line count here.
  193. // Are we on a tag?
  194. if( // If we are on an element tag then
  195. '<' != Data.Data(Index) || // it will start with a < followed by
  196. false == isNameChar(Data.Data(Index + 1)) // a name char (usually alpha).
  197. ) { // If that is not the case then
  198. return 0; // we are already done.
  199. }
  200. // Capture the tag name position.
  201. ++Index; // Move the Index to the start of the
  202. int NameIndex = Index; // name and record that spot.
  203. while(isNameChar(Data.Data(Index))) ++Index; // Move the Index past the name.
  204. int NameEndex = Index; // Record the end position.
  205. // Scan for the end of this tag.
  206. for(;;) { // We're looking for a > character.
  207. if(0 == Data.Data(Index)) { // If we run out of data
  208. return LineCount; // we are done.
  209. }
  210. LineCount += eatSpacesCountLines(Data, Index); // Eat any spaces.
  211. if( // Check for an empty element tag.
  212. '/' == Data.Data(Index) && // It will look like a /
  213. '>' == Data.Data(Index + 1) // followed by a >
  214. ) { // If this is an empty element
  215. Index += 2; // Move past it and return our
  216. return LineCount; // Line Count... consider it
  217. } // eaten.
  218. if('>' == Data.Data(Index)) { // If we come to an ordinary end
  219. ++Index; // of element start tag then move
  220. break; // past it and break out for the
  221. } // next phase.
  222. ++Index; // Just move past anything else.
  223. }
  224. // At this point we've passed the start tag for this element and
  225. // we know it's name. We also know the element is not empty so we'll
  226. // need to go inside it, eat those things, and look for it's end
  227. // tag.
  228. // Scan for the matching end tag and eat children.
  229. while( // Keep going until we get to
  230. '<' != Data.Data(Index) || // an end tag (starts with < followed
  231. '/' != Data.Data(Index + 1) // by a /). If we get to something that
  232. ) { // isn't a tag we're done anyway.
  233. int CheckIndex = Index; // Keep track of where we start.
  234. LineCount += eatNonTagTextCountLines(Data, Index); // Eat up to the next < we encounter.
  235. LineCount += eatElementCountLines(Data, Index); // Eat any elements we encounter.
  236. LineCount += eatCommentsCountLines(Data, Index); // Eat any comments we encounter.
  237. LineCount += eatDocSpecsCountLines(Data, Index); // Eat any doc specs we encounter.
  238. // If we stop moving break out!
  239. if(CheckIndex == Index) { // If we didn't move at all then
  240. break; // we need to break out. Could be
  241. } // out of data or just confused.
  242. };
  243. if( // If we find we are not even on
  244. '<' != Data.Data(Index) || // an end tag then we'll just quit
  245. '/' != Data.Data(Index + 1) // right now.
  246. ) {
  247. return LineCount; // Even so we return our line count.
  248. }
  249. // If we find an end tag - it had better be the one we want.
  250. // If it is not then we'll return with the index pointing at the
  251. // offending end tag so that parent instances will have a shot at it
  252. // and/or discover the problem.
  253. int t = 0; // t is for terminus, it stays in scope.
  254. for(t = 0; (NameIndex + t) < NameEndex; t++) { // Scan over the name and make sure
  255. if(Data.Data(NameIndex + t) != Data.Data(Index + 2 + t)) { // it matches character by character.
  256. return LineCount; // If any don't match, the end tag is
  257. } // wron so we return w/ Index pointing
  258. } // at the bad end tag.
  259. if('>' == Data.Data(Index + 2 + t)) { // If the name matched and the next
  260. Index += (3 + t); // character is our > then we move the
  261. } // Index past it - all is good.
  262. // If not then we leave the index.
  263. return LineCount; // Either way we return the Line Count.
  264. }
  265. // Copy Data and Count Lines
  266. // At some point in the parsing, we need to extract content from our Data
  267. // stream and convert it into a null terminated c string. While we're at it
  268. // we also need to keep track of any new-line characters we cross so we will
  269. // still know what line we're on. This function makes that task a one-liner.
  270. int copyDataCountLines(char* Bfr, XMLReaderData& Data, int Start, int End) {
  271. int Lines = 0; // Keep track of the lines we cross.
  272. int DataIndex = Start; // The Data index is separate from
  273. int BfrIndex = 0; // our Bfr index.
  274. char C = 0; // We will be looking at each character.
  275. while(DataIndex < End) { // While there's more segment to do...
  276. C = Data.Data(DataIndex); // Grab each byte.
  277. Bfr[BfrIndex] = C; // Copy it to our buffer.
  278. if('\n' == C) { // Check to see if it's a new-line
  279. ++Lines; // and count it if it is.
  280. }
  281. ++BfrIndex; // Move our buffer and our
  282. ++DataIndex; // data index pointers and
  283. } // keep on going.
  284. Bfr[BfrIndex] = 0; // At the end, null terminate.
  285. return Lines; // Return our line count.
  286. }
  287. //// XMLReader Element /////////////////////////////////////////////////////
  288. XMLReaderElement::~XMLReaderElement() { // The descrutor clears and deletes all!
  289. // A XMLReader Element is "in charge of" or "owns" all of it's
  290. // down-stream components. So, when it is destroyed, it is responsible
  291. // for destroying all of those parts to prevent memory leaks.
  292. // Delete my attributes
  293. if(0 < myAttributes.size()) { // If we have attributes...
  294. list<XMLReaderAttribute*>::iterator iAttribute; // Iterate through our attributes list.
  295. iAttribute = myAttributes.begin(); // Start at the beginning and
  296. while(iAttribute != myAttributes.end()) { // loop through the whole list.
  297. delete (*iAttribute); // Delete each attribute
  298. iAttribute++; // then move the iterator.
  299. } // When we're done deleting them
  300. myAttributes.clear(); // clear the list.
  301. }
  302. // Delete my sub-elements
  303. if(0 < myElements.size()) { // If we have elements...
  304. list<XMLReaderElement*>::iterator iElement; // Iterate through our elements list.
  305. iElement = myElements.begin(); // Start at the beginning and
  306. while(iElement != myElements.end()) { // loop through the whole list.
  307. delete (*iElement); // Delete each element
  308. iElement++; // then move the iterator.
  309. } // When we're done deleting them
  310. myElements.clear(); // clear the list.
  311. }
  312. // Delete my mnemonics
  313. if(0 < myMnemonics.size()) { // If we have mnemonics...
  314. list<XMLReaderMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list.
  315. iMnemonic = myMnemonics.begin(); // Start at the beginning and
  316. while(iMnemonic != myMnemonics.end()) { // loop through the whole list.
  317. delete (*iMnemonic); // Delete each mnemonic
  318. iMnemonic++; // then move the iterator.
  319. } // When we're done deleting them
  320. myMnemonics.clear(); // clear the list.
  321. }
  322. // Delete my translators
  323. if(0 < myTranslators.size()) { // If we have translators...
  324. list<XMLReaderTranslator*>::iterator iTranslator; // Iterate through our translators list.
  325. iTranslator = myTranslators.begin(); // Start at the beginning and
  326. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  327. delete (*iTranslator); // Delete each translator
  328. iTranslator++; // then move the iterator.
  329. } // When we're done deleting them
  330. myTranslators.clear(); // clear the list.
  331. }
  332. // zero things out
  333. myLine = 0; // If I'm going away then I will leave
  334. myIndex = 0; // with everything at zero and clean.
  335. myLength = 0;
  336. myCleanFlag = true;
  337. }
  338. XMLReaderElement& XMLReaderElement::Element( // Mapping factory for convenience,
  339. const string Name, // requires a name, of course,
  340. XMLReaderTranslator& newTranslator) { // Add a Translator to this element.
  341. XMLReaderElement* N = new XMLReaderElement( // Create a new Element with the
  342. Name, // name provided and
  343. (*this)); // myself as the parent.
  344. myElements.push_back(N); // Add it to the list.
  345. N->mapTo(newTranslator); // Map the translator to it.
  346. return (*N); // Return the new element.
  347. }
  348. XMLReaderElement& XMLReaderElement::Element( // Mapping factory for convenience,
  349. const string Name, // requires a name, of course,
  350. string& x, string init) { // Map to a string.
  351. XMLReaderElement* N = new XMLReaderElement( // Create a new Element with the
  352. Name, // name provided and
  353. (*this)); // myself as the parent.
  354. myElements.push_back(N); // Add it to the list.
  355. N->mapTo(x, init); // Map the variable into it.
  356. return (*N); // Return the new element.
  357. }
  358. XMLReaderElement& XMLReaderElement::Element( // Mapping factory for convenience,
  359. const string Name, // requires a name, of course,
  360. int& x, int init, int radix) { // Map to an int.
  361. XMLReaderElement* N = new XMLReaderElement( // Create a new Element with the
  362. Name, // name provided and
  363. (*this)); // myself as the parent.
  364. myElements.push_back(N); // Add it to the list.
  365. N->mapTo(x, init, radix); // Map the variable into it.
  366. return (*N); // Return the new element.
  367. }
  368. XMLReaderElement& XMLReaderElement::Element( // Mapping factory for convenience,
  369. const string Name, // requires a name, of course,
  370. double& x, double init) { // Map to a double.
  371. XMLReaderElement* N = new XMLReaderElement( // Create a new Element with the
  372. Name, // name provided and
  373. (*this)); // myself as the parent.
  374. myElements.push_back(N); // Add it to the list.
  375. N->mapTo(x, init); // Map the variable into it.
  376. return (*N); // Return the new element.
  377. }
  378. XMLReaderElement& XMLReaderElement::Element( // Mapping factory for convenience,
  379. const string Name, // requires a name, of course,
  380. bool& x, bool init) { // Map to a boolean.
  381. XMLReaderElement* N = new XMLReaderElement( // Create a new Element with the
  382. Name, // name provided and
  383. (*this)); // myself as the parent.
  384. myElements.push_back(N); // Add it to the list.
  385. N->mapTo(x, init); // Map the variable into it.
  386. return (*N); // Return the new element.
  387. }
  388. XMLReaderAttribute& XMLReaderElement::Attribute(const string Name) { // Add an attribute using a c++ string.
  389. XMLReaderAttribute* N = // Create a new attribute by name and
  390. new XMLReaderAttribute(Name, (*this)); // provide myself as the parent.
  391. myCleanFlag = false; // New attributes make us dirty.
  392. myAttributes.push_back(N); // Add the attribute to my list,
  393. return (*N); // dereference and return it.
  394. }
  395. XMLReaderAttribute& XMLReaderElement::Attribute( // Mapping factory for convenience,
  396. const string Name, // requires a name, of course,
  397. XMLReaderTranslator& newTranslator) { // Add a Translator to this element.
  398. myCleanFlag = false; // New attributes make us dirty.
  399. XMLReaderAttribute* N = // Create a new attribute by name and
  400. new XMLReaderAttribute(Name, (*this)); // provide myself as the parent.
  401. myAttributes.push_back(N); // Add the attribute to my list.
  402. N->mapTo(newTranslator); // Map in the provided translator.
  403. return(*N); // Dereference and return the attribute.
  404. }
  405. XMLReaderAttribute& XMLReaderElement::Attribute( // Mapping factory for convenience,
  406. const string Name, // requires a name, of course,
  407. string& x, string init) { // Map to a string.
  408. myCleanFlag = false; // New attributes make us dirty.
  409. XMLReaderAttribute* N = // Create a new attribute by name and
  410. new XMLReaderAttribute(Name, (*this)); // provide myself as the parent.
  411. myAttributes.push_back(N); // Add the attribute to my list.
  412. N->mapTo(x, init); // Map in the provided variable.
  413. return(*N); // Dereference and return the attribute.
  414. }
  415. XMLReaderAttribute& XMLReaderElement::Attribute( // Mapping factory for convenience,
  416. const string Name, // requires a name, of course,
  417. int& x, int init, int radix) { // Map to an int.
  418. myCleanFlag = false; // New attributes make us dirty.
  419. XMLReaderAttribute* N = // Create a new attribute by name and
  420. new XMLReaderAttribute(Name, (*this)); // provide myself as the parent.
  421. myAttributes.push_back(N); // Add the attribute to my list.
  422. N->mapTo(x, init, radix); // Map in the provided variable.
  423. return(*N); // Dereference and return the attribute.
  424. }
  425. XMLReaderAttribute& XMLReaderElement::Attribute( // Mapping factory for convenience,
  426. const string Name, // requires a name, of course,
  427. double& x, double init) { // Map to a double.
  428. myCleanFlag = false; // New attributes make us dirty.
  429. XMLReaderAttribute* N = // Create a new attribute by name and
  430. new XMLReaderAttribute(Name, (*this)); // provide myself as the parent.
  431. myAttributes.push_back(N); // Add the attribute to my list.
  432. N->mapTo(x, init); // Map in the provided variable.
  433. return(*N); // Dereference and return the attribute.
  434. }
  435. XMLReaderAttribute& XMLReaderElement::Attribute( // Mapping factory for convenience,
  436. const string Name, // requires a name, of course,
  437. bool& x, bool init) { // Map to a boolean.
  438. myCleanFlag = false; // New attributes make us dirty.
  439. XMLReaderAttribute* N = // Create a new attribute by name and
  440. new XMLReaderAttribute(Name, (*this)); // provide myself as the parent.
  441. myAttributes.push_back(N); // Add the attribute to my list.
  442. N->mapTo(x, init); // Map in the provided variable.
  443. return(*N); // Dereference and return the attribute.
  444. }
  445. XMLReaderElement& XMLReaderElement::mapTo( // Add a Translator to this element.
  446. XMLReaderTranslator& newTranslator) { // Given a new translator I can own,
  447. myTranslators.push_back(&newTranslator); // add the translator to my list
  448. myCleanFlag = false; // get dirty for the new translator
  449. return(*this); // then dereference and return myself.
  450. }
  451. XMLReaderElement& XMLReaderElement::mapTo( // Map to a string.
  452. string& x, string init) { // Given a string and init value,
  453. XMLReaderTranslator* N = // create a new translator for it
  454. new StringTranslator(x, init); // with the values i'm given,
  455. myTranslators.push_back(N); // push it onto my list, then
  456. myCleanFlag = false; // get dirty for the new translator
  457. return(*this); // then dereference and return myself.
  458. }
  459. XMLReaderElement& XMLReaderElement::mapTo( // Map to an int.
  460. int& x, int init, int radix) { // Given an int and init values,
  461. XMLReaderTranslator* N = // create a new translator for it
  462. new IntegerTranslator(x, init, radix); // with the values i'm given,
  463. myTranslators.push_back(N); // push it onto my list, then
  464. myCleanFlag = false; // get dirty for the new translator
  465. return(*this); // then dereference and return myself.
  466. }
  467. XMLReaderElement& XMLReaderElement::mapTo( // Map to a double.
  468. double& x, double init) { // Given a double and it's init value,
  469. XMLReaderTranslator* N = // create a new translator for it
  470. new DoubleTranslator(x, init); // with the values i'm given,
  471. myTranslators.push_back(N); // push it onto my list, then
  472. myCleanFlag = false; // get dirty for the new translator
  473. return(*this); // then dereference and return myself.
  474. }
  475. XMLReaderElement& XMLReaderElement::mapTo( // Map to a boolean.
  476. bool& x, bool init) { // Given a bool and it's init value,
  477. XMLReaderTranslator* N = // create a new translator for it
  478. new BoolTranslator(x, init); // with the values i'm given,
  479. myTranslators.push_back(N); // push it onto my list, then
  480. myCleanFlag = false; // get dirty for the new translator
  481. return(*this); // then dereference and return myself.
  482. }
  483. XMLReaderElement& XMLReaderElement::RawData(std::string &RawData) {
  484. myRawTranslator.setRawDataAssignment(RawData);
  485. return(*this);
  486. }
  487. XMLReaderElement& XMLReaderElement::RawIndex(int &Index, int &Endex) {
  488. myRawTranslator.setIndexAssignment(Index, Endex);
  489. return(*this);
  490. }
  491. void XMLReaderElement::initialize() { // Reset all translators to defaults.
  492. // Initialize the elements below me
  493. if(0 < myElements.size()) { // If we have elements...
  494. list<XMLReaderElement*>::iterator iElement; // Iterate through our elements list.
  495. iElement = myElements.begin(); // Start at the beginning and
  496. while(iElement != myElements.end()) { // loop through the whole list.
  497. (*iElement)->initialize(); // Initialize each element
  498. iElement++; // then move the iterator.
  499. }
  500. }
  501. // Once that's done, see about myself
  502. if(true == myCleanFlag) return; // If I'm already clean, return.
  503. // Initialize my own translators
  504. if(0 < myTranslators.size()) { // If we have translators...
  505. list<XMLReaderTranslator*>::iterator iTranslator; // Iterate through our translators list.
  506. iTranslator = myTranslators.begin(); // Start at the beginning and
  507. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  508. (*iTranslator)->initialize(); // Initialize each translator
  509. iTranslator++; // then move the iterator.
  510. }
  511. }
  512. // Initialize my own attributes
  513. if(0 < myAttributes.size()) { // If we have attributes...
  514. list<XMLReaderAttribute*>::iterator iAttribute; // Iterate through our attributes list.
  515. iAttribute = myAttributes.begin(); // Start at the beginning and
  516. while(iAttribute != myAttributes.end()) { // loop through the whole list.
  517. (*iAttribute)->initialize(); // Initialize each attribute
  518. ++iAttribute; // then move the iterator.
  519. }
  520. }
  521. // Zero things out
  522. myLine = 0; // Initialized means to be as if
  523. myIndex = 0; // no interpet() call has been made.
  524. myLength = 0;
  525. *wasProcessed = false;
  526. // At this point we know we are clean
  527. myCleanFlag = true; // Clean as a whistle!
  528. }
  529. void XMLReaderElement::runStartXMLerators(XMLReaderData& D) { // Does what it says ;-)
  530. list<XMLerator*>::iterator iXMLerator; // Iterate through our XMLerators list.
  531. iXMLerator = myStartXMLerators.begin(); // Start at the beginning and
  532. while(iXMLerator != myStartXMLerators.end()) { // loop through the whole list.
  533. (** iXMLerator)(*this, D); // Launch each configurator with self.
  534. ++iXMLerator; // Move to the next.
  535. }
  536. }
  537. void XMLReaderElement::runEndXMLerators(XMLReaderData& D) { // Does what it says ;-)
  538. list<XMLerator*>::iterator iXMLerator; // Iterate through our XMLerators list.
  539. iXMLerator = myEndXMLerators.begin(); // Start at the beginning and
  540. while(iXMLerator != myEndXMLerators.end()) { // loop through the whole list.
  541. (** iXMLerator)(*this, D); // Launch each configurator with self.
  542. ++iXMLerator; // Move to the next.
  543. }
  544. }
  545. bool XMLReaderElement::interpret(XMLReaderData& Data) { // (re) Interpret this data.
  546. int Index = Data.Index(); // Our working index.
  547. int Startdex = 0; // Where our data starts.
  548. int Stopdex = 0; // Where our data stops.
  549. int NewLines = 0; // Keep a count of new lines.
  550. //// Pre-Processing / Cleanup / Find <name...
  551. // Eat anything up to the first <
  552. // Eat any comments <!-- this is a comment etc -->
  553. // Eat any doctype headers <?xml version="1.0" etc ?>
  554. for(;;) {
  555. int StartingPoint = Index; // Where did we start each pass?
  556. NewLines += eatNonTagTextCountLines(Data, Index); // Eat any spaces we find.
  557. NewLines += eatCommentsCountLines(Data, Index); // Eat any <!-- -->
  558. NewLines += eatDocSpecsCountLines(Data, Index); // Eat any <? ?>
  559. if(StartingPoint == Index) { break; } // If we didn't move on this pass
  560. } // then we are done with cleanup!
  561. // Update Data to move past any of the preceeding junk. This way, other
  562. // element processors will be able to skip any cleanup work we did.
  563. Data.Index(Index); // Move the Index.
  564. Data.addNewLines(NewLines); // Update the Line Number.
  565. NewLines = 0; // Reset our internal Lines counter.
  566. // Find my name.
  567. if(Data.Data(Index) != '<') { // If we're not on a tag open then
  568. return false; // we are not at an element.
  569. } else { // Otherwise we are safe to move
  570. ++Index; // past it and scan for our name.
  571. }
  572. for(unsigned int I = 0; I < myName.length(); I++) { // For the length of our name,
  573. char x = Data.Data(Index + I); // get each corresponding Data byte
  574. if(x != myName.at(I)) { // check it sudden death style.
  575. return false; // No-Match means we are not it.
  576. }
  577. } // If the name checks out then
  578. if (!isspace(Data.Data(Index + myName.length())) && // Next character must be whitespace
  579. (Data.Data(Index + myName.length()) != '/') && // or closing of the tag.
  580. (Data.Data(Index + myName.length()) != '>') ) {
  581. return false;
  582. }
  583. Index += myName.length(); // move the Index past our name.
  584. // At this point we have found ourselves so we will activate and interpret
  585. // our Data.
  586. if(true == myInitOnInterpretFlag) { // If we are supposed to Init before
  587. initialize(); // we Interpret then do it.
  588. }
  589. // Since we are activating we must set our state so we know where we are.
  590. myLine = Data.Line(); // We know where we start...
  591. myIndex = Data.Index(); // We know our index...
  592. myLength = 0; // We don't know our length yet.
  593. runStartXMLerators(Data); // Run the start XMLerators.
  594. myCleanFlag = false; // Now we start to get dirty.
  595. // First, we will run through any attributes we have.
  596. bool ThisIsAnEmptyElement = false; // We'll use this to signal empties.
  597. for(;;) { // This is how we roll..
  598. NewLines += eatSpacesCountLines(Data, Index); // Eat any spaces we find.
  599. Data.Index(Index); // Move the Index.
  600. Data.addNewLines(NewLines); // Update the Line Number.
  601. NewLines = 0; // Reset our internal Lines counter.
  602. // Now we look at the next character. Either it's an attribute, or
  603. // it's the end of the tag, or it's some kind of junk. If it's junk
  604. // we will skip it. We will continue parsing until we get to the end
  605. // of the Data or the end of the opening tag (either stopping at / if
  606. // the element is empty or > if the element is not empty.
  607. if(isalpha(Data.Data(Index))) { // If it looks like an attribute...
  608. bool ParseHappened = false; // Start pessimistically at each pass.
  609. list<XMLReaderAttribute*>::iterator iAttribute; // Iterate through our attributes list.
  610. iAttribute = myAttributes.begin(); // Start at the beginning and
  611. while(iAttribute != myAttributes.end()) { // loop through the whole list.
  612. ParseHappened = (* iAttribute)->interpret(Data); // Have each attribute interpret(Data)
  613. ++iAttribute; // Remember to move to the next one.
  614. if(ParseHappened) break; // If a Parse Happened, break the inner
  615. } // loop and start the next pass.
  616. if(false == ParseHappened) { // If we didn't recognize the attribute
  617. NewLines += eatAttributeCountLines(Data, Index); // then eat it.
  618. Data.Index(Index); // Sync up our Index.
  619. Data.addNewLines(NewLines); // Sync up our NewLines.
  620. NewLines = 0; // Zero our NewLines count.
  621. } else { // If we DID recognize the attribute then
  622. Index = Data.Index(); // sync up our Index for the next one.
  623. }
  624. } else
  625. if(0 == Data.Data(Index)) { // If it looks like the end of Data
  626. break; // we will break out - we're done.
  627. } else
  628. if( // If it looks like the end of an empty
  629. '/' == Data.Data(Index) && // element (starts with / and ends with
  630. '>' == Data.Data(Index + 1) // >) then this must be an empty element.
  631. ) {
  632. ThisIsAnEmptyElement = true; // Set the empty element flag and
  633. Index += 2; // Move past the end of the tag and
  634. break; // break out of the loop.
  635. } else
  636. if('>' == Data.Data(Index)) { // If it looks like the end of an open
  637. Index += 1; // tag then move past the end and
  638. break; // break out of the loop.
  639. } else { // If it looks like anything else then
  640. ++Index; // we don't know what it is so we creep
  641. } // past it.
  642. }
  643. Data.Index(Index); // Sync up our index
  644. // At this point we're done processing our open tag and any attributes it
  645. // may have contained, and we are syncrhonized with Data.
  646. if(ThisIsAnEmptyElement) { // If the element was self closing then
  647. myEndex = Data.Index() - 1;
  648. myRawTranslator.translate(myIndex, myEndex, Data);
  649. runEndXMLerators(Data); // run the End XMLerators and return
  650. *wasProcessed = true;
  651. return true; // true to the caller.
  652. }
  653. // At this point we have contents and/or elements to process. We will keep
  654. // track of any contents using Startdex and Stopdex.
  655. Startdex = Index;
  656. // Now we will process through any elements there may be until we reach
  657. // our end tag. If we have no sub-elements listed, we'll simply skip that
  658. // step on each pass... So, we roll like this:
  659. // Check for end of Data.
  660. // Check for our end tag.
  661. // Check for a sub-element.
  662. // If none of these work then break out.
  663. for(;;) { // Loop through our content like this.
  664. int CheckPoint = Index; // Where did we start each pass?
  665. // Check for end of data //
  666. if(0 == Data.Data(Index)) { // If we are at end of data then we're
  667. return false; // broken so we return false.
  668. } else
  669. // Check for our own end tag //
  670. if( // If this looks like an end tag
  671. '<' == Data.Data(Index) && // (Starts with < followed by
  672. '/' == Data.Data(Index + 1) // a / character)
  673. ) { // Then it _should_ be our own.
  674. Stopdex = Index; // Capture this position for content.
  675. Index += 2; // Move Index to where the name starts.
  676. for(unsigned int I = 0; I < myName.length(); I++) { // For the length of the name,
  677. char x = Data.Data(Index + I); // check each corresponding Data byte.
  678. if(x != myName.at(I)) { // If we fail to match at any point
  679. return false; // then things are very broken
  680. } // so we return false.
  681. } // If the name checks out then
  682. Index += myName.length(); // move past our name.
  683. if('>' != Data.Data(Index)) { // Being very strict, if the next
  684. return false; // byte is not > then fail!
  685. } else { // If all goes well then we move
  686. ++Index; // past the > and we are done.
  687. break; // Break to move to the next step.
  688. }
  689. } else
  690. // Check for a subordinate element //
  691. if( // If this looks like an element
  692. '<' == Data.Data(Index) && // starting with < and a name
  693. isalpha(Data.Data(Index + 1)) // beginning with an alpha character...
  694. ) {
  695. bool ElementHappened = false; // We'll check our elements.
  696. Data.Index(Index); // Sync our index.
  697. Data.addNewLines(NewLines); // Sync our lines.
  698. NewLines = 0; // Reset our new lines count.
  699. if(0 < myElements.size()) { // If we have elements check them.
  700. list<XMLReaderElement*>::iterator iElement; // Iterate through our elements list.
  701. iElement = myElements.begin(); // Start at the beginning and
  702. while(iElement != myElements.end()) { // loop through the whole list.
  703. XMLReaderElement& doNode = **iElement; // Grab the element we're on.
  704. ElementHappened = doNode.interpret(Data); // Have each element interpret(Data)
  705. Index = Data.Index(); // Capitalze on any cleanup work.
  706. ++iElement; // Remember to move to the next.
  707. if(ElementHappened) break; // If an Element Happened, break the
  708. } // loop and start the next pass.
  709. if(false == ElementHappened) { // If we did not recognize the Element
  710. NewLines += eatElementCountLines(Data, Index); // then eat it *****
  711. Data.Index(Index); // Resync our Index.
  712. Data.addNewLines(NewLines); // Sync our line count.
  713. NewLines = 0; // Reset our internal count.
  714. }
  715. } else { // If we don't own any elements then
  716. NewLines += eatElementCountLines(Data, Index); // eat the ones we find.
  717. }
  718. // Handle any untidy messes here //
  719. } else { // If we're on something unknown then
  720. NewLines += eatSpacesCountLines(Data, Index); // Eat any spaces we find.
  721. NewLines += eatCommentsCountLines(Data, Index); // Eat any <!-- -->
  722. NewLines += eatDocSpecsCountLines(Data, Index); // Eat any <? ?>
  723. NewLines += eatNonTagTextCountLines(Data, Index); // Eat any non tag bytes.
  724. Data.Index(Index); // Sync our Index.
  725. Data.addNewLines(NewLines); // Sync our line number.
  726. NewLines = 0; // Clear our lines count.
  727. }
  728. // If we get stuck looping on something we don't know how to clean
  729. // and don't know how to interpret then we need to break out of the
  730. // insanity. This way, anything that doesn't make sense won't be able
  731. // to stall us or cause us to interpret something incorrectly later
  732. // on... If we're the top element, the interpret() process will end.
  733. // If we are deeper then it is likely our superirors will also not
  734. // understand and so they will also end the same way.
  735. if(CheckPoint == Index) return false; // If we haven't moved, punt!
  736. }
  737. // When we're done with our loop sync up with Data again.
  738. Data.Index(Index); // Sync up our Index.
  739. Data.addNewLines(NewLines); // Sync up our NewLines count.
  740. NewLines = 0; // zero our local count.
  741. // Once our elements have been procssed and we get to our end tag we can
  742. // process our content (if we have Translators registered).
  743. if(
  744. 0 < myTranslators.size() && // If we have translators and
  745. Stopdex > Startdex // we have content to translate
  746. ) { // then translate the content!
  747. // Create the Content buffer...
  748. int BfrSize = Stopdex - Startdex + 1; // How big a buffer do we need?
  749. vector<char> heapBfr(BfrSize, 0); // Make one that size.
  750. char *Bfr = &(heapBfr[0]);
  751. copyDataCountLines(Bfr, Data, Startdex, Stopdex); // Get our data and ignore our lines.
  752. // Now we can get on with translation.
  753. char* TranslationData = Bfr; // TranslationData is what we translate.
  754. // Translate our data by Mnemonic
  755. if(0 < myMnemonics.size()) { // If we have mnemonics...
  756. list<XMLReaderMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list.
  757. iMnemonic = myMnemonics.begin(); // Start at the beginning and
  758. while(iMnemonic != myMnemonics.end()) { // loop through the whole list.
  759. if(true == ((*iMnemonic)->test(TranslationData))) { // Check to see if the mnemonic matches.
  760. TranslationData = const_cast<char*>( // If it does match, substitute it's
  761. (*iMnemonic)->Value().c_str()); // value for translation and stop
  762. break; // looking.
  763. } else { // If it does not match, move to the
  764. ++iMnemonic; // next mnemonic and test again.
  765. } // That is, until we run out of
  766. } // mnemonics to test.
  767. }
  768. // Put our TranslationData through each Translator.
  769. list<XMLReaderTranslator*>::iterator iTranslator; // Iterate through our translators list.
  770. iTranslator = myTranslators.begin(); // Start at the beginning and
  771. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  772. (*iTranslator)->translate(TranslationData); // Pass the data to each one then
  773. ++iTranslator; // move on to the next.
  774. }
  775. }
  776. // And finally, after all is done successfully...
  777. myEndex = Data.Index() - 1;
  778. myRawTranslator.translate(myIndex, myEndex, Data);
  779. runEndXMLerators(Data); // Launch the End XMLerators.
  780. *wasProcessed = true;
  781. return true; // Return our success!
  782. }
  783. //// XMLReader Attribute ///////////////////////////////////////////////////
  784. XMLReaderAttribute::~XMLReaderAttribute() { // Crush, Kill, Destroy!
  785. // Delete my mnemonics
  786. if(0 < myMnemonics.size()) { // If we have mnemonics...
  787. list<XMLReaderMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list.
  788. iMnemonic = myMnemonics.begin(); // Start at the beginning and
  789. while(iMnemonic != myMnemonics.end()) { // loop through the whole list.
  790. delete (*iMnemonic); // Delete each mnemonic
  791. iMnemonic++; // then move the iterator.
  792. } // When we're done deleting them
  793. myMnemonics.clear(); // clear the list.
  794. }
  795. // Delete my translators
  796. if(0 < myTranslators.size()) { // If we have translators...
  797. list<XMLReaderTranslator*>::iterator iTranslator; // Iterate through our translators list.
  798. iTranslator = myTranslators.begin(); // Start at the beginning and
  799. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  800. delete (*iTranslator); // Delete each translator
  801. iTranslator++; // then move the iterator.
  802. } // When we're done deleting them
  803. myTranslators.clear(); // clear the list.
  804. }
  805. // zero things out
  806. myLine = 0; // If I'm going away then I will leave
  807. myIndex = 0; // with everything at zero and clean.
  808. myLength = 0;
  809. }
  810. XMLReaderAttribute& XMLReaderAttribute::mapTo( // Add a Translator to this attribute.
  811. XMLReaderTranslator& newTranslator) { // Given a new translator I can own,
  812. myTranslators.push_back(&newTranslator); // add the translator to my list
  813. myParent.notifyDirty(); // get dirty for the new translator
  814. return(*this); // then dereference and return myself.
  815. }
  816. XMLReaderAttribute& XMLReaderAttribute::mapTo( // Map to a string.
  817. string& x, string init) { // Given a string and init value,
  818. XMLReaderTranslator* N = // create a new translator for it
  819. new StringTranslator(x, init); // with the values i'm given,
  820. myTranslators.push_back(N); // push it onto my list, then
  821. myParent.notifyDirty(); // get dirty for the new translator
  822. return(*this); // dereference and return myself.
  823. }
  824. XMLReaderAttribute& XMLReaderAttribute::mapTo( // Map to an int.
  825. int& x, int init, int radix) { // Given an int and init values,
  826. XMLReaderTranslator* N = // create a new translator for it
  827. new IntegerTranslator(x, init, radix); // with the values i'm given,
  828. myTranslators.push_back(N); // push it onto my list, then
  829. myParent.notifyDirty(); // get dirty for the new translator
  830. return(*this); // dereference and return myself.
  831. }
  832. XMLReaderAttribute& XMLReaderAttribute::mapTo( // Map to a double.
  833. double& x, double init) { // Given a double and it's init value,
  834. XMLReaderTranslator* N = // create a new translator for it
  835. new DoubleTranslator(x, init); // with the values i'm given,
  836. myTranslators.push_back(N); // push it onto my list, then
  837. myParent.notifyDirty(); // get dirty for the new translator
  838. return(*this); // then dereference and return myself.
  839. }
  840. XMLReaderAttribute& XMLReaderAttribute::mapTo( // Map to a boolean.
  841. bool& x, bool init) { // Given a bool and it's init value,
  842. XMLReaderTranslator* N = // create a new translator for it
  843. new BoolTranslator(x, init); // with the values i'm given,
  844. myTranslators.push_back(N); // push it onto my list, then
  845. myParent.notifyDirty(); // get dirty for the new translator
  846. return(*this); // then dereference and return myself.
  847. }
  848. void XMLReaderAttribute::initialize() { // Reset all translators to defaults.
  849. if(0 < myTranslators.size()) { // If we have translators...
  850. list<XMLReaderTranslator*>::iterator iTranslator; // Iterate through our translators list.
  851. iTranslator = myTranslators.begin(); // Start at the beginning and
  852. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  853. (*iTranslator)->initialize(); // initialize each translator
  854. iTranslator++; // then move the iterator.
  855. } // When we're done deleting them
  856. }
  857. // zero things out
  858. myLine = 0; // Initialized means to be as if
  859. myIndex = 0; // no interpet() call has been made.
  860. myLength = 0;
  861. *wasProcessed = false;
  862. }
  863. bool XMLReaderAttribute::interpret(XMLReaderData& Data) { // (re) Interpret this data.
  864. int Index = Data.Index(); // Our working index.
  865. int Startdex = 0; // Where our data starts.
  866. int Stopdex = 0; // Where our data stops.
  867. int NewLines = 0; // Keep a count of new lines.
  868. // Find our name.
  869. for(unsigned int I = 0; I < myName.length(); I++) { // For the length of the name,
  870. char x = Data.Data(Index + I); // get each corresponding Data byte
  871. if(x != myName.at(I)) { // check it sudden death style.
  872. return false; // No-Match means we are not it.
  873. }
  874. } // If the name checks out then
  875. Index += myName.length(); // move the Index past our name.
  876. NewLines += eatSpacesCountLines(Data, Index); // Eat any spaces we find.
  877. // Find our = sign.
  878. if('=' != Data.Data(Index)) { // Next we should see an =
  879. return false; // If we don't we're done.
  880. } else { // If we do then we can
  881. ++Index; // move past it.
  882. }
  883. NewLines += eatSpacesCountLines(Data, Index); // Eat any spaces we find.
  884. // Find our first quote character.
  885. char QuoteCharacter = 0;
  886. if('\'' == Data.Data(Index) || '\"' == Data.Data(Index)) { // Next we should find ' or "
  887. QuoteCharacter = Data.Data(Index); // If we found it record it then
  888. ++Index; Startdex = Index; // move to and record our start of data.
  889. } else { // If we don't
  890. return false; // we are done.
  891. }
  892. // Find our last quote character.
  893. for(;;) { // Here is how we will roll...
  894. char C = Data.Data(Index); // Grab the character at Index.
  895. if(0 == C) { // If we run out of Data then
  896. return false; // We didn't find anything.
  897. }
  898. if(QuoteCharacter == C) { // If we find our QuoteCharacter
  899. Stopdex = Index; // we have our Stopdex and
  900. break; // we can stop the loop.
  901. }
  902. ++Index; // Otherwise keep on looking.
  903. }
  904. // Read our data.
  905. int BfrSize = Stopdex - Startdex + 1; // How big a buffer do we need?
  906. // Add byte for null terminator.
  907. vector<char> heapBfr(BfrSize, 0); // Make one that size.
  908. char *Bfr = &(heapBfr[0]);
  909. NewLines += copyDataCountLines(Bfr, Data, Startdex, Stopdex); // Get our data and count our lines.
  910. // Now we can get on with translation.
  911. char* TranslationData = Bfr; // TranslationData is what we translate.
  912. // Translate our data by Mnemonic
  913. if(0 < myMnemonics.size()) { // If we have mnemonics...
  914. list<XMLReaderMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list.
  915. iMnemonic = myMnemonics.begin(); // Start at the beginning and
  916. while(iMnemonic != myMnemonics.end()) { // loop through the whole list.
  917. if(true == ((*iMnemonic)->test(TranslationData))){ // Check to see if the mnemonic matches.
  918. TranslationData = const_cast<char*>( // If it does match, substitute it's
  919. (*iMnemonic)->Value().c_str()); // value for translation and stop
  920. break; // looking.
  921. } else { // If it does not match, move to the
  922. ++iMnemonic; // next mnemonic and test again.
  923. } // That is, until we run out of
  924. } // mnemonics to test.
  925. }
  926. // Put our TranslationData through each Translator.
  927. if(0 < myTranslators.size()) { // We'd better have translators!
  928. list<XMLReaderTranslator*>::iterator iTranslator; // Iterate through our translators list.
  929. iTranslator = myTranslators.begin(); // Start at the beginning and
  930. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  931. (*iTranslator)->translate(TranslationData); // Pass the data to each one and
  932. ++iTranslator; // move on to the next one.
  933. }
  934. }
  935. // Capture our position data.
  936. myLine = Data.Line(); // Capture the line I was on.
  937. myIndex = Data.Index(); // Capture the Index where I started.
  938. myLength = Stopdex + 1 - myIndex; // Capture my segment length.
  939. // Update Data for the next segment.
  940. Data.Index(Stopdex + 1); // Move the Index.
  941. Data.addNewLines(NewLines); // Update the Line Number.
  942. if (attrFunc) {
  943. (*attrFunc)(*this, Data);
  944. }
  945. *wasProcessed = true;
  946. return true; // If we got here, we succeeded!
  947. }
  948. //// Configuratino Data ////////////////////////////////////////////////////////
  949. XMLReaderData::XMLReaderData(const char* Data, int Length) : // Raw constructor from buffer.
  950. myBufferSize(Length), // and it's length.
  951. myIndex(0), // Our Index is zero
  952. myLine(1) { // We start on line 1
  953. myDataBuffer = new char[myBufferSize + 1]; // Allocate a buffer, with
  954. // room for null byte.
  955. memcpy(myDataBuffer, Data, myBufferSize); // Copy the data.
  956. myDataBuffer[myBufferSize] = '\0'; // Null-terminate.
  957. }
  958. XMLReaderData::XMLReaderData(const char* FileName) :
  959. myDataBuffer(NULL), // No data buffer yet.
  960. myBufferSize(0), // No length yet.
  961. myIndex(0), // Our Index is zero
  962. myLine(1) { // We start on line 1
  963. try { // Capture any throws.
  964. ifstream CFGFile(FileName); // Open the file.
  965. CFGFile.seekg(0,ios::end); // Seek to the end
  966. myBufferSize = CFGFile.tellg(); // to find out what size it is.
  967. myDataBuffer = new char[myBufferSize]; // Make a new buffer the right size.
  968. CFGFile.seekg(0,ios::beg); // Seek to the beginning and
  969. CFGFile.read(myDataBuffer, myBufferSize); // read the file into the buffer.
  970. if(CFGFile.bad()) { // If the read failed, we're unusable!
  971. delete[] myDataBuffer; // Delete the buffer
  972. myDataBuffer = NULL; // and Null it's pointer.
  973. myBufferSize = 0; // Set the length to zero.
  974. } // Usually everything will work
  975. CFGFile.close(); // At the end, always close our file.
  976. } catch (...) { // If something went wrong clean up.
  977. if(NULL != myDataBuffer) { // If the data buffer was allocated
  978. delete[] myDataBuffer; // Delete the buffer
  979. myDataBuffer = NULL; // and Null it's pointer.
  980. }
  981. myBufferSize = 0; // The BufferSize will be zero
  982. } // indicating there is no Data.
  983. }
  984. XMLReaderData::XMLReaderData(const string FileName) : // Raw constructor from file.
  985. myDataBuffer(NULL), // No data buffer yet.
  986. myBufferSize(0), // No length yet.
  987. myIndex(0), // Our Index is zero
  988. myLine(1) { // We start on line 1
  989. try { // Capture any throws.
  990. ifstream CFGFile(FileName.c_str()); // Open the file.
  991. CFGFile.seekg(0,ios::end); // Seek to the end
  992. myBufferSize = CFGFile.tellg(); // to find out what size it is.
  993. if (myBufferSize < 0) {
  994. myBufferSize = 0;
  995. return;
  996. }
  997. myDataBuffer = new char[myBufferSize + 1]; // Make a new buffer the right size,
  998. // including null byte at end.
  999. CFGFile.seekg(0,ios::beg); // Seek to the beginning and
  1000. CFGFile.read(myDataBuffer, myBufferSize); // read the file into the buffer.
  1001. myDataBuffer[myBufferSize] = '\0'; // Null-terminate.
  1002. if(CFGFile.bad()) { // If the read failed, we're unusable!
  1003. delete[] myDataBuffer; // Delete the buffer
  1004. myDataBuffer = NULL; // and Null it's pointer.
  1005. myBufferSize = 0; // Set the length to zero.
  1006. } // Usually everything will work
  1007. CFGFile.close(); // At the end, always close our file.
  1008. } catch (...) { // If something went wrong clean up.
  1009. if(NULL != myDataBuffer) { // If the data buffer was allocated
  1010. delete[] myDataBuffer; // Delete the buffer
  1011. myDataBuffer = NULL; // and Null it's pointer.
  1012. }
  1013. myBufferSize = 0; // The BufferSize will be zero
  1014. } // indicating there is no Data.
  1015. }
  1016. XMLReaderData::~XMLReaderData() { // Destroys the internal buffer etc.
  1017. if(NULL != myDataBuffer) { // If we have allocated a buffer,
  1018. delete[] myDataBuffer; // delete that buffer
  1019. myDataBuffer = NULL; // and null the pointer.
  1020. }
  1021. myBufferSize = 0; // Zero everything for safety.
  1022. myIndex = 0;
  1023. myLine = 0;
  1024. }
  1025. std::string XMLReaderData::extract(int Index, int Endex) const {
  1026. int Start = Index, End = Endex;
  1027. if (0 == myBufferSize) return std::string("");
  1028. if (Start < 0) Start = 0;
  1029. if (Start >= myBufferSize) Start = myBufferSize - 1;
  1030. if (Start > End) End = Start;
  1031. if (End >= myBufferSize) End = myBufferSize - 1;
  1032. return string(myDataBuffer, Start, End - Start + 1);
  1033. }
  1034. //// Utilities /////////////////////////////////////////////////////////////////
  1035. // SetTrueOnComplete XMLerator //////////////////////////////////////////////
  1036. XMLeratorSetTrueOnComplete::XMLeratorSetTrueOnComplete() : // Constructor ensures the pointer
  1037. myBoolean(NULL) { // is NULL for safety.
  1038. }
  1039. void XMLeratorSetTrueOnComplete::setup(bool& Target) { // The setup() method links us to a
  1040. myBoolean = &Target; // target boolean.
  1041. }
  1042. void XMLeratorSetTrueOnComplete::operator()( // The operator()
  1043. XMLReaderElement& E, XMLReaderData& D) { // When activated, this fellow
  1044. if(NULL != myBoolean) { // checks it's pointer for safety
  1045. *myBoolean = true; // and if ok, sets the target to
  1046. } // true.
  1047. }
  1048. } // End of CodeDweller namespace.