aimsdata  4.7.0
Neuroimaging data handling
fdfprocpar_g.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 /*
35  * lecture de fichiers FDF
36  */
37 #ifndef AIMS_IO_FDFPROCPAR_G_H
38 #define AIMS_IO_FDFPROCPAR_G_H
39 
44 
45 #include <aims/io/fdfutil.h>
46 #include <aims/io/fdfprocpar.h>
47 
48 #include <iostream>
49 #include <vector>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <sys/types.h>
53 #include <regex.h>
54 #include <dirent.h>
55 
56 #include <fstream>
57 #include <sstream>
58 #include <iterator>
59 #include <algorithm>
60 
61 namespace aims
62 {
63 
64  template< class T >
65  inline int FdfParType<T>::getFdfType() { return 1; }
66 
67  template< class T >
68  inline T FdfParType<T>::getFdfDefaultValue() { return 0; }
69 
70  template< class T >
71  inline char FdfParType<T>::getFdfSeparator() { return ' '; }
72 
73  template<>
74  inline int FdfParType<std::string>::getFdfType() { return 2; }
75 
76  template<>
77  inline std::string FdfParType<std::string>::getFdfDefaultValue() { return ""; }
78 
79  template<>
80  inline char FdfParType<std::string>::getFdfSeparator() { return '\n'; }
81 
82  template< class T >
83  std::vector<T> FdfProcPar::values(std::string param) {
84  uint start, end;
85  int count;
86  int fdftype;
87  std::vector<T> values;
88  T value;
89  std::string line;
90 
91  // Open file to read
92  std::ifstream file( _name.c_str(), std::ios::in | std::ios::binary );
93 
94  if (!file) {
96  }
97  fdftype = FdfParType<T>::getFdfType();
98 
99  // Search parameter in file
100  count = this->search(file, param, fdftype, 1);
101  char paramsep = FdfParType<T>::getFdfSeparator();
102 
103  for (int index = 0; index < count; index++) {
104 
105  // Read value
106  getline(file, line, paramsep);
107 
108  // Trim right
109  line.erase(line.find_last_not_of (" ") + 1);
110 
111  // Remove starting and ending double quotes
112  start = ( line.find( '"' ) == 0 ? 1 : 0 );
113  end = line.size() - 1;
114  if ( line.rfind( '"' ) == end ) {
115  end--;
116  }
117  line = line.substr( start, end - start + 1 );
118  try {
119  carto::stringTo(line, value);
120  values.push_back(value);
121  }
122  catch(...) {
123  }
124  }
125 
126  file.close();
127 
128  return values;
129  }
130 
131  template< class T >
132  T FdfProcPar::value(std::string param, T defaultvalue) {
133  std::vector<T> foundvalues = values<T>( param );
134 
135  if ( foundvalues.size() > 0 ) {
136  return foundvalues[0];
137  }
138  else {
139  return defaultvalue;
140  }
141  }
142 
143  template< class T >
144  T FdfProcPar::value(std::string param) {
145  T defaultvalue = FdfParType<T>::getFdfDefaultValue();
146  return value<T>(param, defaultvalue);
147  }
148 
149 }
150 
151 #endif
std::string search(std::ifstream &file, std::string param)
The class for EcatSino data write operation.
Definition: border.h:42
T value(std::string param)
Definition: fdfprocpar_g.h:144
static int getFdfType()
Definition: fdfprocpar_g.h:65
static char getFdfSeparator()
Definition: fdfprocpar_g.h:71
static void launchErrnoExcept(const std::string &filename="")
void stringTo(const std::string &value, T &result)
std::vector< T > values(std::string param)
Definition: fdfprocpar_g.h:83
unsigned int uint
static T getFdfDefaultValue()
Definition: fdfprocpar_g.h:68