1 #ifndef MESH_IO_TEXT_H_INCLUDED
2 #define MESH_IO_TEXT_H_INCLUDED
22 *
\8eQ
\8fÆ
\83I
\83\93\83\8a\81[
\82Ì
\8aÈ
\88Õ
\95¶
\8e\9a\97ñ
\83N
\83\89\83X
34 cstr(const char *begin, const char *end)
35 : begin_(begin), end_(end)
38 bool operator==(const char *rhs)const
41 for(const char *r=rhs; *r; ++r, ++l){
52 bool operator!=(const char *rhs)const
57 bool include(char c)const
59 for(const char *l=begin_; l!=end_; ++l){
67 bool startswith(const char *rhs)
70 for(const char *l=begin_; l!=end_ && *r!='\0'; ++l, ++r){
83 std::string str()const{ return std::string(begin_, end_); }
84 const char* begin()const{ return begin_; }
85 const char* end()const{ return end_; }
86 std::pair<const char*, const char*> range()const{
87 return std::make_pair(begin_, end_);
90 template<typename IsTrim>
91 cstr &trim(IsTrim isTrim){
92 while(begin_!=end_ && isTrim(*begin_)){
95 while(end_!=begin_ && isTrim(end_[-1])){
101 inline std::ostream &operator<<(std::ostream &os, const cstr &rhs)
103 return os << rhs.str();
108 bool operator()(char byte)const
113 case '\r': // fall through
124 bool operator()(char byte)const
129 case '\t': // fall through
140 bool operator()(cstr line)const
146 template<class DELIMITER=IsCRLF,
147 class TRIM=IsWhiteSpace,
148 class LINESKIP=IsEmpty>
151 binary::IReader &reader_;
152 unsigned int lineCount_;
153 std::vector<char> buf_;
157 LineReader(binary::IReader &reader)
158 : reader_(reader), lineCount_(0), isEnd_(false)
170 if(LINESKIP()(line)){
179 unsigned int getLineCount()const
194 while(char byte=reader_.getChar()){
195 if(DELIMITER()(byte)){
198 buf_.push_back(byte);
201 while(char byte=reader_.getChar()){
202 if(DELIMITER()(byte)){
205 buf_.push_back(byte);
221 if(front>=buf_.size()){
224 if(!TRIM()(buf_[front])){
230 size_t back=buf_.size()-1;
231 for(; back>=0; --back){
232 if(!TRIM()(buf_[back])){
237 return cstr(&buf_[0]+front, &buf_[0]+back+1);
241 template<class DELIMITER=IsWhiteSpace>
247 LineSplitter(cstr line)
256 const char *current=line_.begin();
257 for(; current!=line_.end();){
258 for(; current!=line_.end(); ++current){
259 if(!DELIMITER()(*current)){
265 for(; current!=line_.end(); ++current){
266 if(DELIMITER()(*current)){
279 line_=cstr(tail+1, line_.end());
280 return cstr(head, tail);
285 return atoi(get().begin());
290 return static_cast<float>(atof(get().begin()));
293 la::Vector2 getVector2()
297 return la::Vector2(x, y);
300 la::Vector3 getVector3()
305 return la::Vector3(x, y, z);
308 la::Vector4 getVector4()
314 return la::Vector4(x, y, z, w);
317 color::fRGBA getFloatRGBA()
323 return color::fRGBA(r, g, b, a);
326 color::bRGBA getByteRGBA()
332 return color::bRGBA(r, g, b, a);
337 const char *begin=line_.begin();
338 for(; begin!=line_.end(); ++begin){
344 assert(begin<=line_.end());
346 const char *c=begin+1;
347 for(; c!=line_.end(); ++c){
353 cstr token=cstr(begin, c);
356 line_=cstr(c+1, line_.end());
362 inline void copyStringAndFillZero(char *dst, const std::string &src)
365 for(; i<src.size(); ++i)
372 for(; i<src.size(); ++i)
379 inline std::wstring cp932_to_unicode(const std::string &text)
381 return to_WideChar(cp932_to_utf8, text);
384 inline std::wstring to_WideChar(UINT uCodePage, const std::string &text)
386 int size=MultiByteToWideChar(uCodePage, 0, text.c_str(), -1, NULL, 0);
387 std::vector<wchar_t> buf(size);
388 size=MultiByteToWideChar(uCodePage, 0, text.c_str(), -1, &buf[0], buf.size());
389 return std::wstring(buf.begin(), buf.begin()+size);
392 inline std::string to_MultiByte(UINT uCodePage, const std::wstring &text)
394 int size=WideCharToMultiByte(uCodePage, 0, text.c_str(), -1, NULL, 0, 0, NULL);
395 std::vector<char> buf(size);
396 size=WideCharToMultiByte(uCodePage, 0, text.c_str(), -1, &buf[0], buf.size(), 0, NULL);
397 return std::string(buf.begin(), buf.begin()+size);
400 inline std::string cp932_to_utf8(const std::string &text)
402 return to_MultiByte(CP_UTF8, to_WideChar(CP_OEMCP, text));
406 inline std::wstring to_unicode(const char *text, const char *fromcode)
408 const char* tocode="WCHAR_T";
409 iconv_t cd=iconv_open(tocode, fromcode);
411 std::cerr << "fail to iconv_open: " << fromcode << " --> " << tocode << std::endl;
416 size_t inbytesleft=std::strlen(text);
417 char *inbuf=const_cast<char*>(text);
419 std::vector<wchar_t> buf;
421 size_t outbytesleft=0;
424 buf.resize(buf.size()+inbytesleft);
425 wchar_t *woutbuf=&buf[pos];
426 outbytesleft=(buf.size()-pos)*sizeof(wchar_t);
427 size_t ret=iconv(cd, &inbuf, &inbytesleft, (char**)&woutbuf, &outbytesleft);
429 std::cerr << "fail to iconv" << std::endl;
442 inline std::wstring cp932_to_unicode(const std::string &text)
444 return to_unicode(text.c_str(), "CP932");
449 inline std::wstring trim(const std::wstring &src){
450 std::wstring::const_iterator end=src.begin();
451 for(; end!=src.end(); ++end){
456 return std::wstring(src.begin(), end);
460 } // namespace meshio
461 #endif // MESH_IO_TEXT_H_INCLUDED