/*
* Writer.h
* Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// SOL9
#pragma once
#include <sol/Object.h>
#include <sol/StringT.h>
#include <sol/StringConverter.h>
#include <sol/Folder.h>
namespace SOL {
class Writer :public Object {
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.
*/
Writer()
:buffer(NULL)
{
buffer = new wchar_t[BUFFER_SIZE+1];
}
public:
~Writer()
{
if (buffer) {
delete [] buffer;
buffer = NULL;
}
}
public:
//2012/01/22
virtual int write(unsigned char* bytes, size_t len)
{
int rc = 0;
if (bytes && len>0) {
//If reportFile were NULL, simply write a string to a console stdio.
//rc = printf("%S", string);
fwrite(bytes, 1, len, stdout);
}
return rc;
}
protected:
//Write NULL-terminated string to a file or somewhere defined in each subclass.
//Implement this method in each subclass inheritting this base class.
virtual int writeString(const wchar_t* string)
{
int rc = 0;
//This base Writer class writeString method writes a string of argument to a console stdout.
if (string) {
rc = printf("%S", string);
}
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;
}
public:
//2012/01/22
virtual int writeln(const wchar_t* format,...)
{
int rc = 0;
if (buffer) {
va_list pos;
va_start(pos, format);
_vsnwprintf_s(buffer, BUFFER_SIZE-2, _TRUNCATE, format, pos);
va_end(pos);
//Append a new-line string "\r\n" for binary-mode to the buffer not "\n".
strcat(buffer, L"\r\n");
rc = writeString(buffer);
}
return rc;
}
};
}
|