1 #include "ConfigFile.h"
2 
ConfigFile(boost::filesystem::path filename,std::string delimiter,std::string comment,std::string sentry)3 ConfigFile::ConfigFile(boost::filesystem::path filename, std::string delimiter,
4 		std::string comment, std::string sentry) :
5 	myDelimiter(delimiter), myComment(comment), mySentry(sentry) {
6 	// Construct a ConfigFile, getting keys and values from given file
7 
8 	boost::filesystem::ifstream in(filename);
9 
10 	if (!in)
11 		throw file_not_found(filename);
12 
13 	in >> (*this);
14 }
15 
ConfigFile()16 ConfigFile::ConfigFile() :
17 	myDelimiter(std::string(1, '=')), myComment(std::string(1, '#')) {
18 	// Construct a ConfigFile without a file; empty
19 }
20 
remove(const std::string & key)21 void ConfigFile::remove(const std::string& key) {
22 	// Remove key and its value
23 	myContents.erase(myContents.find(key));
24 	return;
25 }
26 
keyExists(const std::string & key) const27 bool ConfigFile::keyExists(const std::string& key) const {
28 	// Indicate whether key is found
29 	mapci p = myContents.find(key);
30 	return (p != myContents.end());
31 }
32 
33 /* static */
trim(std::string & s)34 void ConfigFile::trim(std::string& s) {
35 	// Remove leading and trailing whitespace
36 	static const char whitespace[] = " \n\t\v\r\f";
37 	s.erase(0, s.find_first_not_of(whitespace));
38 	s.erase(s.find_last_not_of(whitespace) + 1U);
39 }
40 
operator <<(std::ostream & os,const ConfigFile & cf)41 std::ostream& operator<<(std::ostream& os, const ConfigFile& cf) {
42 	// Save a ConfigFile to os
43 	for (ConfigFile::mapci p = cf.myContents.begin(); p != cf.myContents.end(); ++p) {
44 		os << p->first << " " << cf.myDelimiter << " ";
45 		os << p->second << std::endl;
46 	}
47 	return os;
48 }
49 
operator >>(std::istream & is,ConfigFile & cf)50 std::istream& operator>>(std::istream& is, ConfigFile& cf) {
51 	// Load a ConfigFile from is
52 	// Read in keys and values, keeping internal whitespace
53 	typedef std::string::size_type pos;
54 	const std::string& delim = cf.myDelimiter; // separator
55 	const std::string& comm = cf.myComment; // comment
56 	const std::string& sentry = cf.mySentry; // end of file sentry
57 	const pos skip = delim.length(); // length of separator
58 
59 	std::string nextline = ""; // might need to read ahead to see where value ends
60 
61 	while (is || nextline.length() > 0) {
62 		// Read an entire line at a time
63 		std::string line;
64 		if (nextline.length() > 0) {
65 			line = nextline; // we read ahead; use it now
66 			nextline = "";
67 		} else {
68 			std::getline(is, line);
69 		}
70 
71 		// Ignore comments
72 		line = line.substr(0, line.find(comm));
73 
74 		// Check for end of file sentry
75 		if (sentry != "" && line.find(sentry) != std::string::npos)
76 			return is;
77 
78 		// Parse the line if it contains a delimiter
79 		pos delimPos = line.find(delim);
80 		if (delimPos < std::string::npos) {
81 			// Extract the key
82 			std::string key = line.substr(0, delimPos);
83 			line.replace(0, delimPos + skip, "");
84 
85 			// See if value continues on the next line
86 			// Stop at blank line, next line with a key, end of stream,
87 			// or end of file sentry
88 			bool terminate = false;
89 			while (!terminate && is) {
90 				std::getline(is, nextline);
91 				terminate = true;
92 
93 				std::string nlcopy = nextline;
94 				ConfigFile::trim(nlcopy);
95 				if (nlcopy == "")
96 					continue;
97 
98 				nextline = nextline.substr(0, nextline.find(comm));
99 				if (nextline.find(delim) != std::string::npos)
100 					continue;
101 				if (sentry != "" && nextline.find(sentry) != std::string::npos)
102 					continue;
103 
104 				nlcopy = nextline;
105 				ConfigFile::trim(nlcopy);
106 				if (nlcopy != "")
107 					line += "\n";
108 				line += nextline;
109 				terminate = false;
110 			}
111 
112 			// Store key and value
113 			ConfigFile::trim(key);
114 			ConfigFile::trim(line);
115 			cf.myContents[key] = line; // overwrites if key is repeated
116 		}
117 	}
118 
119 	return is;
120 }
121