Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
raw/get_header.hh
1 // Copyright (C) 2010 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_IO_RAW_GET_HEADER_HH
27 # define MLN_IO_RAW_GET_HEADER_HH
28 
32 
33 # include <iostream>
34 # include <fstream>
35 
36 # include <mln/core/concept/image.hh>
37 # include <mln/core/routine/initialize.hh>
38 # include <mln/core/box_runstart_piter.hh>
39 # include <mln/core/pixel.hh>
40 # include <mln/data/memcpy_.hh>
41 # include <mln/util/array.hh>
42 
43 namespace mln
44 {
45 
46  namespace io
47  {
48 
49  namespace raw
50  {
51 
53  struct raw_header
54  {
55  unsigned dim;
56  std::string value_type;
58  };
59 
60 
62  raw_header get_header(const std::string& filename);
63 
64 
65 # ifndef MLN_INCLUDE_ONLY
66 
67 
68  raw_header get_header(const std::string& filename)
69  {
70  trace::entering("mln::io::raw::get_header");
71 
72  raw_header header;
73 
74  std::string info_filename = filename + ".info";
75 
76  std::ifstream info_file(info_filename.c_str());
77  if (! info_file)
78  {
79  std::cerr << "io::raw::get_header - Error: cannot open file '"
80  << filename << "'!"
81  << std::endl;
82  abort();
83  }
84 
85  std::string file_type;
86  info_file >> file_type;
87  if (file_type != "milena/raw")
88  {
89  std::cerr << "io::raw::load - Error: invalid file type. '"
90  << filename
91  << "' is NOT a valid milena/raw info file!"
92  << std::endl;
93  abort();
94  }
95 
96  char dev_null[30];
97 
98  // Dimension ?
99  // Reading line title "Dim: "
100  info_file.read(dev_null, 5);
101  info_file >> header.dim;
102 
103  // Size information - Skip it, useless.
104  header.size.resize(header.dim);
105  for (unsigned i = 0; i < header.dim; ++i)
106  info_file >> header.size[i];
107  // Skipping endline.
108  char c;
109  info_file.get(c);
110 
111 
112  // Value type name ?
113  // Reading line title "data type: "
114  info_file.read(dev_null, 11);
115  // WARNING: value type name limited to 255 characters...
116  char value_type[255];
117  info_file.getline(value_type, 255);
118  header.value_type = value_type;
119 
120  info_file.close();
121 
122  trace::exiting("mln::io::raw::get_header");
123  return header;
124  }
125 
126 
127 # endif // ! MLN_INCLUDE_ONLY
128 
129  } // end of namespace mln::io::raw
130 
131  } // end of namespace mln::io
132 
133 } // end of namespace mln
134 
135 #endif // ! MLN_IO_RAW_GET_HEADER_HH
136