/*
* WFileWriter.h
* Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL9
// 2012/01/20 WFileWriter stands for a WidChar File Writer.
#pragma once
#include <sol/Object.h>
#include <sol/StringT.h>
#include <sol/StringConverter.h>
#include <sol/Folder.h>
#include <sol/File.h>
#include <sol/InvalidArgumentException.h>
namespace SOL {
class WFileWriter :public Writer {
private:
File file;
private:
wchar_t* buffer;
private:
// 2012/01/20 Buffer size 100KB for wchar_t* buffer;
static const int BUFFER_SIZE = 1024*100;
public:
/**
+ Constructor
*
* reportFileName FilePathname to save a text string.
* If this parameter NULL, simply display text on the console.
*/
WFileWriter(const TCHAR* filePath)
:buffer(NULL)
{
buffer = new wchar_t[BUFFER_SIZE+1];
//Create a file of filePath
file.create(filePath); //If failed to create a file, this throws 'InvalidHandleException(
//Write UNICODE BOM for UTF-16
char mark[2];
mark[0] = 0xff;
mark[1] = 0xfe;
file.write(mark, 2);
}
public:
~WFileWriter()
{
if (buffer) {
delete [] buffer;
buffer = NULL;
}
file.close();
}
protected:
//Write NULL-terminated string to a file or somewhere defined in each subclass.
//Implement this method in each subclass inheritting this base class.
//Note: Newline is "\r\n". For example, you should call writeString(L"Hello\r\n"),
// not writeString(L"Hello\n"),
virtual int writeString(const wchar_t* string)
{
int rc = 0;
if (string) {
rc = file.write(string);
}
return rc;
}
public:
virtual int writeln(const wchar_t* string)
{
int rc = 0;
if (string) {
rc = writeString(string);
rc += writeString(L"\r\n");
}
return rc;
}
public:
virtual int newLine()
{
int rc = writeString(L"\r\n");
return rc;
}
public:
virtual int write(const wchar_t* format,...)
{
int rc = 0;
if (buffer) {
va_list pos;
va_start(pos, format);
_vsnwprintf_s(buffer, BUFFER_SIZE, _TRUNCATE, format, pos);
va_end(pos);
rc = writeString(buffer);
}
return rc;
}
};
}
|