sprint 1-alpha
|
00001 /****************************************************************************** 00002 * sprint::simple 00003 * 00004 * Copyright (C) 2002-2011 Paolo Medici <www.pmx.it> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 * 00020 *******************************************************************************/ 00021 00026 #ifndef _SPRINT_SIMPLE_ARCHIVE_H 00027 #define _SPRINT_SIMPLE_ARCHIVE_H 00028 00029 #include <sprint/sysdef.h> 00030 #include <string> 00031 #include <string.h> 00032 #include <sprint/container/unordered_map.h> 00033 #include <sprint/smart_cast.h> 00034 00035 namespace sprint { 00036 00038 typedef sprint::unordered_map<std::string, std::string> SimpleTextArchive; 00039 00041 bool LoadSimpleTextArchive(std::vector< std::pair<std::string, std::string> > & s, const char *fileName); 00043 bool SaveSimpleTextArchive(const std::vector< std::pair<std::string, std::string> > & s, const char *fileName); 00044 00046 class SimpleTextArchiveOut { 00047 FILE *m_fp; 00048 public: 00049 SimpleTextArchiveOut(const char *storage); 00050 ~SimpleTextArchiveOut(); 00051 00052 inline bool reading() const { return false; } 00053 inline bool writing() const { return true; } 00054 00055 void Write(const char * key, const char *value); 00056 void Write(const char * key, const std::string & value) 00057 { 00058 Write(key, value.c_str()); 00059 } 00060 00061 template<class T, class R> 00062 inline void operator() (const char * path, T value, R _default) { Write(path, smart_cast<std::string>(value) ); } 00063 00064 template<class T> 00065 inline bool operator() (const char * path, T value) { Write(path, value); return true; } 00066 00067 inline void str (const char * path, const char * buffer, int buflen, const char * _default) 00068 { 00069 Write(path, buffer); 00070 } 00071 00072 inline bool str (const char * path, const char * buffer, int buflen) 00073 { 00074 Write(path, buffer); 00075 return true; 00076 } 00077 }; 00078 00080 class SimpleTextArchiveIn { 00081 SimpleTextArchive m_storage; 00082 public: 00083 00084 SimpleTextArchiveIn(const char *storage) { 00085 sprint::LoadSimpleTextArchive(m_storage, storage); 00086 } 00087 ~SimpleTextArchiveIn(); 00088 00089 inline bool reading() const { return true; } 00090 inline bool writing() const { return false; } 00091 00093 const char *Read(const char * path) const; 00094 00095 template<class T> 00096 inline bool operator() (const char * path, T &value) 00097 { 00098 const char *val = Read(path); 00099 if(val!=NULL) 00100 { 00101 value = smart_cast<T>(val); 00102 return true; 00103 } 00104 else 00105 return false; 00106 } 00107 00108 template<class T, class R> 00109 inline void operator() (const char * path, T &value, R _default) 00110 { 00111 if(!operator()(path, &value)) 00112 value = _default; 00113 } 00114 00115 inline bool str (const char * path, char * buffer, int buflen) 00116 { 00117 const char *val = Read(path); 00118 if(val!=NULL) 00119 { 00120 strncpy(buffer, val, buflen); 00121 return true; 00122 } 00123 else 00124 return false; 00125 } 00126 00127 inline void str (const char * path, char * buffer, int buflen, const char * _default) 00128 { 00129 if(!str(path, buffer, buflen)) strcpy(buffer, _default); 00130 } 00131 00132 00133 }; 00134 00135 } // namespace 00136 00137 #endif