Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

configuration.cpp 87KB

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