WSL/SLF GitLab Repository

Commit 1c182be7 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Trying to close some potential security flaws (using FlawFinder). All files...

Trying to close some potential security flaws (using FlawFinder). All files opened for reading are now checked and special files (devices, etc) are refused.
parent 7e3ac39a
......@@ -76,7 +76,7 @@ std::string cleanPath(std::string in_path, const bool& resolve)
#else //POSIX
std::replace(in_path.begin(), in_path.end(), '\\', '/');
char *real_path = realpath(in_path.c_str(), NULL); //POSIX
char *real_path = realpath(in_path.c_str(), NULL); //POSIX 2008
if(real_path!=NULL) {
const std::string tmp(real_path);
free(real_path);
......@@ -170,7 +170,13 @@ std::string getCWD()
bool fileExists(const std::string& filename)
{
return ( GetFileAttributes( filename.c_str() ) != INVALID_FILE_ATTRIBUTES );
const DWORD attributes = GetFileAttributes( filename.c_str() );
if (attributes==INVALID_FILE_ATTRIBUTES || attributes==FILE_ATTRIBUTE_VIRTUAL
|| attributes==FILE_ATTRIBUTE_DIRECTORY || attributes==FILE_ATTRIBUTE_DEVICE)
return false;
return true;
}
void readDirectory(const std::string& path, std::list<std::string>& dirlist, const std::string& pattern)
......@@ -222,11 +228,14 @@ bool fileExists(const std::string& filename)
{
struct stat buffer ;
if ((stat( filename.c_str(), &buffer))==0) {//File exists if stat returns 0
return true ;
if ((stat( filename.c_str(), &buffer))!=0) {//File exists if stat returns 0
return false;
}
return false;
if (S_ISREG(buffer.st_mode) || S_ISFIFO(buffer.st_mode) || S_ISLNK(buffer.st_mode))
return true;
else
return false; //exclude char device, block device, sockets, etc
}
void readDirectory(const std::string& path, std::list<std::string>& dirlist, const std::string& pattern)
......
......@@ -16,6 +16,7 @@
along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
*/
#include <meteoio/IOUtils.h>
#include <meteoio/IOHandler.h>
#cmakedefine PLUGIN_ALPUG
......@@ -400,8 +401,8 @@ void IOHandler::create_exclude_map()
const std::string path = IOUtils::getPath(prefix+exclude_file, true); //clean & resolve path
const std::string filename = path + "/" + IOUtils::getFilename(exclude_file);
std::ifstream fin; //Input file streams
fin.open(filename.c_str(), std::ifstream::in);
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
std::ifstream fin(filename.c_str(), std::ifstream::in);
if (fin.fail()) throw FileAccessException(filename, AT);
try {
......@@ -463,8 +464,8 @@ void IOHandler::create_keep_map()
const std::string path = IOUtils::getPath(prefix+keep_file, true); //clean & resolve path
const std::string filename = path + "/" + IOUtils::getFilename(keep_file);
std::ifstream fin; //Input file streams
fin.open(filename.c_str(), std::ifstream::in);
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
std::ifstream fin(filename.c_str(), std::ifstream::in);
if (fin.fail()) throw FileAccessException(filename, AT);
try {
......
......@@ -204,7 +204,8 @@ std::string ProcessingBlock::getName() const {
void ProcessingBlock::readCorrections(const std::string& filter, const std::string& filename, const char& c_type, const double& init, std::vector<double> &corrections)
{
std::ifstream fin(filename.c_str());
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
std::ifstream fin(filename.c_str(), std::ifstream::in);
if (fin.fail()) {
std::ostringstream ss;
ss << "Filter " << filter << ": ";
......
......@@ -198,8 +198,8 @@ void A3DIO::read1DStation(StationData& sd)
double xcoord=IOUtils::nodata, ycoord=IOUtils::nodata, altitude=IOUtils::nodata;
std::map<std::string, std::string> header; // A map to save key value pairs of the file header
std::ifstream fin;
fin.open (meteo1d.c_str(), std::ifstream::in);
if (!IOUtils::fileExists(meteo1d)) throw FileAccessException(meteo1d, AT); //prevent invalid filenames
std::ifstream fin(meteo1d.c_str(), std::ifstream::in);
if (fin.fail()) {
throw FileAccessException(meteo1d, AT);
}
......@@ -247,8 +247,8 @@ void A3DIO::read1DMeteo(const Date& dateStart, const Date& dateEnd, std::vector<
throw FileNotFoundException(meteo1d, AT);
}
std::ifstream fin;
fin.open (meteo1d.c_str(), std::ifstream::in);
if (!IOUtils::fileExists(meteo1d)) throw FileAccessException(meteo1d, AT); //prevent invalid filenames
std::ifstream fin(meteo1d.c_str(), std::ifstream::in);
if (fin.fail()) {
throw FileAccessException(meteo1d,AT);
}
......@@ -491,8 +491,8 @@ size_t A3DIO::getNrOfStations(std::vector<std::string>& filenames, std::map<std:
for (size_t ii=0; ii<filenames.size(); ii++) {
const std::string filename = filenames[ii];
std::ifstream fin;
fin.open (filename.c_str(), std::ifstream::in);
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
std::ifstream fin(filename.c_str(), std::ifstream::in);
if (fin.fail()) throw FileAccessException(filename, AT);
const char eoln = IOUtils::getEoln(fin); //get the end of line character for the file
......@@ -519,8 +519,8 @@ void A3DIO::read2DMeteoData(const std::string& filename, const std::string& para
std::map<std::string,size_t>& hashStations,
std::vector< std::vector<MeteoData> >& vecM, size_t& bufferindex)
{
std::ifstream fin;
fin.open (filename.c_str(), std::ifstream::in);
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
std::ifstream fin(filename.c_str(), std::ifstream::in);
if (fin.fail()) {
throw FileAccessException(filename, AT);
}
......@@ -614,8 +614,8 @@ void A3DIO::read2DMeteoData(const std::string& filename, const std::string& para
void A3DIO::read2DMeteoHeader(const std::string& filename, std::map<std::string,size_t>& hashStations,
std::vector<StationData>& vecS)
{
std::ifstream fin;
fin.open (filename.c_str(), std::ifstream::in);
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
std::ifstream fin(filename.c_str(), std::ifstream::in);
if (fin.fail()) {
throw FileAccessException(filename, AT);
}
......@@ -686,8 +686,8 @@ void A3DIO::readPOI(std::vector<Coords>& pts)
throw FileNotFoundException(filename, AT);
}
std::ifstream fin;
fin.open (filename.c_str(), std::ifstream::in);
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
std::ifstream fin(filename.c_str(), std::ifstream::in);
if (fin.fail()) {
throw FileAccessException(filename,AT);
}
......@@ -878,8 +878,7 @@ int A3DIO::write2DmeteoFile(const std::vector< std::vector<MeteoData> >& data,
void A3DIO::write2DMeteo(const std::vector< std::vector<MeteoData> >& data)
{
std::string tmp_path;
cfg.getValue("METEOPATH", "Output", tmp_path);
std::string tmp_path = cfg.get("METEOPATH", "Output");
write2DmeteoFile(data, mio::MeteoData::TA, tmp_path+"/tair", "air temperature");
write2DmeteoFile(data, mio::MeteoData::RH, tmp_path+"/rhum", "relative humidity");
......
......@@ -104,8 +104,7 @@ void ALPUG::parseInputOutputSection()
IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
//Parse input section: extract number of files to read and store filenames in vecFiles
std::string in_meteo;
cfg.getValue("METEO", "Input", in_meteo, IOUtils::nothrow);
std::string in_meteo = cfg.get("METEO", "Input", IOUtils::nothrow);
if (in_meteo == "ALPUG") { //keep it synchronized with IOHandler.cc for plugin mapping!!
cfg.getValue("METEOPATH", "Input", inpath);
vecIDs.clear();
......@@ -113,8 +112,7 @@ void ALPUG::parseInputOutputSection()
readMetaData();
cfg.getValue("WRAP_MONTH", "Input", wrap_month, IOUtils::nothrow);
string fields;
cfg.getValue("ALPUG_FIELDS", "Input", fields);
const string fields = cfg.get("ALPUG_FIELDS", "Input");
IOUtils::readLineToVec(fields, vecFields, ',');
if (vecFields.empty())
throw InvalidArgumentException("Please provide a comma delimited list of fields!", AT);
......@@ -136,8 +134,8 @@ void ALPUG::readMetaData()
const string filename = cfg.get("METAFILE", "Input");
const string metafile = inpath + "/" + filename;
std::ifstream fin;
fin.open (metafile.c_str(), std::ifstream::in);
if (!IOUtils::fileExists(metafile)) throw FileAccessException(metafile, AT); //prevent invalid filenames
std::ifstream fin(metafile.c_str(), std::ifstream::in);
if (fin.fail()) {
ostringstream ss;
ss << "File \'" << metafile << "\' could not be opened. Possible reason: " << strerror(errno) << "\n";
......@@ -341,10 +339,9 @@ void ALPUG::readMetoFile(const size_t& station_index, const Date& dateStart, con
if (std::find(dirlist.begin(), dirlist.end(), filename) == dirlist.end()) //this file does not exist
continue;
std::ifstream fin; //Input file streams
fin.clear();
const string file_and_path = inpath + "/" + filename;
fin.open (file_and_path.c_str(), ios::in|ios::binary); //ascii does end of line translation, which messes up the pointer code
if (!IOUtils::fileExists(file_and_path)) throw FileAccessException(file_and_path, AT); //prevent invalid filenames
std::ifstream fin(file_and_path.c_str(), ios::in|ios::binary); //ascii does end of line translation, which messes up the pointer code
if (fin.fail())
throw FileAccessException("Could not open \'" + file_and_path +"\'. Possible reason: " + strerror(errno) + "\n", AT);
......
......@@ -130,8 +130,7 @@ ARCIO::ARCIO(const Config& cfgreader)
void ARCIO::getGridPaths() {
grid2dpath_in.clear(), grid2dpath_out.clear();
string tmp;
cfg.getValue("GRID2D", "Input", tmp, IOUtils::nothrow);
string tmp = cfg.get("GRID2D", "Input", IOUtils::nothrow);
if (tmp == "ARC") //keep it synchronized with IOHandler.cc for plugin mapping!!
cfg.getValue("GRID2DPATH", "Input", grid2dpath_in);
tmp.clear();
......@@ -186,7 +185,7 @@ void ARCIO::read2DGrid_internal(Grid2DObject& grid_out, const std::string& full_
throw FileAccessException(ss.str(), AT);
}
char eoln = IOUtils::getEoln(fin); //get the end of line character for the file
const char eoln = IOUtils::getEoln(fin); //get the end of line character for the file
//Go through file, save key value pairs
try {
......@@ -287,22 +286,19 @@ void ARCIO::read2DGrid(Grid2DObject& grid_out, const MeteoGrids::Parameters& par
void ARCIO::readDEM(DEMObject& dem_out)
{
string filename;
cfg.getValue("DEMFILE", "Input", filename);
const string filename = cfg.get("DEMFILE", "Input");
read2DGrid_internal(dem_out, filename);
}
void ARCIO::readLanduse(Grid2DObject& landuse_out)
{
string filename;
cfg.getValue("LANDUSEFILE", "Input", filename);
const string filename = cfg.get("LANDUSEFILE", "Input");
read2DGrid_internal(landuse_out, filename);
}
void ARCIO::readAssimilationData(const Date& date_in, Grid2DObject& da_out)
{
string filepath;
cfg.getValue("DAPATH", "Input", filepath);
const string filepath = cfg.get("DAPATH", "Input");
string dateStr( date_in.toString(Date::NUM) );
dateStr.erase( dateStr.size()-2, string::npos); //remove the seconds
......@@ -336,7 +332,8 @@ void ARCIO::readPOI(std::vector<Coords>&)
void ARCIO::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
{
std::string full_name = grid2dpath_out+"/"+name;
const std::string full_name = grid2dpath_out+"/"+name;
if (!IOUtils::fileExists(full_name)) throw FileAccessException(full_name, AT); //prevent invalid filenames
fout.open(full_name.c_str());
if (fout.fail()) {
ostringstream ss;
......
......@@ -410,6 +410,7 @@ void ARPSIO::openGridFile(const std::string& in_filename)
unsigned int v1;
filename = in_filename;
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
if((fin=fopen(filename.c_str(),"r")) == NULL) {
cleanup();
throw FileAccessException("Can not open file "+filename, AT);
......
......@@ -197,6 +197,7 @@ void CNRMIO::read2DGrid_internal(Grid2DObject& grid_out, const std::string& file
vector<string> dimname;
vector<size_t> dimlen;
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
ncpp::open_file(filename, NC_NOWRITE, ncid);
ncpp::get_variable(ncid, varname, varid);
ncpp::get_dimension(ncid, varname, varid, dimid, dim_varid, dimname, dimlen);
......@@ -285,6 +286,7 @@ void CNRMIO::readStationData(const Date&, std::vector<StationData>& vecStation)
const string filename = cfg.get("METEOFILE", "Input");
int ncid;
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
ncpp::open_file(filename, NC_NOWRITE, ncid);
readMetaData(ncid, vecMetaData);
ncpp::close_file(filename, ncid);
......@@ -346,6 +348,7 @@ void CNRMIO::readMeteoData(const Date& dateStart, const Date& dateEnd, std::vect
const string filename = cfg.get("METEOFILE", "Input");
int ncid;
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
ncpp::open_file(filename, NC_NOWRITE, ncid);
if (vecMetaData.empty()) readMetaData(ncid, vecMetaData);
......
......@@ -243,7 +243,9 @@ void CosmoXMLIO::openIn_XML(const std::string& in_meteofile)
xmlInitParser();
xmlKeepBlanksDefault(0);
if(in_encoding==XML_CHAR_ENCODING_NONE) {
if (!IOUtils::fileExists(in_meteofile)) throw FileAccessException(in_meteofile, AT); //prevent invalid filenames
if (in_encoding==XML_CHAR_ENCODING_NONE) {
in_doc = xmlParseFile(in_meteofile.c_str());
} else {
xmlParserCtxtPtr ctxt = xmlCreateFileParserCtxt( in_meteofile.c_str() );
......@@ -256,14 +258,14 @@ void CosmoXMLIO::openIn_XML(const std::string& in_meteofile)
throw FileNotFoundException("Could not open/parse file \""+in_meteofile+"\"", AT);
}
if(in_xpathCtx != NULL) xmlXPathFreeContext(in_xpathCtx); //free variable if this was not freed before
if (in_xpathCtx != NULL) xmlXPathFreeContext(in_xpathCtx); //free variable if this was not freed before
in_xpathCtx = xmlXPathNewContext(in_doc);
if(in_xpathCtx == NULL) {
if (in_xpathCtx == NULL) {
closeIn_XML();
throw IOException("Unable to create new XPath context", AT);
}
if(xmlXPathRegisterNs(in_xpathCtx, xml_namespace_abrev, xml_namespace) != 0) {
if (xmlXPathRegisterNs(in_xpathCtx, xml_namespace_abrev, xml_namespace) != 0) {
throw IOException("Unable to register namespace with prefix", AT);
}
}
......@@ -319,14 +321,14 @@ bool CosmoXMLIO::parseStationData(const std::string& station_id, const xmlXPathC
const std::string xpath = StationData_xpath+"[@id='station_abbreviation' and text()='"+xpath_id+"']/.."; //and we take the parent node <row>
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression((const xmlChar*)xpath.c_str(), xpathCtx);
if(xpathObj == NULL) return false;
if (xpathObj == NULL) return false;
//check the number of matches
const xmlNodeSetPtr &metadata = xpathObj->nodesetval;
const int nr_metadata = (metadata) ? metadata->nodeNr : 0;
if(nr_metadata==0)
if (nr_metadata==0)
throw NoAvailableDataException("No metadata found for station \""+station_id+"\"", AT);
if(nr_metadata>1)
if (nr_metadata>1)
throw InvalidFormatException("Multiple definition of metadata for station \""+station_id+"\"", AT);
//collect all the data fields
......@@ -427,21 +429,21 @@ CosmoXMLIO::MeteoReadStatus CosmoXMLIO::parseMeteoDataPoint(const Date& dateStar
size_t CosmoXMLIO::getFileIdx(const Date& start_date) const
{
if(cache_meteo_files.empty())
if (cache_meteo_files.empty())
throw InvalidArgumentException("No input files found or configured!", AT);
//find which file we should open
if(cache_meteo_files.size()==1) {
if (cache_meteo_files.size()==1) {
return 0;
} else {
for(size_t idx=1; idx<cache_meteo_files.size(); idx++) {
if(start_date>=cache_meteo_files[idx-1].first && start_date<cache_meteo_files[idx].first) {
for (size_t idx=1; idx<cache_meteo_files.size(); idx++) {
if (start_date>=cache_meteo_files[idx-1].first && start_date<cache_meteo_files[idx].first) {
return idx--;
}
}
//not found, we take the closest timestamp we have
if(start_date<cache_meteo_files.front().first)
if (start_date<cache_meteo_files.front().first)
return 0;
else
return cache_meteo_files.size()-1;
......@@ -473,21 +475,21 @@ bool CosmoXMLIO::parseMeteoData(const Date& dateStart, const Date& dateEnd, cons
const std::string xpath = MeteoData_xpath+"[@id='identifier' and text()='"+station_id+"']";
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression((const xmlChar*)xpath.c_str(), xpathCtx);
if(xpathObj == NULL) return false;
if (xpathObj == NULL) return false;
//check the number of matches
const xmlNodeSetPtr &data = xpathObj->nodesetval;
const int nr_data = (data) ? data->nodeNr : 0;
if(nr_data==0)
if (nr_data==0)
throw NoAvailableDataException("No data found for station \""+station_id+"\"", AT);
//loop over all data for this station_id
for(int ii=0; ii<nr_data; ii++) {
for (int ii=0; ii<nr_data; ii++) {
MeteoData md( Date(), sd);
const MeteoReadStatus status = parseMeteoDataPoint(dateStart, dateEnd, data->nodeTab[ii], md);
if(status==read_stop) break;
if(status==read_ok) vecMeteo.push_back( md );
if (status==read_stop) break;
if (status==read_ok) vecMeteo.push_back( md );
}
xmlXPathFreeObject(xpathObj);
......@@ -513,7 +515,7 @@ void CosmoXMLIO::readMeteoData(const Date& dateStart, const Date& dateEnd,
//read all the stations' metadata
std::vector<StationData> vecStation;
for(size_t ii=0; ii<input_id.size(); ii++) {
for (size_t ii=0; ii<input_id.size(); ii++) {
StationData sd;
if(!parseStationData(input_id[ii], in_xpathCtx, sd)) {
closeIn_XML();
......@@ -523,27 +525,27 @@ void CosmoXMLIO::readMeteoData(const Date& dateStart, const Date& dateEnd,
}
//read all the stations' data
for(size_t ii=0; ii<input_id.size(); ii++) {
for (size_t ii=0; ii<input_id.size(); ii++) {
const string station_id = xml_stations_id[ input_id[ii] ];
//do we already have a vector with this station meteo?
size_t found_id = IOUtils::npos;
for(size_t jj=0; jj<vecMeteo.size(); jj++) {
if(vecMeteo[jj].front().meta.stationID==input_id[ii]) {
for (size_t jj=0; jj<vecMeteo.size(); jj++) {
if (vecMeteo[jj].front().meta.stationID==input_id[ii]) {
found_id = jj;
break;
}
}
if(found_id==IOUtils::npos) { //creating the station
if (found_id==IOUtils::npos) { //creating the station
vector<MeteoData> vecTmp;
if(!parseMeteoData(dateStart, nextDate, station_id, vecStation[ii], in_xpathCtx, vecTmp)) {
if (!parseMeteoData(dateStart, nextDate, station_id, vecStation[ii], in_xpathCtx, vecTmp)) {
closeIn_XML();
throw IOException("Unable to evaluate xpath expression for station \""+input_id[ii]+"\"", AT);
}
vecMeteo.push_back(vecTmp);
} else { //appending to the station
if(!parseMeteoData(dateStart, nextDate, station_id, vecStation[ii], in_xpathCtx, vecMeteo[found_id])) {
if (!parseMeteoData(dateStart, nextDate, station_id, vecStation[ii], in_xpathCtx, vecMeteo[found_id])) {
closeIn_XML();
throw IOException("Unable to evaluate xpath expression for station \""+input_id[ii]+"\"", AT);
}
......
......@@ -434,6 +434,7 @@ bool GRIBIO::read2DGrid_indexed(const double& in_marsParam, const long& i_levelT
void GRIBIO::read2DGrid(Grid2DObject& grid_out, const std::string& i_name)
{
const std::string filename = grid2dpath_in+"/"+i_name;
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
fp = fopen(filename.c_str(),"r");
if(fp==NULL) {
ostringstream ss;
......@@ -461,6 +462,7 @@ void GRIBIO::read2DGrid(Grid2DObject& grid_out, const std::string& i_name)
void GRIBIO::indexFile(const std::string& filename)
{
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
fp = fopen(filename.c_str(),"r");
if(fp==NULL) {
ostringstream ss;
......@@ -665,9 +667,7 @@ void GRIBIO::read2DGrid(const std::string& filename, Grid2DObject& grid_out, con
void GRIBIO::readDEM(DEMObject& dem_out)
{
const Date d; //ie: undef. This will be caught when reading the GRIB file
std::string filename;
cfg.getValue("DEMFILE", "Input", filename);
const std::string filename = cfg.get("DEMFILE", "Input");
read2DGrid(filename, dem_out, MeteoGrids::DEM, d);
if(update_dem) {
dem_out.update();
......
......@@ -108,9 +108,11 @@ void NetCDFIO::parseInputOutputSection()
cfg.getValue("TIME_ZONE", "Output", out_dflt_TZ, IOUtils::nothrow);
cfg.getValue("DEM_FROM_PRESSURE", "Input", dem_altimeter, IOUtils::nothrow);
const string in_schema = IOUtils::strToUpper( cfg.get("NETCDF_SCHEMA", "Input", IOUtils::nothrow) );
string in_schema = "ECMWF";
in_schema = IOUtils::strToUpper( cfg.get("NETCDF_SCHEMA", "Input", IOUtils::nothrow) );
initAttributesMap(in_schema, in_attributes);
const string out_schema = IOUtils::strToUpper( cfg.get("NETCDF_SCHEMA", "Output", IOUtils::nothrow) );
string out_schema = "ECMWF";
out_schema = IOUtils::strToUpper( cfg.get("NETCDF_SCHEMA", "Output", IOUtils::nothrow) );
initAttributesMap(out_schema, out_attributes);
}
......@@ -353,6 +355,7 @@ bool NetCDFIO::read2DGrid_internal(Grid2DObject& grid_out, const std::string& fi
vector<string> dimname;
vector<size_t> dimlen;
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
ncpp::open_file(filename, NC_NOWRITE, ncid);
if (!ncpp::check_variable(ncid, varname)) return false;
ncpp::get_variable(ncid, varname, varid);
......
......@@ -244,7 +244,7 @@ void PGMIO::readPOI(std::vector<Coords>&)
void PGMIO::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
{
std::string full_name = grid2dpath_out+"/"+name;
const std::string full_name = grid2dpath_out+"/"+name;
const unsigned int nr_colors = 256;
fout.open(full_name.c_str());
if (fout.fail()) {
......@@ -263,9 +263,7 @@ void PGMIO::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
try {
const double min_value = grid_in.grid2D.getMin();
const double max_value = grid_in.grid2D.getMax();
double scaling;
if(min_value!=max_value) scaling = 1./(max_value - min_value) * (double)(nr_colors-1); //so we keep color 0 for nodata
else scaling = 1.;
const double scaling = (min_value==max_value)? 1. : 1./(max_value - min_value) * (double)(nr_colors-1); //so we keep color 0 for nodata
//writing the header
fout << "P2\n";
......
......@@ -188,8 +188,7 @@ void PSQLIO::getParameters(const Config& cfg)
cfg.getValue("STATIONS", "Input", stations, IOUtils::nothrow);
IOUtils::readLineToVec(stations, vecFixedStationID, ',');
string exclude_file;
cfg.getValue("EXCLUDE", "Input", exclude_file, IOUtils::nothrow);
const string exclude_file = cfg.get("EXCLUDE", "Input", IOUtils::nothrow);
if (!exclude_file.empty() && IOUtils::fileExists(exclude_file)) {
//if this is a relative path, prefix the path with the current path
const std::string prefix = ( IOUtils::isAbsolutePath(exclude_file) )? "" : cfg.getConfigRootDir()+"/";
......@@ -204,8 +203,8 @@ void PSQLIO::getParameters(const Config& cfg)
void PSQLIO::create_shadow_map(const std::string& exclude_file)
{
std::ifstream fin; //Input file streams
fin.open(exclude_file.c_str(), std::ifstream::in);
if (!IOUtils::fileExists(exclude_file)) throw FileAccessException(exclude_file, AT); //prevent invalid filenames
std::ifstream fin(exclude_file.c_str(), std::ifstream::in);
if (fin.fail()) throw FileAccessException(exclude_file, AT);
try {
......@@ -384,7 +383,6 @@ void PSQLIO::readData(const Date& dateStart, const Date& dateEnd, std::vector<Me
{
string sql_query(sql_data);
string id = vecFixedStationID.at(stationindex);
string date_start = dateStart.toString(Date::ISO);
string date_end = dateEnd.toString(Date::ISO);
std::replace(date_start.begin(), date_start.end(), 'T', ' ');
......@@ -396,8 +394,8 @@ void PSQLIO::readData(const Date& dateStart, const Date& dateEnd, std::vector<Me
PGresult *result = sql_exec(sql_query);
if (result) {
int rows = PQntuples(result);
int columns = PQnfields(result);
const int rows = PQntuples(result);
const int columns = PQnfields(result);
vector<size_t> index;
MeteoData tmpmeteo;
......@@ -509,7 +507,7 @@ void PSQLIO::checkForUsedParameters(const std::vector<MeteoData>& vecMeteo, std:
* If a parameter is in use, then vecParamInUse[index_of_parameter] is set to true and the column
* name is set in vecColumnName[index_of_parameter]
*/
size_t nr_of_parameters = vecMeteo[0].getNrOfParameters();
const size_t nr_of_parameters = vecMeteo[0].getNrOfParameters();
vecParamInUse.resize(nr_of_parameters, false);
vecColumnName.resize(nr_of_parameters, "NULL");
......@@ -540,7 +538,7 @@ void PSQLIO::add_meta_data(const unsigned int& index, const StationData& sd)
{
//Adding a new station to the table FIXED_STATION
string stationName = (sd.stationName != "" ? sd.stationName : sd.stationID);
const string stationName = (sd.stationName != "" ? sd.stationName : sd.stationID);
stringstream values;
values << "(" << index << ","
......@@ -550,8 +548,7 @@ void PSQLIO::add_meta_data(const unsigned int& index, const StationData& sd)
<< sd.position.getAltitude() << ","
<< sd.position.getEPSG() << ")";
string query = "INSERT INTO FIXED_STATION (ID_FIXED_STATION,STATION_NAME,COORD_X,COORD_Y,ALTITUDE, EPSG) VALUES " + values.str() + ";";
//cout << "EXEC: " << query << endl;
const string query = "INSERT INTO FIXED_STATION (ID_FIXED_STATION,STATION_NAME,COORD_X,COORD_Y,ALTITUDE, EPSG) VALUES " + values.str() + ";";
sql_exec(query, false);
}
......@@ -560,18 +557,18 @@ int PSQLIO::get_sensor_index()
//Get first id of new sensors
int sensor_index = 1;
string query = "SELECT max(id_fixed_sensor) from fixed_sensor;";
const string query = "SELECT max(id_fixed_sensor) from fixed_sensor;";
PGresult *result = sql_exec(query, false);
if (result) {
int rows = PQntuples(result);
int columns = PQnfields(result);
const int rows = PQntuples(result);
const int columns = PQnfields(result);
if (rows != 1 || columns != 1) {
throw IOException("ERROR", AT);
}
string val( PQgetvalue(result, 0, 0) );
const string val( PQgetvalue(result, 0, 0) );
IOUtils::convertString(sensor_index, val);