fastdo  0.6.16
archives.hpp
浏览该文件的文档.
1 #ifndef __ARCHIVES_HPP__
2 #define __ARCHIVES_HPP__
3 //
4 // archives 提供一些文档的读写功能
5 //
6 
7 namespace winux
8 {
11 {
12 private:
13  String _configFile;
14  StringStringMap _rawParams;
15 
16  static int _FindConfigRef( String const & str, int offset, int * length, String * name );
17  String _expandVarNoStripSlashes( String const & name, StringArray * chains ) const;
18 
19  //返回加载的配置变量个数
20  int _load( String const & configFile, StringStringMap * rawParams, StringArray * loadFileChains );
21 public:
23 
25  Configure();
26 
30  Configure( String const & configFile );
31 
33  int load( String const & configFile );
34 
36  bool has( String const & name ) const { return _rawParams.find(name) != _rawParams.end(); }
37 
39  String get( String const & name, bool stripslashes = false, bool expand = false ) const;
40 
42  String operator [] ( String const & name ) const;
43 
45  String operator () ( String const & name ) const;
46 
50  void setRaw( String const & name, String const & value );
51 
56  void set( String const & name, String const & value );
57 
59  bool del( String const & name );
60 
62  void clear();
63 
65  StringStringMap const & getAll() const { return _rawParams; }
66 };
67 
72 {
73 public:
78  ConfigureSettings( String const & settingsFile = $T("") );
80  ConfigureSettings( ConfigureSettings const & other );
82  ConfigureSettings & operator = ( ConfigureSettings const & other );
83  ConfigureSettings & operator = ( ConfigureSettings && other );
84 
86  size_t load( String const & settingsFile );
87 
92  Mixed & update( String const & multiname, String const & updateExprStr = $T("") );
93 
95  Mixed & execRef( String const & exprStr ) const;
97  Mixed execVal( String const & exprStr, Mixed const & defval = mxNull ) const;
98 
100  Mixed const & operator [] ( String const & name ) const;
102  Mixed & operator [] ( String const & name );
103 
105  bool has( String const & name ) const;
106 
108  Mixed const & get( String const & name ) const;
109 
111  ConfigureSettings & set( String const & name, Mixed const & value );
112 
114  Mixed const & val() const;
116  Mixed & val();
117 
119  Mixed const & expr() const;
121  Mixed & expr();
122 
123 private:
124  size_t _load( String const & settingsFile, winux::Mixed * collAsVal, winux::Mixed * collAsExpr, StringArray * loadFileChains );
125 
126  String _settingsFile; // 设置文件
127  Mixed _collectionVal; // 存储值
128  Mixed _collectionExpr; // 存储表达式串
129 #if defined(OS_WIN)
131 #else
133 #endif
134 };
135 
138 {
139 public:
140  CsvWriter( IFile * outputFile );
141 
143  void write( Mixed const & records, Mixed const & columnHeaders = mxNull );
145  void writeRecord( Mixed const & record );
146 
147 private:
148  IFile * _outputFile;
149 };
150 
153 {
154 public:
155  CsvReader( IFile * inputFile, bool hasColumnHeaders = false );
156  CsvReader( String const & content, bool hasColumnHeaders = false );
157 
159  Mixed const & operator [] ( int iRow ) const { return _records[iRow]; }
161  Mixed const & operator () ( int iRow, String const & name ) const { return _records[iRow][ _columns[name] ]; }
163  size_t getCount() const { return _records.getCount(); }
164 
166  Mixed & getRecords() { return _records; }
168  Mixed & getColumnHeaders() { return _columns; }
170  void read( String const & content, bool hasColumnHeaders = false );
171 
172 private:
173  Mixed _columns; // 第一行列名代表的索引
174  Mixed _records;
175 };
176 
190 {
191 public:
197  TextArchive( FileEncoding fileEncoding = feMultiByte, winux::AnsiString const & contentEncoding = "", winux::AnsiString const & mbsEncoding = "" ) : _fileEncoding(fileEncoding), _contentEncoding(contentEncoding), _mbsEncoding(mbsEncoding)
198  {
199  }
200 
202  void setFileEncoding( FileEncoding fileEncoding )
203  {
204  this->_fileEncoding = fileEncoding;
205  }
206 
209  {
210  return this->_fileEncoding;
211  }
212 
214  void setContentEncoding( winux::AnsiString const & contentEncoding )
215  {
216  this->_contentEncoding = contentEncoding;
217  }
218 
221  {
222  return this->_contentEncoding;
223  }
224 
226  void setMultiByteEncoding( winux::AnsiString const & mbsEncoding )
227  {
228  this->_mbsEncoding = mbsEncoding;
229  }
230 
233  {
234  return this->_mbsEncoding;
235  }
236 
243  void load( winux::String const & filePath, bool isConvert, winux::AnsiString const & targetEncoding = "", winux::AnsiString const & mbsEncoding = "" )
244  {
245  this->load( winux::FileGetContentsEx( filePath, false ), isConvert, targetEncoding, mbsEncoding );
246  }
247 
254  void load( winux::IFile * f, bool isConvert, winux::AnsiString const & targetEncoding = "", winux::AnsiString const & mbsEncoding = "" )
255  {
256  this->load( f->buffer(true), isConvert, targetEncoding, mbsEncoding );
257  }
258 
265  void load( winux::Buffer const & content, bool isConvert, winux::AnsiString const & targetEncoding = "", winux::AnsiString const & mbsEncoding = "" )
266  {
267  this->setMultiByteEncoding(mbsEncoding);
268 
269  size_t i = 0;
270  this->_recognizeEncode( content, &i );
271  this->_processContent( winux::Buffer( content.getBuf<winux::byte>() + i, content.getSize() - i, true ), isConvert, targetEncoding );
272  }
273 
275  template < typename _ChTy >
276  void write( winux::XString<_ChTy> const & content )
277  {
278  this->write( winux::Buffer( (void *)content.c_str(), content.length() * sizeof(_ChTy), true ) );
279  }
280 
285  template < typename _ChTy >
286  void write( winux::XString<_ChTy> const & content, winux::AnsiString const & encoding )
287  {
288  this->write( winux::Buffer( (void *)content.c_str(), content.length() * sizeof(_ChTy), true ), encoding );
289  }
290 
292  void write( winux::Buffer const & content )
293  {
294  this->_pureContent.append(content);
295  }
296 
301  void write( winux::Buffer const & content, winux::AnsiString const & encoding )
302  {
303  this->setContentEncoding(encoding);
304  this->_pureContent.append(content);
305  }
306 
308  void save( winux::GrowBuffer * output )
309  {
310  this->save( output, this->_fileEncoding );
311  }
312 
314  void save( winux::IFile * f )
315  {
316  this->save( f, this->_fileEncoding );
317  }
318 
320  void save( winux::String const & filePath )
321  {
322  this->save( filePath, this->_fileEncoding );
323  }
324 
329  void save( winux::GrowBuffer * output, FileEncoding fileEncoding )
330  {
331  this->saveEx( this->_pureContent, this->_contentEncoding, output, fileEncoding );
332  }
333 
338  void save( winux::IFile * f, FileEncoding fileEncoding )
339  {
340  this->saveEx( this->_pureContent, this->_contentEncoding, f, fileEncoding );
341  }
342 
347  void save( winux::String const & filePath, FileEncoding fileEncoding )
348  {
349  this->saveEx( this->_pureContent, this->_contentEncoding, filePath, fileEncoding );
350  }
351 
358  void saveEx( winux::Buffer const & content, winux::AnsiString const & encoding, winux::GrowBuffer * output, FileEncoding fileEncoding );
359 
366  void saveEx( winux::Buffer const & content, winux::AnsiString const & encoding, winux::IFile * f, FileEncoding fileEncoding )
367  {
368  winux::GrowBuffer buf;
369  this->saveEx( content, encoding, &buf, fileEncoding );
370  f->write(buf);
371  }
372 
379  void saveEx( winux::Buffer const & content, winux::AnsiString const & encoding, winux::String const & filePath, FileEncoding fileEncoding )
380  {
381  winux::File file( filePath, $T("wb") );
382  this->saveEx( content, encoding, &file, fileEncoding );
383  }
384 
386  template < typename _ChTy >
388  {
389  return this->_pureContent.toString<_ChTy>();
390  }
391 
393  void clear()
394  {
395  this->_pureContent.free();
396  }
397 
398 private:
399  // 处理内容成pure content
400  void _processContent( winux::Buffer const & content, bool isConvert, winux::AnsiString const & encoding = "" );
401  // 识别BOM文件编码,*pI返回跳过BOM真正文本开始的位置
402  void _recognizeEncode( winux::Buffer const & content, size_t * pI );
403 
404  FileEncoding _fileEncoding; // 文件编码
405  winux::GrowBuffer _pureContent; // 纯内容
406  mutable winux::AnsiString _contentEncoding; // 内容编码
407  mutable winux::AnsiString _mbsEncoding; // 多字节编码,当文件编码是MultiByte时,此成员指定是哪个多字节编码
408 };
409 
410 
411 } // namespace winux
412 
413 #endif // __ARCHIVES_HPP__
XString< char > AnsiString
Definition: utilities.hpp:257
void * getBuf() const
暴露缓冲区指针
Definition: utilities.hpp:993
Mixed const mxNull
Mixed(MT_NULL)常量对象
#define WINUX_DLL
Definition: utilities.hpp:60
winux::AnsiString const & getMultiByteEncoding() const
获取多字节编码
Definition: archives.hpp:232
配置文件类
Definition: archives.hpp:10
Mixed & getColumnHeaders()
获取所有列标头
Definition: archives.hpp:168
void save(winux::GrowBuffer *output)
保存本对象的内容到缓冲区
Definition: archives.hpp:308
XString< tchar > String
Definition: utilities.hpp:261
size_t getCount() const
获取读取到的记录数
Definition: archives.hpp:163
void saveEx(winux::Buffer const &content, winux::AnsiString const &encoding, winux::IFile *f, FileEncoding fileEncoding)
保存内容到文件对象
Definition: archives.hpp:366
更强大的配置文件类
Definition: archives.hpp:71
std::map< String, String > StringStringMap
Definition: utilities.hpp:274
winux::XString< _ChTy > toString() const
转成指定字符的字符串
Definition: archives.hpp:387
void load(winux::Buffer const &content, bool isConvert, winux::AnsiString const &targetEncoding="", winux::AnsiString const &mbsEncoding="")
载入指定内容
Definition: archives.hpp:265
文件操作类
Definition: filesys.hpp:430
size_t getSize() const
获取数据大小
Definition: utilities.hpp:1025
virtual size_t write(void const *data, size_t size)
写数据,返回写入的字节数
void save(winux::String const &filePath)
保存本对象的内容到文件
Definition: archives.hpp:320
void saveEx(winux::Buffer const &content, winux::AnsiString const &encoding, winux::String const &filePath, FileEncoding fileEncoding)
保存内容到文件
Definition: archives.hpp:379
void save(winux::String const &filePath, FileEncoding fileEncoding)
保存本对象的内容到文件
Definition: archives.hpp:347
void write(winux::XString< _ChTy > const &content, winux::AnsiString const &encoding)
写入字符串内容
Definition: archives.hpp:286
CSV文件写入器
Definition: archives.hpp:137
void save(winux::GrowBuffer *output, FileEncoding fileEncoding)
保存本对象的内容到缓冲区
Definition: archives.hpp:329
FileEncoding
文件编码
Definition: filesys.hpp:165
void setContentEncoding(winux::AnsiString const &contentEncoding)
设置内容编码。当给文本文档写入内容时,内容默认是什么编码,用这个函数指定。
Definition: archives.hpp:214
std::basic_string< _ChTy, std::char_traits< _ChTy >, std::allocator< _ChTy > > XString
Definition: utilities.hpp:255
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:906
void write(winux::Buffer const &content, winux::AnsiString const &encoding)
写入内容
Definition: archives.hpp:301
TextArchive(FileEncoding fileEncoding=feMultiByte, winux::AnsiString const &contentEncoding="", winux::AnsiString const &mbsEncoding="")
构造函数1
Definition: archives.hpp:197
void setMultiByteEncoding(winux::AnsiString const &mbsEncoding)
设置多字节编码。当文件编码为多字节时,这个参数指示是哪一个多字节编码。
Definition: archives.hpp:226
void load(winux::IFile *f, bool isConvert, winux::AnsiString const &targetEncoding="", winux::AnsiString const &mbsEncoding="")
载入文本文件
Definition: archives.hpp:254
virtual Buffer buffer(bool isPeek=true)
读取整个文件内容,返回缓冲区
static String const ConfigVarsSlashChars
Definition: archives.hpp:22
unsigned char byte
Definition: utilities.hpp:249
void write(winux::Buffer const &content)
写入内容
Definition: archives.hpp:292
高效的可增长缓冲区,1.33倍冗余量
Definition: utilities.hpp:1103
Mixed & getRecords()
获取所有记录的引用,可直接操作
Definition: archives.hpp:166
void clear()
清空文本文档的内容
Definition: archives.hpp:393
void save(winux::IFile *f, FileEncoding fileEncoding)
保存本对象的内容到文件
Definition: archives.hpp:338
文本文档类。可载入文本文件自动识别BOM文件编码,转换为指定编码。
Definition: archives.hpp:189
Plain成员隐藏(By plain block)
Definition: utilities.hpp:536
winux::AnsiString const & getContentEncoding() const
获取内容编码
Definition: archives.hpp:220
混合体,能表示多种类型的值
Definition: utilities.hpp:1440
void setFileEncoding(FileEncoding fileEncoding)
设置文件编码。文件编码当载入文本文件时会自动设置。但如果一开始就只是写文本文档,还是需要手动设置文件...
Definition: archives.hpp:202
FileEncoding const & getFileEncoding() const
获取文件编码
Definition: archives.hpp:208
文件接口
Definition: filesys.hpp:313
CSV文件读取器
Definition: archives.hpp:152
void write(winux::XString< _ChTy > const &content)
写入字符串内容
Definition: archives.hpp:276
void save(winux::IFile *f)
保存本对象的内容到文件
Definition: archives.hpp:314
XStringArray< tchar > StringArray
Definition: utilities.hpp:272
StringStringMap const & getAll() const
取得内部StringStringMap引用
Definition: archives.hpp:65
void load(winux::String const &filePath, bool isConvert, winux::AnsiString const &targetEncoding="", winux::AnsiString const &mbsEncoding="")
载入文本文件
Definition: archives.hpp:243
多字节
Definition: filesys.hpp:168
Buffer FileGetContentsEx(String const &filename, bool textMode)
载入文件内容为一个Buffer,textMode表示是否为文本模式
跨平台基础功能库
Definition: archives.hpp:7
bool has(String const &name) const
判断是否含有该变量
Definition: archives.hpp:36