diff --git a/src/avalanche/gistools/gridfile.C b/src/avalanche/gistools/gridfile.C index c4d9239666255fb829dea5e6576f54193c92c689..0ad5d3586c110c1eafa586ee34a393fdf64b3c9e 100644 --- a/src/avalanche/gistools/gridfile.C +++ b/src/avalanche/gistools/gridfile.C @@ -34,7 +34,7 @@ Author #include <sstream> #include <limits> #include <cmath> -#include <regex> +//#include <regex> // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -139,11 +139,11 @@ int gridfile::read(std::string filename) std::ifstream pFile (filename); if (!pFile.is_open()) { + if (debug) log() << "Cannot open file \"" << filename << "\"" << std::endl; return 0; } std::string line; - std::smatch match; bool readheader = true; @@ -152,54 +152,59 @@ int gridfile::read(std::string filename) { std::getline(pFile, line); - if (std::regex_match(line, match, std::regex("^ncols\\s*(\\S*)$"))) + if (line.find("ncols") != std::string::npos) { - this->ncols_ = std::stod(match[1]); + this->ncols_ = std::stod(line.substr(5)); } - else if (std::regex_match(line, match, std::regex("^nrows\\s*(\\S*)$"))) + else if (line.find("nrows") != std::string::npos) { - this->nrows_ = std::stod(match[1]); + this->nrows_ = std::stod(line.substr(5)); } - else if (std::regex_match(line, match, std::regex("^xllcorner\\s*(\\S*)$"))) + else if (line.find("xllcorner") != std::string::npos) { offsetFromCornerToCenter = true; - this->xllcenter_ = std::stod(match[1]); + this->xllcenter_ = std::stod(line.substr(9)); } - else if (std::regex_match(line, match, std::regex("^xllcenter\\s*(\\S*)$"))) + else if (line.find("xllcenter") != std::string::npos) { - this->xllcenter_ = std::stod(match[1]); + this->xllcenter_ = std::stod(line.substr(9)); } - else if (std::regex_match(line, match, std::regex("^yllcorner\\s*(\\S*)$"))) + else if (line.find("yllcorner") != std::string::npos) { offsetFromCornerToCenter = true; - this->yllcenter_ = std::stod(match[1]); + this->yllcenter_ = std::stod(line.substr(9)); } - else if (std::regex_match(line, match, std::regex("^yllcenter\\s*(\\S*)$"))) + else if (line.find("yllcenter") != std::string::npos) { - this->yllcenter_ = std::stod(match[1]); + this->yllcenter_ = std::stod(line.substr(9)); } - else if (std::regex_match(line, match, std::regex("^cellsize\\s*(\\S*)$"))) + else if (line.find("cellsize") != std::string::npos) { - this->dx_ = std::stod(match[1]); - this->dy_ = std::stod(match[1]); + this->dx_ = std::stod(line.substr(9)); + this->dy_ = std::stod(line.substr(9)); } - else if (std::regex_match(line, match, std::regex("^dx\\s*(\\S*)$"))) + else if (line.find("dx") != std::string::npos) { - this->dx_ = std::stod(match[1]); + this->dx_ = std::stod(line.substr(2)); } - else if (std::regex_match(line, match, std::regex("^dy\\s*(\\S*)$"))) + else if (line.find("dy") != std::string::npos) { - this->dy_ = std::stod(match[1]); + this->dy_ = std::stod(line.substr(2)); } - else if (std::regex_match(line, match, std::regex("^NODATA_value\\s*(\\S*)$"))) + else if (line.find("NODATA_value") != std::string::npos) { - this->NODATA_value_ = std::stod(match[1]); + this->NODATA_value_ = std::stod(line.substr(12)); readheader = false; } + else + { + if (debug) log() << "Skipping line " << line << std::endl; + } } if (this->ncols_ < 1 || this->nrows_ < 1) { + if (debug) log() << "Number of columns (" << this->ncols_ << ") or number of rows (" << this->nrows_ << ") invalid." << std::endl; return 0; } @@ -218,7 +223,6 @@ int gridfile::read(std::string filename) unsigned int i = 0, j = 0; - std::regex e("(\\S+)"); while(!pFile.eof() && i < this->nrows_) { if (debug) log() << "Reading line " << i << "/" << this->nrows_ << std::endl; @@ -226,6 +230,7 @@ int gridfile::read(std::string filename) std::getline(pFile, line); j = 0; #if 0 + std::regex e("(\\S+)"); // The following part is very flexible but slow while (std::regex_search (line, match, e) and j < this->ncols) { try