#include <general.h>
#include <textprofile.h>
// **********************************************************************
// * *
// * *
// * *
// **********************************************************************
void textprofilet::clear()
{
data_elements=0;
}
bool textprofilet::readline(std::istream &in)
{
char line[10000], ch;
if(!in.get(line, 10000-1)) return TRUE;
in.get(ch); // read newline
char *tptr, *tptr2;
clear();
tptr=strchr(line, '\r'); if(tptr!=NULL) *tptr=0;
tptr=strchr(line, '\n'); if(tptr!=NULL) *tptr=0;
if(line[0]==0 || line[0]=='#' || line[0]==';') return TRUE;
tptr=line;
while(*tptr!=0)
{
for(tptr2=tptr; *tptr2!=0 && *tptr2!='=' && *tptr2!=':'; tptr2++);
if(*tptr2==0) break;
*(tptr2++)=0;
const char *id=tptr;
for(tptr=tptr2; *tptr2!=0 && *tptr2!=';'; tptr2++);
*(tptr2++)=0;
do_set(id, tptr);
tptr=tptr2;
}
return FALSE;
}
bool textprofilet::writeline(std::ostream &out)
{
bool first=TRUE;
for(unsigned i=0; i<data_elements; i++)
if(data[i].data[0]!=0)
{
if(first)
first=FALSE;
else
out << ';';
out << data[i].id << '=' << data[i].data;
}
out << '\n';
return FALSE;
}
bool textprofilet::readfile(const char *filename)
{
FILE *infile;
char id[1000], text[1000];
clear();
if((infile=fopen(filename, "rt"))==NULL) return TRUE;
schleife:
switch(readconfigline(infile, id, text, 900))
{
case 0:
do_set(id, text);
goto schleife;
case 1:
break;
default:;
//logf.printf("pt::read", "Warning: error in "
// "\"%s\"!", filename);
}
fclose(infile);
return FALSE;
}
bool textprofilet::writefile(const char *filename)
{
FILE *outfile;
if((outfile=fopen(filename, "wt"))==NULL) return TRUE;
for(unsigned i=0; i<data_elements; i++)
if(data[i].data[0]!=0)
fprintf(outfile, "%s=%s\n", data[i].id, data[i].data);
if(fflush(outfile)!=0)
{
fclose(outfile);
return TRUE;
}
if(fclose(outfile))
return TRUE;
return FALSE;
}
void textprofilet::initialize()
{
data_elements=data_space=0;
data=(datat *)NULL;
}
textprofilet::textprofilet()
{
initialize();
}
textprofilet::textprofilet(const textprofilet &textprofile)
{
initialize();
data_elements=textprofile.data_elements;
data_space=data_elements;
if(data_elements!=0)
{
data=(datat *)malloc(data_space*sizeof(datat));
memcpy(data, textprofile.data, data_space*sizeof(datat));
}
}
textprofilet::~textprofilet()
{
if(data!=NULL)
{
free(data);
data=NULL;
}
data_elements=data_space=0;
}
const char *textprofilet::do_get(const char *id) const
{
for(unsigned i=0; i<data_elements; i++)
if(stricmp(data[i].id, id)==0)
return data[i].data;
return "";
}
const char *textprofilet::get(const char *id) const
{
return do_get(id);
}
char *textprofilet::do_get_ptr(const char *id)
{
unsigned i;
for(i=0; i<data_elements; i++)
if(stricmp(data[i].id, id)==0)
return data[i].data;
do_set(id, "");
for(i=0; i<data_elements; i++)
if(stricmp(data[i].id, id)==0)
return data[i].data;
abort(); // ASSERTION
return (char *)NULL;
}
char *textprofilet::get_ptr(const char *id)
{
return do_get_ptr(id);
}
void textprofilet::get(const char *id, char *dest) const
{
strmaxcpy(dest, do_get(id), TEXTLEN-1);
}
void textprofilet::get(const char *id, std::string &dest) const
{
dest=do_get(id);
}
#define MIN_DATA_SPACE 10
void textprofilet::do_set(const char *id, const char *text)
{
if(stricmp(id, "name")==0)
return;
for(unsigned i=0; i<data_elements; i++)
if(stricmp(data[i].id, id)==0)
{
strmaxcpy(data[i].data, text, TEXTLEN-1);
return;
}
if(data_space==0)
{
data_space=MIN_DATA_SPACE;
data=(datat *)malloc(data_space*sizeof(datat));
if(data==NULL) abort();
}
else if(data_space<(data_elements+1))
{
data_space*=2;
data=(datat *)realloc(data, data_space*sizeof(datat));
if(data==NULL) abort();
}
strmaxcpy(data[data_elements].id, id, TEXTLEN-1);
strmaxcpy(data[data_elements].data, text, TEXTLEN-1);
data_elements++;
}
void textprofilet::set(const char *id, const char *text)
{
do_set(id, text);
}
void textprofilet::set(const char *id, const std::string &text)
{
do_set(id, text.c_str());
}
void textprofilet::set(const char *id, unsigned long text)
{
char tempstr[TEXTLEN];
snprintf(tempstr, TEXTLEN, "%lu", text);
do_set(id, tempstr);
}
void textprofilet::set(const char *id, int text)
{
char tempstr[TEXTLEN];
snprintf(tempstr, TEXTLEN, "%d", text);
do_set(id, tempstr);
}
void textprofilet::set(const char *id, signed long text)
{
char tempstr[TEXTLEN];
snprintf(tempstr, TEXTLEN, "%ld", text);
do_set(id, tempstr);
}
void textprofilet::set(const char *id, bool text)
{
char tempstr[TEXTLEN];
snprintf(tempstr, TEXTLEN, "%d", text);
do_set(id, tempstr);
}
const char *textprofilet::operator|(const char *id) const
{
return do_get(id);
}
syntax highlighted by Code2HTML, v. 0.9.1