Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / MFC

Very secure method to save and restore registry

Rate me:
Please Sign up or sign in to vote.
4.18/5 (10 votes)
23 Aug 20047 min read 71K   988   20  
This article gives a very secure method to save and restore registry keys. It provides a ready to use tool in both command-line and UI modes.
// CmdLineParser.h: interface for the CCmdLineParser class.
// 
// Copyright (c) Pavel Antonov, 2002
//
// This code is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CMDLINEPARSER_H__BE51B7B0_4BC9_44F1_9B88_DF33BE4280DF__INCLUDED_)
#define AFX_CMDLINEPARSER_H__BE51B7B0_4BC9_44F1_9B88_DF33BE4280DF__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

typedef CString CCmdLineParser_String ;

#include <map>
using std::map;

// Example of strings
// /Key1 /Key2 -Key3:Val3 -Key4:"Val 4-with/spaces/and-delimeters" /Key5:Val5
// /Key:"" is equal to /Key: and is equal to /Key
// /Key is equal to -Key
// If getCaseSensitive is false (by default), all keys are made lowercase.

// Examples of use:
// CCmdLineParser parser(_T("/Key1 /Key2: -Key3:Val3 -Key4:\"Val 4-with/spaces/and-delimeters\" /Key5:Val5"));
// ASSERT(parser.HasKey("Key1") == true);
// ASSERT(parser.HasKey("Key10") == false);
// ASSERT(parser.HasVal("Key2") == false);
// parser.GetVal("Key1") -> []; (empty string)
// parser.GetVal("Key2") -> []; (empty string)
// parser.GetVal("Key3") -> [Val3];
// parser.GetVal("Key4") -> [Val 4-with/spaces/and-delimeters];
// CCmdLineParser::POSITION pos = parser.getFirst();	
// CString sKey, sVal;
// while(!parser.isLast(pos)) {
//		parser.getNext(pos, sKey, sVal);
//		printf("Key: [%s], Val: [%s]");
// }


class CCmdLineParser {
public:
	class CValsMap : public map<CCmdLineParser_String, CCmdLineParser_String> {};
	typedef CValsMap::const_iterator POSITION;
public:
	CCmdLineParser(LPCTSTR sCmdLine = NULL, bool bCaseSensitive = false);
	virtual ~CCmdLineParser();

	bool Parse(LPCTSTR sCmdLine);

	LPCTSTR getCmdLine() const { return m_sCmdLine; }

	void setCaseSensitive(bool bSensitive) { m_bCaseSensitive = bSensitive; }
	bool getCaseSensitive() const { return m_bCaseSensitive; }

	const CValsMap& getVals() const { return m_ValsMap; }

	// Start iterating through keys and values
	POSITION getFirst() const;	
	// Get next key-value pair, returns empty sKey if end reached
	POSITION getNext(POSITION& pos, CCmdLineParser_String& sKey, CCmdLineParser_String& sValue) const;
	// just helper ;)
	bool isLast(POSITION& pos) const;

	// TRUE if "Key" present in command line
	bool HasKey(LPCTSTR sKey) const;
	// Is "key" present in command line and have some value
	bool HasVal(LPCTSTR sKey) const;
	// Returns value if value was found or NULL otherwise
	LPCTSTR GetVal(LPCTSTR sKey) const;
	// Returns true if value was found
	bool GetVal(LPCTSTR sKey, CCmdLineParser_String& sValue) const;

private:
	CValsMap::const_iterator findKey(LPCTSTR sKey) const;
private:
	CCmdLineParser_String m_sCmdLine;
	CValsMap	m_ValsMap;
	bool m_bCaseSensitive;

	static const TCHAR m_sDelimeters[];
	static const TCHAR m_sValueSep[];
	static const TCHAR m_sQuotes[];
};

#endif // !defined(AFX_CMDLINEPARSER_H__BE51B7B0_4BC9_44F1_9B88_DF33BE4280DF__INCLUDED_)

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


Written By
Web Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions