//author Naven 2003 for both Win32 and Unix os
class MyProperties {
public:
MyProperties();
MyProperties(istream &is);
MyProperties(const string &filename);
void load(const string &filename);
unsigned long size();
string filename();
string get(const string &name, const string &default_value);
void remove(const string &name);
void set(const string &name, const string &value);
void add(const string &name, const string &value);
string getName(const int &i);
string getValue(const int &i);
void save();
void save(string &filename);
private:
void init(istream &is);
private:
string _file;
vector _vals, _elms;
unsigned long _size;
};
MyProperties::MyProperties()
{
_size = 0;
}
MyProperties::MyProperties(istream &is)
{
_size = 0;
init(is);
}
MyProperties::MyProperties(const string &filename)
{
load(filename);
}
void
MyProperties::load(const string &filename)
{
_elms.clear(); _vals.clear();
_size = 0;
if(filename.empty())
return;
ifstream is(filename.c_str());
_file = filename;
init(is);
}
void
MyProperties::init(istream &is)
{
if(!is) return;
char ln[LINE_SIZE+1], *pval;
_elms.clear(); _vals.clear();
_size = 0;
while(is.getline(ln, LINE_SIZE)) {
trimleft(ln);
if(ln[0] != '#' && (pval = strchr(ln, '=')) != NULL) {
pval[0] = 0; pval++;
trimright(ln); trim(pval);
if(ln[0] != 0 && pval[0] != 0) {
_elms.push_back(ln);
_vals.push_back(pval);
_size ++;
}
}
}
}
unsigned long
MyProperties::size()
{
return _size;
}
string
MyProperties::filename()
{
return _file;
}
string
MyProperties::getName(const int &i)
{
if(i >=0 && i<_elms.size())
return _elms[i];
return "";
}
string
MyProperties::getValue(const int &i)
{
if(i >=0 && i<_vals.size())
return _vals[i];
return "";
}
string
MyProperties::get(const string &name, const string &default_value="")
{
if(name.empty())
return default_value;
vector::const_iterator elm_iter = _elms.begin(), elm_end = _elms.end();
vector::const_iterator val_iter = _vals.begin(), val_end = _elms.end();
while(elm_iter != elm_end && val_iter != val_end) {
if((*elm_iter) == name)
return *val_iter;
elm_iter ++;
val_iter ++;
}
return default_value;
}
void
MyProperties::remove(const string &name)
{
if(name.empty()) return;
vector::iterator elm_iter = _elms.begin(), elm_end = _elms.end();
vector::iterator val_iter = _vals.begin(), val_end = _elms.end();
while(elm_iter != elm_end && val_iter != val_end) {
if((*elm_iter) == name) {
_elms.erase(elm_iter);
_vals.erase(val_iter);
_size --;
}
elm_iter ++;
val_iter ++;
}
}
void
MyProperties::set(const string &name, const string &value)
{
if(name.empty()) return;
vector::iterator elm_iter = _elms.begin(), elm_end = _elms.end();
vector::iterator val_iter = _vals.begin(), val_end = _elms.end();
while(elm_iter != elm_end && val_iter != val_end) {
if((*elm_iter) == name)
*val_iter = value;
elm_iter ++;
val_iter ++;
}
}
void
MyProperties::add(const string &name, const string &value)
{
if(name.empty()) return;
_elms.push_back(name); _vals.push_back(value);
_size ++;
}
void
MyProperties::save(string &filename)
{
if(filename.empty())
return;
ifstream is(filename.c_str());
if(!is) return;
char ln[LINE_SIZE+1], *pval;
ostrstream sbuf; string str, str2; bool exist_flag;
vector vec;
while(is.getline(ln, LINE_SIZE)) {
str = ln; trimleft(ln); exist_flag = false;
if(ln[0] != '#' && (pval = strchr(ln, '=')) != NULL) {
pval[0] = 0; trimright(ln);
if(ln[0] != 0 && !(str2=get(ln)).empty()) {
sbuf << ln << "=" << str2 << endl;
vec.push_back(ln);
exist_flag = true;
}
}
if(exist_flag == false)
sbuf << str << endl;
}
vector::iterator elm_iter = _elms.begin(), elm_end = _elms.end();
vector::iterator val_iter = _vals.begin(), val_end = _elms.end();
vector::iterator vec_iter, vec_end = vec.end();
while(elm_iter != elm_end && val_iter != val_end) {
vec_iter = vec.begin(); exist_flag = false;
while(vec_iter != vec_end) {
if((*vec_iter) == (*elm_iter)) {
exist_flag = true; break;
}
vec_iter ++;
}
if(exist_flag == false)
sbuf << *elm_iter << "=" << *val_iter << endl;
elm_iter ++; val_iter ++;
}
sbuf << ends;
ofstream os(filename.c_str());
if(os) os << sbuf.str();
}
void
MyProperties::save()
{
save(_file);
}
/* trim the space and tab at left */
char*
trimleft( char *s )
{
register int i=0; int sl=0;
char *sc =0;
if( s == NULL || s[0] == 0 ) return NULL;
sl = strlen( s );
/* trim space and tab char at the left */
for( i=0; i s )
{
sl = strlen(sc);
for( i=0; i=0; i-- )
{
/* 32 is space, 9 is tab */
if( s[i] == 32 || s[i] == 9 )
s[i] = 0;
else
break;
}
return s;
}
/* trim space and tab at left and right */
char*
trim( char *s )
{
if( s == NULL || s[0] == 0 ) return NULL;
trimright( s );
trimleft( s );
return s;
}