Skip to content

Instantly share code, notes, and snippets.

@cocomice
Forked from xuhdev/input.txt
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save cocomice/f78ecd4b04970e952704 to your computer and use it in GitHub Desktop.

Select an option

Save cocomice/f78ecd4b04970e952704 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <sstream
#include <typeinfo>
#include <stdlib.h>
#include <map>
#include <fstream>
class Convert
{
public:
template <typename T>
static std::string T_to_string(T const &val)
{
std::ostringstream ostr;
ostr << val;
return ostr.str();
}
template <typename T>
static T string_to_T(std::string const &val)
{
std::istringstream istr(val);
T returnVal;
if (!(istr >> returnVal))
exitWithError("CFG: Not a valid " + (std::string)typeid(T).name() + " received!\n");
return returnVal;
}
template <>
static std::string string_to_T(std::string const &val)
{
return val;
}
};
void exitWithError(const std::string &error)
{
std::cout << error;
std::cin.ignore();
std::cin.get();
exit(EXIT_FAILURE);
}
class ConfigFile
{
private:
std::map<std::string, std::string> contents;
std::string fName;
void removeComment(std::string &line) const
{
if (line.find('#') != line.npos)
line.erase(line.find('#'));
}
bool onlyWhitespace(const std::string &line) const
{
return (line.find_first_not_of(' ') == line.npos);
}
bool validLine(const std::string &line) const
{
std::string temp = line;
temp.erase(0, temp.find_first_not_of("\t "));
if (temp[0] == '=')
return false;
for (size_t i = temp.find('=') + 1; i < temp.length(); i++)
if (temp[i] != ' ')
return true;
return false;
}
void extractKey(std::string &key, size_t const &sepPos, const std::string &line) const
{
key = line.substr(0, sepPos);
if (key.find('\t') != line.npos || key.find(' ') != line.npos)
key.erase(key.find_first_of("\t "));
}
void extractValue(std::string &value, size_t const &sepPos, const std::string &line) const
{
value = line.substr(sepPos + 1);
value.erase(0, value.find_first_not_of("\t "));
value.erase(value.find_last_not_of("\t ") + 1);
}
void extractContents(const std::string &line)
{
std::string temp = line;
temp.erase(0, temp.find_first_not_of("\t "));
size_t sepPos = temp.find('=');
std::string key, value;
extractKey(key, sepPos, temp);
extractValue(value, sepPos, temp);
if (!keyExists(key))
contents.insert(std::pair<std::string, std::string>(key, value));
else
exitWithError("CFG: Can only have unique key names!\n");
}
void parseLine(const std::string &line, size_t const lineNo)
{
if (line.find('=') == line.npos)
exitWithError("CFG: Couldn't find separator on line: " + Convert::T_to_string(lineNo) + "\n");
if (!validLine(line))
exitWithError("CFG: Bad format for line: " + Convert::T_to_string(lineNo) + "\n");
extractContents(line);
}
void ExtractKeys()
{
std::ifstream file;
file.open(fName.c_str());
if (!file)
exitWithError("CFG: File " + fName + " couldn't be found!\n");
std::string line;
size_t lineNo = 0;
while (std::getline(file, line))
{
lineNo++;
std::string temp = line;
if (temp.empty())
continue;
removeComment(temp);
if (onlyWhitespace(temp))
continue;
parseLine(temp, lineNo);
}
file.close();
}
public:
ConfigFile(const std::string &fName)
{
this->fName = fName;
ExtractKeys();
}
bool keyExists(const std::string &key) const
{
return contents.find(key) != contents.end();
}
template <typename ValueType>
ValueType getValueOfKey(const std::string &key, ValueType const &defaultValue = ValueType()) const
{
if (!keyExists(key))
return defaultValue;
return Convert::string_to_T<ValueType>(contents.find(key)->second);
}
};
int main()
{
ConfigFile cfg("config.cfg");
bool exists = cfg.keyExists("car");
std::cout << "car key: " << std::boolalpha << exists << "\n";
exists = cfg.keyExists("fruits");
std::cout << "fruits key: " << exists << "\n";
std::string someValue = cfg.getValueOfKey<std::string>("mykey", "Unknown");
std::cout << "value of key mykey: " << someValue << "\n";
std::string carValue = cfg.getValueOfKey<std::string>("car");
std::cout << "value of key car: " << carValue << "\n";
double doubleVal = cfg.getValueOfKey<double>("double");
std::cout << "value of key double: " << doubleVal << "\n\n";
std::cin.get();
return 0;
}
void Read_Matrix(string filename, double output[], int no_rows, int no_cols)
{
int linenum = 0 ;
int maxSize = 5000 ;
int idx=0 ;
int i, cols ;
FILE * myfile ;
myfile = fopen(filename.c_str(), "r") ;
if (myfile==NULL){
perror("Error opening file");
}else{
char buffer [maxSize];
while ( linenum != no_rows )
{
fgets(buffer, maxSize, myfile) ;
char *pEnd;
char *testbuffer = (char *)malloc(sizeof(char)*maxSize);
for (i=0; i <maxSize; i++) testbuffer[i] = buffer[i];
for (cols =0;cols<no_cols;cols++) // use nDays not nvars, since now they are different
{
idx = no_cols*linenum + cols;
output[idx] = strtod(testbuffer, &pEnd);
testbuffer = pEnd;
}
linenum++;
}
}
fclose(myfile);
}
#include <istream>
#include <string>
#include <sstream>
#include <vector>
// load matrix from an ascii text file.
vector< vector<double> > File_Read_Matrix( string filename )
{
const string& delim = " \t" ;
vector< vector<double> > matrix ;
ifstream fin (filename) ;
string line;
string strnum;
// clear first
matrix.clear();
// parse line by line
if (!fin.is_open()){
cout << "file could not be opened !" <<endl;
}
while (getline(fin, line))
{
if(line.at(0)=='#') continue ;
matrix.push_back(vector<double>());
for (string:: const_iterator i = line.begin(); i != line.end(); ++ i)
{
// If it is not a delim, then append it to strnum
if (delim.find(*i) == string::npos)
{
strnum += *i;
if( i+1 != line.end() ) continue ;
}
// if strnum is still empty, it means the previous char is also a
// delim (several delims appear together). Ignore this char.
if (strnum.empty()) continue ;
// If we reach here, we got a number. Convert it to double.
double number;
istringstream(strnum) >> number;
matrix.back().push_back(number);
strnum.clear();
}
}
fin.close() ;
return matrix ;
}
void Read_Array(string filename, double output[])
{
char num[100] ;
int i=0 ;
FILE * myfile ;
myfile = fopen(filename.c_str(), "r") ;
if (myfile==NULL){
perror("Error opening file");
}else{
while( fgets(num, 100, myfile) ){
output[i] = strtod(num, NULL) ;
i++ ;
}
}
fclose(myfile) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment