//////////////////////////////////////////////////////////////// // // // Configure file Reader for VATL Version2.0 // // // // Author: Wang.Ping // // // // Ver 1.42 // // // // Date: 18/08/99 // // // // (c)Copyright V.A.T.L Development Workgroup // // // // !Read Max Line Length = 2048 // // ---------------------------------------------------------- // // Funciton: // // // // char *GetStrTitle( char *Str, char *Compstr ); // int Get1Par(int Num, char *Par, char *ParStr); // // int Get1Par(int Num, long *Par, char *ParStr); // // int Get1Par(int Num, int *Par, char *ParStr); // // int Get1Par(int Num, float *Par, char *ParStr); // // int Get1Par(int Num, unsigned int *Par, char *ParStr); // // // // int GetField( char *FileName, char *Group, char *Item, // // char *OutStr ); // // int GetField( char *FileName, char *Group, char *Item, // // int Postion, char *OutStr ); // // int GetField( char *FileName, char *Group, char *Item, // // int Postion, int &OutStr ); // // int GetField( char *FileName, char *Group, char *Item, // // int Postion, float &OutStr ); // // int GetField( char *FileName, char *Group, char *Item, // // int Postion, long &OutStr ); // // int GetField( char *FileName, char *Group, char *Item, // // int Postion, unsigned long &OutStr ); // // // // int WriteField( char *FileName, char *Group, char *Item, // // char *OutStr, int Mode=NEW ); // // int WriteField( char *FileName, char *Group, char *Item, // // int &OutStr, int Mode=NEW ); // // int WriteField( char *FileName, char *Group, char *Item, // // float &OutStr, int Mode=NEW ); // // int WriteField( char *FileName, char *Group, char *Item, // // long &OutStr, int Mode=NEW ); // // int WriteField( char *FileName, char *Group, char *Item, // // unsigned long &OutStr, int Mode=NEW ); // // // // int CreatGroupName( char *FileName, char *Group ); // // int ModifyGroupName( char *FileName, char *Group, // // char *NewGroup, int Mode=NEW ); // // // // int DeleteGroup( char *FileName, char *Group ); // // int DeleteField( char *FileName, char *Group, // // char *Item ); // // // // int FindGroupName( char *FileName, char *Group ); // // // // char *READINI::GetVersion( ); // // // ////////////////////////////////////////////////////////////////
#if !defined(_READINI_H) #define _READINI_H
#define NEW 0 #define MODIFY 1 #define PreMemorySize 512 #define NumricSize 256
enum BL{ READINIOFF=0, READINION=1 };
class READINI { long GetFileLen( char *FileName ); //-1: File Not Found //-2: Cancle //-3: Not Enough memory int CopyFile( char *OldName, char *NewName ); BL AutoBackup; public: READINI(); //Mode = 0 ',' //Mode = 1 ';' int SplitMode; void SetSplitMode( int Mode );
void SetAutoBackup( BL Auto=READINIOFF );
void GetZero( char *Buf ); void GetZeroR( char *Buf ); // Return Title // Str = Str-Title // NULL=not found or Not enough memory char *GetStrTitle( char *Str, char *Compstr ); //return 0: OK // -1: Can not get int Parameters // -2: Not enough memory int Get1Par(int Num, char *Par, char *ParStr); int Get1Par(int Num, long *Par, char *ParStr); int Get1Par(int Num, int *Par, char *ParStr); int Get1Par(int Num, float *Par, char *ParStr); int Get1Par(int Num, unsigned int *Par, char *ParStr); //Read Data From "FileName" // 0: OK //-1: Can not open file //-2: No match field found //-3: Not enough memory int GetField( char *FileName, char *Group, char *Item, char *OutStr ); int GetField( char *FileName, char *Group, char *Item, int Postion, char *OutStr ); int GetField( char *FileName, char *Group, char *Item, int Postion, int &OutStr ); //int GetField( char *FileName, char *Group, char *Item, int Postion, unsigned int &OutStr ); int GetField( char *FileName, char *Group, char *Item, int Postion, float &OutStr ); int GetField( char *FileName, char *Group, char *Item, int Postion, long &OutStr ); int GetField( char *FileName, char *Group, char *Item, int Postion, unsigned long &OutStr ); //Modify Data to "FileName" //-1: File Not Found //-3: Not Enough memory int WriteField( char *FileName, char *Group, char *Item, char *OutStr, int Mode=NEW ); int WriteField( char *FileName, char *Group, char *Item, int &OutStr, int Mode=NEW ); int WriteField( char *FileName, char *Group, char *Item, unsigned &OutStr, int Mode=NEW ); int WriteField( char *FileName, char *Group, char *Item, float &OutStr, int Mode=NEW ); int WriteField( char *FileName, char *Group, char *Item, long &OutStr, int Mode=NEW ); int WriteField( char *FileName, char *Group, char *Item, unsigned long &OutStr, int Mode=NEW ); // int CreatGroupName( char *FileName, char *Group ); int ModifyGroupName( char *FileName, char *Group, char *NewGroup, int Mode=NEW ); // int DeleteGroup( char *FileName, char *Group ); int DeleteField( char *FileName, char *Group, char *Item ); // int FindGroupName( char *FileName, char *Group ); //Get Version information char *GetVersion( ); int GetVer( ); };
extern READINI ReadIni; #endif
这是具体实现代码:
//////////////////////////////////////////////////////////////// // // // Configure file Reader for VATL Version2.0 // // // // Author: Wang.Ping // // // // Ver 1.3 // // // // Date: 23/10/97 // // // // (c)Copyright V.A.T.L Development Workgroup // // // //////////////////////////////////////////////////////////////// #include <string.h> #include <stdio.h> #include <stdlib.h> #include <dir.h>
#include "readini.h" #define MAXBUFFER 200
READINI ReadIni;
READINI::READINI( ) { }
void READINI::GetZero( char *Buf ) { int i=0, j=0; int Len=strlen( Buf ); for( i=0; i<Len; i++ ) { if( Buf[i]==' ' ) { j++; continue; } else { for( i=0; i<strlen(Buf)-j; i++ ) Buf[i]=Buf[i+j]; Buf[i]='\0';
break; } }
if( j==Len )
Buf[0]='\0';
for( i=0; i<strlen( Buf ); i++
) if( Buf[i]=='\n' ) { Buf[i]='\0'; break; }
}
void READINI::GetZeroR( char *Buf ) {
int i=0, j=0;
for( i=0; i<strlen( Buf ); i++
) { if( Buf[i]==' ' ) { j++; continue; } else
{ for( i=0; i<strlen(Buf)-j; i++ ) Buf[i]=Buf[i+j];
Buf[i]='\0'; break; } }
for( i=0; i<strlen( Buf ); i++
) { if( Buf[i]!=' ' ) { continue; } else {
Buf[i]='\0'; } }
for( i=0; i<strlen( Buf ); i++
) if( Buf[i]=='\n' ) { Buf[i]='\0'; break; } }
// 0: OK //-1: Can not open file //-2:
No match field found int READINI::GetField( char *FileName, char *Group, char
*Item, int Postion, char *OutStr ) { char Buf[300]; int i=0,
FLAG=0; char *ptr; FILE *Ptr; if(( Ptr=fopen(FileName,
"rt"))==NULL ) return -1;
while( !FLAG ) { if(
fgets( Buf, 256, Ptr )==NULL ) { FLAG=2; break; }
GetZero( Buf );
if( !stricmp( Buf, Group ) ) { for( ;; )
{ if( fgets( Buf, 256, Ptr )==NULL ) break;
GetZero( Buf );
if( Buf[i]=='[' ) { FLAG=2; break; } else if( Buf[i]=='<' )
{ FLAG=2; break; } else if( Buf[i]=='(' ) { FLAG=2; break; }
ptr=strtok( Buf,"=" );
GetZero( ptr ); //GetZeroR( ptr );
if( !stricmp( Item, ptr ) ) { for( i=0;
i<Postion+1; i++ ) { ptr=strtok( NULL,"," ); GetZero(
ptr ); //GetZeroR( ptr ); if(ptr==NULL) { if( FLAG!=1 )
FLAG=2; break; } else { strcpy( OutStr, ptr ); FLAG=1;
} } } if( FLAG==1 || FLAG==2 ) break; }
} }
GetZero( OutStr );
fclose( Ptr );
if( FLAG==1 ) return 0; else return
-2; }
// 0: OK //-1: Can not open file //-2: No match field found int
READINI::GetField( char *FileName, char *Group, char *Item, int Postion, int
&OutStr ) { char Out_Str[300]; int i=GetField( FileName,
Group, Item, Postion, Out_Str );
OutStr = atoi( Out_Str );
return i; }
// 0: OK //-1: Can not open file //-2: No match field found int
READINI::GetField( char *FileName, char *Group, char *Item, int Postion, float
&OutStr ) { char Out_Str[300]; int i=GetField( FileName,
Group, Item, Postion, Out_Str );
OutStr = atof( Out_Str );
return i; }
// 0: OK //-1: Can not open file //-2: No match field
found int READINI::GetField( char *FileName, char *Group, char *Item, int
Postion, long &OutStr ) { char Out_Str[300]; int i=GetField(
FileName, Group, Item, Postion, Out_Str );
OutStr = atol( Out_Str );
return i; }
//return 0: OK // -1: Can not get int Parameters int
READINI::Get1Par(int Num, char* Par, char *ParStr) { char
strptr[MAXBUFFER]; char *ptr; strcpy(strptr, ParStr);
ptr=strtok(strptr,","); if( ptr==NULL ) return -1; //GetZero( ptr
); for(int i=0; i<Num; i++) { if(ptr==NULL)
return -1; else ptr=strtok(NULL,","); } if(
ptr==NULL ) return -1; GetZero( ptr ); strcpy( Par, ptr );
return 0; }
//return 0: OK // -1: Can not get int Parameters int
READINI::Get1Par(int Num, int *Par, char *ParStr) { char
Par_Str[257]; int i=Get1Par( Num, Par_Str, ParStr ); *Par = atoi(
Par_Str ); return i; }
//return 0: OK // -1: Can not get int Parameters int
READINI::Get1Par(int Num, float *Par, char *ParStr) { char
Par_Str[257]; int i=Get1Par( Num, Par_Str, ParStr ); *Par = atof(
Par_Str ); return i; }
//return 0: OK // -1: Can not get int Parameters int
READINI::Get1Par(int Num, unsigned int *Par, char *ParStr) { char
Par_Str[257]; int i=Get1Par( Num, Par_Str, ParStr ); *Par = atoi(
Par_Str ); return i; }
//return 0: OK // -1: Can not get int Parameters int
READINI::Get1Par(int Num, long *Par, char *ParStr) { char
Par_Str[257]; int i=Get1Par( Num, Par_Str, ParStr ); *Par = atol(
Par_Str ); return i; }
char *READINI::GetVersion( ) { return "1.30"; }
由于是纯C++代码,在VC/EVC中直接使用即可。
|