EZC3D
Group.cpp
Go to the documentation of this file.
1 #define EZC3D_API_EXPORTS
2 
10 #include "Group.h"
11 #include "Parameters.h"
12 
13 ezc3d::ParametersNS::GroupNS::Group::Group(const std::string &name, const std::string &description) :
14  _name(name),
15  _description(description),
16  _isLocked(false)
17 {
18 
19 }
20 
22 {
23  std::cout << "groupName = " << name() << std::endl;
24  std::cout << "isLocked = " << isLocked() << std::endl;
25  std::cout << "desc = " << description() << std::endl;
26 
27  for (size_t i=0; i < nbParameters(); ++i){
28  std::cout << "Parameter " << i << std::endl;
29  parameter(i).print();
30  }
31 }
32 
33 void ezc3d::ParametersNS::GroupNS::Group::write(std::fstream &f, int groupIdx, std::streampos &dataStartPosition) const
34 {
35  int nCharName(static_cast<int>(name().size()));
36  if (isLocked())
37  nCharName *= -1;
38  f.write(reinterpret_cast<const char*>(&nCharName), 1*ezc3d::DATA_TYPE::BYTE);
39  if (isLocked())
40  nCharName *= -1;
41  f.write(reinterpret_cast<const char*>(&groupIdx), 1*ezc3d::DATA_TYPE::BYTE);
42  f.write(ezc3d::toUpper(name()).c_str(), nCharName*ezc3d::DATA_TYPE::BYTE);
43 
44  // It is not possible already to know in how many bytes the next parameter is
45  int blank(0);
46  std::streampos pos(f.tellg());
47  f.write(reinterpret_cast<const char*>(&blank), 2*ezc3d::DATA_TYPE::BYTE);
48 
49  int nCharGroupDescription(static_cast<int>(description().size()));
50  f.write(reinterpret_cast<const char*>(&nCharGroupDescription), 1*ezc3d::DATA_TYPE::BYTE);
51  f.write(description().c_str(), nCharGroupDescription*ezc3d::DATA_TYPE::BYTE);
52 
53  std::streampos currentPos(f.tellg());
54  // Go back at the left blank space and write the current position
55  f.seekg(pos);
56  int nCharToNext = int(currentPos - pos);
57  f.write(reinterpret_cast<const char*>(&nCharToNext), 2*ezc3d::DATA_TYPE::BYTE);
58  f.seekg(currentPos);
59 
60  for (size_t i=0; i < nbParameters(); ++i)
61  parameter(i).write(f, -groupIdx, dataStartPosition);
62 
63 }
64 
66  std::fstream &file, int nbCharInName)
67 {
68  if (nbCharInName < 0)
69  _isLocked = true;
70  else
71  _isLocked = false;
72 
73  // Read name of the group
74  _name.assign(c3d.readString(file, static_cast<unsigned int>(abs(nbCharInName) * ezc3d::DATA_TYPE::BYTE)));
75 
76  // number of byte to the next group from here
77  size_t offsetNext(c3d.readUint(params.processorType(), file, 2*ezc3d::DATA_TYPE::BYTE));
78  // Compute the position of the element in the file
79  int nextParamByteInFile;
80  if (offsetNext == 0)
81  nextParamByteInFile = 0;
82  else
83  nextParamByteInFile = static_cast<int>(static_cast<size_t>(file.tellg()) + offsetNext - ezc3d::DATA_TYPE::WORD);
84 
85  // Byte 5+nbCharInName ==> Number of characters in group description
86  int nbCharInDesc(c3d.readInt(params.processorType(), file, 1*ezc3d::DATA_TYPE::BYTE));
87  // Byte 6+nbCharInName ==> Group description
88  if (nbCharInDesc)
89  _description = c3d.readString(file, static_cast<unsigned int>(nbCharInDesc));
90 
91  // Return how many bytes
92  return nextParamByteInFile;
93 }
94 
96 {
97  return _name;
98 }
99 
100 void ezc3d::ParametersNS::GroupNS::Group::name(const std::string name)
101 {
102  _name = name;
103 }
104 
106 {
107  return _description;
108 }
109 
110 void ezc3d::ParametersNS::GroupNS::Group::description(const std::string description)
111 {
112  _description = description;
113 }
114 
116 {
117  return _isLocked;
118 }
119 
121 {
122  _isLocked = true;
123 }
125 {
126  _isLocked = false;
127 }
128 
130 {
131  return _parameters.size();
132 }
133 
134 size_t ezc3d::ParametersNS::GroupNS::Group::parameterIdx(std::string parameterName) const
135 {
136  for (size_t i = 0; i < nbParameters(); ++i)
137  if (!parameter(i).name().compare(parameterName))
138  return i;
139  throw std::invalid_argument("Group::parameterIdx could not find " + parameterName +
140  " in the group " + name());
141 }
142 
144 {
145  try {
146  return _parameters.at(idx);
147  } catch(std::out_of_range) {
148  throw std::out_of_range("Groups::parameter method is trying to access the parameter "
149  + std::to_string(idx) +
150  " while the maximum number of parameter is "
151  + std::to_string(nbParameters()) + " in the group " + name() + ".");
152  }
153 }
154 
156 {
157  try {
158  return _parameters.at(idx);
159  } catch(std::out_of_range) {
160  throw std::out_of_range("Groups::parameter method is trying to access the parameter "
161  + std::to_string(idx) +
162  " while the maximum number of parameters is "
163  + std::to_string(nbParameters()) + " in the group " + name() + ".");
164  }
165 }
166 
168 {
169  return parameter(parameterIdx(parameterName));
170 }
171 
173 {
174  return parameter_nonConst(parameterIdx(parameterName));
175 }
176 
177 int ezc3d::ParametersNS::GroupNS::Group::parameter(ezc3d::c3d &c3d, const Parameters &params, std::fstream &file, int nbCharInName)
178 {
180  int nextParamByteInFile = p.read(c3d, params, file, nbCharInName);
181  parameter(p);
182  return nextParamByteInFile;
183 }
184 
186 {
187  if (p.type() == ezc3d::DATA_TYPE::NO_DATA_TYPE)
188  throw std::runtime_error("Data type is not set");
189 
190  size_t alreadyExistIdx(SIZE_MAX);
191  for (size_t i=0; i < _parameters.size(); ++i)
192  if (!parameter(i).name().compare(p.name())){
193  alreadyExistIdx = i;
194  break;
195  }
196  if (alreadyExistIdx == SIZE_MAX)
197  _parameters.push_back(p);
198  else
199  _parameters[alreadyExistIdx] = p;
200 }
201 
202 const std::vector<ezc3d::ParametersNS::GroupNS::Parameter> &ezc3d::ParametersNS::GroupNS::Group::parameters() const
203 {
204  return _parameters;
205 }
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
Declaration of Group class.
void unlock()
Set the locking status of the group to false.
Definition: Group.cpp:124
size_t nbParameters() const
Get the number of parameters.
Definition: Group.cpp:129
ezc3d::DATA_TYPE type() const
Return the type of the data.
Definition: Parameter.cpp:253
size_t parameterIdx(std::string parameterName) const
Get the index of a parameter in the group.
Definition: Group.cpp:134
const std::string & description() const
Get the description of the group.
Definition: Group.cpp:105
PROCESSOR_TYPE processorType() const
Get the processor type the file was writen on.
Definition: Parameters.cpp:294
void print() const
Print the group by calling the print method of all the parameters.
Definition: Group.cpp:21
const ezc3d::ParametersNS::GroupNS::Parameter & parameter(size_t idx) const
Get a particular parameter of index idx from the group.
Definition: Group.cpp:143
const std::string & name() const
Get the name of the parameter.
Definition: Parameter.cpp:193
void write(std::fstream &f, int groupIdx, std::streampos &dataStartPosition) const
Write the group to an opened file by calling the write method of all the parameters.
Definition: Group.cpp:33
Main class for C3D holder.
Definition: ezc3d.h:154
const std::string & name() const
Get the name of the group.
Definition: Group.cpp:95
const std::vector< ezc3d::ParametersNS::GroupNS::Parameter > & parameters() const
Get all the parameter from the group.
Definition: Group.cpp:202
Group(const std::string &name="", const std::string &description="")
Create an empty group of parameter.
Definition: Group.cpp:13
bool isLocked() const
Get the locking status of the group.
Definition: Group.cpp:115
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
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
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
ezc3d::ParametersNS::GroupNS::Parameter & parameter_nonConst(size_t idx)
Get a particular parameter of index idx from the group in order to be modified by the caller...
Definition: Group.cpp:155
Parameter of a C3D file.
Definition: Parameter.h:16
int read(ezc3d::c3d &c3d, const Parameters &params, std::fstream &file, int nbCharInName)
Read and store a group of parameter from an opened C3D file.
Definition: Group.cpp:65
void lock()
Set the locking status of the group to true.
Definition: Group.cpp:120
Declaration of Parameters class.