Source code of Windows XP (NT5)
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.

246 lines
6.1 KiB

  1. //
  2. // MODULE: APGTSTSCREAD.CPP
  3. //
  4. // PURPOSE: TSC file reading classes
  5. //
  6. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  7. //
  8. // AUTHOR: Randy Biley
  9. //
  10. // ORIGINAL DATE: 01-19-1999
  11. //
  12. // NOTES:
  13. //
  14. // Version Date By Comments
  15. //--------------------------------------------------------------------
  16. // V3.0 01-19-1999 RAB
  17. //
  18. #include "stdafx.h"
  19. #include "apgtstscread.h"
  20. #include "CharConv.h"
  21. #include "event.h"
  22. // Utilize an unnamed namespace to limit scope to this source file
  23. namespace
  24. {
  25. const CString kstr_CacheSig= _T("TSCACH03");
  26. const CString kstr_MapFrom= _T("MAPFROM ");
  27. const CString kstr_NodeStateDelim= _T(":");
  28. const CString kstr_NodePairDelim= _T(",");
  29. const CString kstr_MapTo= _T("MAPTO ");
  30. const CString kstr_CacheEnd= _T("END");
  31. }
  32. CAPGTSTSCReader::CAPGTSTSCReader( CPhysicalFileReader * pPhysicalFileReader, CCache *pCache )
  33. : CTextFileReader( pPhysicalFileReader )
  34. {
  35. m_pCache= pCache;
  36. }
  37. CAPGTSTSCReader::~CAPGTSTSCReader()
  38. {
  39. }
  40. void CAPGTSTSCReader::Parse()
  41. {
  42. long save_pos = 0;
  43. LOCKOBJECT();
  44. save_pos = GetPos();
  45. SetPos(0);
  46. try
  47. {
  48. vector<CString> arrLines;
  49. // pump file content into array of lines.
  50. CString strLine;
  51. while (GetLine( strLine ))
  52. arrLines.push_back( strLine );
  53. // parse string-by-string.
  54. bool bFirstLine= true;
  55. for (vector<CString>::iterator iCurLine = arrLines.begin(); iCurLine < arrLines.end(); iCurLine++)
  56. {
  57. // Prepare the line for parsing.
  58. CString strCur= *iCurLine;
  59. strCur.TrimLeft();
  60. strCur.TrimRight();
  61. if (bFirstLine)
  62. {
  63. // Verify that this file has the correct signature.
  64. if (-1 == strCur.Find( kstr_CacheSig ))
  65. {
  66. // Unknown type of file, exit the for loop.
  67. // >>> Should there be error handling/reporting here??? RAB-19990119.
  68. break;
  69. }
  70. bFirstLine= false;
  71. }
  72. else if (-1 != strCur.Find( kstr_CacheEnd ))
  73. {
  74. // Located the end of file marker, exit the for loop.
  75. break;
  76. }
  77. else
  78. {
  79. // Look for the first line of a MapFrom-MapTo pair.
  80. int nPos= strCur.Find( kstr_MapFrom );
  81. if (-1 != nPos)
  82. {
  83. CBasisForInference BasisForInference;
  84. bool bHasBasisForInference= false;
  85. // Move the position marker over the MapFrom key word.
  86. nPos+= kstr_MapFrom.GetLength();
  87. // Extract all of the node state pairs from the MapFrom line.
  88. do
  89. {
  90. CString strNode;
  91. CString strState;
  92. int nNodePos;
  93. // Jump over the leading line format or the node pair delimiter.
  94. strCur= strCur.Mid( nPos );
  95. strCur.TrimLeft();
  96. // Look for the delimiter between a node-state pair.
  97. nNodePos= strCur.Find( kstr_NodeStateDelim );
  98. if (-1 != nNodePos)
  99. {
  100. // Extract the string containing the node value and
  101. // then step over the node state delimiter.
  102. strNode= strCur.Left( nNodePos );
  103. strCur= strCur.Mid( nNodePos + kstr_NodeStateDelim.GetLength() );
  104. // Extract the string containing the state value.
  105. nPos= strCur.Find( kstr_NodePairDelim );
  106. if (-1 == nPos)
  107. {
  108. // We have found the last state value, copy the remaining string.
  109. strState= strCur;
  110. }
  111. else
  112. {
  113. // Extract up to the node pair delimiter and move the
  114. // position marker past that point.
  115. strState= strCur.Left( nPos );
  116. nPos+= kstr_NodePairDelim.GetLength();
  117. }
  118. if (strNode.GetLength() && strState.GetLength())
  119. {
  120. // It appears that we have a valid node-state pair so add
  121. // them to the basis for inference.
  122. NID nNid= atoi( strNode );
  123. IST nIst= atoi( strState );
  124. BasisForInference.push_back( CNodeStatePair( nNid, nIst ));
  125. bHasBasisForInference= true;
  126. }
  127. else
  128. {
  129. // >>> This condition should not occur,
  130. // error handling/reporting??? RAB-19990119.
  131. nPos= -1;
  132. }
  133. }
  134. else
  135. nPos= -1;
  136. } while (-1 != nPos) ;
  137. // Now search for recommendations if the basis for inference was okay.
  138. CRecommendations Recommendations;
  139. bool bHasRecommendations= false;
  140. if (bHasBasisForInference)
  141. {
  142. // Move to the next line to prepare for searching for a matching
  143. // MapTo line.
  144. iCurLine++;
  145. if (iCurLine < arrLines.end())
  146. {
  147. // Prep the temporary string.
  148. strCur= *iCurLine;
  149. strCur.TrimLeft();
  150. strCur.TrimRight();
  151. // Look for the matching MapTo element.
  152. nPos= strCur.Find( kstr_MapTo );
  153. if (-1 != nPos)
  154. {
  155. CString strRecommend;
  156. // Extract all of the recommendations from the MapTo line.
  157. nPos+= kstr_MapTo.GetLength();
  158. do
  159. {
  160. // Jump over the leading line format or the node pair delimiter.
  161. strCur= strCur.Mid( nPos );
  162. strCur.TrimLeft();
  163. // Extract the recommendations string value.
  164. nPos= strCur.Find( kstr_NodePairDelim );
  165. if (-1 == nPos)
  166. strRecommend= strCur;
  167. else
  168. {
  169. strRecommend= strCur.Left( nPos );
  170. nPos+= kstr_NodePairDelim.GetLength();
  171. }
  172. if (strRecommend.GetLength())
  173. {
  174. Recommendations.push_back( atoi( strRecommend ) );
  175. bHasRecommendations= true;
  176. }
  177. else
  178. {
  179. // >>> This condition should not occur,
  180. // error handling/reporting??? RAB-19990119.
  181. nPos= -1;
  182. }
  183. } while (-1 != nPos) ;
  184. }
  185. }
  186. }
  187. // We have both items so add them to the cache.
  188. if (bHasRecommendations && bHasBasisForInference)
  189. m_pCache->AddCacheItem( BasisForInference, Recommendations );
  190. }
  191. }
  192. }
  193. }
  194. catch (exception& x)
  195. {
  196. SetPos(save_pos);
  197. UNLOCKOBJECT();
  198. CString str;
  199. // Note STL exception in event log and rethrow exception.
  200. CBuildSrcFileLinenoStr SrcLoc( __FILE__, __LINE__ );
  201. CEvent::ReportWFEvent( SrcLoc.GetSrcFileLineStr(),
  202. SrcLoc.GetSrcFileLineStr(),
  203. CCharConversion::ConvertACharToString(x.what(), str),
  204. _T(""),
  205. EV_GTS_STL_EXCEPTION );
  206. throw;
  207. }
  208. SetPos(save_pos);
  209. UNLOCKOBJECT();
  210. return;
  211. }