3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Just Standard Profile Kernel
6 * Copyright (C) 2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
9 * 上記著作権者は,以下の (1)〜(4) の条件か,Free Software Foundation
10 * によって公表されている GNU General Public License の Version 2 に記
11 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
12 * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
14 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
15 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
17 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
18 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
19 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
21 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
22 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
24 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
25 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
26 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
28 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
29 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
31 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
32 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
33 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
34 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
36 * @(#) $Id: mpstrstream.cpp,v 1.1 2009/01/31 05:27:37 suikan Exp $
39 // $Header: /cvsroot/toppersjsp4bf/jsp/cfg/base/mpstrstream.cpp,v 1.1 2009/01/31 05:27:37 suikan Exp $
41 #include "base/mpstrstream.h"
46 * 複数部位からなるストリームの一部位に相当するクラス
50 MultipartStream::Part::Part(string _name) throw() : name(_name)
54 MultipartStream::Part::Part(const MultipartStream::Part & src) throw() : name("")
57 name.assign(src.name);
58 setContents(src.getContents());
63 string MultipartStream::Part::getContents(void) const throw(Exception)
68 result = stream.str();
70 ExceptionMessage("Operation was performed against an invalid stream.","無効なストリームに対して操作が行われました").throwException();
76 void MultipartStream::Part::setContents(string contents) throw(Exception)
83 ExceptionMessage("Operation was performed against an invalid stream.","無効なストリームに対して操作が行われました").throwException();
89 * 複数部位からなるストリームの一部位に相当するクラス
93 MultipartStream::MultipartStream(string _filename) throw() : filename(_filename), current(0), dirty(false), output(true)
95 dirty = isValid(); //空であってもファイルを作らないといけないので...
98 MultipartStream::~MultipartStream(void) throw()
100 if(isValid() && dirty) {
108 string MultipartStream::setFilename(string _filename) throw(Exception)
113 if(!_filename.empty()) {
115 filename = _filename;
119 ExceptionMessage("Empty filename should not be allowed.","空のファイル名は使用できない").throwException();
122 ExceptionMessage("Invalid object can not perform the request.","無効なオブジェクトに対する要求は実行できない").throwException();
127 //ファイル出力 (本当にファイルに出力したときだけtrue)
128 bool MultipartStream::serialize(void) throw(Exception)
132 if(isValid() && dirty && output) {
134 fstream file(filename.c_str(), ios::out);
136 list<Part>::iterator scope;
139 scope = parts.begin();
140 while(scope != parts.end()) {
141 file << scope->getContents();
150 ExceptionMessage("File could not open [%]","ファイルが開けない [%]") << filename << throwException;
151 disableOutput(); //デストラクタがもう一度挑戦するので
156 ExceptionMessage("Invalid object can not perform the request.","無効なオブジェクトに対する要求は実行できない").throwException();
163 MultipartStream & MultipartStream::createPart(string name, bool precedence) throw(Exception)
166 list<Part>::iterator scope;
167 list<Part>::iterator newnode;
170 scope = parts.begin();
171 while(scope != parts.end()) {
172 if(scope->getName().compare(name) == 0) {
173 ExceptionMessage("The part \"%\" is already created.","部位名[%]はすでに利用されている") << name << throwException;
180 if(scope == parts.end()) {
184 scope = parts.begin();
185 while(scope != parts.end() && &(*scope) != current)
188 //挿入位置の調節 (現在位置を後ろにずらす when precedence = false; )
189 if(scope != parts.end() && !precedence)
196 newnode = parts.insert(scope, Part(name));
197 current = &(*newnode);
200 ExceptionMessage("Invalid object can not perform the request.","無効なオブジェクトに対する要求は実行できない").throwException();
206 MultipartStream & MultipartStream::movePart(string name) throw(Exception)
208 list<Part>::iterator scope;
210 if(this != 0 && !name.empty()) {
213 scope = parts.begin();
214 while(scope != parts.end()) {
215 if(scope->getName() == name) {
223 if(scope == parts.end())
224 ExceptionMessage("Unknown part [%] specified.","無効な識別名 [%]") << name << throwException;
228 ExceptionMessage("Invalid object can not perform the request.","無効なオブジェクトに対する要求は実行できない").throwException();
229 else //if(name.empty()) //手前のifの条件より、このifは常に真
230 ExceptionMessage("Empty identifier was passed as a name of part.","空文字が渡された").throwException();
237 /********************************** テストスィート **********************************/
241 #include "coverage_undefs.h"
247 # pragma warning(disable:4101) //ローカル変数は一度も使われていません
250 TESTSUITE_(main,Part, MultipartStream)
252 BEGIN_CASE("constructor/isValid","コンストラクタ / 正当性判定") {
253 BEGIN_CASE("1","名前つきで作成したら有効なパートが作れる") {
259 BEGIN_CASE("2","名前なしで作成したら無効なパートになる") {
265 BEGIN_CASE("3","NULLは無効なパート") {
266 if(((Part *)0)->isValid())
270 BEGIN_CASE("4","NULLオブジェクトでコピーコンストラクタを起動しても失敗しない") {
277 BEGIN_CASE("operator <<","operator <<") {
278 BEGIN_CASE("1", "出力できる") {
283 if(part.stream.str().compare("test") != 0)
287 BEGIN_CASE("2", "iomanipが使える") {
290 part << setw(8) << setbase(16) << setfill('0') << 0x1234567;
292 if(part.stream.str().compare("01234567") != 0)
296 BEGIN_CASE("3", "無効なストリームに書いたら例外が起る") {
303 Exception::setThrowControl(true);
305 Message::selectLanguage(Message::NEUTRAL);
306 part << setw(8) << setbase(16) << setfill('0') << 0x1234567;
308 catch(Exception & e) {
309 if(e.getDetails().compare("Operation was performed against an invalid stream.") == 0)
317 BEGIN_CASE("getContents","getContents") {
318 BEGIN_CASE("1", "書いた内容が取得できる") {
323 if(part.getContents().compare("test") != 0)
327 BEGIN_CASE("2", "書いて読んでまた書いて読める") {
331 if(part.getContents().compare("abc") != 0)
335 if(part.getContents().compare("abcdef") != 0)
339 BEGIN_CASE("3", "無効なストリームから読んだら例外が起る") {
346 Exception::setThrowControl(true);
348 string work = part.getContents();
350 catch(Exception & e) {
351 if(e.getDetails().compare("Operation was performed against an invalid stream.") == 0)
359 BEGIN_CASE("setContents","setContents") {
360 BEGIN_CASE("1", "設定した内容が取得できる") {
363 part.setContents("test");
365 if(part.getContents().compare("test") != 0)
369 BEGIN_CASE("2", "setContentsで設定したあとに追記できる") {
372 part.setContents("abc");
374 if(part.getContents().compare("abcdef") != 0)
378 BEGIN_CASE("3", "無効なストリームに設定したら例外が起る") {
385 Exception::setThrowControl(true);
387 part.setContents("test");
389 catch(Exception & e) {
390 if(e.getDetails().compare("Operation was performed against an invalid stream.") == 0)
398 BEGIN_CASE("getName","getName") {
399 BEGIN_CASE("1", "パート名が正しく取得できる") {
400 Part part("name_of_stream");
402 if(part.getName().compare("name_of_stream") != 0)
406 BEGIN_CASE("2", "不正なパートの名前を取ると例外が起る") {
410 Exception::setThrowControl(true);
411 try { string work = part.getName(); }
412 catch(Exception & e) {
413 if(e.getDetails().compare("Operation was performed against an invalid stream.") == 0)
421 BEGIN_CASE("CopyConstructor","コピーコンストラクタ") {
426 BEGIN_CASE("1","ストリームの内容が正しくコピーできている") {
427 if(dest.getContents().compare("abcdefg") != 0)
431 BEGIN_CASE("2","部位名が正しくコピーできている") {
432 if(dest.getName() != source.getName())
438 TESTSUITE(main,MultipartStream)
440 BEGIN_CASE("constructor/isValid","constructor/isValid") {
441 BEGIN_CASE("1","ファイル名を指定して生成したオブジェクトは有効") {
442 MultipartStream mps("test.dat");
448 BEGIN_CASE("2","ファイル名を持たないオブジェクトは無効") {
455 BEGIN_CASE("3","空のファイル名を持つオブジェクトは無効") {
456 MultipartStream mps("");
462 BEGIN_CASE("4","NULLオブジェクトは無効") {
463 if(((MultipartStream *)0)->isValid())
468 BEGIN_CASE("setFilename","setFilename") {
469 BEGIN_CASE("1","ファイル名を指定せずに生成したファイルに名前をつける") {
474 BEGIN_CASE("1","ストリームは有効になる") {
475 mps.setFilename("test");
480 BEGIN_CASE("2","ストリームの名前が変更されている") {
481 if(mps.filename.compare("test") != 0)
486 BEGIN_CASE("2","ファイル名を指定して生成したファイルに名前をつける") {
487 MultipartStream mps("initialname");
491 BEGIN_CASE("1","ストリームは有効になる") {
492 mps.setFilename("test");
497 BEGIN_CASE("2","ストリームの名前が変更されている") {
498 if(mps.filename.compare("test") != 0)
503 BEGIN_CASE("3","空文字を渡すと例外") {
507 Exception::setThrowControl(true);
508 try { mps.setFilename(""); }
515 BEGIN_CASE("4","NULLオブジェクトに名前をつけると例外") {
518 Exception::setThrowControl(true);
519 try { ((MultipartStream *)0)->setFilename("test"); }
527 BEGIN_CASE("serialize","serialize") {
529 BEGIN_CASE("1", "NULLオブジェクトに対するシリアライズは例外") {
532 Exception::setThrowControl(true);
534 { ((MultipartStream *)0)->serialize(); }
542 BEGIN_CASE("2", "無効なオブジェクトに対するシリアライズも例外") {
546 Exception::setThrowControl(true);
556 BEGIN_CASE("3", "正常にシリアライズできる") {
557 MultipartStream mps("debug.out");
561 mps.parts.push_back(part);
564 ::remove("debug.out");
565 BEGIN_CASE("1", "シリアライズはtrueを返す") {
570 BEGIN_CASE("2", "出力したファイルの内容が正しい") {
571 if(!TestSuite::compareFileContents("debug.out","abcdefg"))
577 BEGIN_CASE("4", "内容があってもdirty=falseなら出力されない") {
578 MultipartStream mps("debug.out");
582 mps.parts.push_back(part);
585 ::remove("debug.out");
586 BEGIN_CASE("1", "シリアライズはfalseを返す") {
591 BEGIN_CASE("2", "ファイルは出力されていない") {
592 fstream file("debug.out",ios::in);
598 BEGIN_CASE("5", "内容があっても出力禁止なら出力されない") {
599 MultipartStream mps("debug.out");
603 mps.parts.push_back(part);
608 ::remove("debug.out");
609 BEGIN_CASE("1", "シリアライズはfalseを返す") {
614 BEGIN_CASE("2", "ファイルは出力されていない") {
615 fstream file("debug.out",ios::in);
622 BEGIN_CASE("Destructor","Destructor") {
623 BEGIN_CASE("1","作って壊す") {
624 MultipartStream mps("debug.out");
628 mps.parts.push_back(part);
631 ::remove("debug.out");
634 BEGIN_CASE("2","内容がファイルに格納されている") {
635 if(!TestSuite::compareFileContents("debug.out","abcdefg"))
640 BEGIN_CASE("createPart","createPart") {
641 BEGIN_CASE("1","パートを登録する") {
642 MultipartStream mps("debug.out");
645 BEGIN_CASE("1","例外なく登録できる") {
646 Exception::setThrowControl(true);
647 mps .createPart("abc")
652 BEGIN_CASE("2","中身が登録順で並んでいる") {
653 list<Part>::iterator scope;
654 scope = mps.parts.begin();
655 if(scope->getName().compare("abc") != 0)
658 if(scope->getName().compare("def") != 0)
661 if(scope->getName().compare("ghi") != 0)
664 if(scope != mps.parts.end())
670 BEGIN_CASE("2","パートを逆順で登録する") {
671 MultipartStream mps("debug.out");
674 BEGIN_CASE("1","例外なく登録できる") {
675 Exception::setThrowControl(true);
676 mps .createPart("abc",true)
677 .createPart("def",true)
678 .createPart("ghi",true);
681 BEGIN_CASE("2","中身が登録した逆順で並んでいる") {
682 list<Part>::iterator scope;
683 scope = mps.parts.begin();
684 if(scope->getName().compare("ghi") != 0)
687 if(scope->getName().compare("def") != 0)
690 if(scope->getName().compare("abc") != 0)
693 if(scope != mps.parts.end())
699 BEGIN_CASE("3","NULLに対する操作で例外が発生する") {
701 Exception::setThrowControl(true);
703 ((MultipartStream *)0)->createPart("test");
711 BEGIN_CASE("4","同じ名前のパートを作ると例外が起る") {
712 BEGIN_CASE("1","同じ名前のパートを作ると例外が起る") {
714 MultipartStream mps("debug.out");
715 Exception::setThrowControl(true);
718 mps .createPart("abc")
729 BEGIN_CASE("2","例外を封じても正しく動く") {
731 MultipartStream mps("debug.out");
732 Exception::setThrowControl(false);
734 BEGIN_CASE("1","例外はスローされない") {
736 mps .createPart("abc")
747 BEGIN_CASE("2","現在位置は変化しない") {
748 if(mps.current == 0 || mps.current->getName().compare("def") != 0)
754 Exception::setThrowControl(true);
756 BEGIN_CASE("5","登録すると現在位置が変化する") {
757 MultipartStream mps("debug.out");
760 BEGIN_CASE("1","登録すると位置が変化する (1)") {
761 mps.createPart("abc");
762 if(mps.current == 0 || mps.current->getName().compare("abc") != 0)
766 BEGIN_CASE("2","登録すると位置が変化する (2)") {
767 mps.createPart("def");
768 if(mps.current == 0 || mps.current->getName().compare("def") != 0)
772 BEGIN_CASE("3","登録すると位置が変化する (3)") {
773 mps.createPart("ghi");
774 if(mps.current == 0 || mps.current->getName().compare("ghi") != 0)
780 BEGIN_CASE("opeator <<","operator <<") {
782 BEGIN_CASE("1","operator <<が使える") {
783 MultipartStream mps("debug.out");
785 mps.createPart("test");
787 mps << "abcdefghijklmn";
790 BEGIN_CASE("2","出力された中身が正しい") {
791 if(!TestSuite::compareFileContents("debug.out","abcdefghijklmn"))
795 BEGIN_CASE("3","NULLオブジェクトに出力すると例外") {
797 Exception::setThrowControl(true);
799 *((MultipartStream *)0) << "test";
807 BEGIN_CASE("4","パートを全く作っていないオブジェクトに出力すると例外") {
809 Exception::setThrowControl(true);
811 MultipartStream mps("debug.out");
813 *((MultipartStream *)0) << "test";
823 BEGIN_CASE("movePart/operator []","movePart/operator []") {
824 BEGIN_CASE("1","普通に移動する") {
825 MultipartStream mps("debug.out");
828 mps .createPart("abc")
832 BEGIN_CASE("1","移動できる") {
835 if(mps.current->getName().compare("def") != 0)
839 BEGIN_CASE("2","無効な名前を入れると例外") {
841 Exception::setThrowControl(true);
844 mps.movePart("unknwon");
852 BEGIN_CASE("3","空文字を入れると例外") {
854 Exception::setThrowControl(true);
866 BEGIN_CASE("2", "NULLオブジェクト相手に操作すると例外") {
868 Exception::setThrowControl(true);
871 ((MultipartStream *)0)->movePart("");