⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ini.cpp

📁 采用Platform Builder编写的多线程程序
💻 CPP
字号:
// INI.cpp: implementation of the CINI class.
//
//////////////////////////////////////////////////////////////////////

#include <stdafx.h>
#include <stdio.h>

#include <stdlib.h>
#include <malloc.h>



#include "ini.h"

#include <Winbase.h>     //CreateFile()函数


////////////////////////////////////////////////
// 通用接口
////////////////////////////////////////////////

//初始化
CIni::CIni()
{
	m_lDataLen = 0;
	m_strData = NULL;
	IndexNum = 0;
	IndexList = NULL;
}

//初始化
CIni::CIni(char filename[MAX_PATH],LPCTSTR  LPfilename)
{
	m_lDataLen=0;
	m_strData=NULL;
	IndexNum=0;
	IndexList=NULL;
	Open(filename,LPfilename);
}

//析构释放
CIni::~CIni()
{
	if( m_lDataLen != 0 )
	{
		SAFE_DELETE( m_strData );
		m_lDataLen = 0;
	}

	if( IndexNum != 0 )
	{
		SAFE_DELETE( IndexList );
		IndexNum = 0;
	}
}



//读入文件
bool CIni::Open(char  filename[],LPCTSTR  LPfilename)
{
	strcpy(m_strFileName, filename);

	SAFE_FREE( m_strData );
	 
	HANDLE hFile;
   //以下仅仅用于读取文件的大小
	hFile = CreateFile (LPfilename,      // Open txt
                      GENERIC_READ|GENERIC_WRITE
                       ,           // Open for reading  and  writing 
                      0,                      // Do not share
                      NULL,                   // No security
                      OPEN_EXISTING,          // Existing file only
                      FILE_ATTRIBUTE_NORMAL,  // Normal file
                      NULL);                  // No template file
  if (hFile == INVALID_HANDLE_VALUE)
  {
    // Your error-handling code goes here.
     printf("Could not open Haitu.ini!");
    return false;
  }
  else
  {
	     // Try to obtain hFile's size 
         m_lDataLen = GetFileSize (hFile, NULL) ; 
         // Result on failure. 
         if (m_lDataLen == 0xFFFFFFFF) 
		 { 
			 // Your error-handling code goes here.
             printf( "Open  Haitu.ini  error!" );
             return false; 
		 } // End of error handler
  }

  
  CloseHandle(hFile);//关闭文件

	///////////////////////////////////////////
	//////////////////////////////////////////////
	
	FILE *fp;
  	m_strData=new char[m_lDataLen];

   if( (fp = fopen(filename, "r+t" )) != NULL )
   {
      /* Attempt to read in  characters */
      int numread = fread( m_strData, sizeof( char ), m_lDataLen, fp );
      fclose( fp );
	  InitIndex();   //初始化索引
	  return true;
   }
   else
   { printf( "File could not be opened\n" );return false;}
  

}




////////////////////////////////////////////////
// 内部函数
////////////////////////////////////////////////

//计算出所有的索引位置
void CIni::InitIndex()
{
	IndexNum=0;

	for(int i=0; i<m_lDataLen; i++)
	{
		//找到
		if( m_strData[i]=='[' && ( m_strData[i-1]=='\n' || i==0 ) )
		{
			IndexNum++;
		}
	}

	//申请内存
	SAFE_DELETE( IndexList );
	if( IndexNum>0 )
		IndexList=new int[IndexNum];

	int n=0;

	for(i=0; i<m_lDataLen; i++)
	{
		if( m_strData[i]=='[' && ( m_strData[i-1]=='\n' || i==0 ) )
		{
			IndexList[n]=i+1;
			n++;
		}
	}

}




///////////////////////////////////////////////////////////
///     以下为正确的函数     ///////////////////////////////////////////////////////////////

//1.IndexList[]   数组  没错

//2.从当前位置p 开始计算 下一行的 字节位置      
int CIni::GotoNextLine_position(int p)
{

	for(int i=p; i<m_lDataLen; i++)
	{
		if( m_strData[i]=='\n' )  //每行以回车换行符号\n 结束
			return i+1;
	}
	return i;

}

//3.在指定位置读取索引值 Index    
char *CIni::ReadIndexAt_p(int p)
{
	char chr;
	char *Ret;
	int n=p, m=0;

	int LineNum = GotoNextLine_position(p) - p + 1;
	Ret=new char[LineNum];
	
	memset(Ret, 0, LineNum);//将Ret初始化为0

	for(int i=0; i<m_lDataLen-p; i++)
	{
		chr = m_strData[n];

		//结束
		if( chr == ']')
		{
			//ShowMessage(Ret);
			return Ret;
		}
		
		Ret[m]=chr;
		m++;
		n++;
	}

	return Ret;
}

//4.返回索引值为  index   的位置
int CIni::FindIndex_position(char *index)
{
	
	for(int i=0; i<IndexNum; i++)
	{
        
		char *str=ReadIndexAt_p( IndexList[i] );

		if( strcmp(index, str) == 0 )
		{
			SAFE_FREE( str );
			return IndexList[i];
		}
		SAFE_FREE( str );
	}
	return -1;
}

//5.返回name的字节位置    其中p为4.返回的Index的位置
int CIni::FindNamePosition_at_IndexPos_p(int p, char *name0)
{
	
	while(1)
	{
		p=GotoNextLine_position(p);//逐行扫描
		ReadNameAndDataAt_p(p,1);//注:此时参数随便取
		if( strcmp(name, name0)==0 )
		{
			return p;
		}
	}
	return -1;
}



//6.在指定的位置读取  name和data, 其中p为5.返回的name的位置
   //当tagNameOrData=1时,返回name,=2时返回data
char *CIni::ReadNameAndDataAt_p(int p, int tagNameOrData)
{
	char chr;
	
	int m=0;

	name=new char[64];
	memset(name, 0, 64);

    //int i=p;
	chr = m_strData[p];
	while(chr!='=')
	{
		name[m]=chr;
		m++;
		p++;
		chr = m_strData[p];
	}

	m=0;
	data=new char[64];
	memset(data,0,64);
	p++;
	chr = m_strData[p];
	while(chr!='\n')
	{
		data[m]=chr;
		m++;
		p++;
		chr = m_strData[p];
	}

	if(tagNameOrData==1)
	{return name;}
	if(tagNameOrData==2)
	{return data;}

	return name;//默认时返回name

}


///////////////////////////////////////////////
////////////////////////////////////////////////



double CIni::ReadData(char *index,char *name)
{
	int p1=FindIndex_position(index);
	int p2=FindNamePosition_at_IndexPos_p(p1,name);
	char *str=ReadNameAndDataAt_p(p2,2);

	double a=atof(str);
	return a;
	
}



//以普通方式写一字符串数据
bool CIni::Write(char *index, char *name, char *string)
{
	int n=FindIndex_position(index);
	


	//存在数据
	ModityData(n, name, string);	//修改一个数据

	return true;
}

//在当前位置修改一个数据的值,  其中string为要修改的数据(字符串)
bool CIni::ModityData(int p, char *name, char *string)
{


	int n=FindNamePosition_at_IndexPos_p(p, name);
	char* strname=ReadNameAndDataAt_p(n,1);
	char* strdata=ReadNameAndDataAt_p(n,2);
	
	n=n+strlen(strname)+1;
	p=n+strlen(strdata);



	int newlen=strlen(string);
	int oldlen=strlen(strdata);
	if( strlen(strname)>0 ) free(strname);
    if( strlen(strdata)>0 ) free(strdata);



	m_strData = (char *)realloc(m_strData, m_lDataLen+newlen-oldlen);	//重新分配内存

	char *temp=new char[m_lDataLen-p];
	memcpy(temp, &m_strData[p], m_lDataLen-p);
	memcpy(&m_strData[n+newlen], temp, m_lDataLen-p);			//把后面的搬到末尾
	memcpy(&m_strData[n], string, newlen);
	m_lDataLen+=newlen-oldlen;

	SAFE_DELETE( temp );
	return true;
}


//写入文件
bool CIni::Save(char *filename)
{
	if( filename==NULL )
	{
		filename=m_strFileName;
	}

	FILE *fp;
	fp=fopen(filename, "wb");

	fwrite(m_strData, m_lDataLen, 1, fp);
	fclose(fp);

	return true;
}




  /*  关闭文件
void CIni::Close()
{
	if( m_lDataLen != 0 )
	{
		SAFE_DELETE( m_strData );
		m_lDataLen = 0;
	}

	if( IndexNum != 0 )
	{
		SAFE_DELETE( IndexList );
		IndexNum = 0;
	}
}

  */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -