Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

configuration.cpp 85KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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; // How big a buffer do we need?
  730. char Bfr[BfrSize]; // Make one that size.
  731. copyDataCountLines(Bfr, Data, Startdex, Stopdex); // Get our data and ignore our lines.
  732. // Now we can get on with translation.
  733. char* TranslationData = Bfr; // TranslationData is what we translate.
  734. // Translate our data by Mnemonic
  735. if(0 < myMnemonics.size()) { // If we have mnemonics...
  736. list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list.
  737. iMnemonic = myMnemonics.begin(); // Start at the beginning and
  738. while(iMnemonic != myMnemonics.end()) { // loop through the whole list.
  739. if(true == ((*iMnemonic)->test(TranslationData))) { // Check to see if the mnemonic matches.
  740. TranslationData = const_cast<char*>( // If it does match, substitute it's
  741. (*iMnemonic)->Value().c_str()); // value for translation and stop
  742. break; // looking.
  743. } else { // If it does not match, move to the
  744. ++iMnemonic; // next mnemonic and test again.
  745. } // That is, until we run out of
  746. } // mnemonics to test.
  747. }
  748. // Put our TranslationData through each Translator.
  749. list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list.
  750. iTranslator = myTranslators.begin(); // Start at the beginning and
  751. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  752. (*iTranslator)->translate(TranslationData); // Pass the data to each one then
  753. ++iTranslator; // move on to the next.
  754. }
  755. }
  756. // And finally, after all is done successfully...
  757. runEndConfigurators(Data); // Launch the End Configurators.
  758. return true; // Return our success!
  759. }
  760. //// Configuration Attribute ///////////////////////////////////////////////////
  761. ConfigurationAttribute::~ConfigurationAttribute() { // Crush, Kill, Destroy!
  762. // Delete my mnemonics
  763. if(0 < myMnemonics.size()) { // If we have mnemonics...
  764. list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list.
  765. iMnemonic = myMnemonics.begin(); // Start at the beginning and
  766. while(iMnemonic != myMnemonics.end()) { // loop through the whole list.
  767. delete (*iMnemonic); // Delete each mnemonic
  768. iMnemonic++; // then move the iterator.
  769. } // When we're done deleting them
  770. myMnemonics.clear(); // clear the list.
  771. }
  772. // Delete my translators
  773. if(0 < myTranslators.size()) { // If we have translators...
  774. list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list.
  775. iTranslator = myTranslators.begin(); // Start at the beginning and
  776. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  777. delete (*iTranslator); // Delete each translator
  778. iTranslator++; // then move the iterator.
  779. } // When we're done deleting them
  780. myTranslators.clear(); // clear the list.
  781. }
  782. // zero things out
  783. myLine = 0; // If I'm going away then I will leave
  784. myIndex = 0; // with everything at zero and clean.
  785. myLength = 0;
  786. }
  787. ConfigurationAttribute& ConfigurationAttribute::mapTo( // Add a Translator to this attribute.
  788. ConfigurationTranslator& newTranslator) { // Given a new translator I can own,
  789. myTranslators.push_back(&newTranslator); // add the translator to my list
  790. myParent.notifyDirty(); // get dirty for the new translator
  791. return(*this); // then dereference and return myself.
  792. }
  793. ConfigurationAttribute& ConfigurationAttribute::mapTo( // Map to a string.
  794. string& x, string init) { // Given a string and init value,
  795. ConfigurationTranslator* N = // create a new translator for it
  796. new StringTranslator(x, init); // with the values i'm given,
  797. myTranslators.push_back(N); // push it onto my list, then
  798. myParent.notifyDirty(); // get dirty for the new translator
  799. return(*this); // dereference and return myself.
  800. }
  801. ConfigurationAttribute& ConfigurationAttribute::mapTo( // Map to an int.
  802. int& x, int init, int radix) { // Given an int and init values,
  803. ConfigurationTranslator* N = // create a new translator for it
  804. new IntegerTranslator(x, init, radix); // with the values i'm given,
  805. myTranslators.push_back(N); // push it onto my list, then
  806. myParent.notifyDirty(); // get dirty for the new translator
  807. return(*this); // dereference and return myself.
  808. }
  809. ConfigurationAttribute& ConfigurationAttribute::mapTo( // Map to a double.
  810. double& x, double init) { // Given a double and it's init value,
  811. ConfigurationTranslator* N = // create a new translator for it
  812. new DoubleTranslator(x, init); // with the values i'm given,
  813. myTranslators.push_back(N); // push it onto my list, then
  814. myParent.notifyDirty(); // get dirty for the new translator
  815. return(*this); // then dereference and return myself.
  816. }
  817. ConfigurationAttribute& ConfigurationAttribute::mapTo( // Map to a boolean.
  818. bool& x, bool init) { // Given a bool and it's init value,
  819. ConfigurationTranslator* N = // create a new translator for it
  820. new BoolTranslator(x, init); // with the values i'm given,
  821. myTranslators.push_back(N); // push it onto my list, then
  822. myParent.notifyDirty(); // get dirty for the new translator
  823. return(*this); // then dereference and return myself.
  824. }
  825. void ConfigurationAttribute::initialize() { // Reset all translators to defaults.
  826. if(0 < myTranslators.size()) { // If we have translators...
  827. list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list.
  828. iTranslator = myTranslators.begin(); // Start at the beginning and
  829. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  830. (*iTranslator)->initialize(); // initialize each translator
  831. iTranslator++; // then move the iterator.
  832. } // When we're done deleting them
  833. }
  834. // zero things out
  835. myLine = 0; // Initialized means to be as if
  836. myIndex = 0; // no interpet() call has been made.
  837. myLength = 0;
  838. }
  839. bool ConfigurationAttribute::interpret(ConfigurationData& Data) { // (re) Interpret this data.
  840. int Index = Data.Index(); // Our working index.
  841. int Startdex = 0; // Where our data starts.
  842. int Stopdex = 0; // Where our data stops.
  843. int NewLines = 0; // Keep a count of new lines.
  844. // Find our name.
  845. for(unsigned int I = 0; I < myName.length(); I++) { // For the length of the name,
  846. char x = Data.Data(Index + I); // get each corresponding Data byte
  847. if(x != myName.at(I)) { // check it sudden death style.
  848. return false; // No-Match means we are not it.
  849. }
  850. } // If the name checks out then
  851. Index += myName.length(); // move the Index past our name.
  852. NewLines += eatSpacesCountLines(Data, Index); // Eat any spaces we find.
  853. // Find our = sign.
  854. if('=' != Data.Data(Index)) { // Next we should see an =
  855. return false; // If we don't we're done.
  856. } else { // If we do then we can
  857. ++Index; // move past it.
  858. }
  859. NewLines += eatSpacesCountLines(Data, Index); // Eat any spaces we find.
  860. // Find our first quote character.
  861. char QuoteCharacter = 0;
  862. if('\'' == Data.Data(Index) || '\"' == Data.Data(Index)) { // Next we should find ' or "
  863. QuoteCharacter = Data.Data(Index); // If we found it record it then
  864. ++Index; Startdex = Index; // move to and record our start of data.
  865. } else { // If we don't
  866. return false; // we are done.
  867. }
  868. // Find our last quote character.
  869. for(;;) { // Here is how we will roll...
  870. char C = Data.Data(Index); // Grab the character at Index.
  871. if(0 == C) { // If we run out of Data then
  872. return false; // We didn't find anything.
  873. }
  874. if(QuoteCharacter == C) { // If we find our QuoteCharacter
  875. Stopdex = Index; // we have our Stopdex and
  876. break; // we can stop the loop.
  877. }
  878. ++Index; // Otherwise keep on looking.
  879. }
  880. // Read our data.
  881. int BfrSize = Stopdex - Startdex; // How big a buffer do we need?
  882. char Bfr[BfrSize]; // Make one that size.
  883. NewLines += copyDataCountLines(Bfr, Data, Startdex, Stopdex); // Get our data and count our lines.
  884. // Now we can get on with translation.
  885. char* TranslationData = Bfr; // TranslationData is what we translate.
  886. // Translate our data by Mnemonic
  887. if(0 < myMnemonics.size()) { // If we have mnemonics...
  888. list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list.
  889. iMnemonic = myMnemonics.begin(); // Start at the beginning and
  890. while(iMnemonic != myMnemonics.end()) { // loop through the whole list.
  891. if(true == ((*iMnemonic)->test(TranslationData))){ // Check to see if the mnemonic matches.
  892. TranslationData = const_cast<char*>( // If it does match, substitute it's
  893. (*iMnemonic)->Value().c_str()); // value for translation and stop
  894. break; // looking.
  895. } else { // If it does not match, move to the
  896. ++iMnemonic; // next mnemonic and test again.
  897. } // That is, until we run out of
  898. } // mnemonics to test.
  899. }
  900. // Put our TranslationData through each Translator.
  901. if(0 < myTranslators.size()) { // We'd better have translators!
  902. list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list.
  903. iTranslator = myTranslators.begin(); // Start at the beginning and
  904. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  905. (*iTranslator)->translate(TranslationData); // Pass the data to each one and
  906. ++iTranslator; // move on to the next one.
  907. }
  908. }
  909. // Capture our position data.
  910. myLine = Data.Line(); // Capture the line I was on.
  911. myIndex = Data.Index(); // Capture the Index where I started.
  912. myLength = Stopdex + 1 - myIndex; // Capture my segment length.
  913. // Update Data for the next segment.
  914. Data.Index(Stopdex + 1); // Move the Index.
  915. Data.addNewLines(NewLines); // Update the Line Number.
  916. return true; // If we got here, we succeeded!
  917. }
  918. //// Configuratino Data ////////////////////////////////////////////////////////
  919. ConfigurationData::ConfigurationData(const char* Data, int Length) : // Raw constructor from buffer.
  920. myBufferSize(Length), // and it's length.
  921. myIndex(0), // Our Index is zero
  922. myLine(1) { // We start on line 1
  923. myDataBuffer = new char[myBufferSize]; // Allocate a buffer.
  924. memcpy(myDataBuffer, Data, myBufferSize); // Copy the data.
  925. }
  926. ConfigurationData::ConfigurationData(const char* FileName) :
  927. myDataBuffer(NULL), // No data buffer yet.
  928. myBufferSize(0), // No length yet.
  929. myIndex(0), // Our Index is zero
  930. myLine(1) { // We start on line 1
  931. try { // Capture any throws.
  932. ifstream CFGFile(FileName); // Open the file.
  933. CFGFile.seekg(0,ios::end); // Seek to the end
  934. myBufferSize = CFGFile.tellg(); // to find out what size it is.
  935. myDataBuffer = new char[myBufferSize]; // Make a new buffer the right size.
  936. CFGFile.seekg(0,ios::beg); // Seek to the beginning and
  937. CFGFile.read(myDataBuffer, myBufferSize); // read the file into the buffer.
  938. if(CFGFile.bad()) { // If the read failed, we're unusable!
  939. delete[] myDataBuffer; // Delete the buffer
  940. myDataBuffer = NULL; // and Null it's pointer.
  941. myBufferSize = 0; // Set the length to zero.
  942. } // Usually everything will work
  943. CFGFile.close(); // At the end, always close our file.
  944. } catch (...) { // If something went wrong clean up.
  945. if(NULL != myDataBuffer) { // If the data buffer was allocated
  946. delete[] myDataBuffer; // Delete the buffer
  947. myDataBuffer = NULL; // and Null it's pointer.
  948. }
  949. myBufferSize = 0; // The BufferSize will be zero
  950. } // indicating there is no Data.
  951. }
  952. ConfigurationData::ConfigurationData(const string FileName) : // Raw constructor from file.
  953. myDataBuffer(NULL), // No data buffer yet.
  954. myBufferSize(0), // No length yet.
  955. myIndex(0), // Our Index is zero
  956. myLine(1) { // We start on line 1
  957. try { // Capture any throws.
  958. ifstream CFGFile(FileName.c_str()); // Open the file.
  959. CFGFile.seekg(0,ios::end); // Seek to the end
  960. myBufferSize = CFGFile.tellg(); // to find out what size it is.
  961. myDataBuffer = new char[myBufferSize]; // Make a new buffer the right size.
  962. CFGFile.seekg(0,ios::beg); // Seek to the beginning and
  963. CFGFile.read(myDataBuffer, myBufferSize); // read the file into the buffer.
  964. if(CFGFile.bad()) { // If the read failed, we're unusable!
  965. delete[] myDataBuffer; // Delete the buffer
  966. myDataBuffer = NULL; // and Null it's pointer.
  967. myBufferSize = 0; // Set the length to zero.
  968. } // Usually everything will work
  969. CFGFile.close(); // At the end, always close our file.
  970. } catch (...) { // If something went wrong clean up.
  971. if(NULL != myDataBuffer) { // If the data buffer was allocated
  972. delete[] myDataBuffer; // Delete the buffer
  973. myDataBuffer = NULL; // and Null it's pointer.
  974. }
  975. myBufferSize = 0; // The BufferSize will be zero
  976. } // indicating there is no Data.
  977. }
  978. ConfigurationData::~ConfigurationData() { // Destroys the internal buffer etc.
  979. if(NULL != myDataBuffer) { // If we have allocated a buffer,
  980. delete[] myDataBuffer; // delete that buffer
  981. myDataBuffer = NULL; // and null the pointer.
  982. }
  983. myBufferSize = 0; // Zero everything for safety.
  984. myIndex = 0;
  985. myLine = 0;
  986. }
  987. //// Utilities /////////////////////////////////////////////////////////////////
  988. // SetTrueOnComplete Configurator //////////////////////////////////////////////
  989. ConfiguratorSetTrueOnComplete::ConfiguratorSetTrueOnComplete() : // Constructor ensures the pointer
  990. myBoolean(NULL) { // is NULL for safety.
  991. }
  992. void ConfiguratorSetTrueOnComplete::setup(bool& Target) { // The setup() method links us to a
  993. myBoolean = &Target; // target boolean.
  994. }
  995. void ConfiguratorSetTrueOnComplete::operator()( // The operator()
  996. ConfigurationElement& E, ConfigurationData& D) { // When activated, this fellow
  997. if(NULL != myBoolean) { // checks it's pointer for safety
  998. *myBoolean = true; // and if ok, sets the target to
  999. } // true.
  1000. }