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

📄 params.cpp

📁 机器人程序
💻 CPP
字号:
// Params.cpp - by Robin Hewitt, 2005
// http://www.robinhewitt.com/mavis
// This is free software. See license at the bottom
// of this file for details.
//

#include "Params.h"
#include "MavisErr.h"
#include <windows.h>

using namespace std;

//////////////////////////////////////////////////////////////
// Implementation of the ParamData class
//

//////////////////
//  Constructor
//
ParamData::ParamData(const char * s, const char * n, const char * v)
{
	setName(s,n);
	setValue(v);
}

ParamData::~ParamData()
{
	if(section) delete[] section;
	if(paramName) delete[] paramName;
	if(paramValue) delete[] paramValue;

	section = 0;
	paramName = 0;
	paramValue = 0;
}

void ParamData::setName(const char * s, const char * n)
{
	section = new char[1+strlen(s)];
	strcpy(section, s);

	paramName = new char[1+strlen(n)];
	strcpy(paramName, n);
}

void ParamData::setValue(const char * v)
{
	paramValue = new char[1+strlen(v)];
	strcpy(paramValue, v);

	if( paramValue[0] ) validParam = true;
}

void ParamData::save(const char * filename)
{
	WritePrivateProfileString(
		section,     // section name
		paramName,   // key name
		paramValue,  // key value
		filename     // ini filename
	);
}


//////////////////////////////////////////////////////////////
// Implementation of the ParamSection class
//

ParamSection::ParamSection(const char * s)
{
	sectionName = new char[1+strlen(s)];
	strcpy(sectionName, s);
}

ParamSection::~ParamSection()
{
	if(sectionName) delete[] sectionName;
	sectionName = 0;

	vector<ParamData *>::iterator p;
	p = paramVector.begin();
	while( p != paramVector.end() )
	{
		delete (*p);
		p = paramVector.erase(p);
	}
}

void ParamSection::addParam(const char * n, const char * v)
{
	ParamData * pParamData = new ParamData(sectionName, n, v);
	paramVector.push_back(pParamData);
}


void ParamSection::setParam(const char * paramName, const char * value)
{
	int nChars = strlen(paramName);
	vector<ParamData *>::iterator p;
	p = paramVector.begin();
	while( p != paramVector.end() )
	{
		const char * s2 = (*p)->getParamName();
		if( strlen(s2) == nChars )
			if( !strnicmp(paramName, s2, nChars) )
			{
				(*p)->setValue(value);
				return;
			}
		p++;
	}

	// param name wasn't found, so add it as a new param
	addParam(paramName, value);
}


const char * ParamSection::getParamValue(const char * s1)
{
	int nChars = strlen(s1);
	vector<ParamData *>::iterator p;
	p = paramVector.begin();
	while( p != paramVector.end() )
	{
		const char * s2 = (*p)->getParamName();
		if( strlen(s2) == nChars )
			if( !strnicmp(s1, s2, nChars) )
				return (*p)->getStringValue();
		p++;
	}

	return NULL;
}

void ParamSection::save(const char * filename)
{
	vector<ParamData *>::iterator p;
	p = paramVector.begin();
	while( p != paramVector.end() )
	{
		(*p)->save(filename);
		p++;
	}
}


//////////////////////////////////////////////////////////////
// Implementation of the Params class
//

const char * Params::INI_FILENAME = "mavis.ini";

// initialize static member
int Params::instances = 0;


//////////////////
//  FUNCTION:   Constructor
//
Params::Params()
{
	// Params is a singleton
	if(instances)
		throw MavisErr( "Only one instance of Params is allowed." );
	else
		++instances;


	//////////////////////////////////
	// Use ini-file accessors to build dot-delimited name strings
	// for each key.
	//////////////////////////////////

	const int BUFSIZE = 5000;
	char buf[BUFSIZE];         // buffer for section names

	// This call returns the names in a buffer. Each name has a
	// null character, '\0', after it. The last name has two null
	// characters after it.
	GetPrivateProfileSectionNames(
		buf,
		BUFSIZE,
		INI_FILENAME
	);

	// If no section names are found (or the ini file isn't found),
	// the buffer will start with a null string.
	if( !buf[0] )
	{
		char msg[200];
		sprintf(msg, "Problem reading ini file %s.", INI_FILENAME );
		throw MavisErr(msg);
	}


	// For each section name, read in param names and values
	bool moreSections = true;
	char * pSectionName = buf;
	const int PARAMBUFSIZE = 100000;
	char paramBuf[PARAMBUFSIZE];
	do {
		//read all params for this section
		GetPrivateProfileSection(
			pSectionName,
			paramBuf,
			PARAMBUFSIZE,
			INI_FILENAME
		);

		ParamSection * pParamSection = new ParamSection(pSectionName);

		// For each parameter in the section, extract name and value
		char * pNVPair = paramBuf;
		bool moreNVPairs = true;
		pNVPair = paramBuf;
		do {
			char * pValue   = pNVPair;
			char * pName    = pNVPair;
			char * pComment = pNVPair;

			pValue   = strchr(pNVPair, '=');
			pComment = strchr(pNVPair, ';');
			pNVPair += strlen(pNVPair) + 1;     // first, find the next NVPair
			if(pComment && pComment < pNVPair)  // then, truncate this pair at the comment
				*pComment = '\0';
			if(pValue && pValue < pNVPair)
			{
				*pValue = '\0'; // replace '=' with '\0'
				++pValue;

				// trim blanks
				pName  = strtok(pName,  " \t");
				pValue = strtok(pValue, " \t");

				// Add the name/value pair to the section
				// (unless it's a comment line)
				if(pName[0] != '#')
					pParamSection->addParam(pName, pValue);
			}

			if( !pNVPair[0] ) moreNVPairs = false;
		} while(moreNVPairs);


		sectionVector.push_back(pParamSection);
		pSectionName += strlen(pSectionName) + 1;
		if( !pSectionName[0] ) moreSections = false;

	} while(moreSections);
}


//////////////////
//  FUNCTION:   Destructor
//
Params::~Params()
{
	// write params to ini file
	save();

	// free memory resources
	vector<ParamSection *>::iterator p;
	p = sectionVector.begin();
	while( p != sectionVector.end() )
	{
		delete (*p);
		p = sectionVector.erase(p);
	}

	--instances;
}


//////////////////
//  FUNCTION:   getStringValue
//
const char * Params::getStringValue(const char * s1, const char * n)
{
	int nChars = strlen(s1);
	vector<ParamSection *>::iterator p;
	p = sectionVector.begin();
	while( p != sectionVector.end() )
	{
		const char * s2 = (*p)->getSectionName();
		if( strlen(s2) == nChars )
			if( !strnicmp(s1, s2, nChars) )
				return (*p)->getParamValue(n);
		p++;
	}

	return NULL;
}


//////////////////
//  FUNCTION:   getFloatPtValue
//
double Params::getFloatPtValue(const char * s, const char * n, bool * pParamExists)
{
	const char * strValue = getStringValue(s, n);

	double floatPtVal = 0.0;
	if(strValue)
	{
		float tmp;
		sscanf(strValue, "%f", &tmp);
		floatPtVal = (double)tmp;
		*pParamExists = true;
	}
	else
		*pParamExists = false;

	return floatPtVal;
}


//////////////////
//  FUNCTION:   getIntValue
//
int Params::getIntValue(const char * s, const char * n, bool * pParamExists)
{
	const char * strValue = getStringValue(s, n);

	int intVal = 0;
	double val = getFloatPtValue(s, n, pParamExists);
	if(pParamExists) intVal = (int)val;

	return intVal;
}


//////////////////
//  FUNCTION:   setParam(string value)
//
void Params::setParam(const char * section, const char * paramName, const char * value)
{
	int nChars = strlen(section);
	vector<ParamSection *>::iterator p;
	p = sectionVector.begin();
	while( p != sectionVector.end() )
	{
		const char * s2 = (*p)->getSectionName();
		if( strlen(s2) == nChars )
			if( !strnicmp(section, s2, nChars) )
			{
				(*p)->setParam(paramName, value);
				return;
			}
		p++;
	}

	// section name wasn't found, so add it as a new section
	// then add the param to it
	ParamSection * pParamSection = new ParamSection(section);
	pParamSection->addParam(paramName, value);
	sectionVector.push_back(pParamSection);
}


//////////////////
//  FUNCTION:   setParam(floating-point value)
//
void Params::setParam(const char * section, const char * paramName, double value)
{
	char buf[100];
	sprintf(buf, "%.2f", value);
	setParam(section, paramName, (const char *)buf);
}


//////////////////
//  FUNCTION:   setParam(integer value)
//
void Params::setParam(const char * section, const char * paramName, int value)
{
	char buf[100];
	sprintf(buf, "%d", value);
	setParam(section, paramName, (const char *)buf);
}


//////////////////
//  FUNCTION:   save
//
void Params::save()
{
	vector<ParamSection *>::iterator p;
	p = sectionVector.begin();
	while( p != sectionVector.end() )
	{
		(*p)->save(INI_FILENAME);
		p++;
	}
}

///////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 
//
// By downloading, copying, installing or using the software you agree to this
// license. If you do not agree to this license, do not download, install, copy or
// use the software.
//
//
//                        Mavis License Agreement 
//
// Copyright (c) 2005, Robin Hewitt (http://www.robin-hewitt.com).
// Third party copyrights are property of their respective owners. 
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
// 
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
// 
// This software is provided "as is" and any express or implied warranties, including,
// but not limited to, the implied warranties of merchantability and fitness for a
// particular purpose are disclaimed. In no event shall the authors or contributors be
// liable for any direct, indirect, incidental, special, exemplary, or consequential
// damages (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused and on any
// theory of liability, whether in contract, strict liability, or tort (including
// negligence or otherwise) arising in any way out of the use of this software, even
// if advised of the possibility of such damage. 
///////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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