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.

445 lines
9.1 KiB

  1. #include "Clist.h"
  2. BOOL GetNextLineFromCsv(FILE *pFile, TCHAR *szBucketId, TCHAR *szBucketString,TCHAR *szResponse1, TCHAR *szResponse2, TCHAR *szCount)
  3. {
  4. TCHAR Buffer[1024];
  5. TCHAR *CurrPos;
  6. TCHAR *Dest;
  7. ZeroMemory(Buffer, sizeof Buffer);
  8. if ( !_fgetts(Buffer,1024 , pFile))
  9. {
  10. return FALSE;
  11. }
  12. // Does the buffer contain anything?
  13. if (!_tcscmp(Buffer, _T("\0")))
  14. {
  15. return FALSE;
  16. }
  17. // do we have at least 1 comma?
  18. if (!_tcsstr(Buffer, _T(",")))
  19. {
  20. return FALSE;
  21. }
  22. CurrPos = Buffer;
  23. Dest = szBucketId;
  24. while ( (*CurrPos != _T(',')) && (*CurrPos != _T('\0')))
  25. {
  26. *Dest = *CurrPos;
  27. ++Dest, ++CurrPos;
  28. }
  29. // null Terminate the string.
  30. *Dest = _T('\0');
  31. if (*CurrPos == _T('\0'))
  32. goto DONE;
  33. Dest = szBucketString;
  34. ++ CurrPos; // Skip the ,
  35. while ( (*CurrPos != _T(',')) && (*CurrPos != _T('\0')))
  36. {
  37. *Dest = *CurrPos;
  38. ++Dest, ++CurrPos;
  39. }
  40. // null Terminate the string.
  41. *Dest = _T('\0');
  42. if (*CurrPos == _T('\0'))
  43. goto DONE;
  44. Dest = szResponse1;
  45. ++ CurrPos; // Skip the ,
  46. while ( (*CurrPos != _T(',')) && (*CurrPos != _T('\0')))
  47. {
  48. *Dest = *CurrPos;
  49. ++Dest, ++CurrPos;
  50. }
  51. // null Terminate the string.
  52. *Dest = _T('\0');
  53. if (*CurrPos == _T('\0'))
  54. goto DONE;
  55. Dest = szResponse2;
  56. ++ CurrPos; // Skip the ,
  57. while ( (*CurrPos != _T(',')) && (*CurrPos != _T('\0')))
  58. {
  59. *Dest = *CurrPos;
  60. ++Dest, ++CurrPos;
  61. }
  62. // null Terminate the string.
  63. *Dest = _T('\0');
  64. if (*CurrPos == _T('\0'))
  65. goto DONE;
  66. Dest = szCount;
  67. ++ CurrPos; // Skip the ,
  68. ZeroMemory(szCount,sizeof szCount);
  69. while ( (*CurrPos != _T(',')) && (*CurrPos != _T('\r')) && (*CurrPos != _T('\0')))
  70. {
  71. *Dest = *CurrPos;
  72. ++Dest, ++CurrPos;
  73. }
  74. // null Terminate the string.
  75. *Dest = _T('\0');
  76. DONE:
  77. return TRUE;
  78. }
  79. BOOL Clist::LoadCsvFile()
  80. {
  81. TCHAR szBucketID[100];
  82. FILE *pFile = NULL;
  83. TCHAR szResponse1[255];
  84. TCHAR szResponse2[255];
  85. TCHAR szCount [100];
  86. TCHAR szBucketString[255];
  87. TCHAR *Temp;
  88. TCHAR szFilePath[MAX_PATH];
  89. // int iElement = 0;
  90. // Zero out the strings
  91. ZeroMemory(szBucketID, sizeof szBucketID);
  92. ZeroMemory(szResponse1, sizeof szResponse1);
  93. ZeroMemory(szResponse2, sizeof szResponse2);
  94. ZeroMemory(szCount, sizeof szCount);
  95. ZeroMemory(szBucketString, sizeof szBucketString);
  96. if (StringCbCopy(szFilePath, sizeof szFilePath, m_szCsvFileName) != S_OK)
  97. {
  98. goto ERRORS;
  99. }
  100. Temp = szFilePath;
  101. Temp+= _tcslen(szFilePath);
  102. if (Temp != szFilePath)
  103. {
  104. while ( (*Temp != _T('\\')) && (Temp != szFilePath))
  105. {
  106. -- Temp;
  107. }
  108. if (Temp != szFilePath)
  109. {
  110. *Temp = _T('\0');
  111. }
  112. if (!PathIsDirectory(szFilePath))
  113. {
  114. CreateDirectory(szFilePath, NULL);
  115. }
  116. }
  117. // Open the file
  118. pFile = _tfopen(m_szCsvFileName, _T("r"));
  119. if (pFile)
  120. {
  121. // get next line
  122. if (GetNextLineFromCsv(pFile, szBucketID, szBucketString, szResponse1, szResponse2, szCount))
  123. {
  124. do
  125. {
  126. // Build new node
  127. AddEntry(szBucketID, szBucketString, szResponse1, szResponse2, szCount);
  128. ZeroMemory(szBucketID, sizeof szBucketID);
  129. ZeroMemory(szResponse1, sizeof szResponse1);
  130. ZeroMemory(szResponse2, sizeof szResponse2);
  131. ZeroMemory(szCount, sizeof szCount);
  132. ZeroMemory(szBucketString, sizeof szBucketString);
  133. }while (GetNextLineFromCsv(pFile, szBucketID, szBucketString, szResponse1, szResponse2, szCount));
  134. fclose(pFile);
  135. return TRUE;
  136. }
  137. else
  138. {
  139. fclose (pFile);
  140. return TRUE;
  141. }
  142. }
  143. ERRORS:
  144. return FALSE;
  145. }
  146. BOOL Clist::Initialize(TCHAR *CsvFileName)
  147. {
  148. if (StringCbCopy(m_szCsvFileName,sizeof m_szCsvFileName, CsvFileName) != S_OK)
  149. {
  150. bInitialized = FALSE;
  151. return FALSE;
  152. }
  153. else
  154. {
  155. if (!LoadCsvFile())
  156. {
  157. bInitialized = FALSE;
  158. return FALSE;
  159. }
  160. else
  161. {
  162. bInitialized = TRUE;
  163. return TRUE;
  164. }
  165. }
  166. }
  167. BOOL Clist::AddEntry(TCHAR *szSBucketID,
  168. TCHAR *szSBucketString,
  169. TCHAR *szSResponse1,
  170. TCHAR *szGResponse2,
  171. TCHAR *szCount
  172. )
  173. {
  174. PCSV_LAYOUT NewNode;
  175. PCSV_LAYOUT Temp;
  176. NewNode = (PCSV_LAYOUT) malloc (sizeof CSV_LAYOUT);
  177. if (NewNode)
  178. {
  179. if (StringCbCopy (NewNode->szSBucketString,sizeof NewNode->szSBucketString, szSBucketString) != S_OK)
  180. {
  181. goto ERRORS;
  182. }
  183. NewNode->iSBucketID = _ttoi(szSBucketID);
  184. NewNode->icount = _ttoi(szCount);
  185. if (StringCbCopy (NewNode->szSBucketResponse, sizeof NewNode->szSBucketResponse, szSResponse1) != S_OK)
  186. {
  187. goto ERRORS;
  188. }
  189. if (StringCbCopy (NewNode->szGBucketResponse, sizeof NewNode->szGBucketResponse,szGResponse2) != S_OK)
  190. {
  191. goto ERRORS;
  192. }
  193. if (m_Head == NULL)
  194. {
  195. m_Head = NewNode;
  196. NewNode->Prev = NULL;
  197. NewNode->Next = NULL;
  198. }
  199. else
  200. {
  201. Temp = m_Head;
  202. while (Temp->Next != NULL )
  203. Temp = Temp->Next;
  204. // Insert the new node.
  205. Temp->Next = NewNode;
  206. NewNode->Prev = Temp;
  207. NewNode->Next = NULL;
  208. }
  209. return TRUE;
  210. }
  211. else
  212. {
  213. return FALSE;
  214. }
  215. ERRORS:
  216. return FALSE;
  217. }
  218. BOOL Clist::GetNextEntry(TCHAR *szSBucketID,
  219. TCHAR *szSBucketString,
  220. TCHAR *szSResponse1,
  221. TCHAR *szGResponse2,
  222. TCHAR *szCount,
  223. BOOL *bEOF
  224. )
  225. {
  226. if (m_CurrentPos == NULL)
  227. {
  228. *bEOF = TRUE;
  229. return FALSE;
  230. }
  231. if (StringCbCopy (szSBucketString,MAX_PATH *sizeof TCHAR, m_CurrentPos->szSBucketString) != S_OK)
  232. {
  233. goto ERRORS;
  234. }
  235. if (StringCbPrintf(szSBucketID,100 * sizeof TCHAR, _T("%d"), m_CurrentPos->iSBucketID)!= S_OK)
  236. {
  237. goto ERRORS;
  238. }
  239. if (StringCbPrintf(szCount,100 * sizeof TCHAR, _T("%d"), m_CurrentPos->icount) != S_OK)
  240. {
  241. goto ERRORS;
  242. }
  243. if (StringCbCopy (szSResponse1,MAX_PATH * sizeof TCHAR, m_CurrentPos->szSBucketResponse ) != S_OK)
  244. {
  245. goto ERRORS;
  246. }
  247. if (StringCbCopy (szGResponse2,MAX_PATH * sizeof TCHAR, m_CurrentPos->szGBucketResponse ) != S_OK)
  248. {
  249. goto ERRORS;
  250. }
  251. m_CurrentPos = m_CurrentPos->Next;
  252. return TRUE;
  253. ERRORS:
  254. return FALSE;
  255. }
  256. BOOL Clist::UpdateList( TCHAR *szSBucketID,
  257. TCHAR *szSBucketString,
  258. TCHAR *szSResponse1,
  259. TCHAR *szGResponse2
  260. )
  261. {
  262. PCSV_LAYOUT NewNode;
  263. PCSV_LAYOUT Temp;
  264. BOOL Done = FALSE;
  265. NewNode = (PCSV_LAYOUT) malloc (sizeof CSV_LAYOUT);
  266. if (NewNode)
  267. {
  268. if (StringCbCopy (NewNode->szSBucketString, sizeof NewNode->szSBucketString, szSBucketString) != S_OK)
  269. {
  270. goto ERRORS;
  271. }
  272. NewNode->iSBucketID = _ttoi(szSBucketID);
  273. NewNode->icount = 1;
  274. if (StringCbCopy (NewNode->szSBucketResponse,sizeof NewNode->szSBucketResponse, szSResponse1) != S_OK)
  275. {
  276. goto ERRORS;
  277. }
  278. if (StringCbCopy (NewNode->szGBucketResponse, sizeof NewNode->szGBucketResponse, szGResponse2) != S_OK)
  279. {
  280. goto ERRORS;
  281. }
  282. if (m_Head == NULL)
  283. {
  284. m_Head = NewNode;
  285. NewNode->Prev = NULL;
  286. NewNode->Next = NULL;
  287. }
  288. else
  289. {
  290. Temp = m_Head;
  291. do
  292. {
  293. if (!_tcsicmp(Temp->szSBucketString, NewNode->szSBucketString))
  294. {
  295. // We found one update the count
  296. Temp->icount++;
  297. // Now free the new node we built we no longer need it
  298. if (NewNode)
  299. {
  300. free (NewNode);
  301. }
  302. Done = TRUE;
  303. }
  304. else
  305. {
  306. Temp = Temp->Next;
  307. }
  308. }while ( (Temp != NULL) && (!Done));
  309. if (!Done)
  310. {
  311. // We hit the end of the list without finding the node.
  312. // Add it. Move temp to the end of the list
  313. Temp = m_Head;
  314. while (Temp->Next != NULL)
  315. {
  316. Temp = Temp->Next;
  317. }
  318. Temp->Next = NewNode;
  319. NewNode->Prev = Temp;
  320. NewNode->Next = NULL;
  321. }
  322. }
  323. return TRUE; // Prefix - This is not a memory leak the memory will be freed when the list
  324. // is deallocated in the destructor
  325. }
  326. else
  327. {
  328. return FALSE;
  329. }
  330. ERRORS:
  331. if (NewNode)
  332. free(NewNode);
  333. return FALSE;
  334. }
  335. void Clist::WriteCsv()
  336. {
  337. TCHAR szSBucketID[100];
  338. TCHAR szSBucketString[MAX_PATH];
  339. TCHAR szSResponse1[MAX_PATH];
  340. TCHAR szGResponse2[MAX_PATH];
  341. TCHAR szCount[100];
  342. BOOL bEOF;
  343. DWORD dwBytesWritten;
  344. TCHAR *Buffer = NULL;
  345. HANDLE hCsv;
  346. DWORD dwBufferSize = 0;
  347. hCsv = CreateFile( m_szCsvFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  348. if (hCsv != INVALID_HANDLE_VALUE)
  349. {
  350. dwBufferSize = 200 * sizeof TCHAR + 3* (MAX_PATH *sizeof TCHAR);
  351. Buffer = (TCHAR *) malloc (dwBufferSize);
  352. if (Buffer)
  353. {
  354. ResetCurrPos();
  355. while (GetNextEntry(szSBucketID,
  356. szSBucketString,
  357. szSResponse1,
  358. szGResponse2,
  359. szCount,
  360. &bEOF
  361. ) )
  362. {
  363. if (StringCbPrintf(Buffer,dwBufferSize, _T("%s,%s,%s,%s,%s\r\n"),
  364. szSBucketID,
  365. szSBucketString,
  366. szSResponse1,
  367. szGResponse2,
  368. szCount ) != S_OK)
  369. {
  370. ;
  371. }
  372. else
  373. WriteFile(hCsv, Buffer, _tcslen(Buffer) *sizeof TCHAR, &dwBytesWritten, NULL);
  374. }
  375. if (Buffer)
  376. {
  377. free (Buffer);
  378. }
  379. }
  380. CloseHandle(hCsv);
  381. }
  382. }
  383. void Clist::CleanupList()
  384. {
  385. PCSV_LAYOUT Temp;
  386. Temp = m_Head;
  387. while (Temp != NULL)
  388. {
  389. if (Temp->Next != NULL)
  390. {
  391. Temp = Temp->Next;
  392. free(Temp->Prev);
  393. Temp->Prev = NULL;
  394. }
  395. else
  396. {
  397. free(Temp);
  398. Temp = NULL;
  399. m_Head=NULL;
  400. }
  401. }
  402. bInitialized = FALSE;
  403. }