Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
init_util.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file init_util.hh
11 /// @brief initialization subroutines for Benchmark
12 /// @author
13 
14 #ifndef INCLUDED_apps_benchmark_init_util_hh
15 #define INCLUDED_apps_benchmark_init_util_hh
16 
17 #include <devel/init.hh>
18 
19 #include <cstring>
20 #include <iostream>
21 #include <string>
22 #include <vector>
23 
24 // @brief For unit tests only. Constructs a mock argc and argv given a stl string.
25 inline
26 char**
27 create_pseudo_commandline( std::string const & cmdin, int & pseudo_argc )
28 {
29  using namespace std;
30  pseudo_argc = 0;
31 
32  vector< int > string_start;
33  vector< int > string_length;
34  bool in_the_middle_of_a_string = false;
35  for ( unsigned int ii = 0; ii < cmdin.size(); ++ii ) {
36  if ( cmdin[ ii ] != ' ' ) {
37  if ( in_the_middle_of_a_string ) {
38  ++string_length[ pseudo_argc - 1 ];
39  } else {
40  string_start.push_back( ii );
41  string_length.push_back( 1 );
42  in_the_middle_of_a_string = true;
43  ++pseudo_argc;
44  }
45  } else {
46  in_the_middle_of_a_string = false;
47  }
48  }
49 
50  vector< string > cmdline;
51  cmdline.reserve( pseudo_argc );
52  for ( int ii = 0; ii < pseudo_argc; ++ii ) {
53  cmdline.push_back( cmdin.substr( string_start[ ii ], string_length[ ii ] ));
54  }
55 
56  char** pseudo_argv = new char * [ pseudo_argc ];
57  for ( int ii = 0; ii < pseudo_argc; ++ii ) {
58  pseudo_argv[ ii ] = new char[ cmdline[ ii ].size() + 1 ];
59  strncpy(pseudo_argv[ii], cmdline[ii].c_str(), cmdline[ii].size());
60  pseudo_argv[ii][ cmdline[ii].size() ] = '\0';
61  }
62 
63  return pseudo_argv;
64 }
65 
66 // @brief For unit tests only. Deallocates the pseudo command line created by a call to
67 // create_pseudo_commandline
68 inline
69 void
70 destroy_pseudo_commandline( int pseudo_argc, char** pseudo_argv )
71 {
72  for ( int ii = 0; ii < pseudo_argc; ++ii ) {
73  delete [] pseudo_argv[ ii ];
74  }
75  delete [] pseudo_argv;
76 }
77 
78 
79 /// @brief For unit tests only. Creates an argc/argv pair, calls init() and deletes argv.
80 /// can be used to init option system more that once, providing user ability to change
81 /// options on the fly.
82 inline void core_init_from_string( std::string const & commandline )
83 {
84  extern char ** command_line_argv; /// We need original filename to be first argument
85 
86  int pseudo_argc;
87  char** pseudo_argv = create_pseudo_commandline( std::string(command_line_argv[0]) + " "
88  + commandline, pseudo_argc );
89  devel::init( pseudo_argc, pseudo_argv );
90  destroy_pseudo_commandline( pseudo_argc, pseudo_argv );
91 }
92 
93 /// @brief For unit tests only. Re-init option system.
94 /// Command line will be = old command line + function argument.
95 inline void core_init_with_additional_options( std::string const & commandline_in )
96 {
97  extern int command_line_argc; extern char ** command_line_argv;
98 
99  std::string commandline(" ");
100  for ( int i=1; i<command_line_argc; i++ ) {
101  commandline = commandline + command_line_argv[i] + " ";
102  }
103  commandline = commandline + commandline_in;
104  //std::cout << "core_init_with_additional_options: " << commandline.c_str() << "\n";
105 
106  core_init_from_string( commandline );
107 }
108 
109 
110 #endif
dictionary size
Definition: amino_acids.py:44
void core_init_from_string(std::string const &commandline)
For unit tests only. Creates an argc/argv pair, calls init() and deletes argv. can be used to init op...
Definition: init_util.hh:82
void init(int argc, char *argv[])
Command line init() version.
Definition: init.cc:23
void core_init_with_additional_options(std::string const &commandline_in)
For unit tests only. Re-init option system. Command line will be = old command line + function argume...
Definition: init_util.hh:95
void destroy_pseudo_commandline(int pseudo_argc, char **pseudo_argv)
Definition: init_util.hh:70
char ** create_pseudo_commandline(std::string const &cmdin, int &pseudo_argc)
Definition: init_util.hh:27
int command_line_argc
char ** command_line_argv