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.

115 lines
2.7 KiB

  1. // ############################################################################
  2. // INCLUDES
  3. #include "pch.hpp"
  4. #include "ccsv.h"
  5. #include "debug.h"
  6. // ############################################################################
  7. // DEFINES
  8. #define chComma ','
  9. #define chNewline '\n'
  10. #define chReturn '\r'
  11. // ############################################################################
  12. //
  13. // CCSVFile - simple file i/o for CSV files
  14. //
  15. CCSVFile::CCSVFile()
  16. {
  17. m_hFile = 0;
  18. m_iLastRead = 0;
  19. m_pchLast = m_pchBuf = NULL;
  20. }
  21. // ############################################################################
  22. CCSVFile::~CCSVFile()
  23. {
  24. AssertSz(!m_hFile,"CCSV file is still open");
  25. }
  26. // ############################################################################
  27. BOOLEAN CCSVFile::Open(LPCSTR pszFileName)
  28. {
  29. AssertSz(!m_hFile, "a file is already open.");
  30. m_hFile = CreateFile((LPCTSTR)pszFileName,
  31. GENERIC_READ, FILE_SHARE_READ,
  32. 0, OPEN_EXISTING, 0, 0);
  33. if (INVALID_HANDLE_VALUE == m_hFile)
  34. {
  35. return FALSE;
  36. }
  37. m_pchLast = m_pchBuf = NULL;
  38. return TRUE;
  39. }
  40. // ############################################################################
  41. BOOLEAN CCSVFile::ReadToken(LPSTR psz, DWORD cbMax)
  42. {
  43. LPSTR pszLast;
  44. char ch;
  45. ch = (char) ChNext();
  46. if (-1 == ch)
  47. {
  48. return FALSE;
  49. }
  50. pszLast = psz + (cbMax - 1);
  51. while (psz < pszLast && chComma != ch && chNewline != ch && -1 != ch)
  52. {
  53. *psz++ = ch;
  54. ch = (char) ChNext(); //Read in the next character
  55. }
  56. *psz++ = '\0';
  57. return TRUE;
  58. }
  59. // ############################################################################
  60. void CCSVFile::Close(void)
  61. {
  62. if (m_hFile)
  63. CloseHandle(m_hFile);
  64. #ifdef DEBUG
  65. if (!m_hFile) Dprintf("CCSVFile::Close was called, but m_hFile was already 0\n");
  66. #endif
  67. m_hFile = 0;
  68. }
  69. // ############################################################################
  70. BOOL CCSVFile::FReadInBuffer(void)
  71. {
  72. //Read another buffer
  73. #ifdef WIN16
  74. if ((m_cchAvail = _read(m_hFile, m_rgchBuf, CCSVFILE_BUFFER_SIZE)) <= 0)
  75. return FALSE;
  76. #else
  77. if (!ReadFile(m_hFile, m_rgchBuf, CCSVFILE_BUFFER_SIZE, &m_cchAvail, NULL) || !m_cchAvail)
  78. {
  79. return FALSE; //nothing more to read
  80. }
  81. #endif
  82. m_pchBuf = m_rgchBuf;
  83. m_pchLast = m_pchBuf + m_cchAvail;
  84. return TRUE; //success
  85. }
  86. // ############################################################################
  87. inline int CCSVFile::ChNext(void)
  88. {
  89. LNextChar:
  90. if (m_pchBuf >= m_pchLast && !FReadInBuffer()) //implies that we finished reading the buffer. Read in some more.
  91. return -1; //nothing more to read
  92. m_iLastRead = *m_pchBuf++;
  93. if (chReturn == m_iLastRead)
  94. goto LNextChar; //faster to NOT make extra function call
  95. return m_iLastRead;
  96. }