forked from kbinani/libvsq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextStream.hpp
More file actions
204 lines (183 loc) · 4.89 KB
/
TextStream.hpp
File metadata and controls
204 lines (183 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* TextStream.h
* Copyright © 2012 kbinani
*
* This file is part of libvsq.
*
* libvsq is free software; you can redistribute it and/or
* modify it under the terms of the BSD License.
*
* libvsq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef __TextStream_h__
#define __TextStream_h__
#include "vsqglobal.hpp"
#include "TextOutputStream.hpp"
#include <string>
#include <cmath>
#include <sstream>
VSQ_BEGIN_NAMESPACE
using namespace std;
/**
* 文字列への読み書きストリーム
* @class table
* @name TextStream
*/
class TextStream : public TextOutputStream{
private:
/**
* 書き込まれたデータを保持するバッファー
* @var table
* @access private
*/
char *_array;
/**
* _array の現在の長さ
*/
int _arrayLength;
/**
* データの現在の長さ
* @var int
* @access private
*/
int _length;// = 0;
/**
* 現在の読み書き位置
* @var int
* @access private
*/
int _position;// = -1;
public:
explicit TextStream(){
_array = NULL;
_arrayLength = 0;
_length = 0;
_position = -1;
}
~TextStream(){
close();
}
/**
* @brief 現在の読み書き位置を取得する
* @return (int) 現在の読み書き位置
*/
int getPointer(){
return _position;
}
/**
* @brief 現在の読み書き位置を設定する
* @param value (int) 設定する読み書き位置
*/
void setPointer( int value ){
_position = value;
}
/**
* @brief 現在の読み込み位置から 1 文字を読み込み、読み書き位置を一つ進める
* @return (string) 読み込んだ文字
*/
const std::string get(){
_position++;
string ret;
ret += _array[_position];
return ret;
}
/**
* @brief 現在の読み込み位置から、改行またはファイル末端まで読み込む
* @return (string) 読み込んだ文字列
*/
const std::string readLine(){
ostringstream sb;
// '\n'が来るまで読み込み
while( _position + 1 < _length ){
_position++;
char c = _array[_position];
if( c == (char)0x0A || c == 0 ){
break;
}
sb << c;
}
return sb.str();
}
/**
* @brief テキストストリームが読み込み可能な状態かどうかを返す
* @return (boolean) 読み込み可能であれば <code>true</code> を、そうでなければ <code>false</code> を返す
*/
bool ready(){
if( 0 <= _position + 1 && _position + 1 < _length ){
return true;
}else{
return false;
}
}
/**
* @brief 文字列をストリームに書きこむ
* @param str (string) 書きこむ文字列
*/
void write( const std::string &str ){
int len = str.size();
int newSize = _position + 1 + len;
int offset = _position + 1;
_ensureCapacity( newSize );
const char *ptr = str.c_str();
for( int i = 0; i < len; i++ ){
_array[offset + i] = ptr[i];
}
_position += len;
_length = std::max( _length, newSize );
}
/**
* @brief 文字列をストリームに書きこむ。末尾に改行文字を追加する
* @param str (string) 書きこむ文字列
*/
void writeLine( const std::string &str ){
int len = str.size();
int offset = _position + 1;
int newSize = offset + len + 1;
_ensureCapacity( newSize );
for( int i = 0; i < len; i++ ){
_array[offset + i] = str[i];
}
_array[offset + len] = (char)0x0A;
_position += len + 1;
_length = std::max( _length, newSize );
}
/**
* @brief ストリームを閉じる
*/
void close(){
if( _array ){
free( _array );
_array = NULL;
}
_length = 0;
}
/**
* @brief ストリームに書きこまれた文字列を連結し、返す
* @return (string) 文字列
*/
const std::string toString(){
string ret;
if( _array ){
ret += _array;
}
return ret;
}
private:
/**
* @brief 内部のバッファー容量を確保する
* @param length (int) 確保したいバッファー容量
*/
void _ensureCapacity( int length ){
if( length > _arrayLength ){
_array = (char *)::realloc( _array, (length + 1) * sizeof( char ) );
for( int i = _arrayLength; i <= length; i++ ){
_array[i] = 0;
}
_arrayLength = length;
}
}
};
VSQ_END_NAMESPACE
#endif