#include "profile.h"
#include "inifile.h"
//-----------------------function define ----------------------------------
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// See if it is '[section]'-like line.
int TProfile::FindName( char* section, char* name )
{
if ( FindSection( section ) )
return FindNameInSection( name ) ;
return 0;
}
int TProfile::GetLine( char* pch, int nCount, char delim )
{
posOldLine = Profile.tellp();
Profile.getline( pch, nCount, delim );
return Profile.good();
}
int TProfile::IsSection( char* buffer, char* secname )
{
if ( delimLEFTBRACKET==buffer[0] )
{
char* s = secname;
char* t = &buffer[1];
while( *s++==*t++ );
if ( !*--s && delimRIGHTBRACKET==*--t )
{
return 1;
}
}
return 0;
}
// See if it is 'name = '-like line.
int TProfile::IsName( char* buffer, char* name )
{
char* s = name;
char* t = buffer;
//correct by ChenHuaJie at 2000/4/1
while( *s==*t ) {
s++;
t++;
}
while ( *t == ' ' && *t ) t++;
if (delimASSIGNMENT==*t && !*s)
return 1;
else
return 0;
//old :
/* while( *s++==*t++ );
for( t--; (delimASSIGNMENT!=*t && *t); t++ );
if ( !*--s && delimASSIGNMENT==*t )
{
return 1;
}
return 0;
*/
}
int TProfile::FindSection( char* secname )
{
if ( Profile.eof() )
Profile.clear();
Profile.seekg( 0, ios::beg );
if ( !Profile )
return 0;
while ( GetLine(Buffer, constLINEBUFFERSIZE, delimENDOFLINE) )
if ( IsSection( Buffer, secname ) ) return 1;
return 0;
}
int TProfile::FindNameInSection( char* name )
{
while( GetLine(Buffer, constLINEBUFFERSIZE, delimENDOFLINE) && delimLEFTBRACKET!=Buffer[0] && !Profile.eof() )
{
if ( IsName( Buffer, name ) )
return 1;//Obtain a valid value-ptr after d'call
}
return 0;
}
// Just suppose it should be some value there
char* TProfile::FirstValue( char* buffer )
{
char* t = buffer;
for( ; delimASSIGNMENT!=*t && *t; t++ );
if ( delimASSIGNMENT==*t )
{
return ++t;
}
return 0;
}
// Check the previous and move to the next
char* TProfile::NextValue( char* buffer )
{
for( ; delimSPACER==*buffer; buffer++ )
if ( !*buffer ) return 0;
for(char* t=buffer;;t++)
{
switch( *t )
{
case delimSPACER:
return t;
case 0:
return 0;
default:
break;
}
}
}
/////////////////////////////////////////////////////////////////////
char* TProfile::FindValue( char* section, char* name, int index )
{
char* buf = 0;
if ( !FindName( section, name ) ) return 0;
for( int i=0; i<index; i++ )
{
if (i)
{// Find next one
if ( !(buf=NextValue(buf)) )
return 0;
}
else
{// First time
if ( !(buf=FirstValue(Buffer)) )
return 0;
}
}
int retval = sscanf( buf, "%s", Token );
if ( !retval || EOF==retval )
return 0;
else
return Token;
}
char* TProfile::FindTValue( char* section, char* name, int index )
{
char* buf = 0;
if ( !FindName( section, name ) ) return 0;
for( int i=0; i<index; i++ )
{
if (i)
{// Find next one
if ( !(buf=NextValue(buf)) )
return 0;
}
else
{// First time
if ( !(buf=FirstValue(Buffer)) )
return 0;
}
}
memset(Token,'\0',sizeof(Token));
strncpy(Token,buf,sizeof(Token)-1);
return Token;
/*
int retval = sscanf( buf, "%c", Token );
if ( !retval || EOF==retval )
return 0;
else
return Token;
*/
}
int TProfile::Backup( char* backup )
{
ofstream backupfile( backup );
char buf;
if ( Profile.good() && backupfile.good() )
{
while ( Profile.get(buf) && EOF!=buf )
backupfile.put(buf);
backupfile.close();
return 1;
}
backupfile.close();
return 0;
}
int TProfile::Restore( char* backup )
{
ifstream backupfile( backup );
char buf;
if ( Profile.good() && backupfile.good() )
{
while ( backupfile.get(buf) && EOF!=buf )
Profile.put(buf) ;
//new add
//put less charater,don't do that,put file data wrong!
int i=0,mCount;
mCount=mLesslen<=constLINESIZE? mLesslen:constLINESIZE;
while ( !Profile.eof() && i<mCount) {
Profile.put(' ') ;
i++;
}
backupfile.close();
return 1;
}
backupfile.close();
return 0;
}
// Interface
int TProfile::NewSection( char* section )
{
if ( FindSection( section ) )
return 0;
if ( Profile.eof() )
Profile.clear();
Profile.seekg( 0, ios::end );
Profile << endl << '[' << section << ']' << endl ;
return 1;
}
// Interface
int TProfile::NewName( char* section, char* name )
{
if ( NewSection( section ) )
{
Profile << name << '=' ;
return 1;
}
return 0;
}
// Interface
int TProfile::NewValue( char* section, char* name, char* value )
{
if ( NewName( section, name ) )
{
Profile << value << endl;
return 1;
}
return 0;
}
// Interface
int TProfile::AddValue( char* section, char* name, char* value )
{
if ( FindName( section, name ) )
{
int lenOldline = strlen(Buffer);
int lenNewline = lenOldline + strlen(value) + 1;
if ( (lenNewline) <= constLINESIZE )
{
Buffer[lenOldline] = ' ';
strcpy( &Buffer[lenOldline+1], value );
return PutLine( posOldLine, Buffer );
}
}
return 0;
}
// Interface
int TProfile::NewNameOfSection( char* section, char* name )
{
if ( FindSection( section ) )
{
posOldLine = Profile.tellp();
sprintf( Buffer, "%s=", name );
return PutLine( posOldLine, Buffer );
}
return 0;
}
int TProfile::PutLine( streampos position, char* newline )
{
char tempFilename[L_tmpnam];
if ( !tmpnam( tempFilename ) )
return 0;
if ( Backup( tempFilename ) )
{
if ( Profile.eof() )
Profile.clear();
Profile.seekp( position );
Profile << newline << endl;
if ( Restore( tempFilename ) )
return (-1==unlink(tempFilename) ? 0 : 1);
}
unlink(tempFilename);
return 0;
}
int TProfile::PutValue( char* section, char* name, char* buffer, int index, char* value )
{
char* curr = 0;
if ( !FindName( section, name ) ) return 0;
for( int i=0; i<index; i++ )
{
if (i)
{// Find next one
if ( !(curr=NextValue(curr)) )
return 0;
}
else
{// First time
if ( !(curr=FirstValue(buffer)) )
return 0;
}
}
char* next = NextValue(curr);
char backup[constLINEBUFFERSIZE];
if( curr )
{
int len1 = curr-buffer;
int len2 = strlen(value);
int len3 = (next ? strlen(next) : 0);
//add
mLesslen=strlen(curr)-strlen(value);
if ( (len1+len2+len3) <= constLINESIZE )
{
if ( next )
strcpy( backup, next );
else
backup[0] = 0;
strcpy( curr, value );
strcpy( curr+len2, backup );
return PutLine( posOldLine, buffer );
}
}
return 0;
}
////////
int TProfile::ReturnSection(char* pContent,char* pSection)
{ //purpose:from pContent to return section name(not incldue "[]"),if not found,pSection return NULL.
char *mP;
int mCount=0,i=0;
if (pContent[0]==delimLEFTBRACKET) {
mP=pContent+1;
mCount=strlen(pContent)-1;
i=0;
// while (mP && *mP!=delimRIGHTBRACKET) //judge section
while ( (i<mCount) && *mP!=delimRIGHTBRACKET) {//judge section
mP++;
i++;
}
if (*mP==delimRIGHTBRACKET) { //copy sections
mP=pContent+1;
// while (mP && *mP!=delimRIGHTBRACKET)
mCount=mCount-1;
i=0;
while (i<mCount) {
if (*mP==delimRIGHTBRACKET)
break;
*pSection++=*mP++;
i++;
}
*pSection++=0; //string end
return 1;
}
}
return 0;//1 is true,0 is false
}
////////
int TProfile::ReadSections( char** & pSections)
{ //purpose:read all sections name,result section name each end with '\0'
//if not found ,pSections=NULL,not neccessary to deal memory point
char *pResult,*pResultBuf,**pSectionsArray;
int mI=0,mCount=0,i=0;
if ( Profile.eof() )
Profile.clear();
Profile.seekg( 0, ios::beg );
if ( !Profile ) {
return 0;//1 is true,0 is false
}
pResultBuf=new char[constLINESIZE];
//SectionsArray init
pSectionsArray=new char*[constSECTIONMAX];
for (mI=0;mI<constSECTIONMAX;mI++)
*(pSectionsArray+mI)=NULL;
//search section
mCount=0;
while ( GetLine(Buffer, constLINEBUFFERSIZE, delimENDOFLINE) ) {
memset(pResultBuf,0,c
评论0