DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
TextFileOperator.cxx
Go to the documentation of this file.
1 
18 #include "utlCore.h"
19 #include "TextFileOperatorCLP.h"
20 
21 
22 enum
23 {
32 };
33 
34 void
35 SetOperationWithChecking( int &operation, int value )
36 {
37  if ( operation == OP_NULL )
38  {
39  operation = value;
40  }
41  else
42  {
43  std::cerr << "Only one type of operation is allowed!" << std::endl;
44  exit( EXIT_FAILURE );
45  }
46 }
47 
48 
52 int
53 main (int argc, char const* argv[])
54 {
55  PARSE_ARGS;
56 
57  int operation = OP_NULL;
58 
59  if ( _InfoArg.isSet() )
60  SetOperationWithChecking( operation, OP_INFO );
61  if ( _TransposeArg.isSet() )
63  if (_FileConnectRowArg.isSet())
65  if (_FileConnectColumnArg.isSet())
67  if ( _FileIndexRowsArg.isSet() )
69  if ( _ExtractSubMatrixArg.isSet() )
71  if ( _ScaleArg.isSet() )
72  SetOperationWithChecking( operation, OP_SCALE );
73  // if ( _ScaleColumnArg.isSet() )
74  // SetOperationWithChecking( operation, OP_SCALE_COLUMN );
75  // if ( _ScaleRowArg.isSet() )
76  // SetOperationWithChecking( operation, OP_SCALE_ROW );
77 
78  std::vector<std::vector<std::string> > input, input2, output;
79  utl::ReadLines(_InputFile, input, " \t,");
80 
81  int NumberRows = input.size();
82  utlException(NumberRows==0, "no data");
83 
84  int NumberColumns=input[0].size();
85  bool isSameColumn=true;
86  for ( int i = 1; i < input.size(); i += 1 )
87  {
88  if (NumberColumns!=input[i].size())
89  isSameColumn = false;
90  if (NumberColumns<input[i].size())
91  NumberColumns = input[i].size();
92  }
93  for ( int i = 0; i < input.size(); i += 1 )
94  {
95  int numberPush=NumberColumns-input[i].size();
96  for ( int j = 0; j < numberPush; j += 1 )
97  input[i].push_back(" ");
98  }
99 
100 
101  std::ofstream out;
102  if (_OutputfileArg.isSet())
103  {
104  out.open ( _Outputfile.c_str() );
105  utlException (!out, "ERROR : failed to open output file " << _Outputfile);
106  }
107 
108  switch ( operation )
109  {
110  case OP_INFO :
111  {
112  std::cout << "File information" << std::endl << std::flush;
113  std::cout << "Row: " << NumberRows << ", Column: " << NumberColumns << std::endl << std::flush;
114  if (isSameColumn)
115  std::cout << "all rows have the same number of columns" << std::endl << std::flush;
116  else
117  std::cout << "not all rows have the same number of columns" << std::endl << std::flush;
118  break;
119  }
120  case OP_TRANSPOSE :
121  {
122  std::cout << "File transpose" << std::endl << std::flush;
123  utlException (!_OutputfileArg.isSet(), "no output");
124  for ( int i = 0; i < NumberColumns; i += 1 )
125  {
126  int NumberColumns = input[i].size();
127  for ( int j = 0; j < NumberRows-1; j += 1 )
128  out << input[j][i] << " ";
129  out << input[NumberRows-1][i] << "\n";
130  }
131  break;
132  }
133  case OP_CONNECT_COLUMN :
134  {
135  std::cout << "Connect columns" << std::endl << std::flush;
136  utlException (!_OutputfileArg.isSet(), "no output");
137  utl::ReadLines(_FileConnectColumn, input2);
138  for ( int i = 0; i < input.size(); i += 1 )
139  {
140  std::vector<std::string> element1 = input[i];
141  if (i<input2.size())
142  {
143  std::vector<std::string> element2 = input2[i];
144  for ( int j = 0; j < element2.size(); j += 1 )
145  element1.push_back(element2[j]);
146  }
147  input[i] = element1;
148  }
149  utl::Save2DVector(input, out);
150  break;
151  }
152  case OP_CONNECT_ROW :
153  {
154  std::cout << "Connect rows" << std::endl << std::flush;
155  utlException (!_OutputfileArg.isSet(), "no output");
156  utl::ReadLines(_FileConnectRow, input2);
157  utl::Save2DVector(input, out);
158  utl::Save2DVector(input2, out);
159  break;
160  }
161  case OP_INDEX_ROW :
162  {
163  std::cout << "select rows" << std::endl << std::flush;
164  utlException (!_OutputfileArg.isSet(), "no output");
165  std::vector<int> index;
166  utl::ReadVector(_FileIndexRows, index);
167  input2 = utl::SelectVector(input, index);
168  utl::Save2DVector(input2, out);
169  break;
170  }
171  case OP_EXTRACT:
172  {
173  std::cout << "Extract sub-matrix" << std::endl << std::flush;
174  _ExtractSubMatrix[0] = utl::max(_ExtractSubMatrix[0], 0);
175  _ExtractSubMatrix[1] = utl::min(_ExtractSubMatrix[1], NumberRows-1);
176  _ExtractSubMatrix[2] = utl::max(_ExtractSubMatrix[2], 0);
177  _ExtractSubMatrix[3] = utl::min(_ExtractSubMatrix[3], NumberColumns-1);
178  // utlPrintVar4(true, _ExtractSubMatrix[0], _ExtractSubMatrix[1], _ExtractSubMatrix[2], _ExtractSubMatrix[3]);
179  utlException (!_OutputfileArg.isSet(), "no output");
180  for ( int i = _ExtractSubMatrix[0]; i <= _ExtractSubMatrix[1]; i += 1 )
181  {
182  std::vector<std::string> tmp;
183  for ( int j = _ExtractSubMatrix[2]; j <= _ExtractSubMatrix[3]; j += 1 )
184  tmp.push_back(input[i][j]);
185  output.push_back(tmp);
186  }
187  utl::Save2DVector(output, out);
188  break;
189  }
190  case OP_SCALE:
191  {
192  utlException (!_OutputfileArg.isSet(), "no output");
193  for ( int i = 0; i < NumberRows; i += 1 )
194  {
195  std::vector<std::string> tmp;
196  for ( int j = 0; j < NumberColumns; j += 1 )
197  {
198  std::string strTemp = input[i][j];
199  if (utl::IsNumber(strTemp))
200  {
201  double floatTemp = utl::ConvertStringToNumber<double>(strTemp);
202  floatTemp *= _Scale;
203  strTemp = utl::ConvertNumberToString(floatTemp);
204  }
205  tmp.push_back(strTemp);
206  }
207  output.push_back(tmp);
208  }
209  utl::Save2DVector(output, out);
210  break;
211  }
212  default :
213  utlException(true, "wrong operation");
214  break;
215  }
216 
217  if (_OutputfileArg.isSet())
218  {
219  std::cout << "save to " << _Outputfile << std::endl << std::flush;
220  out.close();
221  }
222 
223  return 0;
224 }
void SetOperationWithChecking(int &operation, int value)
bool IsNumber(const std::string &input)
Definition: utlCore.h:726
std::string ConvertNumberToString(const T value, const int precision=6)
Definition: utlCore.h:740
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
const T & max(const T &a, const T &b)
Return the maximum between a and b.
Definition: utlCore.h:263
void Save2DVector(const Vector2D &vv, std::ostream &out=std::cout)
Definition: utlCore.h:1850
const T & min(const T &a, const T &b)
Return the minimum between a and b.
Definition: utlCore.h:257
int main(int argc, char const *argv[])
operators for text file
void ReadVector(const std::string &vectorStr, std::vector< T > &vec, const char *cc=" ")
Definition: utlCore.h:1159
std::vector< T > SelectVector(const std::vector< T > &vec, const std::vector< int > &index)
Definition: utlCore.h:1312
void ReadLines(const std::string &filename, std::vector< std::vector< std::string > > &strVec, const char *cc=" ")
Definition: utlCore.h:862