AcFStream.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 Autodesk, Inc. All rights reserved.
  4. //
  5. // Use of this software is subject to the terms of the Autodesk license
  6. // agreement provided at the time of installation or download, or which
  7. // otherwise accompanies this software in either electronic or hard copy form.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. //
  11. // Name: AcFStream.h
  12. //
  13. // Description: Wrapper classes for std:ofstream and std::ifstream.
  14. //
  15. //////////////////////////////////////////////////////////////////////////////
  16. // Note: 1. this header should be included *after* fstream headers
  17. //
  18. #pragma once
  19. #ifdef _ADESK_MAC_
  20. #include <malloc/malloc.h>
  21. #else
  22. #include "malloc.h" // for _alloca()
  23. #endif
  24. #include "AdAChar.h"
  25. #include "AdCharFmt.h"
  26. #ifdef ASSERT
  27. #define AcFStream_Assert ASSERT
  28. #elif defined assert
  29. #define AcFStream_Assert assert
  30. #else
  31. #define AcFStream_Assert(T)
  32. #endif
  33. #ifdef UNICODE
  34. inline int AcFStream_wideToMulti(const wchar_t *pSrc, int nSrcSize,
  35. char *pDest, int nDestSize);
  36. inline std::locale AcFStream_curLocale();
  37. #endif
  38. class AcIfstream : public
  39. #ifndef UNICODE
  40. std::ifstream {
  41. #else
  42. std::wifstream {
  43. #endif
  44. public:
  45. AcIfstream() {};
  46. AcIfstream(const ACHAR *pName);
  47. #ifdef UNICODE
  48. void open(const wchar_t *pName);
  49. void open(const wchar_t *pName, ios_base::openmode nMode);
  50. #endif
  51. };
  52. class AcOfstream : public
  53. #ifndef UNICODE
  54. std::ofstream {
  55. #else
  56. std::wofstream {
  57. #endif
  58. public:
  59. AcOfstream() {};
  60. AcOfstream(const ACHAR *pName);
  61. #ifdef UNICODE
  62. void open(const wchar_t *pName);
  63. void open(const wchar_t *pName, ios_base::openmode nMode);
  64. #endif
  65. };
  66. #ifndef UNICODE
  67. inline AcIfstream::AcIfstream(const ACHAR *pName) : std::ifstream(pName)
  68. {
  69. }
  70. inline AcOfstream::AcOfstream(const ACHAR *pName) : std::ofstream(pName)
  71. {
  72. }
  73. #else
  74. inline AcIfstream::AcIfstream(const ACHAR *pName)
  75. {
  76. this->open(pName);
  77. }
  78. inline AcOfstream::AcOfstream(const ACHAR *pName)
  79. {
  80. this->open(pName);
  81. }
  82. inline std::locale AcFStream_curLocale()
  83. {
  84. #ifdef _ADESK_WINDOWS_
  85. char buf[100];
  86. sprintf_s(buf, 100, ".%d", GetACP());
  87. // Only set LC_CTYPE, because we don't want it to start
  88. // formatting numbers with comma separators, etc
  89. return std::locale(buf, LC_CTYPE);
  90. #else
  91. STUB_WARNING(AcFStream_curLocale);
  92. return std::locale();
  93. #endif
  94. }
  95. inline int AcFStream_wideToMulti(const wchar_t *pSrc, int nSrcSize,
  96. char *pDest, int nDestSize)
  97. {
  98. return ::WideCharToMultiByte(
  99. CP_ACP,
  100. #if defined(WINVER) && (WINVER >= 0x0500)
  101. // This function is currently only being used to
  102. // convert filenames from widechar to ansi. So
  103. // we want it to fail if we can't get an exact
  104. // mapping.
  105. WC_NO_BEST_FIT_CHARS,
  106. #else
  107. 0,
  108. #endif
  109. pSrc,
  110. nSrcSize,
  111. pDest,
  112. nDestSize,
  113. NULL, // lpDefaultChar
  114. NULL); // lpUsedDefaultChar
  115. }
  116. inline void AcIfstream::open(const wchar_t *pName)
  117. {
  118. #ifdef _ADESK_WINDOWS_
  119. this->std::wifstream::open(pName);
  120. this->imbue(AcFStream_curLocale());
  121. #else
  122. STUB_WARNING(AcIfstream::open);
  123. #endif
  124. }
  125. inline void AcIfstream::open(const wchar_t *pName, ios_base::openmode nMode)
  126. {
  127. #ifdef _ADESK_WINDOWS_
  128. this->std::wifstream::open(pName, nMode);
  129. this->imbue(AcFStream_curLocale());
  130. #else
  131. STUB_WARNING(AcIfstream::open);
  132. #endif
  133. }
  134. inline void AcOfstream::open(const wchar_t *pName)
  135. {
  136. #ifdef _ADESK_WINDOWS_
  137. this->std::wofstream::open(pName);
  138. this->imbue(AcFStream_curLocale());
  139. #else
  140. STUB_WARNING(AcOfstream::open);
  141. #endif
  142. }
  143. inline void AcOfstream::open(const wchar_t *pName, ios_base::openmode nMode)
  144. {
  145. #ifdef _ADESK_WINDOWS_
  146. this->std::wofstream::open(pName, nMode);
  147. this->imbue(AcFStream_curLocale());
  148. #else
  149. STUB_WARNING(AcOfstream::open);
  150. #endif
  151. }
  152. #endif