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.

71 lines
1.3 KiB

  1. //---------------------------------------------------------------------------------------
  2. //
  3. // @doc
  4. //
  5. // @module passportfile.hpp | Passport smart class to wrap FILE*
  6. //
  7. // Author: stevefu
  8. //
  9. // Date: 05/28/2000
  10. //
  11. // Copyright <cp> 1999-2000 Microsoft Corporation. All Rights Reserved.
  12. //
  13. //---------------------------------------------------------------------------------------
  14. #ifndef PASSPORTFILE_HPP
  15. #define PASSPORTFILE_HPP
  16. #pragma once
  17. #include <stdio.h>
  18. #include <tchar.h>
  19. class PassportFile
  20. {
  21. private:
  22. FILE* m_hFile;
  23. public:
  24. PassportFile() : m_hFile(NULL) { }
  25. ~PassportFile()
  26. {
  27. if ( m_hFile != NULL) fclose(m_hFile);
  28. }
  29. BOOL Open( const TCHAR* filename, const TCHAR* mode)
  30. {
  31. m_hFile = _tfopen(filename, mode);
  32. return ( NULL != m_hFile );
  33. }
  34. void Close()
  35. {
  36. ATLASSERT(m_hFile != NULL);
  37. fclose(m_hFile);
  38. }
  39. // return length, -1 if error or eof
  40. int ReadLine(TCHAR* string, int n)
  41. {
  42. ATLASSERT(m_hFile != NULL);
  43. int len = -1;
  44. if ( _fgetts(string, n, m_hFile) )
  45. {
  46. len = _tcslen(string);
  47. // trim the new line
  48. if ( len > 0 && string[len-1] == TEXT('\n') )
  49. {
  50. string[len-1] = TEXT('\0');
  51. len--;
  52. }
  53. }
  54. return len;
  55. }
  56. inline operator FILE*() { return m_hFile; }
  57. };
  58. #endif // PASSPORTFILE_HPP