Fix a corruption bug.
[xy_vsfilter.git] / src / subtitles / text_reader.h
blob1fe687032f396b88ac972a3e94f4f0915d8105ff
1 /************************************************************************/
2 /* author: xy */
3 /* date: 20110511 */
4 /************************************************************************/
5 #ifndef __TEXT_READER_H_B2D9DE8F_8730_436C_9F0A_219EE6D78352__
6 #define __TEXT_READER_H_B2D9DE8F_8730_436C_9F0A_219EE6D78352__
8 #include <afx.h>
10 namespace xy_utils
13 template<typename ReaderType>
14 class TextReader
16 public:
17 typedef enum {ASCII, UTF8, LE16, BE16, ANSI} Encoding;
18 TextReader(ReaderType &reader, Encoding encoding): _inner_reader(reader),_encoding(encoding)
21 ~TextReader() {}
23 bool ReadLine( CStringW * str )
25 if(str==NULL) return false;
27 bool fEOF = true;
29 str->Empty();
31 if(_encoding == ASCII || _encoding == ANSI)
33 CStringA stra;
34 char c;
35 while( _inner_reader.Read(&c, sizeof(c)) == sizeof(c) )
37 fEOF = false;
38 if(c == '\r') continue;
39 if(c == '\n') break;
40 stra += c;
42 *str = CStringW(CString(stra)); // TODO: codepage
44 else if(_encoding == UTF8)
46 BYTE b;
47 while(_inner_reader.Read(&b, sizeof(b)) == sizeof(b))
49 fEOF = false;
50 WCHAR c = '?';
51 if(!(b&0x80)) // 0xxxxxxx
53 c = b&0x7f;
55 else if((b&0xe0) == 0xc0) // 110xxxxx 10xxxxxx
57 c = (b&0x1f)<<6;
58 if(_inner_reader.Read(&b, sizeof(b)) != sizeof(b)) break;
59 c |= (b&0x3f);
61 else if((b&0xf0) == 0xe0) // 1110xxxx 10xxxxxx 10xxxxxx
63 c = (b&0x0f)<<12;
64 if(_inner_reader.Read(&b, sizeof(b)) != sizeof(b)) break;
65 c |= (b&0x3f)<<6;
66 if(_inner_reader.Read(&b, sizeof(b)) != sizeof(b)) break;
67 c |= (b&0x3f);
69 if(c == '\r') continue;
70 if(c == '\n') break;
71 *str += c;
74 else if(_encoding == LE16)
76 WCHAR wc;
77 while(_inner_reader.Read(&wc, sizeof(wc)) == sizeof(wc))
79 fEOF = false;
80 if(wc == '\r') continue;
81 if(wc == '\n') break;
82 *str += wc;
85 else if(_encoding == BE16)
87 WCHAR wc;
88 while(_inner_reader.Read(&wc, sizeof(wc)) == sizeof(wc))
90 fEOF = false;
91 wc = ((wc>>8)&0x00ff)|((wc<<8)&0xff00);
92 if(wc == '\r') continue;
93 if(wc == '\n') break;
94 *str += wc;
98 return(!fEOF);
100 int GetEncoding() const { return _encoding; }
102 private:
103 ReaderType& _inner_reader;
104 Encoding _encoding;
107 } //namespace xy_utils
109 #endif // end of __TEXT_READER_H_B2D9DE8F_8730_436C_9F0A_219EE6D78352__