1 /* Copyright 2009-2014 e-soul.org. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  *   1. Redistributions of source code must retain the above copyright notice, this list of conditions and
7  *      the following disclaimer.
8  *   2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
9  *      and the following disclaimer in the documentation and/or other materials provided with the
10  *      distribution.
11  *
12  * THIS SOFTWARE IS PROVIDED BY E-SOUL.ORG ``AS IS'' AND ANY EXPRESS OR IMPLIED
13  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
14  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL E-SOUL.ORG
15  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
19  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21  *
22  * The views and conclusions contained in the software and documentation are those of the authors and
23  * should not be interpreted as representing official policies, either expressed or implied, of e-soul.org.
24  */
25 
26 #include <unistd.h>
27 
28 #include <string>
29 #include <map>
30 #include <iostream>
31 #include <fstream>
32 #include <cstdlib>
33 
34 #include "Preferences.hh"
35 
36 using namespace std;
37 
Preferences()38 Preferences::Preferences()
39 {
40 }
41 
~Preferences()42 Preferences::~Preferences()
43 {
44 }
45 
load()46 void Preferences::load()
47 {
48 	char* home = getenv("HOME");
49 	string config(home);
50 	config += "/.portrac";
51 	ifstream configFile(config.c_str());
52 	if(!configFile.fail())
53 	{
54 		// file exists
55 		string line;
56 		while(!configFile.eof())
57 		{
58 			getline(configFile, line);
59 			processInputLine(line);
60 		}
61 		configFile.close();
62 	}
63 	else
64 	{
65 		// initialise default values
66 		preferencesMap[INDEX_PREF] = "http://www.freebsd.org/ports/INDEX-10.bz2";
67 		preferencesMap[CHECK_INTERVAL_PREF] = "3";
68 	}
69 }
70 
persist()71 void Preferences::persist()
72 {
73 	char* home = getenv("HOME");
74 	string config(home);
75 	config += "/.portrac";
76 	ofstream configFile(config.c_str(), ios::out | ios::trunc);
77 	map<string, string>::iterator i = preferencesMap.begin();
78 	while(i != preferencesMap.end())
79 	{
80 		configFile << i->first << '=' << i->second << endl;
81 		i++;
82 	}
83 	configFile.close();
84 }
85 
getPreference(string preference)86 string Preferences::getPreference(string preference)
87 {
88 	return preferencesMap[preference];
89 }
90 
setPreference(string preference,string value)91 void Preferences::setPreference(string preference, string value)
92 {
93 	preferencesMap[preference] = value;
94 }
95 
processInputLine(string line)96 void Preferences::processInputLine(string line)
97 {
98 	string::size_type loc = line.find('=');
99 	if(loc != string::npos)
100 	{
101 		string pref = line.substr(0, loc);
102 		string val = line.substr(loc + 1);
103 		if((pref.compare(INDEX_PREF) == 0)
104 			|| (pref.compare(CHECK_INTERVAL_PREF) == 0))
105 		{
106 			setPreference(pref, val);
107 		}
108 	}
109 }
110