Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

configuration.cpp 86KB

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