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.

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