CFile (Win32 API IO) Refactoring

1. Rename CFile to Win32_IO_File (plus some modification in class).
2. Add new method writeStr() in Win32_IO_File class to write "char*" & string.
3. Change method names of Utf8_16_Write class and make writeFile() method return accurate type (boolean).

Close #10612
This commit is contained in:
Don Ho 2021-10-01 17:39:05 +02:00
parent 11b2dd0f6b
commit 682a8edafa
8 changed files with 147 additions and 167 deletions

View File

@ -115,19 +115,19 @@ generic_string relativeFilePathToFullFilePath(const TCHAR *relativeFilePath)
void writeFileContent(const TCHAR *file2write, const char *content2write) void writeFileContent(const TCHAR *file2write, const char *content2write)
{ {
CFile file(file2write, CFile::Mode::WRITE); Win32_IO_File file(file2write, Win32_IO_File::Mode::WRITE);
if (file.IsOpened()) if (file.isOpened())
file.Write(content2write, static_cast<unsigned long>(strlen(content2write))); file.writeStr(content2write);
} }
void writeLog(const TCHAR *logFileName, const char *log2write) void writeLog(const TCHAR *logFileName, const char *log2write)
{ {
CFile file(logFileName, CFile::Mode::APPEND); Win32_IO_File file(logFileName, Win32_IO_File::Mode::APPEND);
if (file.IsOpened()) if (file.isOpened())
file.Write(log2write, static_cast<unsigned long>(strlen(log2write))); file.writeStr(log2write);
} }

View File

@ -1,5 +1,5 @@
// This file is part of Notepad++ project // This file is part of Notepad++ project
// Copyright (C)2021 Don HO <don.h@free.fr> // Copyright (C)2021 Pavel Nedev (pg.nedev@gmail.com)
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
@ -18,7 +18,7 @@
#include "FileInterface.h" #include "FileInterface.h"
CFile::CFile(const char *fname, Mode fmode) : _hMode(fmode) Win32_IO_File::Win32_IO_File(const char *fname, Mode fmode) : _hMode(fmode)
{ {
if (fname) if (fname)
{ {
@ -38,7 +38,7 @@ CFile::CFile(const char *fname, Mode fmode) : _hMode(fmode)
} }
CFile::CFile(const wchar_t *fname, Mode fmode) : _hMode(fmode) Win32_IO_File::Win32_IO_File(const wchar_t *fname, Mode fmode) : _hMode(fmode)
{ {
if (fname) if (fname)
{ {
@ -58,9 +58,9 @@ CFile::CFile(const wchar_t *fname, Mode fmode) : _hMode(fmode)
} }
void CFile::Close() void Win32_IO_File::close()
{ {
if (IsOpened()) if (isOpened())
{ {
if (_written) if (_written)
{ {
@ -75,21 +75,21 @@ void CFile::Close()
} }
int_fast64_t CFile::GetSize() int_fast64_t Win32_IO_File::getSize()
{ {
LARGE_INTEGER r; LARGE_INTEGER r;
r.QuadPart = -1; r.QuadPart = -1;
if (IsOpened()) if (isOpened())
::GetFileSizeEx(_hFile, &r); ::GetFileSizeEx(_hFile, &r);
return static_cast<int_fast64_t>(r.QuadPart); return static_cast<int_fast64_t>(r.QuadPart);
} }
unsigned long CFile::Read(void *rbuf, unsigned long buf_size) unsigned long Win32_IO_File::read(void *rbuf, unsigned long buf_size)
{ {
if (!IsOpened() || (rbuf == nullptr) || (buf_size == 0)) if (!isOpened() || (rbuf == nullptr) || (buf_size == 0))
return 0; return 0;
DWORD bytes_read = 0; DWORD bytes_read = 0;
@ -100,10 +100,9 @@ unsigned long CFile::Read(void *rbuf, unsigned long buf_size)
return bytes_read; return bytes_read;
} }
bool Win32_IO_File::write(const void *wbuf, unsigned long buf_size)
bool CFile::Write(const void *wbuf, unsigned long buf_size)
{ {
if (!IsOpened() || (wbuf == nullptr) || (buf_size == 0) || ((_hMode != Mode::WRITE) && (_hMode != Mode::APPEND))) if (!isOpened() || (wbuf == nullptr) || (buf_size == 0) || ((_hMode != Mode::WRITE) && (_hMode != Mode::APPEND)))
return false; return false;
DWORD bytes_written = 0; DWORD bytes_written = 0;
@ -119,7 +118,7 @@ bool CFile::Write(const void *wbuf, unsigned long buf_size)
// Helper function to auto-fill CreateFile params optimized for Notepad++ usage. // Helper function to auto-fill CreateFile params optimized for Notepad++ usage.
void CFile::fillCreateParams(DWORD &access, DWORD &share, DWORD &disp, DWORD &attrib) void Win32_IO_File::fillCreateParams(DWORD &access, DWORD &share, DWORD &disp, DWORD &attrib)
{ {
access = GENERIC_READ; access = GENERIC_READ;
attrib = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_POSIX_SEMANTICS; // Distinguish between upper/lower case in name attrib = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_POSIX_SEMANTICS; // Distinguish between upper/lower case in name

View File

@ -1,5 +1,5 @@
// This file is part of Notepad++ project // This file is part of Notepad++ project
// Copyright (C)2021 Don HO <don.h@free.fr> // Copyright (C)2021 Pavel Nedev (pg.nedev@gmail.com)
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
@ -18,47 +18,48 @@
#pragma once #pragma once
#include <windows.h> #include <windows.h>
#include <string>
#include <tchar.h> #include <tchar.h>
#include <cstdint> #include <cstdint>
class CFile class Win32_IO_File final
{ {
public: public:
enum class Mode enum class Mode {
{
READ, READ,
WRITE, WRITE,
APPEND APPEND
}; };
CFile(const char *fname, Mode fmode = Mode::READ); Win32_IO_File(const char *fname, Mode fmode = Mode::READ);
CFile(const wchar_t *fname, Mode fmode = Mode::READ); Win32_IO_File(const wchar_t *fname, Mode fmode = Mode::READ);
~CFile() Win32_IO_File() = delete;
{ Win32_IO_File(const Win32_IO_File&) = delete;
Close(); Win32_IO_File& operator=(const Win32_IO_File&) = delete;
}
bool IsOpened() ~Win32_IO_File() {
{ close();
};
bool isOpened() {
return (_hFile != INVALID_HANDLE_VALUE); return (_hFile != INVALID_HANDLE_VALUE);
} };
void Close(); void close();
int_fast64_t getSize();
unsigned long read(void *rbuf, unsigned long buf_size);
bool write(const void *wbuf, unsigned long buf_size);
int_fast64_t GetSize(); bool writeStr(const std::string& str) {
return write(str.c_str(), static_cast<unsigned long>(str.length()));
unsigned long Read(void *rbuf, unsigned long buf_size); };
bool Write(const void *wbuf, unsigned long buf_size);
private: private:
CFile(const CFile&) = delete;
CFile& operator=(const CFile&) = delete;
void fillCreateParams(DWORD &access, DWORD &share, DWORD &disp, DWORD &attrib);
HANDLE _hFile {INVALID_HANDLE_VALUE}; HANDLE _hFile {INVALID_HANDLE_VALUE};
Mode _hMode {Mode::READ}; Mode _hMode {Mode::READ};
bool _written {false}; bool _written {false};
void fillCreateParams(DWORD& access, DWORD& share, DWORD& disp, DWORD& attrib);
}; };

View File

@ -869,16 +869,17 @@ bool FileManager::backupCurrentBuffer()
::SetFileAttributes(fullpath, dwFileAttribs); ::SetFileAttributes(fullpath, dwFileAttribs);
} }
if (UnicodeConvertor.fopen(fullpath)) if (UnicodeConvertor.openFile(fullpath))
{ {
int lengthDoc = _pNotepadPlus->_pEditView->getCurrentDocLen(); int lengthDoc = _pNotepadPlus->_pEditView->getCurrentDocLen();
char* buf = (char*)_pNotepadPlus->_pEditView->execute(SCI_GETCHARACTERPOINTER); //to get characters directly from Scintilla buffer char* buf = (char*)_pNotepadPlus->_pEditView->execute(SCI_GETCHARACTERPOINTER); //to get characters directly from Scintilla buffer
long items_written = 0; boolean isWrittenSuccessful = false;
if (encoding == -1) //no special encoding; can be handled directly by Utf8_16_Write if (encoding == -1) //no special encoding; can be handled directly by Utf8_16_Write
{ {
items_written = UnicodeConvertor.fwrite(buf, static_cast<unsigned long>(lengthDoc)); isWrittenSuccessful = UnicodeConvertor.writeFile(buf, static_cast<unsigned long>(lengthDoc));
if (lengthDoc == 0) if (lengthDoc == 0)
items_written = 1; isWrittenSuccessful = true;
} }
else else
{ {
@ -894,15 +895,14 @@ bool FileManager::backupCurrentBuffer()
int incompleteMultibyteChar = 0; int incompleteMultibyteChar = 0;
const char *newData = wmc.encode(SC_CP_UTF8, encoding, buf+i, grabSize, &newDataLen, &incompleteMultibyteChar); const char *newData = wmc.encode(SC_CP_UTF8, encoding, buf+i, grabSize, &newDataLen, &incompleteMultibyteChar);
grabSize -= incompleteMultibyteChar; grabSize -= incompleteMultibyteChar;
items_written = UnicodeConvertor.fwrite(newData, static_cast<unsigned long>(newDataLen)); isWrittenSuccessful = UnicodeConvertor.writeFile(newData, static_cast<unsigned long>(newDataLen));
} }
if (lengthDoc == 0) if (lengthDoc == 0)
items_written = 1; isWrittenSuccessful = true;
} }
UnicodeConvertor.fclose(); UnicodeConvertor.closeFile();
// Note that fwrite() doesn't return the number of bytes written, but rather the number of ITEMS. if (isWrittenSuccessful) // backup file has been saved
if (items_written == 1) // backup file has been saved
{ {
buffer->setModifiedStatus(false); buffer->setModifiedStatus(false);
result = true; //all done result = true; //all done
@ -993,18 +993,19 @@ SavingStatus FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool i
int encoding = buffer->getEncoding(); int encoding = buffer->getEncoding();
if (UnicodeConvertor.fopen(fullpath)) if (UnicodeConvertor.openFile(fullpath))
{ {
_pscratchTilla->execute(SCI_SETDOCPOINTER, 0, buffer->_doc); //generate new document _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, buffer->_doc); //generate new document
int lengthDoc = _pscratchTilla->getCurrentDocLen(); int lengthDoc = _pscratchTilla->getCurrentDocLen();
char* buf = (char*)_pscratchTilla->execute(SCI_GETCHARACTERPOINTER); //to get characters directly from Scintilla buffer char* buf = (char*)_pscratchTilla->execute(SCI_GETCHARACTERPOINTER); //to get characters directly from Scintilla buffer
long items_written = 0; boolean isWrittenSuccessful = false;
if (encoding == -1) //no special encoding; can be handled directly by Utf8_16_Write if (encoding == -1) //no special encoding; can be handled directly by Utf8_16_Write
{ {
items_written = UnicodeConvertor.fwrite(buf, static_cast<unsigned long>(lengthDoc)); isWrittenSuccessful = UnicodeConvertor.writeFile(buf, static_cast<unsigned long>(lengthDoc));
if (lengthDoc == 0) if (lengthDoc == 0)
items_written = 1; isWrittenSuccessful = true;
} }
else else
{ {
@ -1020,20 +1021,19 @@ SavingStatus FileManager::saveBuffer(BufferID id, const TCHAR * filename, bool i
int incompleteMultibyteChar = 0; int incompleteMultibyteChar = 0;
const char *newData = wmc.encode(SC_CP_UTF8, encoding, buf+i, grabSize, &newDataLen, &incompleteMultibyteChar); const char *newData = wmc.encode(SC_CP_UTF8, encoding, buf+i, grabSize, &newDataLen, &incompleteMultibyteChar);
grabSize -= incompleteMultibyteChar; grabSize -= incompleteMultibyteChar;
items_written = UnicodeConvertor.fwrite(newData, static_cast<unsigned long>(newDataLen)); isWrittenSuccessful = UnicodeConvertor.writeFile(newData, static_cast<unsigned long>(newDataLen));
} }
if (lengthDoc == 0) if (lengthDoc == 0)
items_written = 1; isWrittenSuccessful = true;
} }
// check the language du fichier // check the language du fichier
LangType language = detectLanguageFromTextBegining((unsigned char *)buf, lengthDoc); LangType language = detectLanguageFromTextBegining((unsigned char *)buf, lengthDoc);
UnicodeConvertor.fclose(); UnicodeConvertor.closeFile();
// Error, we didn't write the entire document to disk. // Error, we didn't write the entire document to disk.
// Note that fwrite() doesn't return the number of bytes written, but rather the number of ITEMS. if (!isWrittenSuccessful)
if (items_written != 1)
{ {
_pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault); _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault);
return SavingStatus::SaveWrittingFailed; return SavingStatus::SaveWrittingFailed;
@ -1476,8 +1476,8 @@ BufferID FileManager::getBufferFromDocument(Document doc)
bool FileManager::createEmptyFile(const TCHAR * path) bool FileManager::createEmptyFile(const TCHAR * path)
{ {
CFile file(path, CFile::Mode::WRITE); Win32_IO_File file(path, Win32_IO_File::Mode::WRITE);
return file.IsOpened(); return file.isOpened();
} }

View File

@ -551,35 +551,24 @@ void TiXmlElement::SetAttribute( const TCHAR * name, const TCHAR * _value )
} }
} }
const char one_ws[] = " "; void TiXmlElement::Print( Win32_IO_File& cfile, int depth ) const
const char four_ws[] = " ";
const char eol[] = "\r\n";
const generic_string tagOpenSymb = TEXT("<");
const generic_string tagOpenSlash = TEXT("</");
const generic_string tagCloseSymb = TEXT(">");
const char tagCloseChar[] = ">";
const char tagCloseSymbFinal[] = " />";
void TiXmlElement::Print( CFile& cfile, int depth ) const
{ {
int i; int i;
for ( i=0; i<depth; i++ ) for ( i=0; i<depth; i++ )
{ {
cfile.Write(four_ws, 4); cfile.writeStr(" ");
//generic_fprintf( cfile, TEXT(" ") ); //generic_fprintf( cfile, TEXT(" ") );
} }
generic_string tagOpenValue = tagOpenSymb + value; std::string tagOpenValue = "<";
std::string tagOpenValueA = wstring2string(tagOpenValue, CP_UTF8); tagOpenValue += wstring2string(value, CP_UTF8);
cfile.Write(tagOpenValueA.c_str(), static_cast<unsigned long>(tagOpenValueA.length())); cfile.writeStr(tagOpenValue);
//generic_fprintf( cfile, TEXT("<%ls"), value.c_str() ); //generic_fprintf( cfile, TEXT("<%ls"), value.c_str() );
TiXmlAttribute* attrib; TiXmlAttribute* attrib;
for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() ) for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() )
{ {
cfile.Write(one_ws, 1); cfile.writeStr(" ");
//generic_fprintf(cfile, TEXT(" ")); //generic_fprintf(cfile, TEXT(" "));
attrib->Print( cfile, depth ); attrib->Print( cfile, depth );
@ -592,45 +581,45 @@ void TiXmlElement::Print( CFile& cfile, int depth ) const
TiXmlNode* node; TiXmlNode* node;
if ( !firstChild ) if ( !firstChild )
{ {
cfile.Write(tagCloseSymbFinal, 3); cfile.writeStr(" />");
//generic_fprintf( cfile, TEXT(" />") ); //generic_fprintf( cfile, TEXT(" />") );
} }
else if ( firstChild == lastChild && firstChild->ToText() ) else if ( firstChild == lastChild && firstChild->ToText() )
{ {
cfile.Write(tagCloseChar, 1); cfile.writeStr(">");
//generic_fprintf( cfile, TEXT(">") ); //generic_fprintf( cfile, TEXT(">") );
firstChild->Print( cfile, depth + 1 ); firstChild->Print( cfile, depth + 1 );
generic_string tagCloseWithValue = tagOpenSlash + value + tagCloseSymb; std::string tagCloseWithValue = "</";
std::string tagCloseWithValueA = wstring2string(tagCloseWithValue, CP_UTF8); tagCloseWithValue += wstring2string(value, CP_UTF8) + ">";
cfile.Write(tagCloseWithValueA.c_str(), static_cast<unsigned long>(tagCloseWithValueA.length())); cfile.writeStr(tagCloseWithValue);
//generic_fprintf( cfile, TEXT("</%ls>"), value.c_str() ); //generic_fprintf( cfile, TEXT("</%ls>"), value.c_str() );
} }
else else
{ {
cfile.Write(tagCloseChar, 1); cfile.writeStr(">");
//generic_fprintf( cfile, TEXT(">") ); //generic_fprintf( cfile, TEXT(">") );
for ( node = firstChild; node; node=node->NextSibling() ) for ( node = firstChild; node; node=node->NextSibling() )
{ {
if ( !node->ToText() ) if ( !node->ToText() )
{ {
cfile.Write(eol, 2); cfile.writeStr("\r\n");
//generic_fprintf( cfile, TEXT("\n") ); //generic_fprintf( cfile, TEXT("\n") );
} }
node->Print( cfile, depth+1 ); node->Print( cfile, depth+1 );
} }
cfile.Write(eol, 2); cfile.writeStr("\r\n");
//generic_fprintf( cfile, TEXT("\n") ); //generic_fprintf( cfile, TEXT("\n") );
for( i=0; i<depth; ++i ) for( i=0; i<depth; ++i )
cfile.Write(four_ws, 4); cfile.writeStr(" ");
// generic_fprintf( cfile, TEXT(" ") ); // generic_fprintf( cfile, TEXT(" ") );
generic_string tagCloseWithValue = tagOpenSlash + value + tagCloseSymb; std::string tagCloseWithValue = "</";
std::string tagCloseWithValueA = wstring2string(tagCloseWithValue, CP_UTF8); tagCloseWithValue += wstring2string(value, CP_UTF8) + ">";
cfile.Write(tagCloseWithValueA.c_str(), static_cast<unsigned long>(tagCloseWithValueA.length())); cfile.writeStr(tagCloseWithValue);
//generic_fprintf( cfile, TEXT("</%ls>"), value.c_str() ); //generic_fprintf( cfile, TEXT("</%ls>"), value.c_str() );
} }
} }
@ -747,7 +736,7 @@ bool TiXmlDocument::LoadFile( const TCHAR* filename )
if ( file ) if ( file )
{ {
// Get the file size, so we can pre-allocate the generic_string. HUGE speed impact. // Get the file size, so we can pre-allocate the string. HUGE speed impact.
long length = 0; long length = 0;
fseek( file, 0, SEEK_END ); fseek( file, 0, SEEK_END );
length = ftell( file ); length = ftell( file );
@ -800,9 +789,9 @@ bool TiXmlDocument::SaveFile( const TCHAR * filename ) const
return false; return false;
*/ */
CFile file(filename, CFile::Mode::WRITE); Win32_IO_File file(filename, Win32_IO_File::Mode::WRITE);
if (file.IsOpened()) if (file.isOpened())
{ {
Print(file, 0); Print(file, 0);
return true; return true;
@ -831,14 +820,14 @@ TiXmlNode* TiXmlDocument::Clone() const
} }
void TiXmlDocument::Print( CFile& cfile, int depth ) const void TiXmlDocument::Print( Win32_IO_File& cfile, int depth ) const
{ {
TiXmlNode* node; TiXmlNode* node;
for ( node=FirstChild(); node; node=node->NextSibling() ) for ( node=FirstChild(); node; node=node->NextSibling() )
{ {
node->Print( cfile, depth ); node->Print( cfile, depth );
cfile.Write(eol, 2); cfile.writeStr("\r\n");
//generic_fprintf( cfile, TEXT("\n") ); //generic_fprintf( cfile, TEXT("\n") );
} }
} }
@ -879,30 +868,29 @@ TiXmlAttribute* TiXmlAttribute::Previous() const
} }
void TiXmlAttribute::Print( CFile& cfile, int /*depth*/ ) const void TiXmlAttribute::Print( Win32_IO_File& cfile, int /*depth*/ ) const
{ {
TIXML_STRING n, v; TIXML_STRING n, v;
PutString( Name(), &n ); PutString( Name(), &n );
PutString( Value(), &v ); PutString( Value(), &v );
generic_string attrVsValue = n; std::string attrVsValue = wstring2string(n, CP_UTF8);
if (value.find('\"') == TIXML_STRING::npos) if (value.find('\"') == TIXML_STRING::npos)
{ {
attrVsValue += TEXT("=\""); attrVsValue += "=\"";
attrVsValue += v; attrVsValue += wstring2string(v, CP_UTF8);
attrVsValue += TEXT("\""); attrVsValue += "\"";
//generic_fprintf(cfile, TEXT("%ls=\"%ls\""), n.c_str(), v.c_str()); //generic_fprintf(cfile, TEXT("%ls=\"%ls\""), n.c_str(), v.c_str());
} }
else else
{ {
attrVsValue += TEXT("='"); attrVsValue += "='";
attrVsValue += v; attrVsValue += wstring2string(v, CP_UTF8);
attrVsValue += TEXT("'"); attrVsValue += "'";
//generic_fprintf(cfile, TEXT("%ls='%ls'"), n.c_str(), v.c_str()); //generic_fprintf(cfile, TEXT("%ls='%ls'"), n.c_str(), v.c_str());
} }
std::string attrVsValueA = wstring2string(attrVsValue, CP_UTF8); cfile.writeStr(attrVsValue);
cfile.Write(attrVsValueA.c_str(), static_cast<unsigned long>(attrVsValueA.length()));
} }
@ -962,19 +950,18 @@ const double TiXmlAttribute::DoubleValue() const
return generic_atof (value.c_str ()); return generic_atof (value.c_str ());
} }
void TiXmlComment::Print( CFile& cfile, int depth ) const void TiXmlComment::Print( Win32_IO_File& cfile, int depth ) const
{ {
for ( int i=0; i<depth; i++ ) for ( int i=0; i<depth; i++ )
{ {
cfile.Write(four_ws, 4); cfile.writeStr(" ");
//generic_fprintf( cfile, TEXT(" ") ); //generic_fprintf( cfile, TEXT(" ") );
} }
generic_string comment = TEXT("<!--"); std::string comment = "<!--";
comment += value; comment += wstring2string(value, CP_UTF8);
comment += TEXT("-->"); comment += "-->";
std::string commentA = wstring2string(comment, CP_UTF8); cfile.writeStr(comment);
cfile.Write(commentA.c_str(), static_cast<unsigned long>(commentA.length()));
//generic_fprintf( cfile, TEXT("<!--%ls-->"), value.c_str() ); //generic_fprintf( cfile, TEXT("<!--%ls-->"), value.c_str() );
} }
@ -997,13 +984,12 @@ TiXmlNode* TiXmlComment::Clone() const
} }
void TiXmlText::Print( CFile& cfile, int /*depth*/ ) const void TiXmlText::Print( Win32_IO_File& cfile, int /*depth*/ ) const
{ {
TIXML_STRING buffer; TIXML_STRING buffer;
PutString( value, &buffer ); PutString( value, &buffer );
std::string bufferA = wstring2string(buffer, CP_UTF8); cfile.writeStr(wstring2string(buffer, CP_UTF8));
cfile.Write(bufferA.c_str(), static_cast<unsigned long>(bufferA.length()));
//generic_fprintf( cfile, TEXT("%ls"), buffer.c_str() ); //generic_fprintf( cfile, TEXT("%ls"), buffer.c_str() );
} }
@ -1037,43 +1023,38 @@ TiXmlDeclaration::TiXmlDeclaration( const TCHAR * _version,
standalone = _standalone; standalone = _standalone;
} }
const generic_string xmlDclOpen = TEXT("<?xml "); void TiXmlDeclaration::Print( Win32_IO_File& cfile, int /*depth*/ ) const
const generic_string xmlDclClose = TEXT("?>");
void TiXmlDeclaration::Print( CFile& cfile, int /*depth*/ ) const
{ {
generic_string xmlDcl = xmlDclOpen; std::string xmlDcl = "<?xml ";
//generic_fprintf (cfile, TEXT("<?xml ")); //generic_fprintf (cfile, TEXT("<?xml "));
if (!version.empty()) if (!version.empty())
{ {
xmlDcl += TEXT("version=\""); xmlDcl += "version=\"";
xmlDcl += version; xmlDcl += wstring2string(version, CP_UTF8);
xmlDcl += TEXT("\" "); xmlDcl += "\" ";
//generic_fprintf(cfile, TEXT("version=\"%ls\" "), version.c_str()); //generic_fprintf(cfile, TEXT("version=\"%ls\" "), version.c_str());
} }
if (!encoding.empty()) if (!encoding.empty())
{ {
xmlDcl += TEXT("encoding=\""); xmlDcl += "encoding=\"";
xmlDcl += encoding; xmlDcl += wstring2string(encoding, CP_UTF8);
xmlDcl += TEXT("\" "); xmlDcl += "\" ";
//generic_fprintf(cfile, TEXT("encoding=\"%ls\" "), encoding.c_str()); //generic_fprintf(cfile, TEXT("encoding=\"%ls\" "), encoding.c_str());
} }
if (!standalone.empty()) if (!standalone.empty())
{ {
xmlDcl += TEXT("standalone=\""); xmlDcl += "standalone=\"";
xmlDcl += standalone; xmlDcl += wstring2string(standalone, CP_UTF8);
xmlDcl += TEXT("\" "); xmlDcl += "\" ";
//generic_fprintf(cfile, TEXT("standalone=\"%ls\" "), standalone.c_str()); //generic_fprintf(cfile, TEXT("standalone=\"%ls\" "), standalone.c_str());
} }
xmlDcl += xmlDclClose; xmlDcl += "?>";
//generic_fprintf (cfile, TEXT("?>")); //generic_fprintf (cfile, TEXT("?>"));
std::string xmlDclA = wstring2string(xmlDcl, CP_UTF8); cfile.writeStr(xmlDcl);
cfile.Write(xmlDclA.c_str(), static_cast<unsigned long>(xmlDclA.length()));
} }
void TiXmlDeclaration::StreamOut( TIXML_OSTREAM * stream ) const void TiXmlDeclaration::StreamOut( TIXML_OSTREAM * stream ) const
@ -1116,14 +1097,13 @@ TiXmlNode* TiXmlDeclaration::Clone() const
} }
void TiXmlUnknown::Print( CFile& cfile, int depth ) const void TiXmlUnknown::Print( Win32_IO_File& cfile, int depth ) const
{ {
for ( int i=0; i<depth; i++ ) for ( int i=0; i<depth; i++ )
cfile.Write(four_ws, 4); cfile.writeStr(" ");
//generic_fprintf( cfile, TEXT(" ") ); //generic_fprintf( cfile, TEXT(" ") );
std::string valueA = wstring2string(value, CP_UTF8); cfile.writeStr(wstring2string(value, CP_UTF8));
cfile.Write(valueA.c_str(), static_cast<unsigned long>(valueA.length()));
//generic_fprintf( cfile, TEXT("%ls"), value.c_str() ); //generic_fprintf( cfile, TEXT("%ls"), value.c_str() );
} }

View File

@ -132,7 +132,7 @@ public:
(For an unformatted stream, use the << operator.) (For an unformatted stream, use the << operator.)
*/ */
virtual void Print( CFile& cfile, int depth ) const = 0; virtual void Print( Win32_IO_File& cfile, int depth ) const = 0;
/** The world does not agree on whether white space should be kept or /** The world does not agree on whether white space should be kept or
not. In order to make everyone happy, these global, static functions not. In order to make everyone happy, these global, static functions
@ -640,7 +640,7 @@ public:
virtual const TCHAR* Parse( const TCHAR* p, TiXmlParsingData* data ); virtual const TCHAR* Parse( const TCHAR* p, TiXmlParsingData* data );
// [internal use] // [internal use]
virtual void Print( CFile& cfile, int depth ) const; virtual void Print( Win32_IO_File& cfile, int depth ) const;
virtual void StreamOut( TIXML_OSTREAM * out ) const; virtual void StreamOut( TIXML_OSTREAM * out ) const;
// [internal use] // [internal use]
@ -784,7 +784,7 @@ public:
virtual TiXmlNode* Clone() const; virtual TiXmlNode* Clone() const;
// [internal use] // [internal use]
virtual void Print( CFile& cfile, int depth ) const; virtual void Print( Win32_IO_File& cfile, int depth ) const;
protected: protected:
@ -823,7 +823,7 @@ public:
// [internal use] Creates a new Element and returs it. // [internal use] Creates a new Element and returs it.
virtual TiXmlNode* Clone() const; virtual TiXmlNode* Clone() const;
// [internal use] // [internal use]
virtual void Print( CFile& cfile, int depth ) const; virtual void Print( Win32_IO_File& cfile, int depth ) const;
protected: protected:
// used to be public // used to be public
#ifdef TIXML_USE_STL #ifdef TIXML_USE_STL
@ -860,7 +860,7 @@ public:
#endif #endif
// [internal use] // [internal use]
virtual void Print( CFile& cfile, int depth ) const; virtual void Print( Win32_IO_File& cfile, int depth ) const;
protected : protected :
// [internal use] Creates a new Element and returns it. // [internal use] Creates a new Element and returns it.
@ -929,7 +929,7 @@ public:
// [internal use] Creates a new Element and returs it. // [internal use] Creates a new Element and returs it.
virtual TiXmlNode* Clone() const; virtual TiXmlNode* Clone() const;
// [internal use] // [internal use]
virtual void Print( CFile& cfile, int depth ) const; virtual void Print( Win32_IO_File& cfile, int depth ) const;
protected: protected:
// used to be public // used to be public
@ -964,7 +964,7 @@ public:
// [internal use] // [internal use]
virtual TiXmlNode* Clone() const; virtual TiXmlNode* Clone() const;
// [internal use] // [internal use]
virtual void Print( CFile& cfile, int depth ) const; virtual void Print( Win32_IO_File& cfile, int depth ) const;
protected: protected:
#ifdef TIXML_USE_STL #ifdef TIXML_USE_STL
virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag ); virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
@ -1101,7 +1101,7 @@ public:
void Print() const { /*Print(stdout, 0);*/ } void Print() const { /*Print(stdout, 0);*/ }
// [internal use] // [internal use]
virtual void Print( CFile& cfile, int depth = 0 ) const; virtual void Print( Win32_IO_File& cfile, int depth = 0 ) const;
// [internal use] // [internal use]
void SetError( int err, const TCHAR* errorLocation, TiXmlParsingData* prevData ); void SetError( int err, const TCHAR* errorLocation, TiXmlParsingData* prevData );

View File

@ -285,17 +285,17 @@ Utf8_16_Write::Utf8_16_Write()
Utf8_16_Write::~Utf8_16_Write() Utf8_16_Write::~Utf8_16_Write()
{ {
fclose(); closeFile();
} }
bool Utf8_16_Write::fopen(const TCHAR *name) bool Utf8_16_Write::openFile(const TCHAR *name)
{ {
m_pFile = std::make_unique<CFile>(name, CFile::Mode::WRITE); m_pFile = std::make_unique<Win32_IO_File>(name, Win32_IO_File::Mode::WRITE);
if (!m_pFile) if (!m_pFile)
return false; return false;
if (!m_pFile->IsOpened()) if (!m_pFile->isOpened())
{ {
m_pFile = nullptr; m_pFile = nullptr;
return false; return false;
@ -306,12 +306,12 @@ bool Utf8_16_Write::fopen(const TCHAR *name)
return true; return true;
} }
unsigned long Utf8_16_Write::fwrite(const void* p, unsigned long _size) bool Utf8_16_Write::writeFile(const void* p, unsigned long _size)
{ {
// no file open // no file open
if (!m_pFile) if (!m_pFile)
{ {
return 0; return false;
} }
if (m_bFirstWrite) if (m_bFirstWrite)
@ -319,14 +319,14 @@ unsigned long Utf8_16_Write::fwrite(const void* p, unsigned long _size)
switch (m_eEncoding) switch (m_eEncoding)
{ {
case uniUTF8: { case uniUTF8: {
if (!m_pFile->Write(k_Boms[m_eEncoding], 3)) if (!m_pFile->write(k_Boms[m_eEncoding], 3))
return 0; return false;
break; break;
} }
case uni16BE: case uni16BE:
case uni16LE: case uni16LE:
if (!m_pFile->Write(k_Boms[m_eEncoding], 2)) if (!m_pFile->write(k_Boms[m_eEncoding], 2))
return 0; return false;
break; break;
default: default:
// nothing to do // nothing to do
@ -335,7 +335,7 @@ unsigned long Utf8_16_Write::fwrite(const void* p, unsigned long _size)
m_bFirstWrite = false; m_bFirstWrite = false;
} }
unsigned long ret = 0; bool isOK = false;
switch (m_eEncoding) switch (m_eEncoding)
{ {
@ -344,8 +344,8 @@ unsigned long Utf8_16_Write::fwrite(const void* p, unsigned long _size)
case uniCookie: case uniCookie:
case uniUTF8: { case uniUTF8: {
// Normal write // Normal write
if (m_pFile->Write(p, _size)) if (m_pFile->write(p, _size))
ret = 1; isOK = true;
break; break;
} }
case uni16BE_NoBOM: case uni16BE_NoBOM:
@ -365,18 +365,18 @@ unsigned long Utf8_16_Write::fwrite(const void* p, unsigned long _size)
iter8.get(&buf [bufIndex++]); iter8.get(&buf [bufIndex++]);
if (bufIndex == bufSize || !iter8) { if (bufIndex == bufSize || !iter8) {
if (!m_pFile->Write(buf, bufIndex*sizeof(utf16))) return 0; if (!m_pFile->write(buf, bufIndex*sizeof(utf16))) return 0;
bufIndex = 0; bufIndex = 0;
} }
} }
ret = 1; isOK = true;
break; break;
} }
default: default:
break; break;
} }
return ret; return isOK;
} }
@ -448,7 +448,7 @@ void Utf8_16_Write::setEncoding(UniMode eType)
} }
void Utf8_16_Write::fclose() void Utf8_16_Write::closeFile()
{ {
if (m_pNewBuf) if (m_pNewBuf)
{ {

View File

@ -139,16 +139,16 @@ public:
void setEncoding(UniMode eType); void setEncoding(UniMode eType);
bool fopen(const TCHAR *name); bool openFile(const TCHAR *name);
unsigned long fwrite(const void* p, unsigned long _size); bool writeFile(const void* p, unsigned long _size);
void fclose(); void closeFile();
size_t convert(char* p, size_t _size); size_t convert(char* p, size_t _size);
char* getNewBuf() { return reinterpret_cast<char*>(m_pNewBuf); } char* getNewBuf() { return reinterpret_cast<char*>(m_pNewBuf); }
protected: protected:
UniMode m_eEncoding; UniMode m_eEncoding;
std::unique_ptr<CFile> m_pFile; std::unique_ptr<Win32_IO_File> m_pFile;
ubyte* m_pNewBuf; ubyte* m_pNewBuf;
size_t m_nBufSize; size_t m_nBufSize;
bool m_bFirstWrite; bool m_bFirstWrite;