Click here to Skip to main content
Click here to Skip to main content

A UTF-16 Class for Reading and Writing Unicode Files

By , , 15 Jul 2009
 

Introduction

As Unicode becomes more popular, programmers will find themselves performing more file based operations using Unicode. Currently, familiar MFC classes such as CFile and CStdioFile do not properly handle reading and writing of a Unicode file. The class file presented addresses the need to read and write files as UTF-16 Unicode files.

Using the Code

During construction or with the use of the Open() member function, the class will examine the first two bytes of the file after appropriate size checking. The two byte sequence (BOM) 0xFE, 0xFF indicates the file is UTF-16 encoded. If this is the case, m_bIsUnicode is set to TRUE. If the bytes are not present, the class performs a CStdioFile::Seek(0, CFile::begin ) to return the consumed bytes.

CStdioFile::Read( &wcBOM, sizeof( WCHAR ) );

if( wcBOM == UNICODE_BOM ) {

    m_bIsUnicode   = TRUE;
    m_bByteSwapped = FALSE;
}

if( wcBOM == UNICODE_RBOM ) {

    m_bIsUnicode   = TRUE;
    m_bByteSwapped = TRUE;
}

// Not a BOM mark - treat it as an ANSI file
//   and defer to CStdioFile...
if( FALSE == m_bIsUnicode ) {

    CStdioFile::Seek( 0, CFile::begin );
}

ReadString(...) occurs as follows: if m_bIsUnicode is FALSE, the class returns the appropriate CStdioFile::ReadString(...) operation. If the file is UTF-16 encoded, the class will draw from an internal accumulator until a "\r" or "\n" is encountered when using CUTF16File::ReadString(CString& rString ). If using the CUTF16File::ReadString( LPWSTR lpsz, UINT nMax ) overload, CStdioFile::ReadString() behavior is duplicated. See the underlying comment from fgets().

The above read is accomplished through an accumulator. The accumulator is a STL list of WCHARs. When filling the accumulator, byte swapping occurs if a Big Endian stream (0xFF, 0xFE) is encountered.

Writing to a file is accomplished by extending the normal function with WriteString(LPCTSTR lpsz, BOOL bAsUnicode ). CStdioFile will handle the ANSI conversion internally, so CUTF16File simply yields to CStdioFile. If bAsUnicode is TRUE, the program will write the BOM (if file position is 0), and then call CFile::Write(...).

The program will open two files on the hard drive, write out both Unicode and ANSI text files, then read the files back in. The driver program then uses OutputDebugString(...) to write messages to the debugger's output window.

CUTF16File output1( L"unicode_write.txt", CFile::modeWrite |
CFile::modeCreate );
output1.WriteString( L"Hello World from Unicode land!", TRUE );
output1.Close();

...

CString szInput;
CUTF16File input1( L"unicode_write.txt", CFile::modeRead );
input1.ReadString( szInput );

Figure 1 is the result of writing a test file with the provided driver program. Notice that the BOM bytes are swapped on the disk.

Figure 1: Result of test program.

Figure 2 examines a similar file created with Notepad on Windows 2000 while saving the file as Unicode.

Figure 2: A Unicode sample created in Notepad.

Additional Reading

  • http://www.unicode.org/
  • International Programming for Microsoft Windows by D. Schmitt, ISBN 1-57231-956-9
  • Programming Windows with MFC by J. Prosise, ISBN 1-57231-695-0
  • Programming Server-Side Applications for Microsoft Windows 2000 by J. Richter and J. Clark, ISBN 0-73560-753-2

Revisions

  • 10 Feb 2005 Original release
  • 23 Dec 2006 Added Jordan Walters' improvements and bug fixes
  • 23 Dec 2006 Added Jordan Walters as an author
  • 17 Sep 2008 Fixed long-standing bug in 2nd constructor
  • 13 Jul 2009 Correct handling of Unicode characters. If UNICODE/_UNICODE project settings specified, writing ANSI still produces a Unicode output file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Authors

Jeffrey Walton
Systems / Hardware Administrator
United States United States
Member
No Biography provided

Jordan Walters
Software Developer (Senior)
United Kingdom United Kingdom
Member
Ok, it's about time I updated this profile. I still live near 'Beastly' Eastleigh in Hampshire, England. However I have recently been granted a permamant migration visa to Australia - so if you're a potential employer from down under and like the look of me, please get in touch.
Still married - just, still with just a son and daughter. But they are now 8 and 7 resp and when together they have the energy of a nuclear bomb.
I worked at Teleca UK for over 8.5 years (but have now moved to TikitTFB) and have done loads of different things. Heavily involved with MFC, SQL, C#, The latest is ASP.NET with C# and Javascript. Moving away from Trolltech Qt3 and 4.
Jordan.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberRichchu15 Nov '10 - 20:40 
Good job. Thanks!
GeneralPerformance Issue with ReadUnicodeString(CString&)memberChris Meech30 Jul '09 - 6:25 
Just wondering if you are aware of any performance issues with the ReadUnicodeString(CString&) method in this class. I'm investigating a performance problem where this class is used for processing a unicode file. Profiling has shown that about 90% of the processing time is spent in this method. Additionally if I manually convert the unicode file to an ansi file, my processing time goes from 5 minutes a file to 15 seconds a file. My suspicion is that the W2T conversion is the bottleneck, but I haven't isolated it yet. Do either of you have any metrics concerning this class's ReadString(CString&) and the parent's ReadString(CString&) methods. While I can see there being a difference, I wouldn't have expected it to be so significant. Thanks.
 
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

GeneralRe: Performance Issue with ReadUnicodeString(CString&)memberChris Meech30 Jul '09 - 8:25 
I finally tracked down the difficulty. To some extent it is dependant upon the data that the file contains. In my case, I'm working with tab delimited records that have an average record length of something less than 100 characters. If I change the constant ACCUMULATOR_CHAR_COUNT that is declared in the .h file to be 128 instead of 2048, all performance issues disappear. As a suggestion, perhaps the call to StdioFile::Read() in the LoadAccumulator() method could be modified to use a member variable that specifies a record size and limits how much it reads, ie. have it read much less than the ACCUMULATOR_CHAR_COUNT so that the actual read operations are optimized based upon some assigned average record length.
 
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

GeneralClass fails to read russian symbol 'K'membersunrizer10425 Mar '09 - 2:55 
For example if I have string like:
SOME_KEY=Курсив
function ReadString read only "SOME_KEY=", and than fails to read other strings after it. I tested it many times.
GeneralRe: Class fails to read russian symbol 'K'membersunrizer10425 Mar '09 - 3:21 
As I can see it happens because CStdioFile::Read in LoadAccumulator function returns wrong read byte count.
GeneralRe: Class fails to read russian symbol 'K'memberJeffrey Walton25 Mar '09 - 3:51 
Hi Sunriser,
 
sunrizer104 wrote:
function returns wrong read byte count.

I believe the issue arises from assumptions on the code page and the whole SB/MB -> UTF16 codepoint mappings.
 
I'll get the corrected code uploaded shortly.
 
Jeff
GeneralRe: Class fails to read russian symbol 'K' [modified]membersunrizer10425 Mar '09 - 4:04 
Solution found - just replace call from CStdioFile::Read to CFile::Read in LoadAccumulator
 
modified on Wednesday, March 25, 2009 10:36 AM

GeneralRe: Class fails to read russian symbol 'K'membersunrizer10425 Mar '09 - 6:01 
sorry, i hastened with solution - it work very unstable
GeneralRe: Class fails to read russian symbol 'K'memberJordan Walters25 Mar '09 - 11:20 
Welcome back Jeff.
AnswerRe: Class fails to read russian symbol 'K'memberSkyKnight12 Jul '09 - 17:53 
I've solved the problem by reopening file with CFile::typeBinary flag when Unicode has been detected (and for UNICODE build). Something like this works fine for me:
 
CUTF16File in;
in.Open(L"file.txt", CFile::modeRead | CFile::shareDenyNone));
 
if (in.IsUnicodeFile())
{
    in.Close();
    in.Open(L"file.txt", CFile::modeRead | CFile::typeBinary | CFile::shareDenyNone));
}

AnswerRe: Class fails to read russian symbol 'K'memberSkyKnight13 Jul '09 - 6:37 
Another bug has been found. m_dwCurrentFilePointer wasn't initialized in Open function and after reopening file there were problems. Should be:
 
BOOL CUTF16File::Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError /*=NULL*/)
{
    m_dwCurrentFilePointer = 0;
 
    ...
}

AnswerRe: Class fails to read russian symbol 'K'memberJordan Walters13 Jul '09 - 12:58 
Hello SkyNight
I changed the underlying CUTF16File class to always open the file in binary mode. It meant changing the way text is written.
General Note to all users, if your project specifies Unicode, then writing any text via CUTF16File::WriteString(LPCTSTR lpsz, BOOL bAsUnicode /*= FALSE */), will always create and write a Unicode text file, regardless of the value of the bAsUnicode flag. If anyone objects to this and feels that CUTF16File should always write ANSI text if bAsUnicode is FALSE, please say. But I don't believe that the parent MFC CStdioFile class does this - and I've tried to keep behaviour as close to CStdioFile where possible and appropriate.
I have sent an update to this article to CodeProject admin and subject to approval it should appear here within the week Suspicious | :suss:
GeneralFix the bug about cann't call GetLength() after call CUTF16File(LPCTSTR lpszFileName, UINT nOpenFlags) or Open(...)memberbeniii0114 Aug '08 - 21:19 
result method:
 

add virtual DWORD GetLength() const { return m_fileLength;} and DWORD m_fileLength
at CUTF16File.h
 
modify if( CFile::GetLength())< 2) { return; }
to
if( ( m_fileLength = CFile::GetLength()) <2) { return; }
 
at CUTF16File(LPCTSTR lpszFileName, UINT nOpenFlags) in CUTF16File.cpp
 

modify if(CFile::GetLength()<2) { return bResult; }
to
if(( m_fileLength = CFile::GetLength()) < 2) { return bResult; }
 

CUTF16File's object can call GetLength()to recive fileLength at any time.
GeneralRe: Fix the bug about cann't call GetLength() after call CUTF16File(LPCTSTR lpszFileName, UINT nOpenFlags) or Open(...)memberJordan Walters17 Sep '08 - 11:38 
Hi beniiiiiiiiiiiiiiiiiiiiii01
What is the nug with GetLength
I ran the test (with the change you suggested in the previous post) and opened the output1 (Unicode) and 2 (Ansi). Immediately after opening them - GetLength returned 0.
Then I wrote the text to them and called GetLength, and it returned 62 and 27 respectively.
So it all seems to be fine to me.
 
Remeber that CFile::GetLength() returns number of bytes as per MSDN.
GeneralFix a bug in Revised viesionmemberbeniii0114 Aug '08 - 19:37 
CUTF16File::CUTF16File(LPCTSTR lpszFileName, UINT nOpenFlags) :
CStdioFile(lpszFileName, nOpenFlags),
m_bIsUnicode(FALSE),
m_bByteSwapped(FALSE),
m_dwCurrentFilePointer(0)
{
//char uchBOM[2] = {0};
BYTE uchBOM[2] = {0};
...
}
 
modify char to BYTE,else
if(uchBOM[0] == UNICODE_BOM[0] && uchBOM[1] == UNICODE_BOM[1])
{
m_bIsUnicode = TRUE;
m_bByteSwapped = FALSE;
}
will fail to work.
GeneralRe: Fix a bug in Revised viesionmemberJordan Walters17 Sep '08 - 11:39 
Yep, you're right.
I'm posting a revised version as we speak.
Questionit desnt work well in vc6.0?memberbeniii0114 Aug '08 - 16:46 
i set a mfc dialog project with vc6.0. add this UTF-16 class to project and copy test code to it. but wcBOM always equal to 0 when read unicode_write.txt.why? by the way my testproject is set to unicode flag.
Generalthere is a problem! using this member function BOOL CUTF16File::ReadString( CString& rString ) can`t read character "会"memberMember 222813622 Jan '08 - 21:31 
Sniff | :^)
 
I like c++

QuestionRe: there is a problem! using this member function BOOL CUTF16File::ReadString( CString& rString ) can`t read character "会"memberJordan Walters25 Apr '08 - 6:53 
Any chance you could send me your file....so I can check it out?
GeneralI have a problemmemberMember 222813622 Jan '08 - 21:26 
this class can`t read character "?"
 
I like c++

GeneralText getting truncated? [modified]memberjimwillsher26 Jun '06 - 7:24 
I've found when reading from a text file in the following format (hope this pastes okay):
 
01.wav|01 康定情歌(琵琶演奏).wav
02.wav|02 敖包相会(中胡演奏).wav
03.wav|03 二月里来(古筝演奏).wav
04.wav|04 送别(古筝演奏).wav
 

I find that the "accumulator" gets emptied before it should. I've edited UTF16File.cpp, method LoadAccumulator, and changed:
 
for (UINT i = 0; i < uCount / 2; i++)
 
to
 
for (UINT i = 0; i < uCount; i++)
 
and everything now seems to work fine.
 
Has anyone any ideas why the "divide by two" was there? I kow that Unicode characters occupy two bytes, but the buffer is being read using a WCHAR type, so this is automatically handling the double-byte issue.
 
-- modified at 13:25 Monday 26th June, 2006
GeneralRe: Text getting truncated?memberJordan Walters31 Aug '06 - 11:40 
I think the /2 is there because uCount is the number of 1-byte chars (note the sizeof(WCHAR) in its initialisation). Since the for-loop increments the WCHAR pointer pwszBuffer, then we only want to iterate for half the number of 1-byte chars.
I am at a loss as to why this did not work properly for Unicode builds, but it was ok for non-Unicode ones.
Jordan
 

 
Ashes to ashes, DOS to DOS.

GeneralNew Version for non-Unicode builds [modified]memberJordan Walters14 Dec '05 - 9:45 
Hello everybody.
A few months ago I said that I'd made some changes that allowed the code to work with non-Unicode builds. I made a couple of other mods I believe to get it fully working.
I invited the author to contact me so that he could post the new version up - and he did. But he apparently had problems with CodeProject themselves and it never got done.
Every now and then I get emails from people asking for my new version. I don't mind this but of course there is a delay in my replies. If you're like me, and you look for something you want it now, not after the time it takes to write a request and get an email reply.
So I'm pasting the contents of the header and implementation files in this message so you can just go ahead and copy it straight off.......
 
1. First UTF16File.h
 
// UTF16File.h: interface for the CUTF16File class.
//
// Version 5.0, 2 February 2004.
//
// Jeffrey Walton
//
//	Modified by Jordan Walters 27/04/2005 to work with non-Unicode
//	builds as well.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_UTF16File_H__32BEF8AC_25E0_482F_8B00_C40775BCDB81__INCLUDED_)
#define AFX_UTF16File_H__32BEF8AC_25E0_482F_8B00_C40775BCDB81__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#pragma warning(push, 3)
#include <list>
#pragma warning(pop)
 

//
// Under a hex editor, file[0] = 0xFF
//                     file[1] = 0xFE
//
// for a proper UTF-16 BOM
//
// This is different than the in-memory
//   representation of: mem[0] = 0xFE
//                      mem[1] = 0xFF
//
// on an Intel CPU
//
const unsigned char UNICODE_BOM[2]				= {unsigned char(0xFF), unsigned char(0xFE)};
const unsigned char UNICODE_RBOM[2]				= {unsigned char(0xFE), unsigned char(0xFF)};
 
const INT ACCUMULATOR_CHAR_COUNT				= 2048;
 
class CUTF16File: public CStdioFile
{
public:
	
	CUTF16File();
	CUTF16File(LPCTSTR lpszFileName, UINT nOpenFlags);
 
	virtual BOOL	Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL);
	virtual BOOL	ReadString(CString& rString);
    virtual LPTSTR  ReadString(LPTSTR lpsz, UINT nMax);
	virtual VOID	WriteString(LPCTSTR lpsz, BOOL bAsUnicode = FALSE);
 
    virtual LONG    Seek(LONG lOff, UINT nFrom);
 
    BOOL            IsUnicodeFile() { return m_bIsUnicode; }
 
protected:
 
	BOOL            ReadUnicodeString(CString& szString);
    LPTSTR          ReadUnicodeString(LPTSTR lpsz, UINT nMax);
 
    virtual VOID    WriteANSIString(LPCSTR lpsz);
    virtual VOID    WriteUnicodeString(LPCWSTR lpsz);
 
	BOOL m_bIsUnicode;
    BOOL m_bByteSwapped;
 
private:
 
	BOOL LoadAccumulator();
 
    std::list<WCHAR> m_Accumulator;
	DWORD	m_dwCurrentFilePointer;
};
 
#endif // !defined(AFX_UTF16File_H__32BEF8AC_25E0_482F_8B00_C40775BCDB81__INCLUDED_)
 

2. Second UTF16File.cpp
 
// UTF16File.cpp: implementation of the CUTF16File class.
//
// Version 5.0, 2 February 2004.
//
// Jeffrey Walton
//
//	Modified by Jordan Walters 27/04/2005 to work with non-Unicode
//	builds as well.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "UTF16File.h"
#include <atlconv.h>
 
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
 
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CUTF16File::CUTF16File(): CStdioFile(),
	m_bIsUnicode(FALSE),
	m_bByteSwapped(FALSE),
	m_dwCurrentFilePointer(0)
{
}
 
CUTF16File::CUTF16File(LPCTSTR lpszFileName, UINT nOpenFlags) :
	CStdioFile(lpszFileName, nOpenFlags), 
	m_bIsUnicode(FALSE),
	m_bByteSwapped(FALSE),
	m_dwCurrentFilePointer(0)
{
	char uchBOM[2] = {0};
 
	// We only need the BOM check if reading.
	if(CFile::modeWrite == (nOpenFlags & CFile::modeWrite)) { return; }
 
	// BOM is two bytes
	if(CFile::GetLength() < 2) { return; }
 
	m_dwCurrentFilePointer += CStdioFile::Read(reinterpret_cast<LPVOID>(uchBOM), 2);
 
	if(uchBOM[0] == UNICODE_BOM[0] &&  uchBOM[1] == UNICODE_BOM[1])
	{
		m_bIsUnicode   = TRUE;
		m_bByteSwapped = FALSE;
	} 
 
	if(uchBOM[0] == UNICODE_RBOM[0] &&  uchBOM[1] == UNICODE_RBOM[1])
	{
		m_bIsUnicode   = TRUE;
		m_bByteSwapped = TRUE;
	}
 
	// Not a BOM mark - its an ANSI file
	//   so punt to CStdioFile...
	if(FALSE == m_bIsUnicode)
	{
			m_dwCurrentFilePointer = 0;
		CStdioFile::Seek(0, CFile::begin);
	}
 
	m_Accumulator.clear();
}
 
BOOL CUTF16File::Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError /*=NULL*/)
{
	BOOL bResult = FALSE;
 
	unsigned char uchBOM[3] = {0};
 
	bResult = CStdioFile::Open(lpszFileName, nOpenFlags, pError);
 
	// We only need the BOM check if reading.
	if(CFile::modeWrite == (nOpenFlags & CFile::modeWrite)) { return bResult; }
 
	// BOM is two bytes
	if(CFile::GetLength() < 2) { return bResult; }
 
	if(TRUE == bResult)
	{
		m_dwCurrentFilePointer += CStdioFile::Read(reinterpret_cast<LPVOID>(uchBOM), 2);
 
		if(uchBOM[0] == UNICODE_BOM[0] &&  uchBOM[1] == UNICODE_BOM[1])
		{
			m_bIsUnicode   = TRUE;
			m_bByteSwapped = FALSE;
		} 
 
		if(uchBOM[0] == UNICODE_RBOM[0] &&  uchBOM[1] == UNICODE_RBOM[1])
		{
			m_bIsUnicode   = TRUE;
			m_bByteSwapped = TRUE;
		}
 
		// Not a BOM mark - its an ANSI file
		//   so punt to CStdioFile...
		if(FALSE == m_bIsUnicode)
		{
			m_dwCurrentFilePointer = 0;
			CStdioFile::Seek( 0, CFile::begin );
		}
	}
 
	m_Accumulator.clear();
 
	return bResult;
}
 
BOOL CUTF16File::ReadString( CString& rString )
{
	if(TRUE == m_bIsUnicode)
	{
		return ReadUnicodeString(rString);
	}
 
	return CStdioFile::ReadString(rString);
}
 
LPTSTR CUTF16File::ReadString(LPTSTR lpsz, UINT nMax)
{
	if(TRUE == m_bIsUnicode)
	{
		return ReadUnicodeString(lpsz, nMax);
	}
 
	return CStdioFile::ReadString(lpsz, nMax);
}
 
BOOL CUTF16File::ReadUnicodeString(CString& rString)
{
	USES_CONVERSION;
 
	BOOL bRead = FALSE;
 
	WCHAR c[2] = {0};
 
	rString.Empty();
 
	LoadAccumulator();
 
	while(FALSE == m_Accumulator.empty())
	{
		bRead = TRUE;
 
		c[0] = m_Accumulator.front();
 
		m_Accumulator.pop_front();
 
		if(L'\r' == c[0] || L'\n' == c[0])
		{
			// Set the file pointer to the current position of this carriage return - 
			// or one after as it has been read.
			m_dwCurrentFilePointer += 2;
 
			c[0] = m_Accumulator.front();
 
			m_Accumulator.pop_front();
 
			if(L'\r' == c[0] || L'\n' == c[0])
			{
				m_dwCurrentFilePointer += 2;
				Seek(m_dwCurrentFilePointer, CFile::begin);
			}
			break;
		}
 
		m_dwCurrentFilePointer += 2;
		rString += W2T(c);
 
		if(TRUE == m_Accumulator.empty())
		{
			LoadAccumulator();
		}
	}
 
	return bRead;;
}
 
/***
*char *fgets(string, count, stream) - input string from a stream
*
*Purpose:
*       get a string, up to count-1 chars or '\n', whichever comes first,
*       append '\0' and put the whole thing into string. the '\n' IS included
*       in the string. if count<=1 no input is requested. if EOF is found
*       immediately, return NULL. if EOF found after chars read, let EOF
*       finish the string as '\n' would.
*
***/
 
LPTSTR CUTF16File::ReadUnicodeString( LPTSTR lpsz, UINT nMax )
{
	USES_CONVERSION;
 
	BOOL bRead = FALSE;
 
	LPTSTR p = lpsz;
	WCHAR c[2] = {0};
 
	ASSERT(lpsz != NULL);
	ASSERT(AfxIsValidAddress(lpsz, nMax));
	ASSERT(m_pStream != NULL);
 
	if(nMax <= 1) { return lpsz; }
 
	LoadAccumulator();
 
	while(FALSE == m_Accumulator.empty() && --nMax)
	{
		bRead = TRUE;        
 
		c[0] = m_Accumulator.front();
		m_dwCurrentFilePointer += 2;
		*p++ = *(W2T(c));
 
		m_Accumulator.pop_front();
 
		if(L'\r' == c[0] || L'\n' == c[0])
		{
			// Set the file pointer to the current position of this carriage return - 
			// or one after as it has been read.
			m_dwCurrentFilePointer += 2;
 
			c[0] = m_Accumulator.front();
 
			m_Accumulator.pop_front();
 
			if(L'\r' == c[0] || L'\n' == c[0])
			{
				m_dwCurrentFilePointer += 2;
				Seek(m_dwCurrentFilePointer, CFile::begin);
			}
			break;
		}
 
		if(TRUE == m_Accumulator.empty())
		{
			LoadAccumulator();
		}
	}
 
	*p = L'\0';
 
	return TRUE == bRead ? lpsz : NULL;
}
 
VOID CUTF16File::WriteString( LPCTSTR lpsz, BOOL bAsUnicode /*= FALSE */ )
{
	USES_CONVERSION;
	
	if(TRUE == bAsUnicode)
	{
		WriteUnicodeString(T2W(lpsz));
	}
	else
	{
		WriteANSIString(lpsz);
	}
}
 
BOOL CUTF16File::LoadAccumulator()
{
	BYTE cbBuffer[ACCUMULATOR_CHAR_COUNT * sizeof(WCHAR)];
 
	UINT uCount = CStdioFile::Read(cbBuffer, ACCUMULATOR_CHAR_COUNT * sizeof(WCHAR));
 
	WCHAR* pwszBuffer = reinterpret_cast<WCHAR*>(cbBuffer);
 
	for(UINT i = 0; i < uCount / 2; i++)
	{
		WCHAR c = *pwszBuffer++;
 
		if(TRUE == m_bByteSwapped)
		{
			BYTE b1 = BYTE(c >> 8);   // high order
			BYTE b2 = BYTE(c & 0xFF); // low order

			c = WCHAR(b1 | (b2 << 8));
		}
 
		m_Accumulator.push_back(c);
	}
 
	return 0 == uCount;
}
 
LONG CUTF16File::Seek(LONG lOff, UINT nFrom)
{
	LONG lResult = CStdioFile::Seek(lOff, nFrom);
	m_dwCurrentFilePointer = CStdioFile::Seek(0, CFile::current);
 
	m_Accumulator.clear();
 
//	LoadAccumulator();

	// Should there be a test here to set fp = 2 if Unicode,
	//  and the user asks for CFile::begin???

	return lResult;
}
 
VOID CUTF16File::WriteANSIString( LPCSTR lpsz )
{
	CStdioFile::WriteString(lpsz);
}
 
VOID CUTF16File::WriteUnicodeString(LPCWSTR lpsz)
{
	if(0 == CFile::GetPosition())
	{
		CFile::Write(static_cast<LPVOID>(LPVOID(UNICODE_BOM)), sizeof(UNICODE_BOM));
	}
 
	CFile::Write(lpsz, wcslen(lpsz) * sizeof(WCHAR));
}
 
/////////////////////////////////////////////////////////////////////////////
 
That's it. Hope you find this useful.
 
Jordan
 
Ashes to ashes, DOS to DOS!
 
-- modified at 4:55 Sunday 28th May, 2006
AnswerRe: New Version for non-Unicode buildsmemberrobosport27 May '06 - 21:17 
Great Article. Thanks for the non-Unicode version! I found that with Visual Studio 2005 I had to modify the first line of the constructor
 
from
char uchBOM[2] = {0};
to
unsigned char uchBOM[2] = {0};
 
...in order to have this class correctly read/recognize the encoding BOM.
 
robo
 

GeneralRe: New Version for non-Unicode buildsmemberJeffrey Walton23 Dec '06 - 9:05 
Hi Jordan,
 
Merry Christmas! I finally got your changes incorporated. I made a few changes to get a clean compile under VS 2002. I marked them with a comment.
 
Jeff

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 15 Jul 2009
Article Copyright 2005 by Jeffrey Walton, Jordan Walters
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid