Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_XList_cpp)
00056 #define ALIZE_XList_cpp
00057
00058 #include <new>
00059 #include "XList.h"
00060 #include "Exception.h"
00061 #include "XListFileReader.h"
00062 #include "Config.h"
00063 #include <fstream>
00064
00065 using namespace alize;
00066
00067
00068 XList::XList()
00069 :Object(), _current(0) {}
00070
00071 XList::XList(const FileName& f)
00072 :Object(), _current(0) { load(f, Config()); }
00073
00074 XList::XList(const FileName& f, const Config& c)
00075 :Object(), _current(0) { load(f, c); }
00076
00077 XList& XList::create()
00078 {
00079 XList* p = new (std::nothrow) XList();
00080 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00081 return *p;
00082 }
00083
00084 XList::XList(const XList& l)
00085 :Object(), _current(0)
00086 {
00087 for (unsigned long i=0; i<l._vector.size(); i++)
00088 _vector.addObject(l._vector.getObject(i).duplicate());
00089 }
00090
00091 const XList& XList::operator=(const XList& l)
00092 {
00093 if (this != &l)
00094 {
00095 _vector.deleteAllObjects();
00096 for (unsigned long i=0; i<l._vector.size(); i++)
00097 _vector.addObject(l._vector.getObject(i).duplicate());
00098 _current = 0;
00099 }
00100 return *this;
00101 }
00102
00103 bool XList::operator==(const XList& l) const
00104 {
00105 if (_vector.size() != l._vector.size())
00106 return false;
00107 for (unsigned long i=0; i<l._vector.size(); i++)
00108 if (_vector.getObject(i) != l._vector.getObject(i))
00109 return false;
00110 return true;
00111 }
00112
00113 bool XList::operator!=(const XList& l) const { return !(*this==l); }
00114
00115 void XList::load(const FileName& f, const Config& c)
00116 {
00117 XListFileReader(f, c).readList(*this);
00118
00119 }
00120
00121 void XList::save(const FileName& f) const { save(f, Config()); }
00122
00123 void XList::save(const FileName& f, const Config& c) const
00124 {
00125 std::ofstream ff(f.c_str(), std::ios::out);
00126 if (!ff)
00127 throw IOException("Cannot open file", __FILE__, __LINE__, f);
00128 for (unsigned long i=0; i<getLineCount(); i++)
00129 {
00130 XLine& line = _vector.getObject(i);
00131 unsigned long count = line.getElementCount();
00132 for (unsigned long j=0; j<count; j++)
00133 {
00134 ff << line.getElement(j, false);
00135 if (j+1<count)
00136 ff << " ";
00137 }
00138 ff << std::endl;
00139 }
00140 }
00141
00142 XLine& XList::addLine()
00143 {
00144 XLine& l = XLine::create();
00145 _current = _vector.addObject(l);
00146 return l;
00147 }
00148
00149
00150
00151 XLine& XList::addLine(String& key, String& value)
00152 {
00153 XLine& l = XLine::create(key,value);
00154 _current = _vector.addObject(l);
00155 return l;
00156 }
00157
00158 void XList::rewind() const { _current = 0; }
00159
00160 XLine& XList::getLine(unsigned long i) const
00161 {
00162 XLine& line = _vector.getObject(i);
00163
00164 _current = i;
00165 line.rewind();
00166 return line;
00167 }
00168
00169 XLine* XList::getLine() const
00170 {
00171 if (_current >= _vector.size())
00172 return NULL;
00173 _current++;
00174 XLine& line = _vector.getObject(_current-1);
00175 line.rewind();
00176 return &line;
00177 }
00178
00179 XLine* XList::findLine(const String& key, unsigned long idx) const
00180 {
00181 for (unsigned long i=0; i<_vector.size(); i++)
00182 {
00183 XLine& line = _vector.getObject(i);
00184 if (idx < line.getElementCount() && line.getElement(idx) == key)
00185 {
00186 _current = i;
00187 return &line;
00188 }
00189 }
00190 return NULL;
00191 }
00192
00193 XLine& XList::getAllElements() const
00194 {
00195 _line.reset();
00196 for (unsigned long i=0; i<_vector.size(); i++)
00197 {
00198 XLine& l = _vector.getObject(i);
00199 for (unsigned long j=0; j<l.getElementCount(); j++)
00200 _line.addElement(l.getElement(j, false));
00201 }
00202 return _line;
00203 }
00204
00205 void XList::reset()
00206 {
00207 _vector.deleteAllObjects();
00208 _current = 0;
00209 }
00210
00211 unsigned long XList::getLineCount() const { return _vector.size(); }
00212
00213 String XList::getClassName() const { return "XList"; }
00214
00215
00216
00217 String XList::searchValue(String& index)
00218 {
00219 String result = "";
00220 for (unsigned long i=0; i<_vector.size(); i++)
00221 {
00222 XLine& l = _vector.getObject(i);
00223 if (l.getElement(0) == index) {
00224 return l.getElement(1);
00225 break;
00226 }
00227 }
00228 return result;
00229 }
00230
00231 String XList::toString() const
00232 {
00233 String s;
00234 for (unsigned long i=0; i<_vector.size(); i++)
00235 {
00236 s += "\n";
00237 XLine& l = _vector.getObject(i);
00238 for (unsigned long j=0; j<l.getElementCount(); j++)
00239 { s += " " + l.getElement(j); }
00240 }
00241 return Object::toString() + s;
00242 }
00243
00244 XList::~XList() { _vector.deleteAllObjects(); }
00245
00246
00247 #endif // !defined(ALIZE_XList_cpp)
00248