Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

XMLReader.cpp 87KB

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