acado_io_utils.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
36 #include <acado/code_generation/templates/templates.hpp>
37 
38 #if defined( __WIN32__ ) || defined( WIN32 )
39 
40 #include <windows.h>
41 #include <direct.h>
42 
43 #elif defined( LINUX )
44 
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/types.h>
48 #include <unistd.h>
49 
50 #else
51 
52 #warning "File I/O is not supported on this platform"
53 
54 #endif /* defined(__WIN32__) || defined(WIN32) */
55 
56 using namespace std;
57 
59 
60 #define AUTOGEN_NOTICE_LENGTH 15
62 {
63  "This file was auto-generated using the ACADO Toolkit.\n",
64  "\n",
65  "While ACADO Toolkit is free software released under the terms of\n",
66  "the GNU Lesser General Public License (LGPL), the generated code\n",
67  "as such remains the property of the user who used ACADO Toolkit\n",
68  "to generate this code. In particular, user dependent data of the code\n",
69  "do not inherit the GNU LGPL license. On the other hand, parts of the\n",
70  "generated code that are a direct copy of source code from the\n",
71  "ACADO Toolkit or the software tools it is based on, remain, as derived\n",
72  "work, automatically covered by the LGPL license.\n",
73  "\n",
74  "ACADO Toolkit is distributed in the hope that it will be useful,\n",
75  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n",
76  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
77  "\n"
78 };
79 
80 static uint getStringLength(const char* string)
81 {
82  if (string != 0)
83  return strlen(string);
84 
85  return 0;
86 }
87 
89  char** _startString,
90  char** _endString,
91  uint& _width,
92  uint& _precision,
93  char** _colSeparator,
94  char** _rowSeparator
95  )
96 {
97  switch (_printScheme) {
98  case PS_DEFAULT:
99 
100  *_startString = new char[getStringLength(DEFAULT_START_STRING) + 1];
101  strcpy(*_startString, DEFAULT_START_STRING);
102 
103  *_endString = new char[getStringLength(DEFAULT_END_STRING) + 1];
104  strcpy(*_endString, DEFAULT_END_STRING);
105 
106  _width = DEFAULT_WIDTH;
107  _precision = DEFAULT_PRECISION;
108 
109  *_colSeparator = new char[getStringLength(DEFAULT_COL_SEPARATOR) + 1];
110  strcpy(*_colSeparator, DEFAULT_COL_SEPARATOR);
111 
112  *_rowSeparator = new char[getStringLength(DEFAULT_ROW_SEPARATOR) + 1];
113  strcpy(*_rowSeparator, DEFAULT_ROW_SEPARATOR);
114 
115  break;
116 
117  case PS_PLAIN:
118 
119  *_startString = new char[1];
120  (*_startString)[0] = '\0';
121 
122  *_endString = new char[2];
123  (*_endString)[0] = '\n';
124  (*_endString)[1] = '\0';
125 
126  _width = DEFAULT_WIDTH;
127  _precision = DEFAULT_PRECISION;
128 
129  *_colSeparator = new char[2];
130  (*_colSeparator)[0] = ' ';
131  (*_colSeparator)[1] = '\0';
132 
133  *_rowSeparator = new char[2];
134  (*_rowSeparator)[0] = '\n';
135  (*_rowSeparator)[1] = '\0';
136 
137  break;
138 
139  case PS_MATLAB:
140  case PS_MATLAB_BINARY:
141 
142  *_startString = new char[3];
143  (*_startString)[0] = '[';
144  (*_startString)[1] = ' ';
145  (*_startString)[2] = '\0';
146 
147  *_endString = new char[5];
148  (*_endString)[0] = ' ';
149  (*_endString)[1] = ']';
150  (*_endString)[2] = ';';
151  (*_endString)[3] = '\n';
152  (*_endString)[4] = '\0';
153 
154  _width = DEFAULT_WIDTH;
155  _precision = DEFAULT_PRECISION;
156 
157  *_colSeparator = new char[3];
158  (*_colSeparator)[0] = ',';
159  (*_colSeparator)[1] = ' ';
160  (*_colSeparator)[2] = '\0';
161 
162  *_rowSeparator = new char[3];
163  (*_rowSeparator)[0] = ';';
164  (*_rowSeparator)[1] = '\n';
165  (*_rowSeparator)[2] = '\0';
166 
167  break;
168 
169  default:
170  return ACADOERROR( RET_UNKNOWN_BUG );
171  }
172 
173  return SUCCESSFUL_RETURN;
174 }
175 
176 returnValue acadoCopyFile( const std::string& source,
177  const std::string& destination,
178  const std::string& commentString,
179  bool printCodegenNotice
180  )
181 {
182  std::ifstream src( source.c_str() );
183  if (src.is_open() == false)
184  {
185  LOG( LVL_ERROR ) << "Could not open the source file: " << source << std::endl;
187  }
188 
189  std::ofstream dst( destination.c_str() );
190  if (dst.is_open() == false)
191  {
192  LOG( LVL_ERROR ) << "Could not open the destination file: " << destination << std::endl;
194  }
195 
196  if (printCodegenNotice == BT_TRUE)
197  {
198  if (commentString.empty())
199  {
200  dst << "/*" << endl;
201  for (unsigned i = 0; i < AUTOGEN_NOTICE_LENGTH; ++i)
202  dst << " * " << autogenerationNotice[ i ];
203  dst << " */" << endl << endl;
204  }
205  else
206  {
207  dst << commentString << endl;
208  for (unsigned i = 0; i < AUTOGEN_NOTICE_LENGTH; ++i)
209  dst << commentString << " " << autogenerationNotice[ i ];
210  dst << endl;
211  }
212  }
213 
214  dst << src.rdbuf();
215 
216  src.close();
217  dst.close();
218 
219  return SUCCESSFUL_RETURN;
220 }
221 
222 returnValue acadoCopyTemplateFile( const std::string& source,
223  const std::string& destination,
224  const std::string& commentString,
225  bool printCodegenNotice
226  )
227 {
228  const string folders( TEMPLATE_PATHS );
229  ifstream inputFile;
230  size_t oldPos = 0;
231 
232  while( 1 )
233  {
234  size_t pos;
235  string tmp;
236 
237  pos = folders.find(";", oldPos);
238  tmp = folders.substr(oldPos, pos) + "/" + source;
239 
240  inputFile.open(tmp.c_str());
241 
242  if (inputFile.is_open() == true)
243  return acadoCopyFile(tmp, destination, commentString, printCodegenNotice);
244 
245  if (pos == string::npos)
246  break;
247 
248  oldPos = pos + 1;
249  }
250 
251  LOG( LVL_ERROR ) << "Could not open the template file: " << source << std::endl;
253 }
254 
255 returnValue acadoCreateFolder( const std::string& name
256  )
257 {
258 #if defined( __WIN32__ ) || defined( WIN32 )
259 
260  int status = _mkdir( name.c_str() );
261  errno_t err;
262  _get_errno( &err );
263  if (status && err != EEXIST)
264  {
265  LOG( LVL_ERROR ) << "Problem creating directory " << name << endl;
267  }
268 
269 #elif defined( LINUX )
270 
271  struct stat st = {0};
272 
273  if (stat(name.c_str(), &st) == -1)
274  {
275  if (mkdir(name.c_str(), 0700) == -1)
276  {
277  // TODO give here an error code
278  LOG( LVL_ERROR ) << "Problem creating directory " << name << endl;
280  }
281  }
282 
283 #else
284 
285  return ACADOERRORTEXT(RET_INVALID_OPTION, "Unsupported platform.");
286 
287 #endif /* defined(__WIN32__) || defined(WIN32) */
288 
289  return SUCCESSFUL_RETURN;
290 }
291 
292 /*
293  * p r i n t C o p y r i g h t N o t i c e
294  */
295 returnValue acadoPrintCopyrightNotice( const std::string& subpackage
296  )
297 {
298  if (subpackage.empty())
299  cout << "\nACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.\n" \
300  "Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,\n" \
301  "Milan Vukov, Rien Quirynen, KU Leuven.\n" \
302  "Developed within the Optimization in Engineering Center (OPTEC)\n" \
303  "under supervision of Moritz Diehl. All rights reserved.\n\n" \
304  "ACADO Toolkit is distributed under the terms of the GNU Lesser\n" \
305  "General Public License 3 in the hope that it will be useful,\n" \
306  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
307  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \
308  "GNU Lesser General Public License for more details.\n\n";
309  else
310  cout << "\nACADO Toolkit::" << subpackage << endl
311  << "Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,\n" \
312  "Milan Vukov, Rien Quirynen, KU Leuven.\n" \
313  "Developed within the Optimization in Engineering Center (OPTEC)\n" \
314  "under supervision of Moritz Diehl. All rights reserved.\n\n" \
315  "ACADO Toolkit is distributed under the terms of the GNU Lesser\n" \
316  "General Public License 3 in the hope that it will be useful,\n" \
317  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
318  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \
319  "GNU Lesser General Public License for more details.\n\n";
320 
321  return SUCCESSFUL_RETURN;
322 }
323 
324 
326  const std::string& commentString
327  )
328 {
329  if (stream.is_open() == false)
330  return RET_INVALID_ARGUMENTS;
331 
332  if (commentString.empty())
333  {
334  stream << "/*\n";
335  for (unsigned i = 0; i < AUTOGEN_NOTICE_LENGTH; ++i)
336  stream << " * " << autogenerationNotice[ i ];
337  stream << " */\n";
338  }
339  else
340  {
341  stream << commentString << endl;
342  for (unsigned i = 0; i < AUTOGEN_NOTICE_LENGTH; ++i)
343  stream << commentString << " " << autogenerationNotice[ i ];
344  }
345 
346  stream << endl << endl;
347 
348  return SUCCESSFUL_RETURN;
349 }
350 
351 
352 
353 /*
354  * g e t C P U t i m e
355  */
356 double acadoGetTime( )
357 {
358  double current_time = 0.0;
359 
360  #if defined(__WIN32__) || defined(WIN32)
361  LARGE_INTEGER counter, frequency;
362  QueryPerformanceFrequency(&frequency);
363  QueryPerformanceCounter(&counter);
364  current_time = ((double) counter.QuadPart) / ((double) frequency.QuadPart);
365  #elif defined(LINUX)
366  struct timeval theclock;
367  gettimeofday( &theclock,0 );
368  current_time = 1.0*theclock.tv_sec + 1.0e-6*theclock.tv_usec;
369  #endif
370 
371  return current_time;
372 }
373 
375 
376 /*
377  * end of file
378  */
returnValue getGlobalStringDefinitions(PrintScheme _printScheme, char **_startString, char **_endString, uint &_width, uint &_precision, char **_colSeparator, char **_rowSeparator)
const char DEFAULT_END_STRING[4]
#define AUTOGEN_NOTICE_LENGTH
#define LOG(level)
Just define a handy macro for getting the logger.
int counter
Definition: powerkite_c.cpp:40
const uint DEFAULT_PRECISION
static uint getStringLength(const char *string)
Allows to pass back messages to the calling function.
const char DEFAULT_COL_SEPARATOR[2]
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
returnValue acadoPrintAutoGenerationNotice(std::ofstream &stream, const std::string &commentString)
Returned value is a error.
double acadoGetTime()
#define CLOSE_NAMESPACE_ACADO
const char DEFAULT_START_STRING[3]
static const char * autogenerationNotice[AUTOGEN_NOTICE_LENGTH]
returnValue acadoCopyTemplateFile(const std::string &source, const std::string &destination, const std::string &commentString, bool printCodegenNotice)
PrintScheme
returnValue acadoPrintCopyrightNotice(const std::string &subpackage)
returnValue acadoCreateFolder(const std::string &name)
returnValue acadoCopyFile(const std::string &source, const std::string &destination, const std::string &commentString, bool printCodegenNotice)
#define BT_TRUE
Definition: acado_types.hpp:47
const uint DEFAULT_WIDTH
const char DEFAULT_ROW_SEPARATOR[6]
#define BEGIN_NAMESPACE_ACADO
#define ACADOERROR(retval)
#define ACADOERRORTEXT(retval, text)


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:27