AcCFileWrappers.h 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. //
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Copyright 2015 Autodesk, Inc. All rights reserved.
  5. //
  6. // Use of this software is subject to the terms of the Autodesk license
  7. // agreement provided at the time of installation or download, or which
  8. // otherwise accompanies this software in either electronic or hard copy form.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. //
  12. // Name: AcCFileWrappers.h
  13. //
  14. // Description: Wrapper classes for CFile and CStdioFile. Expose all
  15. // of the underlying classes, and override the Read,
  16. // Write, ReadString and WriteString methods.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19. // Note: 1. this header should be included *after* mfc headers
  20. // 2. there is a duplicate copy of this header file in heidi\source\heidi,
  21. // please update the copy in heidi whenever this file gets updated.
  22. //
  23. #pragma once
  24. #pragma warning(push, 4)
  25. #include "AdCharFmt.h"
  26. #include "adesk.h"
  27. #ifdef ASSERT
  28. #define AcCFile_Assert ASSERT
  29. #elif defined assert
  30. #define AcCFile_Assert assert
  31. #elif defined _ASSERTE
  32. #define AcCFile_Assert _ASSERTE
  33. #else
  34. #define AcCFile_Assert(x)
  35. #endif
  36. // forward declarations
  37. #ifdef UNICODE
  38. void acByteSwap(wchar_t &wch);
  39. void acWriteWCharToCFile(wchar_t wch, CFile *pFile, unsigned nFmt, bool bUseCIF);
  40. bool acReadCIFFromCFile(CFile *pCFile, wchar_t &wch);
  41. bool acReadAnsiCharFromCFile(CFile *pCFile, wchar_t &wch, AdCharFormatter *pChFmtr);
  42. bool acReadUtf8CharFromCFile(CFile *pCFile, wchar_t &wch, AdCharFormatter *pChFmtr);
  43. bool acReadUtf16CharFromCFile(CFile *pCFile, wchar_t &wch, AdCharFormatter *pChFmtr);
  44. #endif
  45. unsigned acCheckCFileCharFormat(CFile *pCFile);
  46. // Used by AcCFile and AcCStdioFile
  47. class AcOutputBufMgr
  48. {
  49. public:
  50. AcOutputBufMgr();
  51. ~AcOutputBufMgr();
  52. bool attachBuffer(void *pBuffer, unsigned nBufSize);
  53. bool detachBuffer();
  54. bool hasBuffer() const;
  55. unsigned byteCount() const;
  56. void * pointer() const;
  57. void * requestBytes(unsigned nBytesNeeded);
  58. unsigned takeBytes(unsigned nBytesUsed);
  59. bool reset();
  60. private:
  61. void * mpBuffer;
  62. unsigned mnBufSize; // total size of the buffer, in bytes
  63. unsigned mnByteCount; // how many bytes are in the buffer now
  64. unsigned mnBytesRequested;
  65. };
  66. inline AcOutputBufMgr::AcOutputBufMgr()
  67. : mpBuffer(NULL), mnBufSize(0), mnByteCount(0), mnBytesRequested(0)
  68. {
  69. }
  70. inline AcOutputBufMgr::~AcOutputBufMgr()
  71. {
  72. AcCFile_Assert(this->mnByteCount == 0); // should be flushed by now
  73. AcCFile_Assert(this->mnBytesRequested == 0);
  74. }
  75. inline bool AcOutputBufMgr::hasBuffer() const
  76. {
  77. return this->mpBuffer != NULL;
  78. }
  79. inline unsigned AcOutputBufMgr::byteCount() const
  80. {
  81. return this->mnByteCount;
  82. }
  83. inline void * AcOutputBufMgr::pointer() const
  84. {
  85. return this->mpBuffer;
  86. }
  87. inline bool AcOutputBufMgr::attachBuffer(void *pBuffer, unsigned nBufSize)
  88. {
  89. AcCFile_Assert(pBuffer != NULL);
  90. AcCFile_Assert(nBufSize > 2);
  91. AcCFile_Assert(nBufSize < 0x10000000); // 256M sanity check
  92. AcCFile_Assert(this->mpBuffer == NULL);
  93. AcCFile_Assert(this->mnBufSize == 0);
  94. AcCFile_Assert(this->mnByteCount == 0);
  95. AcCFile_Assert(this->mnBytesRequested == 0);
  96. if (this->mpBuffer != NULL || pBuffer == NULL)
  97. return false; // bad input or unexpected call
  98. this->mpBuffer = pBuffer;
  99. this->mnBufSize = nBufSize;
  100. return true; // success
  101. }
  102. inline bool AcOutputBufMgr::detachBuffer()
  103. {
  104. AcCFile_Assert(this->mpBuffer != NULL);
  105. AcCFile_Assert(this->mnBufSize != 0);
  106. AcCFile_Assert(this->mnByteCount == 0); // should be flushed by now
  107. AcCFile_Assert(this->mnBytesRequested == 0);
  108. if (this->mpBuffer == NULL || this->mnByteCount != 0
  109. || this->mnBytesRequested != 0)
  110. return false; // no buffer to release, or it's still busy
  111. this->mpBuffer = NULL;
  112. this->mnBufSize = 0;
  113. return true; // success
  114. }
  115. // client requests the pointer and says how many bytes he may need
  116. inline void * AcOutputBufMgr::requestBytes(unsigned nBytesNeeded)
  117. {
  118. AcCFile_Assert(this->hasBuffer()); // better have a buffer!
  119. AcCFile_Assert(this->mnBytesRequested == 0);
  120. if (!this->hasBuffer() || this->mnBytesRequested != 0)
  121. return NULL; // no buffering, or buffer already reserved
  122. AcCFile_Assert(this->mnBufSize >= 2);
  123. AcCFile_Assert(this->mnByteCount <= this->mnBufSize);
  124. AcCFile_Assert(nBytesNeeded >= 1);
  125. AcCFile_Assert(nBytesNeeded < this->mnBufSize);
  126. if (nBytesNeeded < 1 || nBytesNeeded >= this->mnBufSize)
  127. return NULL; // invalid reserved size request
  128. if (this->mnByteCount + nBytesNeeded > this->mnBufSize)
  129. return NULL; // buffer is full, needs to be flushed
  130. this->mnBytesRequested = nBytesNeeded;
  131. // return next free slot in the buffer
  132. return reinterpret_cast<char *>(this->mpBuffer) + this->mnByteCount;
  133. }
  134. // client tells us how many bytes he used, and we return how many
  135. // bytes are left in the buffer.
  136. inline unsigned AcOutputBufMgr::takeBytes(unsigned nBytesUsed)
  137. {
  138. AcCFile_Assert(this->hasBuffer()); // better have a buffer!
  139. AcCFile_Assert(this->mnBytesRequested != 0);
  140. AcCFile_Assert(this->mnBytesRequested >= nBytesUsed);
  141. if (!this->hasBuffer() || this->mnBytesRequested < nBytesUsed)
  142. return 0; // no buffering, or trying to release too much
  143. this->mnBytesRequested = 0;
  144. this->mnByteCount += nBytesUsed;
  145. ASSERT(this->mnByteCount <= this->mnBufSize);
  146. return this->mnBufSize - this->mnByteCount; // return how many bytes left
  147. }
  148. inline bool AcOutputBufMgr::reset()
  149. {
  150. AcCFile_Assert(this->hasBuffer()); // better have a buffer!
  151. AcCFile_Assert(this->mnBytesRequested == 0);
  152. if (!this->hasBuffer() || this->mnBytesRequested != 0)
  153. return false; // no buffer, or unmatched request
  154. this->mnByteCount = 0;
  155. return true; // mission accomplished
  156. }
  157. class AcCFile : public CFile
  158. {
  159. public:
  160. // declare compatible ctors and dtor
  161. AcCFile();
  162. AcCFile(HANDLE hFile);
  163. AcCFile(LPCTSTR lpszFileName, UINT nOpenFlags);
  164. virtual ~AcCFile();
  165. // Override the base filing operations
  166. virtual UINT Read(void *lpBuf, UINT nCount);
  167. virtual void Write(const void *lpBuf, UINT nCount);
  168. // And add our own overloads
  169. virtual UINT Read(LPTSTR lpBuf, UINT nCount);
  170. virtual void Write(LPCTSTR pString, UINT nCount); // write string
  171. virtual void Write(LPCTSTR pString); // write null terminated string
  172. // Format related methods
  173. unsigned getCharFormat() const { return this->mChFmtr.getFormat(); }
  174. unsigned setCharFormat(unsigned nFmt) {
  175. return this->mChFmtr.setFormat(nFmt); }
  176. bool getUseCIF() const { return this->mChFmtr.getUseCIF(); }
  177. bool setUseCIF(bool bUseCIF) {
  178. return this->mChFmtr.setUseCIF(bUseCIF); }
  179. bool getExpandLF() const { return this->mChFmtr.getExpandLF(); }
  180. bool setExpandLF(bool bExpandLF) {
  181. return this->mChFmtr.setExpandLF(bExpandLF); }
  182. bool readBOM();
  183. bool writeBOM();
  184. // Buffering methods, for performance. Client can provide a buffer
  185. // for output, so that a minimum number of writes to the underlying
  186. // file are done. This helps greatly when writing across a high
  187. // latency network.
  188. //
  189. bool attachBuffer(void *pBuffer, unsigned nBufSize);
  190. bool detachBuffer();
  191. bool flushBytes();
  192. bool hasBuffer() const;
  193. private:
  194. AdCharFormatter mChFmtr;
  195. AcOutputBufMgr mOutputBufMgr;
  196. };
  197. #ifdef _ADESK_WINDOWS_
  198. class AcCStdioFile : public CStdioFile
  199. {
  200. public:
  201. // declare compatible ctors and dtor
  202. AcCStdioFile();
  203. AcCStdioFile(FILE* pOpenStream);
  204. AcCStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags);
  205. virtual ~AcCStdioFile();
  206. // Override the string filing operations
  207. virtual void WriteString(LPCTSTR lpsz);
  208. virtual LPTSTR ReadString(LPTSTR lpsz, UINT nMax);
  209. virtual BOOL ReadString(CString& rString);
  210. // Format related methdos
  211. unsigned getCharFormat() const { return this->mChFmtr.getFormat(); }
  212. unsigned setCharFormat(unsigned nFmt) {
  213. return this->mChFmtr.setFormat(nFmt); }
  214. bool getUseCIF() const { return this->mChFmtr.getUseCIF(); }
  215. bool setUseCIF(bool bUseCIF) {
  216. return this->mChFmtr.setUseCIF(bUseCIF); }
  217. bool getExpandLF() const { return this->mChFmtr.getExpandLF(); }
  218. bool setExpandLF(bool bExpandLF) {
  219. return this->mChFmtr.setExpandLF(bExpandLF); }
  220. bool readBOM();
  221. bool writeBOM();
  222. // Buffering methods, for performance. Client can provide a buffer
  223. // for output, so that a minimum number of writes to the underlying
  224. // file are done. This helps greatly when writing across a high
  225. // latency network.
  226. //
  227. bool attachBuffer(void *pBuffer, unsigned nBufSize);
  228. bool detachBuffer();
  229. bool flushBytes();
  230. bool hasBuffer() const;
  231. private:
  232. AdCharFormatter mChFmtr;
  233. AcOutputBufMgr mOutputBufMgr;
  234. };
  235. #endif //_ADESK_WINDOWS_ MAC_PORT
  236. // This is the helper function to determine file type of the
  237. // currently opened CFile. It is compiled both under MBCS and
  238. // UNICODE builds. If no BOM is present, we return kAnsi.
  239. inline unsigned acCheckCFileCharFormat(CFile *pCFile)
  240. {
  241. AcCFile_Assert(pCFile != NULL);
  242. AcCFile_Assert((pCFile->m_hFile) != (CFile::hFileNull));
  243. // Skip type check for too small files.
  244. const int nLength = (int) pCFile->GetLength();
  245. if(nLength <= 3)
  246. return AdCharFormatter::kAnsi;
  247. // Backup current file pointer position.
  248. // Note: we might want to require that this method be called right
  249. // after open, when the file pointer is still 0. And then we
  250. // could leave the file pointer positioned after the BOM on return.
  251. //
  252. const ULONGLONG ullPrevPos = pCFile->GetPosition();
  253. pCFile->Seek(0L, SEEK_SET);
  254. DWORD dwMagicCode = 0; // Read file header (BOM), if any.
  255. // Note: if file is not opened for read, this will throw
  256. // an exception.
  257. pCFile->Read(&dwMagicCode, 4);
  258. // Restore file pointer position.
  259. pCFile->Seek(ullPrevPos, SEEK_SET);
  260. // Note: if we found a BOM, we might want to seek beyond
  261. // it, so caller doesn't run into it.
  262. // Determine file's char type by header.
  263. const unsigned nCharFmt = AdCharFormatter::lookupBOM(dwMagicCode);
  264. return (nCharFmt == AdCharFormatter::kUnknown) ?
  265. AdCharFormatter::kAnsi : nCharFmt;
  266. }
  267. // CFile never expands LFs (you can't open a CFile with
  268. // typeBinary mode). So callers won't be expecting it
  269. // to happen. They can override this behavior by calling
  270. // AcCFile::setExpandLF(true) if they want to.
  271. inline AcCFile::AcCFile()
  272. : mChFmtr(AdCharFormatter::kAnsi,
  273. false, // useCIF
  274. false) // expandLF
  275. {
  276. this->mChFmtr.setExpandLF(false);
  277. }
  278. inline AcCFile::AcCFile(HANDLE hFile)
  279. : CFile(hFile),
  280. mChFmtr(AdCharFormatter::kAnsi,
  281. false, // useCIF
  282. false) // expandLF
  283. {
  284. }
  285. inline AcCFile::AcCFile(LPCTSTR lpszFileName, UINT nOpenFlags)
  286. : CFile(lpszFileName, nOpenFlags),
  287. mChFmtr(AdCharFormatter::kAnsi,
  288. false, // useCIF
  289. false) // expandLF
  290. {
  291. }
  292. inline AcCFile::~AcCFile()
  293. {
  294. }
  295. #ifdef UNICODE
  296. inline void acWriteWCharToCFile(wchar_t wch, CFile *pFile,
  297. const AdCharFormatter &charFmt)
  298. {
  299. char chBuf[8]; // CIF is 7 bytes, utf-32 cr-lf might be 8
  300. const int nBytes = charFmt.wcharToBytes(wch, chBuf,
  301. sizeof(chBuf));
  302. AcCFile_Assert(nBytes >= 1);
  303. AcCFile_Assert(nBytes <= 8);
  304. pFile->Write(chBuf, nBytes);
  305. }
  306. #endif
  307. inline void AcCFile::Write(const void * lpBuf, UINT nCount)
  308. {
  309. this->CFile::Write(lpBuf, nCount);
  310. }
  311. inline void AcCFile::Write(LPCTSTR lpBuf, UINT nCount)
  312. {
  313. AcCFile_Assert(nCount < 0x1000000); // 16M sanity check
  314. #ifndef UNICODE
  315. this->CFile::Write(lpBuf, nCount);
  316. #else
  317. const bool bHasBuffer = this->hasBuffer();
  318. for (unsigned i = 0; i < nCount; i++) {
  319. if (!bHasBuffer)
  320. ::acWriteWCharToCFile(lpBuf[i], this, this->mChFmtr);
  321. else {
  322. // worst case is 8 bytes (utf-32 cr-lf)
  323. const int kReservedSize = 8;
  324. void *pOutBuf = this->mOutputBufMgr.requestBytes(kReservedSize);
  325. const int nBytes = this->mChFmtr.wcharToBytes(lpBuf[i],
  326. reinterpret_cast<char *>(pOutBuf),
  327. kReservedSize);
  328. AcCFile_Assert(nBytes >= 1);
  329. AcCFile_Assert(nBytes <= kReservedSize);
  330. const unsigned nBytesLeft = this->mOutputBufMgr.takeBytes(nBytes);
  331. if (nBytesLeft <= kReservedSize)
  332. this->flushBytes();
  333. }
  334. }
  335. #endif
  336. }
  337. inline void AcCFile::Write(LPCTSTR lpBuf)
  338. {
  339. #ifndef UNICODE
  340. // Unfortunately, hard cast is needed to change size_t
  341. // to UINT to avoid new 64-bit compatible warnings.
  342. this->Write(lpBuf, (UINT)strlen(lpBuf));
  343. #else
  344. this->Write(lpBuf, AdCharFormatter::wcsLength(lpBuf));
  345. #endif
  346. }
  347. inline UINT AcCFile::Read(void * lpBuf, UINT nCount)
  348. {
  349. return this->CFile::Read(lpBuf, nCount);
  350. }
  351. inline bool AcCFile::readBOM()
  352. {
  353. const ULONGLONG dwPosition = this->GetPosition();
  354. AcCFile_Assert(dwPosition == 0);
  355. if (dwPosition != 0)
  356. return false; // Can't do this unless we're at start of file.
  357. unsigned short nVal;
  358. if (this->Read(&nVal, 2) == 2) {
  359. if (nVal == 0xfeff) {
  360. this->setCharFormat(AdCharFormatter::kUtf16LE);
  361. return true;
  362. }
  363. if (nVal == 0xfffe) {
  364. this->setCharFormat(AdCharFormatter::kUtf16BE);
  365. return true;
  366. }
  367. if (nVal == 0xbbef) {
  368. unsigned char nByte3;
  369. if (this->Read(&nByte3, 1) == 1) {
  370. if (nByte3 == 0xbf) {
  371. this->setCharFormat(AdCharFormatter::kUtf8);
  372. return true;
  373. }
  374. }
  375. }
  376. else if (nVal == 0 || nVal == 0xfeff) {
  377. unsigned nVal2;
  378. if (this->Read(&nVal2, 2) == 2) {
  379. if (nVal == 0 && nVal2 == 0xfffe) {
  380. this->setCharFormat(AdCharFormatter::kUtf32BE);
  381. return true;
  382. }
  383. else if (nVal == 0xfeff && nVal2 == 0) {
  384. this->setCharFormat(AdCharFormatter::kUtf32LE);
  385. return true;
  386. }
  387. }
  388. }
  389. }
  390. // If got here, then no BOM found, so reset
  391. // to file beginning. Leave format what it was.
  392. this->SeekToBegin();
  393. return false;
  394. }
  395. inline bool AcCFile::writeBOM()
  396. {
  397. AcCFile_Assert((this->m_hFile) != CFile::hFileNull);
  398. if ((this->m_hFile) == CFile::hFileNull)
  399. return false; // There must be an associated file.
  400. const ULONGLONG dwPosition = this->GetPosition();
  401. AcCFile_Assert(dwPosition == 0);
  402. if (dwPosition != 0)
  403. return false; // Can't do this unless we're at start of file.
  404. unsigned nBom = 0;
  405. const int cbBomSize = AdCharFormatter::getBOM(nBom,
  406. this->getCharFormat());
  407. if (cbBomSize == 0) // AdcharFormatter::getBOM would
  408. return false; // have brought up an assertion.
  409. this->Write(&nBom, cbBomSize);
  410. return true;
  411. }
  412. inline bool AcCFile::attachBuffer(void *pBuffer, unsigned nBufSize)
  413. {
  414. return this->mOutputBufMgr.attachBuffer(pBuffer, nBufSize);
  415. }
  416. inline bool AcCFile::detachBuffer()
  417. {
  418. return this->mOutputBufMgr.detachBuffer();
  419. }
  420. inline bool AcCFile::hasBuffer() const
  421. {
  422. return this->mOutputBufMgr.hasBuffer();
  423. }
  424. inline bool AcCFile::flushBytes()
  425. {
  426. AcCFile_Assert(this->hasBuffer());
  427. if (!this->hasBuffer())
  428. return false;
  429. const unsigned nBytes = this->mOutputBufMgr.byteCount();
  430. if (nBytes != 0) {
  431. const void *pBuf = this->mOutputBufMgr.pointer();
  432. this->Write(pBuf, nBytes);
  433. }
  434. this->mOutputBufMgr.reset();
  435. return true;
  436. }
  437. #ifdef UNICODE
  438. inline void acByteSwap(wchar_t &wch)
  439. {
  440. // the &-ing is probably unnecessary, but it doesn't hurt
  441. const wchar_t lobits = (wch >> 8) & 0x00ff;
  442. const wchar_t hibits = (wch << 8) & 0xff00;
  443. wch = lobits | hibits;
  444. }
  445. inline bool acReadCIFFromCFile(CFile *pCFile, wchar_t &wch)
  446. {
  447. // leading slash has already been read. See if we can read
  448. // a \U+xxxx or \M+nxxyy sequence.
  449. const ULONGLONG nCurPos = pCFile->GetPosition();
  450. char chbuf[9];
  451. chbuf[0] = '\\'; // set up for formatting function
  452. // try reading 6 more, assuming its CIF. If it looks like
  453. // MIF, then read one more.
  454. const unsigned nCharRead = pCFile->Read(chbuf+1, 1);
  455. AcCFile_Assert(nCharRead <= 1);
  456. if (nCharRead == 1) {
  457. if (chbuf[1] == 'U' || chbuf[1] == 'u') {
  458. // read five more chars, to get "+xxxx"
  459. const unsigned nMoreCharsRead = pCFile->Read(chbuf+2, 5);
  460. AcCFile_Assert(nMoreCharsRead <= 5);
  461. chbuf[2 + nMoreCharsRead] = 0; // null terminate it
  462. if (AdCharFormatter::isCIFString(chbuf))
  463. if (AdCharFormatter::parseCIF(chbuf, wch))
  464. return true;
  465. }
  466. else if (chbuf[1] == 'M' || chbuf[1] == 'm') {
  467. // read six more chars, to get "+nxxyy"
  468. const unsigned nMoreCharsRead = pCFile->Read(chbuf+2, 6);
  469. AcCFile_Assert(nMoreCharsRead <= 6);
  470. chbuf[2 + nMoreCharsRead] = 0; // null terminate it
  471. if (AdCharFormatter::isCIFString(chbuf))
  472. if (AdCharFormatter::parseCIF(chbuf, wch))
  473. return true;
  474. }
  475. }
  476. // If got here, then no MIF or CIF
  477. pCFile->Seek(nCurPos, SEEK_SET); // restore file pointer
  478. return false; // no CIF found
  479. }
  480. inline bool acReadAnsiCharFromCFile(CFile *pCFile, wchar_t &wch, AdCharFormatter *pChFmtr)
  481. {
  482. char chbuf[2];
  483. const bool bUseCIF = pChFmtr->getUseCIF();
  484. // Note: we are here calling the Read method which takes a
  485. // void * argument. If pCFile is an AcCFile object, then
  486. // we end up in AcCFile::Read, which then calls CFile::Read.
  487. // If pCFile is an AcCStdioFile, then we end up in
  488. // CFile::Read immediately.
  489. //
  490. // Note also about Read(): if the file is open as a text
  491. // file, then \r\n are automatically converted to \n. But
  492. // if file is opened as binary, then the \r and \n come
  493. // back separately. That is probably not desired by the caller,
  494. // but that's the way it work. If they don't like it, they
  495. // should open the file as text.
  496. //
  497. const int nCharsRead = pCFile->Read(chbuf, 1);
  498. if (nCharsRead <= 0)
  499. return false; // probably end of file
  500. AcCFile_Assert(nCharsRead == 1);
  501. if (chbuf[0] >= 0 && static_cast<unsigned char>(chbuf[0]) <= 0x7f) {
  502. wch = chbuf[0];
  503. if (chbuf[0] == '\r' && pChFmtr->getExpandLF()) {
  504. unsigned char newLine = 0;
  505. const int nBytesRead = pCFile->Read(&newLine, 1);
  506. if (nBytesRead < 1) // '\r' at the end of file...
  507. return true; // ... weird but we'll take it.
  508. if (newLine != '\n') { // Not a new line character!
  509. pCFile->Seek(-1L, CFile::current);
  510. return true; // Return '\r' as it is...
  511. }
  512. wch = L'\n'; // Translated to new line character.
  513. }
  514. if (chbuf[0] == '\\' && bUseCIF && acReadCIFFromCFile(pCFile, wch))
  515. return true; // wch has the native char gotten from CIF
  516. return true;
  517. }
  518. bool bDoubleByte = false;
  519. if (::IsDBCSLeadByte(chbuf[0])) {
  520. const int nTrailingCharsRead = pCFile->Read(chbuf+1, 1);
  521. AcCFile_Assert(nTrailingCharsRead == 1);
  522. if (nTrailingCharsRead != 1) {
  523. // malformed file, ends halfway through a doublebyte char!
  524. // should we throw an exception?
  525. return false;
  526. }
  527. bDoubleByte = true;
  528. }
  529. const int nConverted = ::MultiByteToWideChar(
  530. CP_ACP,
  531. 0, // flags
  532. chbuf,
  533. bDoubleByte ? 2 : 1,
  534. &wch,
  535. 1);
  536. AcCFile_Assert(nConverted == 1);
  537. if (nConverted != 1) {
  538. // error condition?
  539. }
  540. return true;
  541. }
  542. inline bool acReadUtf8CharFromCFile(CFile *pCFile,
  543. wchar_t &wch,
  544. AdCharFormatter *pChFmtr)
  545. {
  546. char chbuf[4];
  547. wch = 0;
  548. // Note: UTF-8 encodes a single character in 1 to 4 bytes.
  549. // The following bit patterns in the first byte determine the
  550. // length of bytes that a particular character was encoded in:
  551. //
  552. // 0xxxxxxx (0x7F): Character that takes up one byte.
  553. // 110xxxxx (0xC0): Character that takes up two bytes.
  554. // 1110xxxx (0xE0): Character that takes up tree bytes.
  555. // 11110xxx (0xF0): Character that takes up four bytes.
  556. //
  557. unsigned nCharsRead = pCFile->Read(chbuf, 1);
  558. if (nCharsRead <= 0)
  559. return false; // probably end of file
  560. AcCFile_Assert(nCharsRead == 1);
  561. unsigned nByteCount = 1;
  562. unsigned char firstByte = (unsigned char) chbuf[0];
  563. // Characters that taking up one byte.
  564. if (!(firstByte & 0x80)) // Highest bit not set.
  565. {
  566. wch = (wchar_t) firstByte;
  567. if (firstByte == '\r' && pChFmtr->getExpandLF()) {
  568. unsigned char newLine = 0;
  569. const int nBytesRead = pCFile->Read(&newLine, 1);
  570. if (nBytesRead < 1) // '\r' at the end of file...
  571. return true; // ... weird but we'll take it.
  572. if (newLine != '\n') { // Not a new line character?
  573. pCFile->Seek(-1L, CFile::current);
  574. return true; // Return '\r' as it is...
  575. }
  576. wch = L'\n'; // Translated to new line character.
  577. }
  578. return true;
  579. }
  580. // Characters that taking up multiple bytes. Note the sequence
  581. // of checking for these bits is important, always start with
  582. // 0xF0 and later move on to a lower value. (For an example the
  583. // case of 0xF0, it will be true for all these test cases.)
  584. if ((firstByte & 0xF0) == 0xF0)
  585. nByteCount = 4;
  586. else if ((firstByte & 0xE0) == 0xE0)
  587. nByteCount = 3;
  588. else if ((firstByte & 0xC0) == 0xC0)
  589. nByteCount = 2;
  590. // Read the remaining bytes to complete the character.
  591. if(nByteCount > 1)
  592. {
  593. nCharsRead = pCFile->Read(&chbuf[1], nByteCount - 1);
  594. AcCFile_Assert(nCharsRead >= 1);
  595. AcCFile_Assert(nCharsRead == (nByteCount - 1));
  596. }
  597. const int nConverted = ::MultiByteToWideChar(
  598. CP_UTF8,
  599. 0,
  600. chbuf,
  601. nByteCount,
  602. &wch,
  603. 1);
  604. // Unicode: If we frequently encounter ill-formed UTF8 file,
  605. // we might want to consider returning false instead of bringing
  606. // up an assertion.
  607. AcCFile_Assert(nConverted == 1);
  608. if (nConverted != 1) {
  609. // error condition?
  610. }
  611. return true;
  612. }
  613. inline bool acReadUtf16CharFromCFile(CFile *pCFile,
  614. wchar_t &wch,
  615. AdCharFormatter *pChFmtr)
  616. {
  617. AcCFile_Assert(pChFmtr != NULL);
  618. const int nCharsRead = pCFile->Read(&wch, 2);
  619. if (nCharsRead < 2) {
  620. wch = 0; // (or we could set it to -1)
  621. return false; // Probably end of file.
  622. }
  623. AcCFile_Assert(nCharsRead == 2);
  624. const unsigned nFileFormat = pChFmtr->getFormat();
  625. // Perform a byte swap for Big Endian case.
  626. if (nFileFormat == AdCharFormatter::kUtf16BE)
  627. acByteSwap(wch);
  628. // New line expansion expected, read next wchar_t.
  629. if (wch == L'\r' && pChFmtr->getExpandLF()) {
  630. wchar_t wchNewLine = 0;
  631. const int nBytesRead = pCFile->Read(&wchNewLine, 2);
  632. if (nBytesRead < 2) // '\r' at the end of file?
  633. return true; // Weird, but we'll take it as it is.
  634. if (nFileFormat == AdCharFormatter::kUtf16BE)
  635. acByteSwap(wchNewLine);
  636. if (wchNewLine != '\n') { // Not a new line? Back-off!
  637. pCFile->Seek(-2L, CFile::current);
  638. return true; // Take '\r' as it is.
  639. }
  640. wch = '\n'; // Translated to new line.
  641. return true;
  642. }
  643. return true;
  644. }
  645. #endif
  646. inline UINT AcCFile::Read(LPTSTR lpBuf, UINT nCount)
  647. {
  648. #ifndef UNICODE
  649. return this->CFile::Read(lpBuf, nCount);
  650. #else
  651. // read a char at a time, converting to unicode and
  652. // appending to the buffer or CString
  653. UINT nDestIndex = 0;
  654. for (;;) {
  655. AcCFile_Assert(nDestIndex <= nCount);
  656. if (nDestIndex == nCount)
  657. break;
  658. wchar_t wch = 0;
  659. bool bReadOk = false;
  660. switch(this->mChFmtr.getFormat())
  661. {
  662. case AdCharFormatter::kAnsi:
  663. bReadOk = ::acReadAnsiCharFromCFile(this, wch, &(this->mChFmtr));
  664. break;
  665. case AdCharFormatter::kUtf8:
  666. bReadOk = ::acReadUtf8CharFromCFile(this, wch, &(this->mChFmtr));
  667. break;
  668. case AdCharFormatter::kUtf16LE:
  669. case AdCharFormatter::kUtf16BE:
  670. bReadOk = ::acReadUtf16CharFromCFile(this, wch, &(this->mChFmtr));
  671. break;
  672. }
  673. if (!bReadOk)
  674. break; // end of file or error
  675. lpBuf[nDestIndex] = wch;
  676. nDestIndex++;
  677. }
  678. return nDestIndex; // also is number of wchars read
  679. #endif
  680. }
  681. #ifdef _ADESK_WINDOWS_
  682. // CStdioFile::Write() will expand LFs if the file is open
  683. // as typeText. It will leave them unexpanded if it's open
  684. // as typeBinary. Since we send our chars to this Write()
  685. // method, the LF expansion is handled for us and we want to
  686. // disable our own LF expansion. Thus, expandLF is set to false.
  687. //
  688. inline AcCStdioFile::AcCStdioFile()
  689. : mChFmtr(AdCharFormatter::kAnsi,
  690. true, // useCIF
  691. false) // expandLF
  692. {
  693. }
  694. inline AcCStdioFile::AcCStdioFile(FILE* pOpenStream)
  695. : CStdioFile(pOpenStream),
  696. mChFmtr(AdCharFormatter::kAnsi,
  697. true, // useCIF
  698. false) // expandLF
  699. {
  700. }
  701. inline AcCStdioFile::AcCStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags)
  702. : CStdioFile(lpszFileName, nOpenFlags),
  703. mChFmtr(AdCharFormatter::kAnsi,
  704. true, // useCIF
  705. false) // expandLF
  706. {
  707. }
  708. inline AcCStdioFile::~AcCStdioFile()
  709. {
  710. }
  711. inline void AcCStdioFile::WriteString(LPCTSTR lpsz)
  712. {
  713. #ifndef UNICODE
  714. this->CStdioFile::WriteString(lpsz);
  715. #else
  716. const bool bHasBuffer = this->hasBuffer();
  717. for (;;) {
  718. const wchar_t wch = *lpsz;
  719. if (wch == L'\0')
  720. break;
  721. if (!bHasBuffer)
  722. ::acWriteWCharToCFile(wch, this, this->mChFmtr);
  723. else {
  724. // worst case is 8 bytes (utf-32 cr-lf)
  725. const int kReservedSize = 8;
  726. void *pOutBuf = this->mOutputBufMgr.requestBytes(kReservedSize);
  727. const int nBytes = this->mChFmtr.wcharToBytes(wch,
  728. reinterpret_cast<char *>(pOutBuf),
  729. kReservedSize);
  730. AcCFile_Assert(nBytes >= 1);
  731. AcCFile_Assert(nBytes <= kReservedSize);
  732. const unsigned nBytesLeft = this->mOutputBufMgr.takeBytes(nBytes);
  733. if (nBytesLeft <= kReservedSize)
  734. this->flushBytes();
  735. }
  736. lpsz++;
  737. }
  738. #endif
  739. }
  740. // nMax is the maximum number of characters to read, not counting
  741. // the null terminator. This method always appends a null terminator.
  742. // Thus, nMax should be one less than the size of the Buffer.
  743. inline LPTSTR AcCStdioFile::ReadString(LPTSTR lpsz, UINT nMax)
  744. {
  745. #ifndef UNICODE
  746. return this->CStdioFile::ReadString(lpsz, nMax);
  747. #else
  748. AcCFile_Assert(lpsz != NULL);
  749. AcCFile_Assert(nMax >= 1);
  750. if (nMax < 1)
  751. return NULL;
  752. // read a char at a time, converting to unicode and
  753. // appending to the buffer
  754. bool bGotAnyData = false;
  755. UINT nDestIndex = 0;
  756. for (;;) {
  757. AcCFile_Assert(nDestIndex < nMax - 1);
  758. if (nDestIndex >= nMax - 1)
  759. break; // -1 for null character.
  760. wchar_t wch = 0;
  761. bool bReadOk = false;
  762. switch(this->mChFmtr.getFormat())
  763. {
  764. case AdCharFormatter::kAnsi:
  765. bReadOk = ::acReadAnsiCharFromCFile(this, wch, &(this->mChFmtr));
  766. break;
  767. case AdCharFormatter::kUtf8:
  768. bReadOk = ::acReadUtf8CharFromCFile(this, wch, &(this->mChFmtr));
  769. break;
  770. case AdCharFormatter::kUtf16LE:
  771. case AdCharFormatter::kUtf16BE:
  772. bReadOk = ::acReadUtf16CharFromCFile(this, wch, &(this->mChFmtr));
  773. break;
  774. }
  775. if (!bReadOk)
  776. break; // end of file or error
  777. lpsz[nDestIndex] = wch;
  778. nDestIndex++;
  779. bGotAnyData = true;
  780. if (wch == L'\n' || nDestIndex >= nMax - 1)
  781. break; // buffer is full
  782. }
  783. AcCFile_Assert(nDestIndex < nMax);
  784. lpsz[nDestIndex] = 0; // null terminate it
  785. return bGotAnyData ? lpsz : NULL;
  786. #endif
  787. }
  788. inline BOOL AcCStdioFile::ReadString(CString& rString)
  789. {
  790. #ifndef UNICODE
  791. return this->CStdioFile::ReadString(rString);
  792. #else
  793. rString.Empty();
  794. // read a char at a time, converting to unicode and
  795. // appending to the CString
  796. bool bGotAnyData = false;
  797. for (;;) {
  798. wchar_t wch = 0;
  799. bool bReadOk = false;
  800. switch(this->mChFmtr.getFormat())
  801. {
  802. case AdCharFormatter::kAnsi:
  803. bReadOk = ::acReadAnsiCharFromCFile(this, wch, &(this->mChFmtr));
  804. break;
  805. case AdCharFormatter::kUtf8:
  806. bReadOk = ::acReadUtf8CharFromCFile(this, wch, &(this->mChFmtr));
  807. break;
  808. case AdCharFormatter::kUtf16LE:
  809. case AdCharFormatter::kUtf16BE:
  810. bReadOk = ::acReadUtf16CharFromCFile(this, wch, &(this->mChFmtr));
  811. break;
  812. }
  813. if (!bReadOk)
  814. break; // end of file or error
  815. bGotAnyData = true; // at least one char was read
  816. if (wch == L'\n')
  817. break; // the newline doesn't go into the CString
  818. rString.AppendChar(wch);
  819. }
  820. return bGotAnyData;
  821. #endif
  822. }
  823. inline bool AcCStdioFile::readBOM()
  824. {
  825. const ULONGLONG dwPosition = this->GetPosition();
  826. AcCFile_Assert(dwPosition == 0);
  827. if (dwPosition != 0)
  828. return false; // Can't do this unless we're at start of file.
  829. unsigned short nVal;
  830. if (this->Read(&nVal, 2) == 2) {
  831. if (nVal == 0xfeff) {
  832. this->setCharFormat(AdCharFormatter::kUtf16LE);
  833. return true;
  834. }
  835. if (nVal == 0xfffe) {
  836. this->setCharFormat(AdCharFormatter::kUtf16BE);
  837. return true;
  838. }
  839. if (nVal == 0xbbef) {
  840. unsigned char nByte3;
  841. if (this->Read(&nByte3, 1) == 1) {
  842. if (nByte3 == 0xbf) {
  843. this->setCharFormat(AdCharFormatter::kUtf8);
  844. return true;
  845. }
  846. }
  847. }
  848. else if (nVal == 0 || nVal == 0xfeff) {
  849. unsigned nVal2;
  850. if (this->Read(&nVal2, 2) == 2) {
  851. if (nVal == 0 && nVal2 == 0xfffe) {
  852. this->setCharFormat(AdCharFormatter::kUtf32BE);
  853. return true;
  854. }
  855. else if (nVal == 0xfeff && nVal2 == 0) {
  856. this->setCharFormat(AdCharFormatter::kUtf32LE);
  857. return true;
  858. }
  859. }
  860. }
  861. }
  862. // If got here, then no BOM found, so reset
  863. // to file beginning. Leave format what it was.
  864. this->SeekToBegin();
  865. return false;
  866. }
  867. inline bool AcCStdioFile::writeBOM()
  868. {
  869. AcCFile_Assert((this->m_hFile) != CFile::hFileNull);
  870. if ((this->m_hFile) == CFile::hFileNull)
  871. return false; // There must be an associated file.
  872. const ULONGLONG dwPosition = this->GetPosition();
  873. AcCFile_Assert(dwPosition == 0);
  874. if (dwPosition != 0)
  875. return false; // Can't do this unless we're at start of file.
  876. unsigned nBom = 0;
  877. const int cbBomSize = AdCharFormatter::getBOM(nBom,
  878. this->getCharFormat());
  879. if (cbBomSize == 0) // AdcharFormatter::getBOM would
  880. return false; // have brought up an assertion.
  881. this->Write(&nBom, cbBomSize);
  882. return true;
  883. }
  884. inline bool AcCStdioFile::attachBuffer(void *pBuffer, unsigned nBufSize)
  885. {
  886. return this->mOutputBufMgr.attachBuffer(pBuffer, nBufSize);
  887. }
  888. inline bool AcCStdioFile::detachBuffer()
  889. {
  890. return this->mOutputBufMgr.detachBuffer();
  891. }
  892. inline bool AcCStdioFile::hasBuffer() const
  893. {
  894. return this->mOutputBufMgr.hasBuffer();
  895. }
  896. inline bool AcCStdioFile::flushBytes()
  897. {
  898. AcCFile_Assert(this->hasBuffer());
  899. if (!this->hasBuffer())
  900. return false;
  901. const unsigned nBytes = this->mOutputBufMgr.byteCount();
  902. if (nBytes != 0) {
  903. const void *pBuf = this->mOutputBufMgr.pointer();
  904. this->Write(pBuf, nBytes);
  905. }
  906. this->mOutputBufMgr.reset();
  907. return true;
  908. }
  909. #endif //_ADESK_WINDOWS_ MAC_PORT
  910. #pragma warning(pop)