📄 mpstrstream.cpp
字号:
/*
* TOPPERS/JSP Kernel
* Toyohashi Open Platform for Embedded Real-Time Systems/
* Just Standard Profile Kernel
*
* Copyright (C) 2003 by Embedded and Real-Time Systems Laboratory
* Toyohashi Univ. of Technology, JAPAN
*
* 忋婰挊嶌尃幰偼丆埲壓偺 (1)乣(4) 偺忦審偐丆Free Software Foundation
* 偵傛偭偰岞昞偝傟偰偄傞 GNU General Public License 偺 Version 2 偵婰
* 弎偝傟偰偄傞忦審傪枮偨偡応崌偵尷傝丆杮僜僼僩僂僃傾乮杮僜僼僩僂僃傾
* 傪夵曄偟偨傕偺傪娷傓丏埲壓摨偠乯傪巊梡丒暋惢丒夵曄丒嵞攝晍乮埲壓丆
* 棙梡偲屇傇乯偡傞偙偲傪柍彏偱嫋戻偡傞丏
* (1) 杮僜僼僩僂僃傾傪僜乕僗僐乕僪偺宍偱棙梡偡傞応崌偵偼丆忋婰偺挊嶌
* 尃昞帵丆偙偺棙梡忦審偍傛傃壓婰偺柍曐徹婯掕偑丆偦偺傑傑偺宍偱僜乕
* 僗僐乕僪拞偵娷傑傟偰偄傞偙偲丏
* (2) 杮僜僼僩僂僃傾傪丆儔僀僽儔儕宍幃側偳丆懠偺僜僼僩僂僃傾奐敪偵巊
* 梡偱偒傞宍偱嵞攝晍偡傞応崌偵偼丆嵞攝晍偵敽偆僪僉儏儊儞僩乮棙梡
* 幰儅僯儏傾儖側偳乯偵丆忋婰偺挊嶌尃昞帵丆偙偺棙梡忦審偍傛傃壓婰
* 偺柍曐徹婯掕傪宖嵹偡傞偙偲丏
* (3) 杮僜僼僩僂僃傾傪丆婡婍偵慻傒崬傓側偳丆懠偺僜僼僩僂僃傾奐敪偵巊
* 梡偱偒側偄宍偱嵞攝晍偡傞応崌偵偼丆師偺偄偢傟偐偺忦審傪枮偨偡偙
* 偲丏
* (a) 嵞攝晍偵敽偆僪僉儏儊儞僩乮棙梡幰儅僯儏傾儖側偳乯偵丆忋婰偺挊
* 嶌尃昞帵丆偙偺棙梡忦審偍傛傃壓婰偺柍曐徹婯掕傪宖嵹偡傞偙偲丏
* (b) 嵞攝晍偺宍懺傪丆暿偵掕傔傞曽朄偵傛偭偰丆TOPPERS僾儘僕僃僋僩偵
* 曬崘偡傞偙偲丏
* (4) 杮僜僼僩僂僃傾偺棙梡偵傛傝捈愙揑傑偨偼娫愙揑偵惗偠傞偄偐側傞懝
* 奞偐傜傕丆忋婰挊嶌尃幰偍傛傃TOPPERS僾儘僕僃僋僩傪柶愑偡傞偙偲丏
*
* 杮僜僼僩僂僃傾偼丆柍曐徹偱採嫙偝傟偰偄傞傕偺偱偁傞丏忋婰挊嶌尃幰偍
* 傛傃TOPPERS僾儘僕僃僋僩偼丆杮僜僼僩僂僃傾偵娭偟偰丆偦偺揔梡壜擻惈傕
* 娷傔偰丆偄偐側傞曐徹傕峴傢側偄丏傑偨丆杮僜僼僩僂僃傾偺棙梡偵傛傝捈
* 愙揑傑偨偼娫愙揑偵惗偠偨偄偐側傞懝奞偵娭偟偰傕丆偦偺愑擟傪晧傢側偄丏
*
* @(#) $Id: mpstrstream.cpp,v 1.1 2006/04/14 02:46:06 9564907 Exp $
*/
// $Header: J:\\SapporoSoft\\usb20\\S1R72V05\\Debug\\Repository/S1R72V05_Toppers/Toppers/cfg/base/mpstrstream.cpp,v 1.1 2006/04/14 02:46:06 9564907 Exp $
#include "base/mpstrstream.h"
using namespace std;
/*
* 暋悢晹埵偐傜側傞僗僩儕乕儉偺堦晹埵偵憡摉偡傞僋儔僗
*/
//僐儞僗僩儔僋僞
MultipartStream::Part::Part(string _name) throw() : name(_name)
{}
//僐僺乕僐儞僗僩儔僋僞
MultipartStream::Part::Part(const MultipartStream::Part & src) throw() : name("")
{
if(src.isValid()) {
name.assign(src.name);
setContents(src.getContents());
}
}
//僗僩儕乕儉偵彂偄偨撪梕傪庴偗庢傞
string MultipartStream::Part::getContents(void) const throw(Exception)
{
string result;
if(isValid())
result = stream.str();
else
ExceptionMessage("Operation was performed against an invalid stream.","柍岠側僗僩儕乕儉偵懳偟偰憖嶌偑峴傢傟傑偟偨").throwException();
return result;
}
//僗僩儕乕儉偺撪梕傪捈愙愝掕偡傞
void MultipartStream::Part::setContents(string contents) throw(Exception)
{
if(isValid()) {
stream.clear();
stream << contents;
}
else
ExceptionMessage("Operation was performed against an invalid stream.","柍岠側僗僩儕乕儉偵懳偟偰憖嶌偑峴傢傟傑偟偨").throwException();
}
/*
* 暋悢晹埵偐傜側傞僗僩儕乕儉偺堦晹埵偵憡摉偡傞僋儔僗
*/
//僐儞僗僩儔僋僞
MultipartStream::MultipartStream(string _filename) throw() : filename(_filename), current(0), dirty(false), output(true)
{
dirty = isValid(); //嬻偱偁偭偰傕僼傽僀儖傪嶌傜側偄偲偄偗側偄偺偱...
}
MultipartStream::~MultipartStream(void) throw()
{
if(isValid() && dirty) {
//奧
try { serialize(); }
catch(...) {}
}
}
//僼傽僀儖柤偺娭楢晅偗
string MultipartStream::setFilename(string _filename) throw(Exception)
{
string result;
if(this != 0) {
if(!_filename.empty()) {
result = filename;
filename = _filename;
dirty = isValid();
}
else
ExceptionMessage("Empty filename should not be allowed.","嬻偺僼傽僀儖柤偼巊梡偱偒側偄").throwException();
}
else
ExceptionMessage("Invalid object can not perform the request.","柍岠側僆僽僕僃僋僩偵懳偡傞梫媮偼幚峴偱偒側偄").throwException();
return result;
}
//僼傽僀儖弌椡 (杮摉偵僼傽僀儖偵弌椡偟偨偲偒偩偗true)
bool MultipartStream::serialize(void) throw(Exception)
{
bool result = false;
if(isValid() && dirty && output) {
fstream file(filename.c_str(), ios::out);
if(file.is_open()) {
list<Part>::iterator scope;
//慡偰偺晹埵偺撪梕傪弌椡
scope = parts.begin();
while(scope != parts.end()) {
file << scope->getContents();
++ scope;
}
file.close();
dirty = false;
result = true;
}
else {
ExceptionMessage("File could not open [%]","僼傽僀儖偑奐偗側偄 [%]") << filename << throwException;
disableOutput(); //僨僗僩儔僋僞偑傕偆堦搙挧愴偡傞偺偱
}
}
else {
if(!isValid())
ExceptionMessage("Invalid object can not perform the request.","柍岠側僆僽僕僃僋僩偵懳偡傞梫媮偼幚峴偱偒側偄").throwException();
}
return result;
}
//晹埵偺嶌惉
MultipartStream & MultipartStream::createPart(string name, bool precedence) throw(Exception)
{
if(this != 0) {
list<Part>::iterator scope;
list<Part>::iterator newnode;
//摨偠柤慜偑側偄偙偲傪妋擣
scope = parts.begin();
while(scope != parts.end()) {
if(scope->getName().compare(name) == 0) {
ExceptionMessage("The part \"%\" is already created.","晹埵柤[%]偼偡偱偵棙梡偝傟偰偄傞") << name << throwException;
break;
}
++ scope;
}
//摨偠柤慜偑側偄側傜梫慺傪捛壛
if(scope == parts.end()) {
//憓擖埵抲偺専嶕
if(current != 0) {
scope = parts.begin();
while(scope != parts.end() && &(*scope) != current)
++ scope;
//憓擖埵抲偺挷愡 (尰嵼埵抲傪屻傠偵偢傜偡 when precedence = false; )
if(scope != parts.end() && !precedence)
++ scope;
}
else
scope = parts.end();
//梫慺偺憓擖
newnode = parts.insert(scope, Part(name));
current = &(*newnode);
}
}else
ExceptionMessage("Invalid object can not perform the request.","柍岠側僆僽僕僃僋僩偵懳偡傞梫媮偼幚峴偱偒側偄").throwException();
return *this;
}
//晹埵偺慖戰
MultipartStream & MultipartStream::movePart(string name) throw(Exception)
{
list<Part>::iterator scope;
if(this != 0 && !name.empty()) {
//柤慜偑堦抳偡傞傕偺傪扵偡
scope = parts.begin();
while(scope != parts.end()) {
if(scope->getName() == name) {
current = &(*scope);
break;
}
++ scope;
}
//尒偮偐傜側偐偭偨
if(scope == parts.end())
ExceptionMessage("Unknown part [%] specified.","柍岠側幆暿柤 [%]") << name << throwException;
}
else{
if(this == 0)
ExceptionMessage("Invalid object can not perform the request.","柍岠側僆僽僕僃僋僩偵懳偡傞梫媮偼幚峴偱偒側偄").throwException();
else //if(name.empty()) //庤慜偺if偺忦審傛傝丄偙偺if偼忢偵恀
ExceptionMessage("Empty identifier was passed as a name of part.","嬻暥帤偑搉偝傟偨").throwException();
}
return *this;
}
/********************************** 僥僗僩僗傿乕僩 **********************************/
#ifdef TESTSUITE
#include "coverage_undefs.h"
#include <iomanip>
#include <cstdio>
#ifdef _MSC_VER
# pragma warning(disable:4101) //儘乕僇儖曄悢偼堦搙傕巊傢傟偰偄傑偣傫
#endif
TESTSUITE_(main,Part, MultipartStream)
{
BEGIN_CASE("constructor/isValid","僐儞僗僩儔僋僞 / 惓摉惈敾掕") {
BEGIN_CASE("1","柤慜偮偒偱嶌惉偟偨傜桳岠側僷乕僩偑嶌傟傞") {
Part part("test");
if(!part.isValid())
TEST_FAIL;
} END_CASE;
BEGIN_CASE("2","柤慜側偟偱嶌惉偟偨傜柍岠側僷乕僩偵側傞") {
Part part("");
if(part.isValid())
TEST_FAIL;
} END_CASE;
BEGIN_CASE("3","NULL偼柍岠側僷乕僩") {
if(((Part *)0)->isValid())
TEST_FAIL;
} END_CASE;
BEGIN_CASE("4","NULL僆僽僕僃僋僩偱僐僺乕僐儞僗僩儔僋僞傪婲摦偟偰傕幐攕偟側偄") {
Part * part = 0;
Part work(*part);
} END_CASE;
} END_CASE;
BEGIN_CASE("operator <<","operator <<") {
BEGIN_CASE("1", "弌椡偱偒傞") {
Part part("test");
part << "test";
if(part.stream.str().compare("test") != 0)
TEST_FAIL;
} END_CASE;
BEGIN_CASE("2", "iomanip偑巊偊傞") {
Part part("test");
part << setw(8) << setbase(16) << setfill('0') << 0x1234567;
if(part.stream.str().compare("01234567") != 0)
TEST_FAIL;
} END_CASE;
BEGIN_CASE("3", "柍岠側僗僩儕乕儉偵彂偄偨傜椺奜偑婲傞") {
Part part("");
bool result = false;
if(part.isValid())
TEST_FAIL;
Exception::setThrowControl(true);
try {
Message::selectLanguage(Message::NEUTRAL);
part << setw(8) << setbase(16) << setfill('0') << 0x1234567;
}
catch(Exception & e) {
if(e.getDetails().compare("Operation was performed against an invalid stream.") == 0)
result = true;
}
if(!result)
TEST_FAIL;
} END_CASE;
} END_CASE;
BEGIN_CASE("getContents","getContents") {
BEGIN_CASE("1", "彂偄偨撪梕偑庢摼偱偒傞") {
Part part("test");
part << "test";
if(part.getContents().compare("test") != 0)
TEST_FAIL;
} END_CASE;
BEGIN_CASE("2", "彂偄偰撉傫偱傑偨彂偄偰撉傔傞") {
Part part("test");
part << "abc";
if(part.getContents().compare("abc") != 0)
TEST_FAIL;
part << "def";
if(part.getContents().compare("abcdef") != 0)
TEST_FAIL;
} END_CASE;
BEGIN_CASE("3", "柍岠側僗僩儕乕儉偐傜撉傫偩傜椺奜偑婲傞") {
Part part("");
bool result = false;
if(part.isValid())
TEST_FAIL;
Exception::setThrowControl(true);
try {
string work = part.getContents();
}
catch(Exception & e) {
if(e.getDetails().compare("Operation was performed against an invalid stream.") == 0)
result = true;
}
if(!result)
TEST_FAIL;
} END_CASE;
} END_CASE;
BEGIN_CASE("setContents","setContents") {
BEGIN_CASE("1", "愝掕偟偨撪梕偑庢摼偱偒傞") {
Part part("test");
part.setContents("test");
if(part.getContents().compare("test") != 0)
TEST_FAIL;
} END_CASE;
BEGIN_CASE("2", "setContents偱愝掕偟偨偁偲偵捛婰偱偒傞") {
Part part("test");
part.setContents("abc");
part << "def";
if(part.getContents().compare("abcdef") != 0)
TEST_FAIL;
} END_CASE;
BEGIN_CASE("3", "柍岠側僗僩儕乕儉偵愝掕偟偨傜椺奜偑婲傞") {
Part part("");
bool result = false;
if(part.isValid())
TEST_FAIL;
Exception::setThrowControl(true);
try {
part.setContents("test");
}
catch(Exception & e) {
if(e.getDetails().compare("Operation was performed against an invalid stream.") == 0)
result = true;
}
if(!result)
TEST_FAIL;
} END_CASE;
} END_CASE;
BEGIN_CASE("getName","getName") {
BEGIN_CASE("1", "僷乕僩柤偑惓偟偔庢摼偱偒傞") {
Part part("name_of_stream");
if(part.getName().compare("name_of_stream") != 0)
TEST_FAIL;
} END_CASE;
BEGIN_CASE("2", "晄惓側僷乕僩偺柤慜傪庢傞偲椺奜偑婲傞") {
Part part("");
bool result = false;
Exception::setThrowControl(true);
try { string work = part.getName(); }
catch(Exception & e) {
if(e.getDetails().compare("Operation was performed against an invalid stream.") == 0)
result = true;
}
if(!result)
TEST_FAIL;
} END_CASE;
} END_CASE;
BEGIN_CASE("CopyConstructor","僐僺乕僐儞僗僩儔僋僞") {
Part source("test");
source << "abcdefg";
Part dest(source);
BEGIN_CASE("1","僗僩儕乕儉偺撪梕偑惓偟偔僐僺乕偱偒偰偄傞") {
if(dest.getContents().compare("abcdefg") != 0)
TEST_FAIL;
} END_CASE;
BEGIN_CASE("2","晹埵柤偑惓偟偔僐僺乕偱偒偰偄傞") {
if(dest.getName() != source.getName())
TEST_FAIL;
} END_CASE;
} END_CASE;
}
TESTSUITE(main,MultipartStream)
{
BEGIN_CASE("constructor/isValid","constructor/isValid") {
BEGIN_CASE("1","僼傽僀儖柤傪巜掕偟偰惗惉偟偨僆僽僕僃僋僩偼桳岠") {
MultipartStream mps("test.dat");
if(!mps.isValid())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -