LOOS  v2.3.2
SimpleMeta.hpp
1 // -------------------------------------------------
2 // Simple Metadata Handling
3 // -------------------------------------------------
4 
5 /*
6  This file is part of LOOS.
7 
8  LOOS (Lightweight Object-Oriented Structure library)
9  Copyright (c) 2009 Tod D. Romo, Alan Grossfield
10  Department of Biochemistry and Biophysics
11  School of Medicine & Dentistry, University of Rochester
12 
13  This package (LOOS) is free software: you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation under version 3 of the License.
16 
17  This package is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 
27 
28 
29 #if !defined(LOOS_SIMPLEMETA_HPP)
30 #define LOOS_SIMPLEMETA_HPP
31 
32 
33 #include <iostream>
34 #include <string>
35 #include <vector>
36 #include <stdexcept>
37 
38 #include <utils.hpp>
39 
40 namespace loos {
41 
42  namespace DensityTools {
43 
45 
51  class SimpleMeta {
52  public:
53 
54  typedef std::string value_type;
55  typedef std::vector<value_type> container_type;
56  typedef container_type::iterator iterator;
57  typedef container_type::const_iterator const_iterator;
58 
59  SimpleMeta() { }
60  SimpleMeta(const std::string& s) { data_.push_back(s); }
61  SimpleMeta(const std::vector<std::string>& v) : data_(v) { }
62 
64  container_type& data() { return(data_); }
65  const container_type& data() const { return(data_); }
66 
68  iterator begin() { return(data_.begin()); }
69  iterator end() { return(data_.end()); }
70  const_iterator begin() const { return(data_.begin()); }
71  const_iterator end() const { return(data_.end()); }
72  bool empty() const { return(data_.empty()); }
73  unsigned int size() const { return(data_.size()); }
74 
76  void clear() { data_.clear(); }
77 
79  void set(const std::string& s) { data_.clear(); data_.push_back(sanitizeString(s)); }
80 
82  void add(const std::string& s) { data_.push_back(sanitizeString(s)); }
83 
84  friend std::ostream& operator<<(std::ostream& os, const SimpleMeta& m) {
85  for (const_iterator i = m.data_.begin(); i != m.data_.end(); ++i)
86  os << "# " << *i << std::endl;
87  return(os);
88  }
89 
90  friend std::istream& operator>>(std::istream& is, SimpleMeta& m) {
91  std::string buf;
92 
93  m.data_.clear();
94  while (true) {
95  int c = is.peek();
96  if (c != '#')
97  break;
98  std::getline(is, buf);
99  if (is.fail() || is.eof())
100  throw(std::runtime_error("Error while reading metadata"));
101  m.data_.push_back(m.stripper(buf));
102  }
103  return(is);
104  }
105 
106 
107  private:
108 
109 
110  // Strip leading space (skipping meta-marker)
111  std::string stripper(const std::string& s) {
112  unsigned int i;
113  for (i=1; i < s.size() && s[i] == ' '; ++i) ;
114  return(s.substr(i, s.size()));
115  }
116 
117  private:
118  std::vector<std::string> data_;
119  };
120 
121 
122 
123 
124  };
125 
126 
127 };
128 
129 
130 
131 
132 #endif
void clear()
Clear all contained metadata.
Definition: SimpleMeta.hpp:76
container_type & data()
Direct access to stored container of data.
Definition: SimpleMeta.hpp:64
void add(const std::string &s)
Append metadata.
Definition: SimpleMeta.hpp:82
Namespace for most things not already encapsulated within a class.
Simple class for handling metadata.
Definition: SimpleMeta.hpp:51
void set(const std::string &s)
Set metadata to string (deletes existing metadata)
Definition: SimpleMeta.hpp:79
std::string sanitizeString(const std::string &s)
Removes internal newlines from string.
Definition: utils.cpp:378
iterator begin()
Allow STL-iteration.
Definition: SimpleMeta.hpp:68