fastdo  0.6.8
archives.hpp
浏览该文件的文档.
1 #ifndef __ARCHIVES_HPP__
2 #define __ARCHIVES_HPP__
3 //
4 // archives 提供一些文档的读写功能
5 //
6 
7 namespace winux
8 {
9 
12 {
13 private:
14  String _configFile;
15  StringStringMap _rawParams;
16 
17  static int _FindConfigRef( String const & str, int offset, int * length, String * name );
18  String _expandVarNoStripSlashes( String const & name, StringArray * chains ) const;
19 
20  //返回加载的配置变量个数
21  int _load( String const & configFile, StringStringMap * rawParams, StringArray * loadFileChains );
22 public:
24 
26  Configure();
27 
31  Configure( String const & configFile );
32 
34  int load( String const & configFile );
35 
37  bool has( String const & name ) const { return _rawParams.find(name) != _rawParams.end(); }
38 
40  String get( String const & name, bool stripslashes = false, bool expand = false ) const;
41 
43  String operator [] ( String const & name ) const;
44 
46  String operator () ( String const & name ) const;
47 
51  void setRaw( String const & name, String const & value );
52 
57  void set( String const & name, String const & value );
58 
60  bool del( String const & name );
61 
63  void clear();
64 
66  StringStringMap const & getAll() const { return _rawParams; }
67 };
68 
73 {
74 public:
79  ConfigureSettings( String const & settingsFile = "" );
81  ConfigureSettings( ConfigureSettings const & other );
83  ConfigureSettings & operator = ( ConfigureSettings const & other );
84  ConfigureSettings & operator = ( ConfigureSettings && other );
85 
87  size_t load( String const & settingsFile );
88 
93  Mixed & update( String const & multiname, String const & updateExprStr = "" );
94 
96  Mixed & execRef( String const & exprStr ) const;
98  Mixed execVal( String const & exprStr, Mixed const & defval = Mixed() ) const;
99 
101  Mixed const & operator [] ( String const & name ) const;
103  Mixed & operator [] ( String const & name );
104 
106  bool has( String const & name ) const;
107 
109  Mixed const & get( String const & name ) const;
110 
112  ConfigureSettings & set( String const & name, Mixed const & value );
113 
115  Mixed const & val() const;
117  Mixed & val();
118 
120  Mixed const & expr() const;
122  Mixed & expr();
123 
124 private:
125 
126  size_t _load( String const & settingsFile, winux::Mixed * collAsVal, winux::Mixed * collAsExpr, StringArray * loadFileChains );
127 
129 };
130 
133 {
134 public:
135  CsvWriter( IFile * outputFile );
136 
138  void write( Mixed const & records, Mixed const & columnHeaders = Mixed() );
140  void writeRecord( Mixed const & record );
141 
142 private:
143  IFile * _outputFile;
144 };
145 
146 
149 {
150 public:
151  CsvReader( IFile * inputFile, bool hasColumnHeaders = false );
152  CsvReader( String const & content, bool hasColumnHeaders = false );
153 
155  Mixed const & operator [] ( int iRow ) const { return _records[iRow]; }
157  Mixed const & operator () ( int iRow, String const & name ) const { return _records[iRow][ _columns[name] ]; }
159  size_t getCount() const { return _records.getCount(); }
160 
162  Mixed & getRecords() { return _records; }
164  Mixed & getColumnHeaders() { return _columns; }
166  void read( String const & content, bool hasColumnHeaders = false );
167 
168 private:
169  Mixed _columns; // 第一行列名代表的索引
170  Mixed _records;
171 
172  void _readRecord( String const & str, int & i, Mixed & record );
173 
174  void _readString( String const & str, int & i, String & valStr );
175 };
176 
177 
191 {
192 public:
195  {
201  Utf16 = Utf16Be,
204  Utf32 = Utf32Be
205 
206  };
207 
213  TextArchive( FileEncoding fileEncoding = MultiByte, winux::AnsiString const & contentEncoding = "", winux::AnsiString const & mbsEncoding = "" ) : _fileEncoding(fileEncoding), _contentEncoding(contentEncoding), _mbsEncoding(mbsEncoding)
214  {
215  }
216 
218  void setFileEncoding( FileEncoding fileEncoding )
219  {
220  this->_fileEncoding = fileEncoding;
221  }
222 
225  {
226  return this->_fileEncoding;
227  }
228 
230  void setContentEncoding( winux::AnsiString const & contentEncoding )
231  {
232  this->_contentEncoding = contentEncoding;
233  }
234 
237  {
238  return this->_contentEncoding;
239  }
240 
242  void setMultiByteEncoding( winux::AnsiString const & mbsEncoding )
243  {
244  this->_mbsEncoding = mbsEncoding;
245  }
246 
249  {
250  return this->_mbsEncoding;
251  }
252 
259  void load( winux::String const & filePath, bool isConvert, winux::AnsiString const & targetEncoding = "", winux::AnsiString const & mbsEncoding = "" )
260  {
261  this->load( winux::FileGetContentsEx( filePath, false ), isConvert, targetEncoding, mbsEncoding );
262  }
263 
270  void load( winux::IFile * f, bool isConvert, winux::AnsiString const & targetEncoding = "", winux::AnsiString const & mbsEncoding = "" )
271  {
272  size_t n;
273  void * buf = f->buffer(&n);
274  this->load( winux::Buffer( buf, n, true ), isConvert, targetEncoding, mbsEncoding );
275  }
276 
283  void load( winux::Buffer const & content, bool isConvert, winux::AnsiString const & targetEncoding = "", winux::AnsiString const & mbsEncoding = "" )
284  {
285  this->setMultiByteEncoding(mbsEncoding);
286 
287  size_t i = 0;
288  this->_recognizeEncode( content, &i );
289  this->_processContent( winux::Buffer( content.getBuf<winux::byte>() + i, content.getSize() - i, true ), isConvert, targetEncoding );
290  }
291 
293  template < typename _ChTy >
294  void write( winux::XString<_ChTy> const & content )
295  {
296  this->write( winux::Buffer( (void *)content.c_str(), content.length() * sizeof(_ChTy), true ) );
297  }
298 
303  template < typename _ChTy >
304  void write( winux::XString<_ChTy> const & content, winux::AnsiString const & encoding )
305  {
306  this->write( winux::Buffer( (void *)content.c_str(), content.length() * sizeof(_ChTy), true ), encoding );
307  }
308 
310  void write( winux::Buffer const & content )
311  {
312  this->_pureContent.append(content);
313  }
314 
319  void write( winux::Buffer const & content, winux::AnsiString const & encoding )
320  {
321  this->setContentEncoding(encoding);
322  this->_pureContent.append(content);
323  }
324 
326  void save( winux::GrowBuffer * output )
327  {
328  this->save( output, this->_fileEncoding );
329  }
330 
332  void save( winux::IFile * f )
333  {
334  this->save( f, this->_fileEncoding );
335  }
336 
338  void save( winux::String const & filePath )
339  {
340  this->save( filePath, this->_fileEncoding );
341  }
342 
347  void save( winux::GrowBuffer * output, FileEncoding fileEncoding )
348  {
349  this->saveEx( this->_pureContent, this->_contentEncoding, output, fileEncoding );
350  }
351 
356  void save( winux::IFile * f, FileEncoding fileEncoding )
357  {
358  this->saveEx( this->_pureContent, this->_contentEncoding, f, fileEncoding );
359  }
360 
365  void save( winux::String const & filePath, FileEncoding fileEncoding )
366  {
367  this->saveEx( this->_pureContent, this->_contentEncoding, filePath, fileEncoding );
368  }
369 
376  void saveEx( winux::Buffer const & content, winux::AnsiString const & encoding, winux::GrowBuffer * output, FileEncoding fileEncoding );
377 
384  void saveEx( winux::Buffer const & content, winux::AnsiString const & encoding, winux::IFile * f, FileEncoding fileEncoding )
385  {
386  winux::GrowBuffer buf;
387  this->saveEx( content, encoding, &buf, fileEncoding );
388  f->write(buf);
389  }
390 
397  void saveEx( winux::Buffer const & content, winux::AnsiString const & encoding, winux::String const & filePath, FileEncoding fileEncoding )
398  {
399  winux::File file( filePath, "wb" );
400  this->saveEx( content, encoding, &file, fileEncoding );
401  }
402 
404  template < typename _ChTy >
406  {
407  return this->_pureContent.toString<_ChTy>();
408  }
409 
411  void clear()
412  {
413  this->_pureContent.free();
414  }
415 
416 private:
417  // 处理内容成pure content
418  void _processContent( winux::Buffer const & content, bool isConvert, winux::AnsiString const & encoding = "" );
419  // 识别BOM文件编码,*pI返回跳过BOM真正文本开始的位置
420  void _recognizeEncode( winux::Buffer const & content, size_t * pI );
421 
422  FileEncoding _fileEncoding; // 文件编码
423  winux::GrowBuffer _pureContent; // 纯内容
424  mutable winux::AnsiString _contentEncoding; // 内容编码
425  mutable winux::AnsiString _mbsEncoding; // 多字节编码,当文件编码是MultiByte时,此成员指定是哪个多字节编码
426 };
427 
428 } // namespace winux
429 
430 #endif // __ARCHIVES_HPP__
XString< char > AnsiString
Definition: utilities.hpp:212
void * getBuf() const
暴露缓冲区指针
Definition: utilities.hpp:585
#define WINUX_DLL
Definition: utilities.hpp:60
winux::AnsiString const & getMultiByteEncoding() const
获取多字节编码
Definition: archives.hpp:248
配置文件类
Definition: archives.hpp:11
Mixed & getColumnHeaders()
获取所有列标头
Definition: archives.hpp:164
void save(winux::GrowBuffer *output)
保存本对象的内容到缓冲区
Definition: archives.hpp:326
XString< tchar > String
Definition: utilities.hpp:216
size_t getCount() const
获取读取到的记录数
Definition: archives.hpp:159
void saveEx(winux::Buffer const &content, winux::AnsiString const &encoding, winux::IFile *f, FileEncoding fileEncoding)
保存内容到文件对象
Definition: archives.hpp:384
更强大的配置文件类
Definition: archives.hpp:72
std::map< String, String > StringStringMap
Definition: utilities.hpp:229
winux::XString< _ChTy > toString() const
转成指定字符的字符串
Definition: archives.hpp:405
void load(winux::Buffer const &content, bool isConvert, winux::AnsiString const &targetEncoding="", winux::AnsiString const &mbsEncoding="")
载入指定内容
Definition: archives.hpp:283
文件操作类
Definition: filesys.hpp:241
size_t getSize() const
获取数据大小
Definition: utilities.hpp:613
virtual size_t write(void const *data, size_t size)
写数据,返回写入字节数
void save(winux::String const &filePath)
保存本对象的内容到文件
Definition: archives.hpp:338
void saveEx(winux::Buffer const &content, winux::AnsiString const &encoding, winux::String const &filePath, FileEncoding fileEncoding)
保存内容到文件
Definition: archives.hpp:397
void save(winux::String const &filePath, FileEncoding fileEncoding)
保存本对象的内容到文件
Definition: archives.hpp:365
void write(winux::XString< _ChTy > const &content, winux::AnsiString const &encoding)
写入字符串内容
Definition: archives.hpp:304
CSV文件写入器
Definition: archives.hpp:132
void save(winux::GrowBuffer *output, FileEncoding fileEncoding)
保存本对象的内容到缓冲区
Definition: archives.hpp:347
void setContentEncoding(winux::AnsiString const &contentEncoding)
设置内容编码。当给文本文档写入内容时,内容默认是什么编码,用这个函数指定。
Definition: archives.hpp:230
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:528
void write(winux::Buffer const &content, winux::AnsiString const &encoding)
写入内容
Definition: archives.hpp:319
void setMultiByteEncoding(winux::AnsiString const &mbsEncoding)
设置多字节编码。当文件编码为多字节时,这个参数指示是哪一个多字节编码。
Definition: archives.hpp:242
FileEncoding
文件编码
Definition: archives.hpp:194
void load(winux::IFile *f, bool isConvert, winux::AnsiString const &targetEncoding="", winux::AnsiString const &mbsEncoding="")
载入文本文件
Definition: archives.hpp:270
static String const ConfigVarsSlashChars
Definition: archives.hpp:23
unsigned char byte
Definition: utilities.hpp:204
void write(winux::Buffer const &content)
写入内容
Definition: archives.hpp:310
高效的可增长缓冲区,1.33倍冗余量
Definition: utilities.hpp:659
Mixed & getRecords()
获取所有记录的引用,可直接操作
Definition: archives.hpp:162
void clear()
清空文本文档的内容
Definition: archives.hpp:411
void save(winux::IFile *f, FileEncoding fileEncoding)
保存本对象的内容到文件
Definition: archives.hpp:356
文本文档类。可载入文本文件自动识别BOM文件编码,转换为指定编码。
Definition: archives.hpp:190
winux::AnsiString const & getContentEncoding() const
获取内容编码
Definition: archives.hpp:236
混合体,能表示多种类型的值
Definition: utilities.hpp:750
void setFileEncoding(FileEncoding fileEncoding)
设置文件编码。文件编码当载入文本文件时会自动设置。但如果一开始就只是写文本文档,还是需要手动设置文件...
Definition: archives.hpp:218
FileEncoding const & getFileEncoding() const
获取文件编码
Definition: archives.hpp:224
std::basic_string< _ChTy > XString
Definition: utilities.hpp:210
TextArchive(FileEncoding fileEncoding=MultiByte, winux::AnsiString const &contentEncoding="", winux::AnsiString const &mbsEncoding="")
构造函数1
Definition: archives.hpp:213
文件接口
Definition: filesys.hpp:206
CSV文件读取器
Definition: archives.hpp:148
void write(winux::XString< _ChTy > const &content)
写入字符串内容
Definition: archives.hpp:294
void save(winux::IFile *f)
保存本对象的内容到文件
Definition: archives.hpp:332
XStringArray< tchar > StringArray
Definition: utilities.hpp:227
StringStringMap const & getAll() const
取得内部StringStringMap引用
Definition: archives.hpp:66
void load(winux::String const &filePath, bool isConvert, winux::AnsiString const &targetEncoding="", winux::AnsiString const &mbsEncoding="")
载入文本文件
Definition: archives.hpp:259
virtual void * buffer(size_t *size)
取得缓冲区
Buffer FileGetContentsEx(String const &filename, bool textMode)
载入文件内容为一个Buffer,textMode表示是否为文本模式
跨平台基础功能库
Definition: archives.hpp:7
bool has(String const &name) const
判断是否含有该变量
Definition: archives.hpp:37