EZC3D
Parameter.cpp
Go to the documentation of this file.
1 #define EZC3D_API_EXPORTS
2 
10 #include "Parameter.h"
11 #include "Parameters.h"
12 
13 ezc3d::ParametersNS::GroupNS::Parameter::Parameter(const std::string &name, const std::string &description) :
14  _name(name),
15  _description(description),
16  _isLocked(false),
17  _data_type(ezc3d::DATA_TYPE::NO_DATA_TYPE)
18 {
19 
20 }
21 
23 {
24  std::cout << "parameterName = " << name() << std::endl;
25  std::cout << "isLocked = " << isLocked() << std::endl;
26 
27  // Data are not separated according to _dimension, which could help to read
28  if (_data_type == DATA_TYPE::CHAR)
29  for (unsigned int i = 0; i < _param_data_string.size(); ++i)
30  std::cout << "param_data_string[" << i << "] = " << _param_data_string[i] << std::endl;
31  if (_data_type == DATA_TYPE::BYTE)
32  for (unsigned int i = 0; i < _param_data_int.size(); ++i)
33  std::cout << "param_data[" << i << "] = " << _param_data_int[i] << std::endl;
34  if (_data_type == DATA_TYPE::INT)
35  for (unsigned int i = 0; i < _param_data_int.size(); ++i)
36  std::cout << "param_data[" << i << "] = " << _param_data_int[i] << std::endl;
37  if (_data_type == DATA_TYPE::FLOAT)
38  for (unsigned int i = 0; i < _param_data_float.size(); ++i)
39  std::cout << "param_data[" << i << "] = " << _param_data_float[i] << std::endl;
40 
41  std::cout << "description = " << _description << std::endl;
42 }
43 
44 void ezc3d::ParametersNS::GroupNS::Parameter::write(std::fstream &f, int groupIdx, std::streampos &dataStartPosition) const
45 {
46  int nCharName(static_cast<int>(name().size()));
47  if (isLocked())
48  nCharName *= -1;
49  f.write(reinterpret_cast<const char*>(&nCharName), 1*ezc3d::DATA_TYPE::BYTE);
50  if (isLocked())
51  nCharName *= -1;
52  f.write(reinterpret_cast<const char*>(&groupIdx), 1*ezc3d::DATA_TYPE::BYTE);
53  f.write(ezc3d::toUpper(name()).c_str(), nCharName*ezc3d::DATA_TYPE::BYTE);
54 
55  // It is not possible already to know in how many bytes the next parameter is
56  int blank(0);
57  std::streampos pos(f.tellg());
58  f.write(reinterpret_cast<const char*>(&blank), 2*ezc3d::DATA_TYPE::BYTE);
59 
60  // Write the parameter values
61  f.write(reinterpret_cast<const char*>(&_data_type), 1*ezc3d::DATA_TYPE::BYTE);
62  size_t size_dim(_dimension.size());
63  // If it is a scalar, store it as so
64  if (_dimension.size() == 1 && _dimension[0] == 1 && _data_type != DATA_TYPE::CHAR){
65  int _size_dim(0);
66  f.write(reinterpret_cast<const char*>(&_size_dim), 1*ezc3d::DATA_TYPE::BYTE);
67  }
68  else{
69  f.write(reinterpret_cast<const char*>(&size_dim), 1*ezc3d::DATA_TYPE::BYTE);
70  for (unsigned int i=0; i<_dimension.size(); ++i)
71  f.write(reinterpret_cast<const char*>(&_dimension[i]), 1*ezc3d::DATA_TYPE::BYTE);
72  }
73 
74  int hasSize(0);
75  if (_dimension.size() > 0){
76  hasSize = 1;
77  for (unsigned int i=0; i<_dimension.size(); ++i)
78  hasSize *= _dimension[i];
79  }
80  if (hasSize > 0){
81  if (_data_type == DATA_TYPE::CHAR){
82  if (_dimension.size() == 1){
83  f.write(_param_data_string[0].c_str(), static_cast<int>(_param_data_string[0].size())*static_cast<int>(DATA_TYPE::BYTE));
84  } else {
85  writeImbricatedParameter(f, _dimension, 1);
86  }
87  } else {
88  if (!_name.compare("DATA_START")){
89  // This is a special case defined in the standard where you write the number of blocks up to the data
90  dataStartPosition = f.tellg();
91  f.write(reinterpret_cast<const char*>(&blank), 2*ezc3d::DATA_TYPE::BYTE);
92  } else
93  writeImbricatedParameter(f, _dimension);
94  }
95  }
96 
97  // Write description of the parameter
98  int nCharDescription(static_cast<int>(description().size()));
99  f.write(reinterpret_cast<const char*>(&nCharDescription), 1*ezc3d::DATA_TYPE::BYTE);
100  f.write(description().c_str(), nCharDescription*ezc3d::DATA_TYPE::BYTE);
101 
102  // Go back at the left blank space and write the current position
103  std::streampos currentPos(f.tellg());
104  f.seekg(pos);
105  int nCharToNext = int(currentPos - pos);
106  f.write(reinterpret_cast<const char*>(&nCharToNext), 2*ezc3d::DATA_TYPE::BYTE);
107  f.seekg(currentPos);
108 }
109 
110 size_t ezc3d::ParametersNS::GroupNS::Parameter::writeImbricatedParameter(std::fstream &f, const std::vector<size_t>& dim, size_t currentIdx, size_t cmp) const{
111  for (size_t i=0; i<dim[currentIdx]; ++i)
112  if (currentIdx == dim.size()-1){
113  if (_data_type == DATA_TYPE::BYTE)
114  f.write(reinterpret_cast<const char*>(&(_param_data_int[cmp])), static_cast<int>(_data_type));
115  else if (_data_type == DATA_TYPE::INT)
116  f.write(reinterpret_cast<const char*>(&(_param_data_int[cmp])), static_cast<int>(_data_type));
117  else if (_data_type == DATA_TYPE::FLOAT)
118  f.write(reinterpret_cast<const char*>(&(_param_data_float[cmp])), static_cast<int>(_data_type));
119  else if (_data_type == DATA_TYPE::CHAR){
120  f.write(_param_data_string[cmp].c_str(), static_cast<int>(_param_data_string[cmp].size())*static_cast<int>(DATA_TYPE::BYTE));
121  const char buffer = ' ';
122  for (size_t j=_param_data_string[cmp].size(); j<_dimension[0]; ++j)
123  f.write(&buffer, static_cast<int>(DATA_TYPE::BYTE));
124  }
125  ++cmp;
126  }
127  else
128  cmp = writeImbricatedParameter(f, dim, currentIdx + 1, cmp);
129  return cmp;
130 }
131 
132 int ezc3d::ParametersNS::GroupNS::Parameter::read(ezc3d::c3d &c3d, const Parameters &params, std::fstream &file, int nbCharInName)
133 {
134  if (nbCharInName < 0)
135  _isLocked = true;
136  else
137  _isLocked = false;
138 
139  // Read name of the group
140  _name = c3d.readString(file, static_cast<unsigned int>(abs(nbCharInName) * ezc3d::DATA_TYPE::BYTE));
141 
142  // number of byte to the next group from here
143  int offsetNext(static_cast<int>(c3d.readUint(params.processorType(), file, 2*ezc3d::DATA_TYPE::BYTE)));
144  // Compute the position of the element in the file
145  int nextParamByteInFile;
146  if (offsetNext == 0)
147  nextParamByteInFile = 0;
148  else
149  nextParamByteInFile = static_cast<int>(file.tellg()) + offsetNext - ezc3d::DATA_TYPE::WORD;
150 
151  // -1 sizeof(char), 1 byte, 2 int, 4 float
152  int lengthInByte(c3d.readInt(params.processorType(), file, 1*ezc3d::DATA_TYPE::BYTE));
153  if (lengthInByte == -1)
154  _data_type = DATA_TYPE::CHAR;
155  else if (lengthInByte == 1)
156  _data_type = DATA_TYPE::BYTE;
157  else if (lengthInByte == 2)
158  _data_type = DATA_TYPE::INT;
159  else if (lengthInByte == 4)
160  _data_type = DATA_TYPE::FLOAT;
161  else
162  throw std::ios_base::failure ("Parameter type unrecognized");
163 
164  // number of dimension of parameter (0 for scalar)
165  int nDimensions(c3d.readInt(params.processorType(), file, 1*ezc3d::DATA_TYPE::BYTE));
166  if (nDimensions == 0 && _data_type != DATA_TYPE::CHAR) // In the special case of a scalar
167  _dimension.push_back(1);
168  else // otherwise it's a matrix
169  for (int i=0; i<nDimensions; ++i)
170  _dimension.push_back (c3d.readUint(params.processorType(), file, 1*ezc3d::DATA_TYPE::BYTE)); // Read the dimension size of the matrix
171 
172  // Read the data for the parameters
173  if (_data_type == DATA_TYPE::CHAR)
174  c3d.readParam(file, _dimension, _param_data_string);
175  else if (_data_type == DATA_TYPE::BYTE)
176  c3d.readParam(params.processorType(), file, static_cast<unsigned int>(_data_type), _dimension, _param_data_int);
177  else if (_data_type == DATA_TYPE::INT)
178  c3d.readParam(params.processorType(), file, static_cast<unsigned int>(_data_type), _dimension, _param_data_int);
179  else if (_data_type == DATA_TYPE::FLOAT)
180  c3d.readParam(params.processorType(), file, _dimension, _param_data_float);
181 
182 
183  // Byte 5+nbCharInName ==> Number of characters in group description
184  int nbCharInDesc(c3d.readInt(params.processorType(), file, 1*ezc3d::DATA_TYPE::BYTE));
185  // Byte 6+nbCharInName ==> Group description
186  if (nbCharInDesc)
187  _description = c3d.readString(file, static_cast<unsigned int>(nbCharInDesc));
188 
189  // Return how many bytes
190  return nextParamByteInFile;
191 }
192 
194 {
195  return _name;
196 }
197 
198 void ezc3d::ParametersNS::GroupNS::Parameter::name(const std::string paramName)
199 {
200  _name = paramName;
201 }
202 
204 {
205  return _description;
206 }
207 
208 void ezc3d::ParametersNS::GroupNS::Parameter::description(const std::string description)
209 {
210  _description = description;
211 }
212 
214 {
215  return _isLocked;
216 }
217 
219 {
220  _isLocked = true;
221 }
222 
224 {
225  _isLocked = false;
226 }
227 
228 const std::vector<size_t> ezc3d::ParametersNS::GroupNS::Parameter::dimension() const
229 {
230  return _dimension;
231 }
232 
233 bool ezc3d::ParametersNS::GroupNS::Parameter::isDimensionConsistent(size_t dataSize, const std::vector<size_t> &dimension) const {
234  if (dataSize == 0){
235  int dim(1);
236  for (unsigned int i=0; i<dimension.size(); ++i)
237  dim *= dimension[i];
238  if (dimension.size() == 0 || dim == 0)
239  return true;
240  else
241  return false;
242  }
243 
244  size_t dimesionSize(1);
245  for (unsigned int i=0; i<dimension.size(); ++i)
246  dimesionSize *= dimension[i];
247  if (dataSize == dimesionSize)
248  return true;
249  else
250  return false;
251 }
252 
254 {
255  return _data_type;
256 }
257 
259 {
260  set(std::vector<int>()={data});
261 }
262 
264 {
265  set(static_cast<int>(data));
266 }
267 
268 void ezc3d::ParametersNS::GroupNS::Parameter::set(const std::vector<int> &data, const std::vector<size_t> &dimension)
269 {
270  std::vector<size_t> dimensionCopy;
271  if (dimension.size() == 0){
272  dimensionCopy.push_back(data.size());
273  } else {
274  dimensionCopy = dimension;
275  }
276  if (!isDimensionConsistent(data.size(), dimensionCopy))
277  throw std::range_error("Dimension of the data does not correspond to sent dimensions");
278  _data_type = ezc3d::DATA_TYPE::INT;
279  _param_data_int = data;
280  _dimension = dimensionCopy;
281 }
282 
284 {
285  set(std::vector<float>()={data});
286 }
287 
289 {
290  set(std::vector<float>()={static_cast<float>(data)});
291 }
292 
293 void ezc3d::ParametersNS::GroupNS::Parameter::set(const std::vector<float> &data, const std::vector<size_t> &dimension)
294 {
295  std::vector<size_t> dimensionCopy;
296  if (dimension.size() == 0){
297  dimensionCopy.push_back(data.size());
298  } else {
299  dimensionCopy = dimension;
300  }
301  if (!isDimensionConsistent(data.size(), dimensionCopy))
302  throw std::range_error("Dimension of the data does not correspond to sent dimensions");
303  _data_type = ezc3d::DATA_TYPE::FLOAT;
304  _param_data_float = data;
305  _dimension = dimensionCopy;
306 }
307 
308 void ezc3d::ParametersNS::GroupNS::Parameter::set(const std::string& data)
309 {
310  set(std::vector<std::string>() = {data});
311 }
312 
313 void ezc3d::ParametersNS::GroupNS::Parameter::set(const std::vector<std::string> &data, const std::vector<size_t> &dimension)
314 {
315  std::vector<size_t> dimensionCopy;
316  if (dimension.size() == 0){
317  dimensionCopy.push_back(data.size());
318  } else {
319  dimensionCopy = dimension;
320  }
321  if (!isDimensionConsistent(data.size(), dimensionCopy))
322  throw std::range_error("Dimension of the data does not correspond to sent dimensions");
323  // Insert the length of the longest string
324  size_t first_dim(0);
325  for (unsigned int i=0; i<data.size(); ++i)
326  if (data[i].size() > first_dim)
327  first_dim = data[i].size();
328  std::vector<size_t> dimensionWithStrLen = dimensionCopy;
329  dimensionWithStrLen.insert(dimensionWithStrLen.begin(), first_dim);
330 
331  _data_type = ezc3d::DATA_TYPE::CHAR;
332  _param_data_string = data;
333  _dimension = dimensionWithStrLen;
334 }
335 
337 {
338  if (_data_type != DATA_TYPE::BYTE)
339  throw std::invalid_argument("This parameter is not a BYTE");
340  return _param_data_int;
341 }
342 
344 {
345  if (_data_type != DATA_TYPE::INT)
346  throw std::invalid_argument("This parameter is not an INT");
347  return _param_data_int;
348 }
349 
351 {
352  if (_data_type != DATA_TYPE::FLOAT)
353  throw std::invalid_argument("This parameter is not a FLOAT");
354  return _param_data_float;
355 }
356 
357 const std::vector<std::string>& ezc3d::ParametersNS::GroupNS::Parameter::valuesAsString() const
358 {
359  if (_data_type != DATA_TYPE::CHAR)
360  throw std::invalid_argument("This parameter is not string");
361 
362  return _param_data_string;
363 }
364 
365 
366 
void set(int data)
Set the integer scalar value for the parameter.
Definition: Parameter.cpp:258
const std::vector< int > & valuesAsInt() const
Return the vector of values of the parameter.
Definition: Parameter.cpp:343
bool isDimensionConsistent(size_t dataSize, const std::vector< size_t > &dimension) const
Check if the dimension parameter is consistent with the values.
Definition: Parameter.cpp:233
Group holder of C3D parameters.
Definition: Parameters.h:16
size_t readUint(PROCESSOR_TYPE processorType, std::fstream &file, unsigned int nByteToRead, int nByteFromPrevious=0, const std::ios_base::seekdir &pos=std::ios::cur)
Read a unsigned integer of nByteToRead bytes at the position current + nByteFromPrevious from a file...
Definition: ezc3d.cpp:203
void print() const
Print the parameter and its values.
Definition: Parameter.cpp:22
void unlock()
Set the locking status of the parameter to false.
Definition: Parameter.cpp:223
ezc3d::DATA_TYPE type() const
Return the type of the data.
Definition: Parameter.cpp:253
void readParam(PROCESSOR_TYPE processorType, std::fstream &file, unsigned int dataLenghtInBytes, const std::vector< size_t > &dimension, std::vector< int > &param_data, size_t currentIdx=0)
Read a matrix of integer parameters of dimensions dimension with each integer of length dataLengthInB...
Definition: ezc3d.cpp:269
Declaration of Parameter class.
const std::vector< size_t > dimension() const
Return the vector mapping the dimension of the parameter matrix.
Definition: Parameter.cpp:228
PROCESSOR_TYPE processorType() const
Get the processor type the file was writen on.
Definition: Parameters.cpp:294
Namespace ezc3d.
Definition: ezc3d.h:68
Parameter(const std::string &name="", const std::string &description="")
Create an empty parameter.
Definition: Parameter.cpp:13
const std::string & description() const
Get the description of the parameter.
Definition: Parameter.cpp:203
const std::string & name() const
Get the name of the parameter.
Definition: Parameter.cpp:193
size_t writeImbricatedParameter(std::fstream &f, const std::vector< size_t > &dim, size_t currentIdx=0, size_t cmp=0) const
Write a matrix of parameter to a file.
Definition: Parameter.cpp:110
bool isLocked() const
Get the locking status of the parameter.
Definition: Parameter.cpp:213
Main class for C3D holder.
Definition: ezc3d.h:154
void write(std::fstream &f, int groupIdx, std::streampos &dataStartPosition) const
Write the parameter to an opened file.
Definition: Parameter.cpp:44
const std::vector< std::string > & valuesAsString() const
Return the vector of values of the parameter.
Definition: Parameter.cpp:357
std::string toUpper(const std::string &str)
Swap all characters of a string to capital letters.
Definition: ezc3d.cpp:25
int readInt(PROCESSOR_TYPE processorType, std::fstream &file, unsigned int nByteToRead, int nByteFromPrevious=0, const std::ios_base::seekdir &pos=std::ios::cur)
Read an integer of nByteToRead bytes at the position current + nByteFromPrevious from a file...
Definition: ezc3d.cpp:179
const std::vector< float > & valuesAsFloat() const
Return the vector of values of the parameter.
Definition: Parameter.cpp:350
int read(c3d &c3d, const Parameters &params, std::fstream &file, int nbCharInName)
Read and store a parameter from an opened C3D file.
Definition: Parameter.cpp:132
const std::vector< int > & valuesAsByte() const
Return the vector of values of the parameter.
Definition: Parameter.cpp:336
std::string readString(std::fstream &file, unsigned int nByteToRead, int nByteFromPrevious=0, const std::ios_base::seekdir &pos=std::ios::cur)
Read a string (array of char of nByteToRead bytes) at the position current + nByteFromPrevious from a...
Definition: ezc3d.cpp:256
DATA_TYPE
Enum that describes the size of different types.
Definition: ezc3d.h:73
void lock()
Set the locking status of the parameter to true.
Definition: Parameter.cpp:218
Declaration of Parameters class.