You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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