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

📄 snini.cpp

📁 在wince中定时捕获桌面图片信息
💻 CPP
字号:
#include "StdAfx.h"
#include "SNIni.h"
#include <stdlib.h>


SNIni::SNIni()
{
	m_nItemCount = 0;
	m_nNewCount = 0;
	m_chItems = NULL;
	m_chItemValues = NULL;
	m_pf = NULL;
}

int SNIni::Open( const  char* strFileName, int ItemCount )
{
	int iReadStatus = 0; //0表示读取失败
	if( strFileName && strlen(strFileName) > 0 && ItemCount > 0 )
	{
		m_pf = fopen( strFileName, "r" );
		if( m_pf == NULL )
		{ //0表示读取失败
			return iReadStatus;
		}
		else
		{
			iReadStatus = 1; //表示读取
		}

		m_chItemValues = (char**) malloc( ItemCount*sizeof(char**) );
		m_chItems = (char**) malloc( ItemCount*sizeof(char**) );

		int i=0;
		for( i=0; i<ItemCount; i++ )
		{
			m_chItemValues[i]=NULL;
			m_chItems[i]=NULL;
		}
	
		int iEOF = 0;
		char *pchSeparator = NULL;
		while( m_nItemCount < ItemCount )
		{
			iEOF = fscanf( m_pf, "%s\n",m_chReading );
			if( iEOF == EOF )
			{
				iReadStatus = 2;// 表示新建
				break;
			}
			
			pchSeparator = strstr( m_chReading, "=" );
			int iCharCountName = pchSeparator-m_chReading;
			if( iCharCountName < 0 )
			{
				continue;
			}
			int  iCharCountValue = strlen(m_chReading) - iCharCountName - 1;
			m_chReading[ iCharCountName ] = 0;

			//复制数据
			m_chItems[m_nItemCount] = (char*)malloc( iCharCountName+1 );
			strcpy( m_chItems[m_nItemCount], m_chReading );

			m_chItemValues[m_nItemCount] = (char*)malloc( iCharCountValue+1 );
			strcpy( m_chItemValues[m_nItemCount], m_chReading+iCharCountName+1 );
			
			m_nItemCount++;
		}
	}
	return iReadStatus;
}

char* SNIni::Read( const char* Item )
{
    int i=0;
	//modified by jiangchj at 2007-05-17 Item为空时没有处理
	//if( m_nItemCount == 0 ) 
	if( m_nItemCount == 0 || Item == NULL) 
	{
		return NULL;
	}

	while( strcmp( m_chItems[i], Item ) != 0 )
	{
		i++;
		if(i == m_nItemCount )
		{
			return NULL;
		}
	}
	
	return  m_chItemValues[i];
}


void SNIni::Write( char* Item, char* ItemValue )
{
	int strsize = strlen(Item)+1;
	m_chItems[m_nItemCount] = (char*)malloc(strsize);
	strcpy( m_chItems[m_nItemCount], Item );
	
	strsize = strlen(ItemValue)+1;
	m_chItemValues[m_nItemCount]=(char*)malloc(strsize);
	strcpy( m_chItemValues[m_nItemCount], ItemValue );
	
	m_nItemCount++;
	m_nNewCount++;
}

SNIni::~SNIni()
{
	Close();
}

void SNIni::Close()
{
	if( m_pf != NULL )
	{
		fclose( m_pf );
		m_pf = NULL;
	}
	
	int i=0;
	for( i=0; i<m_nItemCount; i++ )
	{   //逐个释放各个子项的内存
		if( m_chItems[i] != NULL )
		{
			free( m_chItems[i] );
			m_chItems[i] = NULL;
		}

		if( m_chItemValues[i] != NULL )
		{
			free( m_chItemValues[i] );
			m_chItemValues[i] = NULL;
		}
	}

	if( m_chItems != NULL )
	{
		free( m_chItems );
		m_chItems = NULL;
	}

	if( m_chItemValues != NULL )
	{
		free( m_chItemValues );
		m_chItemValues = NULL;
	}

	m_nItemCount = 0;
}

char*  SNIni::Modify( char* Item, char* ItemValue )
{
	if( m_nItemCount == 0 )
	{
		return NULL;
	}

    int i=0;
	while( strstr( m_chItems[i], Item ) == NULL )    
	{
		i++;
		if( i == m_nItemCount )
		{
			return NULL;
		}
	}
	
	free( m_chItemValues[i] );
	int len = strlen( ItemValue ) + 1;
	m_chItemValues[i] = (char*)malloc( len );
	strcpy( m_chItemValues[i], ItemValue );

	return  m_chItemValues[i];
}

⌨️ 快捷键说明

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