OSDN Git Service

エンコードが正常になるように変換。
[simplecms/utakata.git] / textarrayformat.cpp
1 #include <vector>
2 #include <string>
3 #include <sstream>
4
5 #include <iostream>
6 #include <exception>
7 #include <assert.h>
8
9
10 #include "textarrayformat.h"
11
12 using namespace std;
13
14 textarrayformat::TextArrayReader::TextArrayReader(std::istream& is) :
15     splitter_(), blocks_()
16 {
17     // openを利用して読出す。
18     // openは例外を返す可能性がある。
19     open(is);
20 }
21
22 void textarrayformat::TextArrayReader::open(std::istream& is)
23 {
24     // isより行単位での読み出しを行う。
25
26     std::string tmp;
27     if (!std::getline(is, tmp))
28     {
29         // 最初の一行が取得できない場合、これは失敗とする。
30         throw "can't get splitter line";
31     }
32
33     // スプリッタとして設定させる。
34     splitter_ = tmp;
35
36     std::string block;
37     while (getline(is, tmp)) {
38         // 取得していく。
39         if (splitter_ != tmp)
40         {
41             //スプリッタ以外の場合はまとめる。
42             block += tmp + "\n";
43         }
44         else
45         {
46             this->blocks_.push_back(block);
47             block.clear();
48         }
49     }
50
51     // 最後に殘っている場合があるのでこうしておく。
52     if (!block.empty())
53     {
54         this->blocks_.push_back(block);
55     }
56 }
57
58 std::string textarrayformat::TextArrayReader::get(int num)
59 {
60     // ここはassertではなく、通常の例外によるチェックを行う。
61     if (num < 0)
62     {
63         throw textarrayformat::OutOfIndexException("Argument must be greater than zero");
64     }
65
66     if (static_cast<size_t>(num) >= blocks_.size())
67     {
68         std::stringstream ss;
69         ss << "Argument must be less than blocks num : size [" << blocks_.size()
70            << "] and receive value is [" << num << "]" << endl;
71             
72         throw textarrayformat::OutOfIndexException(ss.str());
73     }
74
75     // 本当はここでatにしておけば、事前のチェックは必要無いはず。
76     return blocks_[num];
77 }
78
79 textarrayformat::OutOfIndexException::OutOfIndexException(const std::string& str) :
80     str_(str)
81 {
82 }
83
84 const char* textarrayformat::OutOfIndexException::what() const throw()
85 {
86     return str_.c_str();
87 }