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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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. void ConfigurationElement::initialize() { // Reset all translators to defaults.
  483. // Initialize the elements below me
  484. if(0 < myElements.size()) { // If we have elements...
  485. list<ConfigurationElement*>::iterator iElement; // Iterate through our elements list.
  486. iElement = myElements.begin(); // Start at the beginning and
  487. while(iElement != myElements.end()) { // loop through the whole list.
  488. (*iElement)->initialize(); // Initialize each element
  489. iElement++; // then move the iterator.
  490. }
  491. }
  492. // Once that's done, see about myself
  493. if(true == myCleanFlag) return; // If I'm already clean, return.
  494. // Initialize my own translators
  495. if(0 < myTranslators.size()) { // If we have translators...
  496. list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list.
  497. iTranslator = myTranslators.begin(); // Start at the beginning and
  498. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  499. (*iTranslator)->initialize(); // Initialize each translator
  500. iTranslator++; // then move the iterator.
  501. }
  502. }
  503. // Initialize my own attributes
  504. if(0 < myAttributes.size()) { // If we have attributes...
  505. list<ConfigurationAttribute*>::iterator iAttribute; // Iterate through our attributes list.
  506. iAttribute = myAttributes.begin(); // Start at the beginning and
  507. while(iAttribute != myAttributes.end()) { // loop through the whole list.
  508. (*iAttribute)->initialize(); // Initialize each attribute
  509. ++iAttribute; // then move the iterator.
  510. }
  511. }
  512. // Zero things out
  513. myLine = 0; // Initialized means to be as if
  514. myIndex = 0; // no interpet() call has been made.
  515. myLength = 0;
  516. // At this point we know we are clean
  517. myCleanFlag = true; // Clean as a whistle!
  518. }
  519. void ConfigurationElement::runStartConfigurators(ConfigurationData& D) { // Does what it says ;-)
  520. list<Configurator*>::iterator iConfigurator; // Iterate through our Configurators list.
  521. iConfigurator = myStartConfigurators.begin(); // Start at the beginning and
  522. while(iConfigurator != myStartConfigurators.end()) { // loop through the whole list.
  523. (** iConfigurator)(*this, D); // Launch each configurator with self.
  524. ++iConfigurator; // Move to the next.
  525. }
  526. }
  527. void ConfigurationElement::runEndConfigurators(ConfigurationData& D) { // Does what it says ;-)
  528. list<Configurator*>::iterator iConfigurator; // Iterate through our Configurators list.
  529. iConfigurator = myEndConfigurators.begin(); // Start at the beginning and
  530. while(iConfigurator != myEndConfigurators.end()) { // loop through the whole list.
  531. (** iConfigurator)(*this, D); // Launch each configurator with self.
  532. ++iConfigurator; // Move to the next.
  533. }
  534. }
  535. bool ConfigurationElement::interpret(ConfigurationData& Data) { // (re) Interpret this data.
  536. int Index = Data.Index(); // Our working index.
  537. int Startdex = 0; // Where our data starts.
  538. int Stopdex = 0; // Where our data stops.
  539. int NewLines = 0; // Keep a count of new lines.
  540. //// Pre-Processing / Cleanup / Find <name...
  541. // Eat anything up to the first <
  542. // Eat any comments <!-- this is a comment etc -->
  543. // Eat any doctype headers <?xml version="1.0" etc ?>
  544. for(;;) {
  545. int StartingPoint = Index; // Where did we start each pass?
  546. NewLines += eatNonTagTextCountLines(Data, Index); // Eat any spaces we find.
  547. NewLines += eatCommentsCountLines(Data, Index); // Eat any <!-- -->
  548. NewLines += eatDocSpecsCountLines(Data, Index); // Eat any <? ?>
  549. if(StartingPoint == Index) { break; } // If we didn't move on this pass
  550. } // then we are done with cleanup!
  551. // Update Data to move past any of the preceeding junk. This way, other
  552. // element processors will be able to skip any cleanup work we did.
  553. Data.Index(Index); // Move the Index.
  554. Data.addNewLines(NewLines); // Update the Line Number.
  555. NewLines = 0; // Reset our internal Lines counter.
  556. // Find my name.
  557. if(Data.Data(Index) != '<') { // If we're not on a tag open then
  558. return false; // we are not at an element.
  559. } else { // Otherwise we are safe to move
  560. ++Index; // past it and scan for our name.
  561. }
  562. for(unsigned int I = 0; I < myName.length(); I++) { // For the length of our name,
  563. char x = Data.Data(Index + I); // get each corresponding Data byte
  564. if(x != myName.at(I)) { // check it sudden death style.
  565. return false; // No-Match means we are not it.
  566. }
  567. } // If the name checks out then
  568. Index += myName.length(); // move the Index past our name.
  569. // At this point we have found ourselves so we will activate and interpret
  570. // our Data.
  571. if(true == myInitOnInterpretFlag) { // If we are supposed to Init before
  572. initialize(); // we Interpret then do it.
  573. }
  574. // Since we are activating we must set our state so we know where we are.
  575. myLine = Data.Line(); // We know where we start...
  576. myIndex = Data.Index(); // We know our index...
  577. myLength = 0; // We don't know our length yet.
  578. runStartConfigurators(Data); // Run the start configurators.
  579. myCleanFlag = false; // Now we start to get dirty.
  580. // First, we will run through any attributes we have.
  581. bool ThisIsAnEmptyElement = false; // We'll use this to signal empties.
  582. for(;;) { // This is how we roll..
  583. NewLines += eatSpacesCountLines(Data, Index); // Eat any spaces we find.
  584. Data.Index(Index); // Move the Index.
  585. Data.addNewLines(NewLines); // Update the Line Number.
  586. NewLines = 0; // Reset our internal Lines counter.
  587. // Now we look at the next character. Either it's an attribute, or
  588. // it's the end of the tag, or it's some kind of junk. If it's junk
  589. // we will skip it. We will continue parsing until we get to the end
  590. // of the Data or the end of the opening tag (either stopping at / if
  591. // the element is empty or > if the element is not empty.
  592. if(isalpha(Data.Data(Index))) { // If it looks like an attribute...
  593. bool ParseHappened = false; // Start pessimistically at each pass.
  594. list<ConfigurationAttribute*>::iterator iAttribute; // Iterate through our attributes list.
  595. iAttribute = myAttributes.begin(); // Start at the beginning and
  596. while(iAttribute != myAttributes.end()) { // loop through the whole list.
  597. ParseHappened = (* iAttribute)->interpret(Data); // Have each attribute interpret(Data)
  598. ++iAttribute; // Remember to move to the next one.
  599. if(ParseHappened) break; // If a Parse Happened, break the inner
  600. } // loop and start the next pass.
  601. if(false == ParseHappened) { // If we didn't recognize the attribute
  602. NewLines += eatAttributeCountLines(Data, Index); // then eat it.
  603. Data.Index(Index); // Sync up our Index.
  604. Data.addNewLines(NewLines); // Sync up our NewLines.
  605. NewLines = 0; // Zero our NewLines count.
  606. } else { // If we DID recognize the attribute then
  607. Index = Data.Index(); // sync up our Index for the next one.
  608. }
  609. } else
  610. if(0 == Data.Data(Index)) { // If it looks like the end of Data
  611. break; // we will break out - we're done.
  612. } else
  613. if( // If it looks like the end of an empty
  614. '/' == Data.Data(Index) && // element (starts with / and ends with
  615. '>' == Data.Data(Index + 1) // >) then this must be an empty element.
  616. ) {
  617. ThisIsAnEmptyElement = true; // Set the empty element flag and
  618. Index += 2; // Move past the end of the tag and
  619. break; // break out of the loop.
  620. } else
  621. if('>' == Data.Data(Index)) { // If it looks like the end of an open
  622. Index += 1; // tag then move past the end and
  623. break; // break out of the loop.
  624. } else { // If it looks like anything else then
  625. ++Index; // we don't know what it is so we creep
  626. } // past it.
  627. }
  628. Data.Index(Index); // Sync up our index
  629. // At this point we're done processing our open tag and any attributes it
  630. // may have contained, and we are syncrhonized with Data.
  631. if(ThisIsAnEmptyElement) { // If the element was self closing then
  632. runEndConfigurators(Data); // run the End Configurators and return
  633. return true; // true to the caller.
  634. }
  635. // At this point we have contents and/or elements to process. We will keep
  636. // track of any contents using Startdex and Stopdex.
  637. Startdex = Index;
  638. // Now we will process through any elements there may be until we reach
  639. // our end tag. If we have no sub-elements listed, we'll simply skip that
  640. // step on each pass... So, we roll like this:
  641. // Check for end of Data.
  642. // Check for our end tag.
  643. // Check for a sub-element.
  644. // If none of these work then break out.
  645. for(;;) { // Loop through our content like this.
  646. int CheckPoint = Index; // Where did we start each pass?
  647. // Check for end of data //
  648. if(0 == Data.Data(Index)) { // If we are at end of data then we're
  649. return false; // broken so we return false.
  650. } else
  651. // Check for our own end tag //
  652. if( // If this looks like an end tag
  653. '<' == Data.Data(Index) && // (Starts with < followed by
  654. '/' == Data.Data(Index + 1) // a / character)
  655. ) { // Then it _should_ be our own.
  656. Stopdex = Index; // Capture this position for content.
  657. Index += 2; // Move Index to where the name starts.
  658. for(unsigned int I = 0; I < myName.length(); I++) { // For the length of the name,
  659. char x = Data.Data(Index + I); // check each corresponding Data byte.
  660. if(x != myName.at(I)) { // If we fail to match at any point
  661. return false; // then things are very broken
  662. } // so we return false.
  663. } // If the name checks out then
  664. Index += myName.length(); // move past our name.
  665. if('>' != Data.Data(Index)) { // Being very strict, if the next
  666. return false; // byte is not > then fail!
  667. } else { // If all goes well then we move
  668. ++Index; // past the > and we are done.
  669. break; // Break to move to the next step.
  670. }
  671. } else
  672. // Check for a subordinate element //
  673. if( // If this looks like an element
  674. '<' == Data.Data(Index) && // starting with < and a name
  675. isalpha(Data.Data(Index + 1)) // beginning with an alpha character...
  676. ) {
  677. bool ElementHappened = false; // We'll check our elements.
  678. Data.Index(Index); // Sync our index.
  679. Data.addNewLines(NewLines); // Sync our lines.
  680. NewLines = 0; // Reset our new lines count.
  681. if(0 < myElements.size()) { // If we have elements check them.
  682. list<ConfigurationElement*>::iterator iElement; // Iterate through our elements list.
  683. iElement = myElements.begin(); // Start at the beginning and
  684. while(iElement != myElements.end()) { // loop through the whole list.
  685. ConfigurationElement& doNode = **iElement; // Grab the element we're on.
  686. ElementHappened = doNode.interpret(Data); // Have each element interpret(Data)
  687. Index = Data.Index(); // Capitalze on any cleanup work.
  688. ++iElement; // Remember to move to the next.
  689. if(ElementHappened) break; // If an Element Happened, break the
  690. } // loop and start the next pass.
  691. if(false == ElementHappened) { // If we did not recognize the Element
  692. NewLines += eatElementCountLines(Data, Index); // then eat it *****
  693. Data.Index(Index); // Resync our Index.
  694. Data.addNewLines(NewLines); // Sync our line count.
  695. NewLines = 0; // Reset our internal count.
  696. }
  697. } else { // If we don't own any elements then
  698. NewLines += eatElementCountLines(Data, Index); // eat the ones we find.
  699. }
  700. // Handle any untidy messes here //
  701. } else { // If we're on something unknown then
  702. NewLines += eatSpacesCountLines(Data, Index); // Eat any spaces we find.
  703. NewLines += eatCommentsCountLines(Data, Index); // Eat any <!-- -->
  704. NewLines += eatDocSpecsCountLines(Data, Index); // Eat any <? ?>
  705. NewLines += eatNonTagTextCountLines(Data, Index); // Eat any non tag bytes.
  706. Data.Index(Index); // Sync our Index.
  707. Data.addNewLines(NewLines); // Sync our line number.
  708. NewLines = 0; // Clear our lines count.
  709. }
  710. // If we get stuck looping on something we don't know how to clean
  711. // and don't know how to interpret then we need to break out of the
  712. // insanity. This way, anything that doesn't make sense won't be able
  713. // to stall us or cause us to interpret something incorrectly later
  714. // on... If we're the top element, the interpret() process will end.
  715. // If we are deeper then it is likely our superirors will also not
  716. // understand and so they will also end the same way.
  717. if(CheckPoint == Index) return false; // If we haven't moved, punt!
  718. }
  719. // When we're done with our loop sync up with Data again.
  720. Data.Index(Index); // Sync up our Index.
  721. Data.addNewLines(NewLines); // Sync up our NewLines count.
  722. NewLines = 0; // zero our local count.
  723. // Once our elements have been procssed and we get to our end tag we can
  724. // process our content (if we have Translators registered).
  725. if(
  726. 0 < myTranslators.size() && // If we have translators and
  727. Stopdex > Startdex // we have content to translate
  728. ) { // then translate the content!
  729. // Create the Content buffer...
  730. int BfrSize = Stopdex - Startdex; // How big a buffer do we need?
  731. char Bfr[BfrSize]; // Make one that size.
  732. copyDataCountLines(Bfr, Data, Startdex, Stopdex); // Get our data and ignore our lines.
  733. // Now we can get on with translation.
  734. char* TranslationData = Bfr; // TranslationData is what we translate.
  735. // Translate our data by Mnemonic
  736. if(0 < myMnemonics.size()) { // If we have mnemonics...
  737. list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list.
  738. iMnemonic = myMnemonics.begin(); // Start at the beginning and
  739. while(iMnemonic != myMnemonics.end()) { // loop through the whole list.
  740. if(true == ((*iMnemonic)->test(TranslationData))) { // Check to see if the mnemonic matches.
  741. TranslationData = const_cast<char*>( // If it does match, substitute it's
  742. (*iMnemonic)->Value().c_str()); // value for translation and stop
  743. break; // looking.
  744. } else { // If it does not match, move to the
  745. ++iMnemonic; // next mnemonic and test again.
  746. } // That is, until we run out of
  747. } // mnemonics to test.
  748. }
  749. // Put our TranslationData through each Translator.
  750. list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list.
  751. iTranslator = myTranslators.begin(); // Start at the beginning and
  752. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  753. (*iTranslator)->translate(TranslationData); // Pass the data to each one then
  754. ++iTranslator; // move on to the next.
  755. }
  756. }
  757. // And finally, after all is done successfully...
  758. runEndConfigurators(Data); // Launch the End Configurators.
  759. return true; // Return our success!
  760. }
  761. //// Configuration Attribute ///////////////////////////////////////////////////
  762. ConfigurationAttribute::~ConfigurationAttribute() { // Crush, Kill, Destroy!
  763. // Delete my mnemonics
  764. if(0 < myMnemonics.size()) { // If we have mnemonics...
  765. list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list.
  766. iMnemonic = myMnemonics.begin(); // Start at the beginning and
  767. while(iMnemonic != myMnemonics.end()) { // loop through the whole list.
  768. delete (*iMnemonic); // Delete each mnemonic
  769. iMnemonic++; // then move the iterator.
  770. } // When we're done deleting them
  771. myMnemonics.clear(); // clear the list.
  772. }
  773. // Delete my translators
  774. if(0 < myTranslators.size()) { // If we have translators...
  775. list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list.
  776. iTranslator = myTranslators.begin(); // Start at the beginning and
  777. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  778. delete (*iTranslator); // Delete each translator
  779. iTranslator++; // then move the iterator.
  780. } // When we're done deleting them
  781. myTranslators.clear(); // clear the list.
  782. }
  783. // zero things out
  784. myLine = 0; // If I'm going away then I will leave
  785. myIndex = 0; // with everything at zero and clean.
  786. myLength = 0;
  787. }
  788. ConfigurationAttribute& ConfigurationAttribute::mapTo( // Add a Translator to this attribute.
  789. ConfigurationTranslator& newTranslator) { // Given a new translator I can own,
  790. myTranslators.push_back(&newTranslator); // add the translator to my list
  791. myParent.notifyDirty(); // get dirty for the new translator
  792. return(*this); // then dereference and return myself.
  793. }
  794. ConfigurationAttribute& ConfigurationAttribute::mapTo( // Map to a string.
  795. string& x, string init) { // Given a string and init value,
  796. ConfigurationTranslator* N = // create a new translator for it
  797. new StringTranslator(x, init); // with the values i'm given,
  798. myTranslators.push_back(N); // push it onto my list, then
  799. myParent.notifyDirty(); // get dirty for the new translator
  800. return(*this); // dereference and return myself.
  801. }
  802. ConfigurationAttribute& ConfigurationAttribute::mapTo( // Map to an int.
  803. int& x, int init, int radix) { // Given an int and init values,
  804. ConfigurationTranslator* N = // create a new translator for it
  805. new IntegerTranslator(x, init, radix); // with the values i'm given,
  806. myTranslators.push_back(N); // push it onto my list, then
  807. myParent.notifyDirty(); // get dirty for the new translator
  808. return(*this); // dereference and return myself.
  809. }
  810. ConfigurationAttribute& ConfigurationAttribute::mapTo( // Map to a double.
  811. double& x, double init) { // Given a double and it's init value,
  812. ConfigurationTranslator* N = // create a new translator for it
  813. new DoubleTranslator(x, init); // with the values i'm given,
  814. myTranslators.push_back(N); // push it onto my list, then
  815. myParent.notifyDirty(); // get dirty for the new translator
  816. return(*this); // then dereference and return myself.
  817. }
  818. ConfigurationAttribute& ConfigurationAttribute::mapTo( // Map to a boolean.
  819. bool& x, bool init) { // Given a bool and it's init value,
  820. ConfigurationTranslator* N = // create a new translator for it
  821. new BoolTranslator(x, init); // with the values i'm given,
  822. myTranslators.push_back(N); // push it onto my list, then
  823. myParent.notifyDirty(); // get dirty for the new translator
  824. return(*this); // then dereference and return myself.
  825. }
  826. void ConfigurationAttribute::initialize() { // Reset all translators to defaults.
  827. if(0 < myTranslators.size()) { // If we have translators...
  828. list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list.
  829. iTranslator = myTranslators.begin(); // Start at the beginning and
  830. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  831. (*iTranslator)->initialize(); // initialize each translator
  832. iTranslator++; // then move the iterator.
  833. } // When we're done deleting them
  834. }
  835. // zero things out
  836. myLine = 0; // Initialized means to be as if
  837. myIndex = 0; // no interpet() call has been made.
  838. myLength = 0;
  839. }
  840. bool ConfigurationAttribute::interpret(ConfigurationData& Data) { // (re) Interpret this data.
  841. int Index = Data.Index(); // Our working index.
  842. int Startdex = 0; // Where our data starts.
  843. int Stopdex = 0; // Where our data stops.
  844. int NewLines = 0; // Keep a count of new lines.
  845. // Find our name.
  846. for(unsigned int I = 0; I < myName.length(); I++) { // For the length of the name,
  847. char x = Data.Data(Index + I); // get each corresponding Data byte
  848. if(x != myName.at(I)) { // check it sudden death style.
  849. return false; // No-Match means we are not it.
  850. }
  851. } // If the name checks out then
  852. Index += myName.length(); // move the Index past our name.
  853. NewLines += eatSpacesCountLines(Data, Index); // Eat any spaces we find.
  854. // Find our = sign.
  855. if('=' != Data.Data(Index)) { // Next we should see an =
  856. return false; // If we don't we're done.
  857. } else { // If we do then we can
  858. ++Index; // move past it.
  859. }
  860. NewLines += eatSpacesCountLines(Data, Index); // Eat any spaces we find.
  861. // Find our first quote character.
  862. char QuoteCharacter = 0;
  863. if('\'' == Data.Data(Index) || '\"' == Data.Data(Index)) { // Next we should find ' or "
  864. QuoteCharacter = Data.Data(Index); // If we found it record it then
  865. ++Index; Startdex = Index; // move to and record our start of data.
  866. } else { // If we don't
  867. return false; // we are done.
  868. }
  869. // Find our last quote character.
  870. for(;;) { // Here is how we will roll...
  871. char C = Data.Data(Index); // Grab the character at Index.
  872. if(0 == C) { // If we run out of Data then
  873. return false; // We didn't find anything.
  874. }
  875. if(QuoteCharacter == C) { // If we find our QuoteCharacter
  876. Stopdex = Index; // we have our Stopdex and
  877. break; // we can stop the loop.
  878. }
  879. ++Index; // Otherwise keep on looking.
  880. }
  881. // Read our data.
  882. int BfrSize = Stopdex - Startdex; // How big a buffer do we need?
  883. char Bfr[BfrSize]; // Make one that size.
  884. NewLines += copyDataCountLines(Bfr, Data, Startdex, Stopdex); // Get our data and count our lines.
  885. // Now we can get on with translation.
  886. char* TranslationData = Bfr; // TranslationData is what we translate.
  887. // Translate our data by Mnemonic
  888. if(0 < myMnemonics.size()) { // If we have mnemonics...
  889. list<ConfigurationMnemonic*>::iterator iMnemonic; // Iterate through our mnemonics list.
  890. iMnemonic = myMnemonics.begin(); // Start at the beginning and
  891. while(iMnemonic != myMnemonics.end()) { // loop through the whole list.
  892. if(true == ((*iMnemonic)->test(TranslationData))){ // Check to see if the mnemonic matches.
  893. TranslationData = const_cast<char*>( // If it does match, substitute it's
  894. (*iMnemonic)->Value().c_str()); // value for translation and stop
  895. break; // looking.
  896. } else { // If it does not match, move to the
  897. ++iMnemonic; // next mnemonic and test again.
  898. } // That is, until we run out of
  899. } // mnemonics to test.
  900. }
  901. // Put our TranslationData through each Translator.
  902. if(0 < myTranslators.size()) { // We'd better have translators!
  903. list<ConfigurationTranslator*>::iterator iTranslator; // Iterate through our translators list.
  904. iTranslator = myTranslators.begin(); // Start at the beginning and
  905. while(iTranslator != myTranslators.end()) { // loop through the whole list.
  906. (*iTranslator)->translate(TranslationData); // Pass the data to each one and
  907. ++iTranslator; // move on to the next one.
  908. }
  909. }
  910. // Capture our position data.
  911. myLine = Data.Line(); // Capture the line I was on.
  912. myIndex = Data.Index(); // Capture the Index where I started.
  913. myLength = Stopdex + 1 - myIndex; // Capture my segment length.
  914. // Update Data for the next segment.
  915. Data.Index(Stopdex + 1); // Move the Index.
  916. Data.addNewLines(NewLines); // Update the Line Number.
  917. return true; // If we got here, we succeeded!
  918. }
  919. //// Configuratino Data ////////////////////////////////////////////////////////
  920. ConfigurationData::ConfigurationData(const char* Data, int Length) : // Raw constructor from buffer.
  921. myBufferSize(Length), // and it's length.
  922. myIndex(0), // Our Index is zero
  923. myLine(1) { // We start on line 1
  924. myDataBuffer = new char[myBufferSize]; // Allocate a buffer.
  925. memcpy(myDataBuffer, Data, myBufferSize); // Copy the data.
  926. }
  927. ConfigurationData::ConfigurationData(const char* FileName) :
  928. myDataBuffer(NULL), // No data buffer yet.
  929. myBufferSize(0), // No length yet.
  930. myIndex(0), // Our Index is zero
  931. myLine(1) { // We start on line 1
  932. try { // Capture any throws.
  933. ifstream CFGFile(FileName); // Open the file.
  934. CFGFile.seekg(0,ios::end); // Seek to the end
  935. myBufferSize = CFGFile.tellg(); // to find out what size it is.
  936. myDataBuffer = new char[myBufferSize]; // Make a new buffer the right size.
  937. CFGFile.seekg(0,ios::beg); // Seek to the beginning and
  938. CFGFile.read(myDataBuffer, myBufferSize); // read the file into the buffer.
  939. if(CFGFile.bad()) { // If the read failed, we're unusable!
  940. delete[] myDataBuffer; // Delete the buffer
  941. myDataBuffer = NULL; // and Null it's pointer.
  942. myBufferSize = 0; // Set the length to zero.
  943. } // Usually everything will work
  944. CFGFile.close(); // At the end, always close our file.
  945. } catch (...) { // If something went wrong clean up.
  946. if(NULL != myDataBuffer) { // If the data buffer was allocated
  947. delete[] myDataBuffer; // Delete the buffer
  948. myDataBuffer = NULL; // and Null it's pointer.
  949. }
  950. myBufferSize = 0; // The BufferSize will be zero
  951. } // indicating there is no Data.
  952. }
  953. ConfigurationData::ConfigurationData(const string FileName) : // Raw constructor from file.
  954. myDataBuffer(NULL), // No data buffer yet.
  955. myBufferSize(0), // No length yet.
  956. myIndex(0), // Our Index is zero
  957. myLine(1) { // We start on line 1
  958. try { // Capture any throws.
  959. ifstream CFGFile(FileName.c_str()); // Open the file.
  960. CFGFile.seekg(0,ios::end); // Seek to the end
  961. myBufferSize = CFGFile.tellg(); // to find out what size it is.
  962. myDataBuffer = new char[myBufferSize]; // Make a new buffer the right size.
  963. CFGFile.seekg(0,ios::beg); // Seek to the beginning and
  964. CFGFile.read(myDataBuffer, myBufferSize); // read the file into the buffer.
  965. if(CFGFile.bad()) { // If the read failed, we're unusable!
  966. delete[] myDataBuffer; // Delete the buffer
  967. myDataBuffer = NULL; // and Null it's pointer.
  968. myBufferSize = 0; // Set the length to zero.
  969. } // Usually everything will work
  970. CFGFile.close(); // At the end, always close our file.
  971. } catch (...) { // If something went wrong clean up.
  972. if(NULL != myDataBuffer) { // If the data buffer was allocated
  973. delete[] myDataBuffer; // Delete the buffer
  974. myDataBuffer = NULL; // and Null it's pointer.
  975. }
  976. myBufferSize = 0; // The BufferSize will be zero
  977. } // indicating there is no Data.
  978. }
  979. ConfigurationData::~ConfigurationData() { // Destroys the internal buffer etc.
  980. if(NULL != myDataBuffer) { // If we have allocated a buffer,
  981. delete[] myDataBuffer; // delete that buffer
  982. myDataBuffer = NULL; // and null the pointer.
  983. }
  984. myBufferSize = 0; // Zero everything for safety.
  985. myIndex = 0;
  986. myLine = 0;
  987. }
  988. //// Utilities /////////////////////////////////////////////////////////////////
  989. // SetTrueOnComplete Configurator //////////////////////////////////////////////
  990. ConfiguratorSetTrueOnComplete::ConfiguratorSetTrueOnComplete() : // Constructor ensures the pointer
  991. myBoolean(NULL) { // is NULL for safety.
  992. }
  993. void ConfiguratorSetTrueOnComplete::setup(bool& Target) { // The setup() method links us to a
  994. myBoolean = &Target; // target boolean.
  995. }
  996. void ConfiguratorSetTrueOnComplete::operator()( // The operator()
  997. ConfigurationElement& E, ConfigurationData& D) { // When activated, this fellow
  998. if(NULL != myBoolean) { // checks it's pointer for safety
  999. *myBoolean = true; // and if ok, sets the target to
  1000. } // true.
  1001. }
  1002. }