1//
2// This file is part of the aMule Project.
3//
4// Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5//
6// Any parts of this program derived from the xMule, lMule or eMule project,
7// or contributed by third-party developers are copyrighted by their
8// respective authors.
9//
10// This program is free software; you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation; either version 2 of the License, or
13// (at your option) any later version.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
23//
24
25#ifndef	ECFILECONFIG_H
26#define	ECFILECONFIG_H
27
28#include <wx/fileconf.h>
29#include <wx/filename.h>
30#include <wx/utils.h>
31
32#include "MD4Hash.h"		// Needed for CMD4Hash
33#include "OtherFunctions.h"	// Needed for GetConfigDir()
34
35
36/**
37 * Prepends ConfigDir to filename, if it has no PathSeparator chars in it
38 */
39inline wxString FinalizeFilename(const wxString filename)
40{
41	if (wxStrchr(filename, wxFileName::GetPathSeparator()) == NULL) {
42		return GetConfigDir() + filename;
43	}
44	if ((filename.GetChar(0) == '~') && (filename.GetChar(1) == wxFileName::GetPathSeparator())) {
45		return wxGetHomeDir() + filename.Mid(1);
46	}
47	return filename;
48}
49
50
51/**
52 * Extension to wxFileConfig for reading/writing CMD4Hash values.
53 *
54 * This class converts between the text and binary representation of an MD4 hash,
55 * and also maps empty strings to the empty hash and vica versa.
56 */
57class CECFileConfig : public wxFileConfig {
58	public:
59
60		CECFileConfig(const wxString& localFilename = wxEmptyString)
61			: wxFileConfig(wxEmptyString, wxEmptyString, FinalizeFilename(localFilename),
62				wxEmptyString, wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_RELATIVE_PATH)
63			{}
64
65		/**
66		 * Reads a hash from the config file
67		 *
68		 * @param key	the key to be read
69		 * @param hash	the CMD4Hash object to write the hash to
70		 *
71		 * @return true on success, false otherwise.
72		 */
73		bool	ReadHash(const wxString& key, CMD4Hash *hash)
74		{
75			wxString sHash;
76			bool retval = wxFileConfig::Read(key, &sHash, wxEmptyString);
77			if (sHash.IsEmpty()) {
78				hash->Clear();
79			} else {
80				hash->Decode(sHash);
81			}
82			return retval;
83		}
84
85		/**
86		 * Writes a CMD4Hash object to the config file
87		 *
88		 * @param key	the key to write to
89		 * @param hash	the hash to be written
90		 *
91		 * @return true on success, false otherwise.
92		 */
93		bool	WriteHash(const wxString& key, const CMD4Hash& hash)
94		{
95			return wxFileConfig::Write(key, hash.IsEmpty() ? wxString(wxEmptyString) : hash.Encode());
96		}
97};
98
99#endif /* ECFILECONFIG_H */
100// File_checked_for_headers
101