Click here to Skip to main content
13,295,472 members (32,297 online)
Click here to Skip to main content


20 bookmarked
Posted 23 Aug 2004

Very secure method to save and restore registry

, 23 Aug 2004
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_)

#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 {
	class CValsMap : public map<CCmdLineParser_String, CCmdLineParser_String> {};
	typedef CValsMap::const_iterator POSITION;
	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;

	CValsMap::const_iterator findKey(LPCTSTR sKey) const;
	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.


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


About the Author

Web Developer
France France
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 24 Aug 2004
Article Copyright 2004 by JOHN11
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid