Boost.Nowide
args.hpp
1 //
2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_NOWIDE_ARGS_HPP_INCLUDED
9 #define BOOST_NOWIDE_ARGS_HPP_INCLUDED
10 
11 #include <boost/config.hpp>
12 #include <boost/nowide/stackstring.hpp>
13 #include <vector>
14 #ifdef BOOST_WINDOWS
15 #include <boost/nowide/windows.hpp>
16 #endif
17 
18 namespace boost {
19 namespace nowide {
20  #if !defined(BOOST_WINDOWS) && !defined(BOOST_NOWIDE_DOXYGEN)
21  class args {
22  public:
23  args(int &,char **&) {}
24  args(int &,char **&,char **&){}
25  ~args() {}
26  };
27 
28  #else
29 
42  class args {
43  public:
44 
48  args(int &argc,char **&argv) :
49  old_argc_(argc),
50  old_argv_(argv),
51  old_env_(0),
52  old_argc_ptr_(&argc),
53  old_argv_ptr_(&argv),
54  old_env_ptr_(0)
55  {
56  fix_args(argc,argv);
57  }
61  args(int &argc,char **&argv,char **&en) :
62  old_argc_(argc),
63  old_argv_(argv),
64  old_env_(en),
65  old_argc_ptr_(&argc),
66  old_argv_ptr_(&argv),
67  old_env_ptr_(&en)
68  {
69  fix_args(argc,argv);
70  fix_env(en);
71  }
76  {
77  if(old_argc_ptr_)
78  *old_argc_ptr_ = old_argc_;
79  if(old_argv_ptr_)
80  *old_argv_ptr_ = old_argv_;
81  if(old_env_ptr_)
82  *old_env_ptr_ = old_env_;
83  }
84  private:
85  void fix_args(int &argc,char **&argv)
86  {
87  int wargc;
88  wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(),&wargc);
89  if(!wargv) {
90  argc = 0;
91  static char *dummy = 0;
92  argv = &dummy;
93  return;
94  }
95  try{
96  args_.resize(wargc+1,0);
97  arg_values_.resize(wargc);
98  for(int i=0;i<wargc;i++) {
99  if(!arg_values_[i].convert(wargv[i])) {
100  wargc = i;
101  break;
102  }
103  args_[i] = arg_values_[i].c_str();
104  }
105  argc = wargc;
106  argv = &args_[0];
107  }
108  catch(...) {
109  LocalFree(wargv);
110  throw;
111  }
112  LocalFree(wargv);
113  }
114  void fix_env(char **&en)
115  {
116  static char *dummy = 0;
117  en = &dummy;
118  wchar_t *wstrings = GetEnvironmentStringsW();
119  if(!wstrings)
120  return;
121  try {
122  wchar_t *wstrings_end = 0;
123  int count = 0;
124  for(wstrings_end = wstrings;*wstrings_end;wstrings_end+=wcslen(wstrings_end)+1)
125  count++;
126  if(env_.convert(wstrings,wstrings_end)) {
127  envp_.resize(count+1,0);
128  char *p=env_.c_str();
129  int pos = 0;
130  for(int i=0;i<count;i++) {
131  if(*p!='=')
132  envp_[pos++] = p;
133  p+=strlen(p)+1;
134  }
135  en = &envp_[0];
136  }
137  }
138  catch(...) {
139  FreeEnvironmentStringsW(wstrings);
140  throw;
141  }
142  FreeEnvironmentStringsW(wstrings);
143 
144  }
145 
146  std::vector<char *> args_;
147  std::vector<short_stackstring> arg_values_;
148  stackstring env_;
149  std::vector<char *> envp_;
150 
151  int old_argc_;
152  char **old_argv_;
153  char **old_env_;
154 
155  int *old_argc_ptr_;
156  char ***old_argv_ptr_;
157  char ***old_env_ptr_;
158  };
159 
160  #endif
161 
162 } // nowide
163 } // namespace boost
164 #endif
165 
167 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
Definition: args.hpp:18
args(int &argc, char **&argv, char **&en)
Definition: args.hpp:61
args is a class that fixes standard main() function arguments and changes them to UTF-8 under Microso...
Definition: args.hpp:42
args(int &argc, char **&argv)
Definition: args.hpp:48
~args()
Definition: args.hpp:75