BEntryFile::BEntryFile(){
}
BEntryFile::BEntryFile(BString filename){
open(filename);
}
int BEntryFile::open(BString filename){
ofilename = filename;
return access(ofilename.retStr(), R_OK);
}
BEntryFile::~BEntryFile(){
clear();
}
void BEntryFile::clear(){
BEntryList::clear();
ocomments = "";
}
int BEntryFile::read(){
char buf[10240];
char name[1024];
char* t;
char* f;
FILE* ofile;
clear();
if((ofile = fopen(ofilename.retStr(), "r")) == NULL)
return -1;
while(fgets(buf, sizeof(buf), ofile)){
if(strlen(buf)){
if((buf[0] != '#') && (buf[0] != '\n') && (buf[0] != '\r')){
buf[strlen(buf) - 1] = '\0';
if((strlen(buf) > 0) && (buf[strlen(buf) - 1] == '\r'))
buf[strlen(buf) - 1] = '\0';
for(t = name, f = buf; (*f && !isspace(*f));
t++, f++){
*t = *f;
}
*t = 0;
while(*f && isspace(*f))
f++;
t = f;
while(*t)
t++;
*t = 0;
append(BEntry(name, f));
}
else if(buf[0] == '#'){
ocomments = ocomments + buf;
}
}
}
fclose(ofile);
return 0;
}
int BEntryFile::write(){
return writeList(*this);
}
int BEntryFile::writeList(BEntryList& l){
BIter i;
FILE* ofile;
BString name;
BString value;
if((ofile = fopen(ofilename.retStr(), "w")) == NULL)
return -1;
fprintf(ofile, "%s", ocomments.retStr());
for(i = l.begin(); !l.isEnd(i); l.next(i)){
name = l.get(i).getName();
value = l.get(i).getValue();
fprintf(ofile, "%-15s\t%s\n", name.retStr(), value.retStr());
}
fclose(ofile);
return 0;
}