Commit 4a3f1801 authored by Ludovic Courtès's avatar Ludovic Courtès

Add 'header' class, for generic ALTA header parsing.

parent 53118958
/* 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 <unistd.h>
#endif
#include <iostream>
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;
}
}
......@@ -29,6 +29,9 @@
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#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<std::string, std::string> _alist;
};
// I/O error handling in user interfaces.
......
......@@ -19,48 +19,6 @@
# include <endian.h>
#endif
// Key/value association list.
typedef std::map<std::string, std::string> 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<int> vs ; int current_vs = 0 ;
alist header;
read_header(input, header);
header header(input);
{
std::stringstream dim(header["DIM"]);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment