From 4a3f18011dcb5051c20c8401d06a348c7bb5a67f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= Date: Thu, 9 Apr 2015 17:01:19 +0200 Subject: [PATCH] Add 'header' class, for generic ALTA header parsing. --- sources/core/common.cpp | 51 ++++++++++++++++++++++++++++++++++- sources/core/common.h | 34 +++++++++++++++++++++++ sources/core/data_storage.cpp | 45 +------------------------------ 3 files changed, 85 insertions(+), 45 deletions(-) diff --git a/sources/core/common.cpp b/sources/core/common.cpp index ae9b26c..1eed945 100644 --- a/sources/core/common.cpp +++ b/sources/core/common.cpp @@ -1,7 +1,7 @@ /* ALTA --- Analysis of Bidirectional Reflectance Distribution Functions Copyright (C) 2014 CNRS - Copyright (C) 2013, 2014 Inria + Copyright (C) 2013, 2014, 2015 Inria This file is part of ALTA. @@ -22,6 +22,9 @@ #include #endif +#include + + double norm(const vec& a) { double norm = 0.0 ; @@ -179,3 +182,49 @@ unsigned int timer::current_time() const return (unsigned int)_time.tv_sec; #endif } + + +header::header(std::istream& input) +{ + while(input.good()) + { + if (input.peek() == '#') + { + input.get(); // consume the hash sign + + std::string line; + std::getline(input, line); + std::stringstream linestream(line); + + // Lines starting with '# ' are real comments and we ignore them. + // Others are key/value associations that we want to use. + if (linestream.peek() != ' ') + { + std::string key, rest; + linestream >> key; + if (!key.empty()) + { + if (key == "ALTA") + { + std::string first, second; + linestream >> first >> second; + if (second == "HEADER") + { + if (first == "END") + break; + else + _kind = first; + } + } + else + { + getline(linestream, rest); + _alist[key] = rest; + } + } + } + } + // The first non-comment line terminates the header. + else break; + } +} diff --git a/sources/core/common.h b/sources/core/common.h index 9569d46..de58c3e 100644 --- a/sources/core/common.h +++ b/sources/core/common.h @@ -29,6 +29,9 @@ #include #include +#include +#include + #ifdef OLD /*! \brief A core implementation of a vector of double. * \ingroup core @@ -386,6 +389,37 @@ class timer unsigned int _elapsed; }; + +// Reading the ALTA header of data and function files. + + +// Key/value association list. +class header +{ + public: + //! \brief Read the ALTA header on INPUT. + header(std::istream &input); + + //! \brief Return the type of this header--i.e., the "FOO" in + // "#ALTA FOO HEADER". + const std::string& kind() const + { + return _kind; + } + + //! \brief Return the value associated with KEY in this header. + const std::string& operator[](const std::string& key) + { + return _alist[key]; + } + + protected: + std::string _kind; + std::map _alist; +}; + + + // I/O error handling in user interfaces. diff --git a/sources/core/data_storage.cpp b/sources/core/data_storage.cpp index 27b22f1..2a36bd5 100644 --- a/sources/core/data_storage.cpp +++ b/sources/core/data_storage.cpp @@ -19,48 +19,6 @@ # include #endif -// Key/value association list. -typedef std::map alist; - -// Read the ALTA header on INPUT and fill in RESULT as a list of key/value -// pairs. -static void read_header(std::istream &input, alist &result) -{ - while(input.good()) - { - if (input.peek() == '#') - { - input.get(); // consume the hash sign - - std::string line; - std::getline(input, line); - std::stringstream linestream(line); - - // Lines starting with '# ' are real comments and we ignore them. - // Others are key/value associations that we want to use. - if (linestream.peek() != ' ') - { - std::string key, rest; - linestream >> key; - if (!key.empty()) - { - getline(linestream, rest); - if (key == "ALTA" && rest == "END HEADER") - { - break; - } - else - { - result[key] = rest; - } - } - } - } - // The first non-comment line terminates the header. - else break; - } -} - void vertical_segment::load_data_from_text(std::istream& input, vertical_segment& result, const arguments& args) @@ -71,8 +29,7 @@ void vertical_segment::load_data_from_text(std::istream& input, result._nX = 0 ; result._nY = 0 ; std::vector vs ; int current_vs = 0 ; - alist header; - read_header(input, header); + header header(input); { std::stringstream dim(header["DIM"]); -- GitLab