/*
* $Id: Utilities.cpp,v 1.9 2005/10/19 14:05:44 ozawa Exp $
*
* Copyright 2005- ONGS Inc. All rights reserved.
*
* author: Masanori OZAWA (ozawa@ongs.co.jp)
* version: $Revision: 1.9 $
*
* 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 "Utilities.h"
#include <stdio.h>
#include <fcntl.h>
using namespace XDTP;
Utilities::Utilities()
{
}
Utilities::~Utilities()
{
}
bool Utilities::copy(const Glib::ustring& in, const Glib::ustring& out)
{
int ofd = open(out.c_str(), O_CREAT | O_WRONLY | O_EXLOCK, 0640);
if (0 > ofd) return false;
bool result = copy(in, ofd);
close(ofd);
return result;
}
bool Utilities::copy(const Glib::ustring& in, int out)
{
int ifd = open(in.c_str(), O_RDONLY);
if (0 > ifd) return false;
int len = -1;
char buf[4096] = {0};
while (len = read(ifd, buf, sizeof(buf)), 0 < len) {
if (len != write(out, buf, len)) {
len = -1;
break;
}
}
close(ifd);
return (0 == len);
}
bool Utilities::move(const Glib::ustring& in, const Glib::ustring& out)
{
if (!rename(in.c_str(), out.c_str())) {
return true;
}
if (copy(in, out)) {
return (0 == remove(in.c_str()));
}
return false;
}
bool Utilities::printFile(const Glib::ustring& file)
{
size_t len = 0;
char buf[4096] = {0};
FILE* fp = fopen(file.c_str(), "r");
if (!fp) return false;
while (len = fread(buf, 1, sizeof(buf), fp), 0 < len) {
if (len != fwrite(buf, 1, len, stdout)) {
break;
}
}
fclose(fp);
return (0 == len);
}
Glib::ustring Utilities::strTrim(const Glib::ustring& szSrc)
{
return strTrimRight(strTrimLeft(szSrc));
}
/**
* 文字列の前の空白を除去します。
*
* @param szSrc 除去する前の文字列
* @return 前の空白を除去した文字列
*/
Glib::ustring Utilities::strTrimLeft(const Glib::ustring& szSrc)
{
size_t firstPos = szSrc.find_first_not_of(" \t\r\n");
size_t length = szSrc.length();
if (0 > firstPos) firstPos = 0;
else if (length < firstPos) firstPos = length;
return szSrc.substr(firstPos);
}
/**
* 文字列の後の空白を除去します。
*
* @param szSrc 除去する前の文字列
* @return 後の空白を除去した文字列
*/
Glib::ustring Utilities::strTrimRight(const Glib::ustring& szSrc)
{
size_t lastPos = szSrc.find_last_not_of(" \t\r\n");
size_t length = szSrc.length();
if (0 > lastPos) lastPos = length;
else if (length <= lastPos) lastPos = length -1;
return szSrc.substr(0, lastPos +1);
}
/**
* 文字列の終端にある改行コードを除去します。
*
* @param szSrc 除去する前の文字列
* @return 後の改行コードを除去した文字列
*/
Glib::ustring Utilities::strTrimCRLF(const Glib::ustring& szSrc)
{
size_t lastPos = szSrc.find_last_not_of("\r\n");
size_t length = szSrc.length();
if (0 > lastPos) lastPos = length;
else if (length <= lastPos) lastPos = length -1;
return szSrc.substr(0, lastPos +1);
}
/**
* 指定の文字列を全て置換します。
*
* @param szSrc 元の文字列
* @param szOld 置換対象の文字列
* @param szNew 置き換える文字列
*/
Glib::ustring Utilities::strReplaceAll(const Glib::ustring& szSrc,
const Glib::ustring& szOld,
const Glib::ustring& szNew)
{
Glib::ustring result = szSrc;
Glib::ustring::size_type pos;
Glib::ustring::size_type oldlen = szOld.length();
Glib::ustring::size_type newlen = szNew.length();
for (pos = result.find(szOld);
Glib::ustring::npos != pos;
pos = result.find(szOld, pos))
{
result = result.replace(pos, oldlen, szNew);
pos += newlen;
}
return result;
}
syntax highlighted by Code2HTML, v. 0.9.1