Click here to Skip to main content
12,503,829 members (37,957 online)
Click here to Skip to main content

Stats

56.2K views
849 downloads
37 bookmarked
Posted

A STL based XML Config Tool

, 19 Jun 2005
Writing multiple enumerated configuration entries.
Source
Tool Classes
ParamIO
XmlConfig
XmlReadWrite
XmlReadWrite.cpp_
XmlReadWrite.dsp
XmlReadWrite.dsw
Makefile.win
StdAfx.o
XmlReadWrite.dev
#include "ParamIO.h"
#include <sstream>

void ParamIO::parseAccess(const char *str, std::vector<std::string> &access)
{
	access.clear();

   if(str == 0 || strlen(str) == 0)
   {
      return;
   }

	std::istringstream stream(str);

	char *char_str = new char[strlen(str)];

	while(stream.getline(char_str, strlen(str), ':'))
	{
		access.push_back(std::string(char_str));
	}

	delete[] char_str;
}

void ParamIO::readStream(std::istream &is)
{
	std::string str;

	// Copy file content to string
	char c;

	is.get(c);
	while(is.eof() == false)
	{
		str += c;
		is.get(c);
	}

	_treeBuilder.clear();
	_xml.setSubscriber(_treeBuilder);

	// Parse XML
	_xml.parse(str.c_str(), str.size());	
}

void ParamIO::writeStream(std::ostream &os) const
{
	_treeBuilder.print(os);
}

void ParamIO::readFile(const char *filename)
{
	std::ifstream fileIn;
	fileIn.open(filename);
	if(fileIn)
	{
		readStream(fileIn);
		fileIn.close();
	}
}

void ParamIO::writeFile(const char *filename) const
{
	std::ofstream os(filename);
	writeStream(os);
	os.close();
}

bool ParamIO::compare(const ParamIO &old, const char *str) const
{
	std::vector<std::string> strs;
   parseAccess(str, strs);

	return _treeBuilder.compare(old, strs);
}

bool ParamIO::extract(const char *str, ParamIO &subtree) const
{
	std::vector<std::string> strs;
	parseAccess(str, strs);
	if( strs.size() == 0 )
		return false;
	subtree.clear();
	return _treeBuilder.extractSubTree(strs, subtree);
}

bool ParamIO::erase(const char *str)
{
    std::vector<std::string> strs;
    parseAccess(str, strs);

   if(strs.size() == 0)
   {
       return false;
   }

   return _treeBuilder.eraseSubTree(strs);
}

int ParamIO::erase(const char *str, std::vector<std::string>& vArgCmp )
{
   std::vector<std::string> strs;
   parseAccess(str, strs);
   if(strs.size() == 0)
   {
       return false;
   }
   return _treeBuilder.eraseElement( strs, vArgCmp );
}

int ParamIO::removeAttributes( const char *str, std::vector<std::string>& vArgCmp, const std::string &AttributesToRemove )
{
	std::vector<std::string> strs;
	parseAccess(str, strs);
	if(strs.size() == 0)
		return false;
	return _treeBuilder.removeElementAttributes( strs, vArgCmp, AttributesToRemove );
}

int ParamIO::addAttributes( const char *str, std::vector<std::string>& vArgCmp, const std::string &AttributesToAdd )
{
	std::vector<std::string> strs;
	parseAccess(str, strs);
	if(strs.size() == 0)
		return false;
	return _treeBuilder.addElementAttributes( strs, vArgCmp, AttributesToAdd );
}

int ParamIO::addAttributeString( const char *str, std::vector<std::string>& vArgCmp, const char* pstrAttributesToSet )
{
	std::vector<std::string> strs;
	parseAccess(str, strs);
	if(strs.size() == 0)
		return false;
	return _treeBuilder.addAttributeString( strs, vArgCmp, pstrAttributesToSet );
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Tobias Wenig
Software Developer
Germany Germany
Studying and having a degree of medical engineering, but decided to work as a software developer writing medical applications for image processing.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 20 Jun 2005
Article Copyright 2004 by Tobias Wenig
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid