前一段时间,一直有朋友在向我索要EVC版本的ini读取函数,由于是公司其他人完成的代码,我不便于公布,正好这段时间重新拾起了成都英创公司的NetBox / NetBoxII相关开发工作,它的系统平台为DOS,开发平台BC3.0。它提供的一个ini读写的代码,我用了一下,非常好使,好东西就要共享,这里我想成都英创公司也不会反对,毕竟我给他也做了一个小小的宣传(我们公司的隧道广告早期的通信子站就是NetBoxII所开发,目前为Moxa公司的通信设备)。:-)
ini读写头文件源码:
///
/
// //
// ConfigurefileReaderforVATLVersion2.0 //
// //
// Author:Wang.Ping //
// //
// Ver1.42 //
// //
// Date:18/08/99 //
// //
// (c)CopyrightV.A.T.LDevelopmentWorkgroup //
// //
// !ReadMaxLineLength=2048 //
// ---------------------------------------------------------- //
// Funciton: //
// //
// char*GetStrTitle(char*Str,char*Compstr);
// intGet1Par(intNum,char*Par,char*ParStr); //
// intGet1Par(intNum,long*Par,char*ParStr); //
// intGet1Par(intNum,int*Par,char*ParStr); //
// intGet1Par(intNum,float*Par,char*ParStr); //
// intGet1Par(intNum,unsignedint*Par,char*ParStr); //
// //
// intGetField(char*FileName,char*Group,char*Item, //
// char*OutStr); //
// intGetField(char*FileName,char*Group,char*Item, //
// intPostion,char*OutStr); //
// intGetField(char*FileName,char*Group,char*Item, //
// intPostion,int&OutStr); //
// intGetField(char*FileName,char*Group,char*Item, //
// intPostion,float&OutStr); //
// intGetField(char*FileName,char*Group,char*Item, //
// intPostion,long&OutStr); //
// intGetField(char*FileName,char*Group,char*Item, //
// intPostion,unsignedlong&OutStr); //
// //
// intWriteField(char*FileName,char*Group,char*Item, //
// char*OutStr,intMode=NEW); //
// intWriteField(char*FileName,char*Group,char*Item, //
// int&OutStr,intMode=NEW); //
// intWriteField(char*FileName,char*Group,char*Item, //
// float&OutStr,intMode=NEW); //
// intWriteField(char*FileName,char*Group,char*Item, //
// long&OutStr,intMode=NEW); //
// intWriteField(char*FileName,char*Group,char*Item, //
// unsignedlong&OutStr,intMode=NEW); //
// //
// intCreatGroupName(char*FileName,char*Group); //
// intModifyGroupName(char*FileName,char*Group, //
// char*NewGroup,intMode=NEW); //
// //
// intDeleteGroup(char*FileName,char*Group); //
// intDeleteField(char*FileName,char*Group, //
// char*Item); //
// //
// intFindGroupName(char*FileName,char*Group); //
// //
// char*READINI::GetVersion(); //
// //
/// /
#if !defined(_READINI_H)
#define _READINI_H
#define NEW0
#define MODIFY1
#define PreMemorySize512
#define NumricSize256
enum BL{READINIOFF = 0 ,READINION = 1 };
class READINI
{
long GetFileLen( char * FileName);
// -1:FileNotFound
// -2:Cancle
// -3:NotEnoughmemory
int CopyFile( char * OldName, char * NewName);
BLAutoBackup;
public :
READINI();
// Mode=0','
// Mode=1';'
int SplitMode;
void SetSplitMode( int Mode);
void SetAutoBackup(BLAuto = READINIOFF);
void GetZero( char * Buf);
void GetZeroR( char * Buf);
// ReturnTitle
// Str=Str-Title
// NULL=notfoundorNotenoughmemory
char * GetStrTitle( char * Str, char * Compstr);
// return0:OK
// -1:CannotgetintParameters
// -2:Notenoughmemory
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);
// ReadDataFrom"FileName"
// 0:OK
// -1:Cannotopenfile
// -2:Nomatchfieldfound
// -3:Notenoughmemory
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);
// intGetField(char*FileName,char*Group,char*Item,intPostion,unsignedint&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);
// ModifyDatato"FileName"
// -1:FileNotFound
// -3:NotEnoughmemory
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);
// GetVersioninformation
char * GetVersion();
int GetVer();
};
extern READINIReadIni;
#endif
// //
// ConfigurefileReaderforVATLVersion2.0 //
// //
// Author:Wang.Ping //
// //
// Ver1.42 //
// //
// Date:18/08/99 //
// //
// (c)CopyrightV.A.T.LDevelopmentWorkgroup //
// //
// !ReadMaxLineLength=2048 //
// ---------------------------------------------------------- //
// Funciton: //
// //
// char*GetStrTitle(char*Str,char*Compstr);
// intGet1Par(intNum,char*Par,char*ParStr); //
// intGet1Par(intNum,long*Par,char*ParStr); //
// intGet1Par(intNum,int*Par,char*ParStr); //
// intGet1Par(intNum,float*Par,char*ParStr); //
// intGet1Par(intNum,unsignedint*Par,char*ParStr); //
// //
// intGetField(char*FileName,char*Group,char*Item, //
// char*OutStr); //
// intGetField(char*FileName,char*Group,char*Item, //
// intPostion,char*OutStr); //
// intGetField(char*FileName,char*Group,char*Item, //
// intPostion,int&OutStr); //
// intGetField(char*FileName,char*Group,char*Item, //
// intPostion,float&OutStr); //
// intGetField(char*FileName,char*Group,char*Item, //
// intPostion,long&OutStr); //
// intGetField(char*FileName,char*Group,char*Item, //
// intPostion,unsignedlong&OutStr); //
// //
// intWriteField(char*FileName,char*Group,char*Item, //
// char*OutStr,intMode=NEW); //
// intWriteField(char*FileName,char*Group,char*Item, //
// int&OutStr,intMode=NEW); //
// intWriteField(char*FileName,char*Group,char*Item, //
// float&OutStr,intMode=NEW); //
// intWriteField(char*FileName,char*Group,char*Item, //
// long&OutStr,intMode=NEW); //
// intWriteField(char*FileName,char*Group,char*Item, //
// unsignedlong&OutStr,intMode=NEW); //
// //
// intCreatGroupName(char*FileName,char*Group); //
// intModifyGroupName(char*FileName,char*Group, //
// char*NewGroup,intMode=NEW); //
// //
// intDeleteGroup(char*FileName,char*Group); //
// intDeleteField(char*FileName,char*Group, //
// char*Item); //
// //
// intFindGroupName(char*FileName,char*Group); //
// //
// char*READINI::GetVersion(); //
// //
/// /
#if !defined(_READINI_H)
#define _READINI_H
#define NEW0
#define MODIFY1
#define PreMemorySize512
#define NumricSize256
enum BL{READINIOFF = 0 ,READINION = 1 };
class READINI
{
long GetFileLen( char * FileName);
// -1:FileNotFound
// -2:Cancle
// -3:NotEnoughmemory
int CopyFile( char * OldName, char * NewName);
BLAutoBackup;
public :
READINI();
// Mode=0','
// Mode=1';'
int SplitMode;
void SetSplitMode( int Mode);
void SetAutoBackup(BLAuto = READINIOFF);
void GetZero( char * Buf);
void GetZeroR( char * Buf);
// ReturnTitle
// Str=Str-Title
// NULL=notfoundorNotenoughmemory
char * GetStrTitle( char * Str, char * Compstr);
// return0:OK
// -1:CannotgetintParameters
// -2:Notenoughmemory
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);
// ReadDataFrom"FileName"
// 0:OK
// -1:Cannotopenfile
// -2:Nomatchfieldfound
// -3:Notenoughmemory
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);
// intGetField(char*FileName,char*Group,char*Item,intPostion,unsignedint&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);
// ModifyDatato"FileName"
// -1:FileNotFound
// -3:NotEnoughmemory
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);
// GetVersioninformation
char * GetVersion();
int GetVer();
};
extern READINIReadIni;
#endif
这是具体实现代码:
///
/
// //
// ConfigurefileReaderforVATLVersion2.0 //
// //
// Author:Wang.Ping //
// //
// Ver1.3 //
// //
// Date:23/10/97 //
// //
// (c)CopyrightV.A.T.LDevelopmentWorkgroup //
// //
/// /
#include < string .h >
#include < stdio.h >
#include < stdlib.h >
#include < dir.h >
#include " readini.h "
#define MAXBUFFER200
READINIReadIni;
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;
}
}
// //
// ConfigurefileReaderforVATLVersion2.0 //
// //
// Author:Wang.Ping //
// //
// Ver1.3 //
// //
// Date:23/10/97 //
// //
// (c)CopyrightV.A.T.LDevelopmentWorkgroup //
// //
/// /
#include < string .h >
#include < stdio.h >
#include < stdlib.h >
#include < dir.h >
#include " readini.h "
#define MAXBUFFER200
READINIReadIni;
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';
Buf[0]='\0';
for( i=0; i<strlen( Buf ); i++ )
if( Buf[i]=='\n' ) { Buf[i]='\0'; break; }
if( Buf[i]=='\n' ) { Buf[i]='\0'; break; }
}
void READINI::GetZeroR( char *Buf )
{
int i=0, j=0;
{
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;
}
}
{
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'; }
}
{
if( Buf[i]!=' ' ) { continue; }
else { Buf[i]='\0'; }
}
for( i=0; i<strlen( Buf ); i++ )
if( Buf[i]=='\n' ) { Buf[i]='\0'; break; }
}
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;
//-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; }
{
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; }
else if( Buf[i]=='<' ) { FLAG=2; break; }
else if( Buf[i]=='(' ) { FLAG=2; break; }
ptr=strtok( Buf,"=" );
GetZero( ptr );
//GetZeroR( 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;
}
}
}
{
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;
}
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 );
//-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 );
//-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;
}
// -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;
}
// -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;
}
// -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;
}
// -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;
}
// -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中直接使用即可。
注:代码版权归原作者所有