Leaked source code of windows server 2003
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.

310 lines
6.0 KiB

  1. /*
  2. * @doc INTERNAL
  3. *
  4. * @module RTFLOG.CPP - RichEdit RTF log
  5. *
  6. * Contains the code for the RTFLog class which can be used
  7. * to log the number of times RTF tags are read by the RTF reader
  8. * for use in coverage testing
  9. *
  10. * Authors:<nl>
  11. * Created for RichEdit 2.0: Brad Olenick
  12. *
  13. * Copyright (c) 1995-1996, Microsoft Corporation. All rights reserved.
  14. */
  15. #include "_common.h"
  16. #include "_rtflog.h"
  17. #include "tokens.h"
  18. extern INT cKeywords;
  19. extern const KEYWORD rgKeyword[];
  20. /*
  21. * CRTFLog::CRTFLog()
  22. *
  23. * @mfunc
  24. * Constructor -
  25. * 1. Opens a file mapping to log hit counts, creating
  26. * the backing file if neccessary
  27. * 2. Map a view of the file mapping into memory
  28. * 3. Register a windows message for change notifications
  29. *
  30. */
  31. CRTFLog::CRTFLog() : _rgdwHits(NULL), _hfm(NULL), _hfile(NULL)
  32. {
  33. #ifndef PEGASUS
  34. const char cstrMappingName[] = "RTFLOG";
  35. const char cstrWM[] = "RTFLOGWM";
  36. const int cbMappingSize = sizeof(ELEMENT) * ISize();
  37. BOOL fNewFile = FALSE;
  38. // check for existing file mapping
  39. if(!(_hfm = OpenFileMappingA(FILE_MAP_ALL_ACCESS,
  40. TRUE,
  41. cstrMappingName)))
  42. {
  43. // no existing file mapping
  44. // get the file with which to create the file mapping
  45. // first, attempt to open an existing file
  46. if(!(_hfile = CreateFileA(LpcstrLogFilename(),
  47. GENERIC_READ | GENERIC_WRITE,
  48. 0,
  49. NULL,
  50. OPEN_EXISTING,
  51. FILE_ATTRIBUTE_NORMAL,
  52. NULL)))
  53. {
  54. // no existing file, attempt to create new
  55. if(!(_hfile = CreateFileA(LpcstrLogFilename(),
  56. GENERIC_READ | GENERIC_WRITE,
  57. 0,
  58. NULL,
  59. OPEN_ALWAYS,
  60. FILE_ATTRIBUTE_NORMAL,
  61. NULL)))
  62. {
  63. return;
  64. }
  65. fNewFile = TRUE;
  66. }
  67. if(!(_hfm = CreateFileMappingA(_hfile,
  68. NULL,
  69. PAGE_READWRITE,
  70. 0,
  71. cbMappingSize,
  72. cstrMappingName)))
  73. {
  74. return;
  75. }
  76. }
  77. LPVOID lpv;
  78. if(!(lpv = MapViewOfFile(_hfm,
  79. FILE_MAP_ALL_ACCESS,
  80. 0,
  81. 0,
  82. cbMappingSize)))
  83. {
  84. return;
  85. }
  86. // register windows message for change notifications
  87. SideAssert(_uMsg = RegisterWindowMessageA(cstrWM));
  88. // memory-mapped file is now mapped to _rgdwHits
  89. _rgdwHits = (PELEMENT)lpv;
  90. // zero the memory-mapped file if we created it new
  91. // (Win95 gives us a new file w/ garbage in it for some reason)
  92. if(fNewFile)
  93. {
  94. Reset();
  95. }
  96. #endif
  97. }
  98. /*
  99. * CRTFLog::Reset()
  100. *
  101. * @mfunc
  102. * Resets the hitcount of each element in the log to 0
  103. *
  104. */
  105. void CRTFLog::Reset()
  106. {
  107. if(!FInit())
  108. {
  109. return;
  110. }
  111. for(INDEX i = 0; i < ISize(); i++)
  112. {
  113. (*this)[i] = 0;
  114. }
  115. // notify clients of change
  116. ChangeNotifyAll();
  117. }
  118. /*
  119. * CRTFLog::UGetWindowMsg
  120. *
  121. * @mdesc
  122. * Returns the window message id used for change notifications
  123. *
  124. * @rdesc
  125. * UINT window message id
  126. *
  127. * @devnote
  128. * This should be inline, but the AssertSz macro doesn't compile
  129. * properly on the Mac if its placed in a header file
  130. *
  131. */
  132. UINT CRTFLog::UGetWindowMsg() const
  133. {
  134. AssertSz(FInit(), "CRTFLog::UGetWindowMsg(): CRTFLog not initialized properly");
  135. return _uMsg;
  136. }
  137. /*
  138. * CRTFLog::operator[]
  139. *
  140. * @mdesc
  141. * Returns reference to element i of RTF log (l-value)
  142. *
  143. * @rdesc
  144. * ELEMENT & reference to element i of log
  145. *
  146. * @devnote
  147. * This should be inline, but the AssertSz macro doesn't compile
  148. * properly on the Mac if its placed in a header file
  149. *
  150. */
  151. CRTFLog::ELEMENT &CRTFLog::operator[](INDEX i)
  152. {
  153. AssertSz(i < ISize(), "CRTFLog::operator[]: index out of range");
  154. AssertSz(FInit(), "CRTFLog::operator[]: CRTFLog not initialized properly");
  155. return _rgdwHits[i];
  156. }
  157. /*
  158. * CRTFLog::operator[]
  159. *
  160. * @mdesc
  161. * Returns reference to element i of RTF log (r-value)
  162. *
  163. * @rdesc
  164. * const ELEMENT & reference to element i of log
  165. *
  166. * @devnote
  167. * This should be inline, but the AssertSz macro doesn't compile
  168. * properly on the Mac if its placed in a header file
  169. *
  170. */
  171. const CRTFLog::ELEMENT &CRTFLog::operator[](INDEX i) const
  172. {
  173. AssertSz(i < ISize(), "CRTFLog::operator[]: index out of range");
  174. AssertSz(FInit(), "CRTFLog::operator[]: CRTFLog not initialized properly");
  175. return _rgdwHits[i];
  176. }
  177. /*
  178. * CRTFLog::LpcstrLogFilename()
  179. *
  180. * @mfunc
  181. * Returns name of file to be used for log
  182. *
  183. * @rdesc
  184. * LPCSTR pointer to static buffer containing file name
  185. */
  186. LPCSTR CRTFLog::LpcstrLogFilename() const
  187. {
  188. const char cstrLogFilename[] = "RTFLOG";
  189. static char szBuf[MAX_PATH] = "";
  190. #ifndef PEGASUS
  191. if(!szBuf[0])
  192. {
  193. DWORD cchLength;
  194. char szBuf2[MAX_PATH];
  195. SideAssert(cchLength = GetTempPathA(MAX_PATH, szBuf2));
  196. // append trailing backslash if neccessary
  197. if(szBuf2[cchLength - 1] != '\\')
  198. {
  199. szBuf2[cchLength] = '\\';
  200. szBuf2[cchLength + 1] = 0;
  201. }
  202. wsprintfA(szBuf, "%s%s", szBuf2, cstrLogFilename);
  203. }
  204. #endif
  205. return szBuf;
  206. }
  207. /*
  208. * CRTFLog::IIndexOfKeyword(LPCSTR lpcstrKeyword, PINDEX piIndex)
  209. *
  210. * @mfunc
  211. * Returns the index of the log element which corresponds to
  212. * the RTF keyword, lpcstrKeyword
  213. *
  214. * @rdesc
  215. * BOOL flag indicating whether index was found
  216. */
  217. BOOL CRTFLog::IIndexOfKeyword(LPCSTR lpcstrKeyword, PINDEX piIndex) const
  218. {
  219. INDEX i;
  220. for(i = 0; i < ISize(); i++)
  221. {
  222. if(strcmp(lpcstrKeyword, rgKeyword[i].szKeyword) == 0)
  223. {
  224. break;
  225. }
  226. }
  227. if(i == ISize())
  228. {
  229. return FALSE;
  230. }
  231. if(piIndex)
  232. {
  233. *piIndex = i;
  234. }
  235. return TRUE;
  236. }
  237. /*
  238. * CRTFLog::IIndexOfToken(TOKEN token, PINDEX piIndex)
  239. *
  240. * @mfunc
  241. * Returns the index of the log element which corresponds to
  242. * the RTF token, token
  243. *
  244. * @rdesc
  245. * BOOL flag indicating whether index was found
  246. */
  247. BOOL CRTFLog::IIndexOfToken(TOKEN token, PINDEX piIndex) const
  248. {
  249. INDEX i;
  250. for(i = 0; i < ISize(); i++)
  251. {
  252. if(token == rgKeyword[i].token)
  253. {
  254. break;
  255. }
  256. }
  257. if(i == ISize())
  258. {
  259. return FALSE;
  260. }
  261. if(piIndex)
  262. {
  263. *piIndex = i;
  264. }
  265. return TRUE;
  266. }