/* Copyright 2005 Nicholas Bishop * * This file is part of SharpConstruct. * * SharpConstruct is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * SharpConstruct is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with SharpConstruct; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "Preferences.h" #include #include using namespace SharpConstruct; Preferences::~Preferences() {} Preferences& Preferences::Instance() { static Preferences instance; return instance; } std::string Preferences::Get( std::string name, std::string def ) { Map::iterator i = _settings.find( name ); if( i == _settings.end() ) return def; else return ( *i ).second; } void Preferences::Set( std::string name, std::string value ) { _settings[ name ] = value; std::string path = getenv( "HOME" ); path += "/.sharpconstruct/settings"; std::ofstream file( path.c_str(), std::ios::out ); for( Map::iterator i = _settings.begin(); i != _settings.end(); i++ ) file << ( *i ).first << " " << ( *i ).second << std::endl; } void Preferences::Set( std::string name, int value ) { std::ostringstream tmp; tmp << value; Set( name, tmp.str() ); } void Preferences::Set( std::string name, float value ) { std::ostringstream tmp; tmp << value; Set( name, tmp.str() ); } Preferences::Preferences() { std::string key, value, path = getenv( "HOME" ); path += "/.sharpconstruct"; std::ifstream file( ( path + "/settings" ).c_str(), std::ios::in ); if( file ) { while( !file.eof() ) { file >> key >> value; _settings[ key ] = value; } } else { std::string tmp = "mkdir -p "; tmp += path; system( tmp.c_str() ); } }