/*
* $Id: MyXDTPModule.cpp,v 1.6 2006/06/29 07:35:17 ozawa Exp $
*
* XDTP external sample module program.
*
* Copyright 2005- ONGS Inc.
*
* author: Masanori OZAWA (ozawa@ongs.co.jp)
* version: $Revision: 1.6 $
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY ONGS INC ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL ONGS INC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the ONGS Inc.
*
*/
#include "xdtptypes.h"
#include "MyXDTPModule.h"
#include "Utilities.h"
#define REPLACE_TAG "%REPLACE_POINT%"
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// interface
extern "C" u_int32_t getXDTPModuleVersion()
{
return XDTP_MODULE_VERSION;
}
extern "C" XDTP::XDTPModule* buildXDTPModule()
{
return (new MyXDTPModule());
}
/*
extern "C" XDTP::XDTPChecker* buildXDTPChecker()
{
return NULL;
}
*/
extern "C" void freeXDTPModule(XDTP::XDTPModule* module)
{
if (module) delete module;
}
/*
extern "C" void freeXDTPChecker(XDTP::XDTPChecker* checker)
{
// no operation
}
*/
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// MyXDTPModule
MyXDTPModule::MyXDTPModule()
{
}
MyXDTPModule::~MyXDTPModule()
{
}
void MyXDTPModule::setOutputFile(const Glib::ustring& anOutputFile)
{
m_OutputFile = anOutputFile;
}
void MyXDTPModule::setOutputEncoding(const Glib::ustring& anOutputEncoding)
{
m_OutputEncoding = anOutputEncoding;
}
void MyXDTPModule::treatPostFile(const Glib::ustring& aFile)
{
bool bSuccess = false;
Glib::ustring replace_str = "This is replace test.";
// build a temp file
std::string tmpfile = "_xdtp_temp.XXXXXX";
int fd = Glib::mkstemp(tmpfile);
if (0 > fd) {
THROW("Unable to create temp file.");
}
close(fd);
// encoding
Glib::ustring encoding = m_OutputEncoding;
if (encoding.empty()) encoding = DEFAULT_OUTPUT_ENCODING;
// do replace
try {
Glib::ustring line;
Glib::RefPtr<Glib::IOChannel> iioc =
Glib::IOChannel::create_from_file(aFile.c_str(), "r");
Glib::RefPtr<Glib::IOChannel> oioc =
Glib::IOChannel::create_from_file(tmpfile.c_str(), "w");
iioc->set_encoding(encoding);
oioc->set_encoding(encoding);
while (iioc->read_line(line) == Glib::IO_STATUS_NORMAL) {
line = XDTP::Utilities::strReplaceAll
(line, REPLACE_TAG, replace_str);
oioc->write(line);
}
iioc->close();
oioc->close();
bSuccess = true;
}
catch (XDTP::Exception exception) {
exception.print(stderr);
}
catch (Glib::Error error) {
std::string msg = Glib::locale_from_utf8(error.what());
fprintf(stderr, "MyXDTPModule: %s: %s" LINE_SEP, msg.c_str());
}
catch (std::exception exception) {
fprintf(stderr, "MyXDTPModule: %s" LINE_SEP, exception.what());
}
catch (...) {
fprintf(stderr, "MyXDTPModule: unknown error occured." LINE_SEP);
}
if (bSuccess) {
bSuccess = XDTP::Utilities::move(tmpfile, aFile);
}
if (!bSuccess) {
remove(tmpfile.c_str());
THROW("MyXDTPModule: Unable to transform.");
}
}
syntax highlighted by Code2HTML, v. 0.9.1