Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

configuration.cpp 86KB

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