2007-11-12 17:32:23 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/cmdargs.h
|
|
|
|
// Purpose: declaration of wxCmdLineArgsArray helper class
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2007-11-12
|
2019-04-22 08:12:05 -04:00
|
|
|
// Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
|
2007-11-12 17:32:23 -05:00
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_CMDARGS_H_
|
|
|
|
#define _WX_CMDARGS_H_
|
|
|
|
|
|
|
|
#include "wx/arrstr.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxCmdLineArgsArray: helper class used by wxApp::argv
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if wxUSE_UNICODE
|
|
|
|
|
|
|
|
// this class is used instead of either "char **" or "wchar_t **" (neither of
|
|
|
|
// which would be backwards compatible with all the existing code) for argv
|
|
|
|
// field of wxApp
|
|
|
|
//
|
|
|
|
// as it's used for compatibility, it tries to look as much as traditional
|
|
|
|
// (char **) argv as possible, in particular it provides implicit conversions
|
|
|
|
// to "char **" and also array-like operator[]
|
2007-11-13 08:27:30 -05:00
|
|
|
class WXDLLIMPEXP_BASE wxCmdLineArgsArray
|
2007-11-12 17:32:23 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxCmdLineArgsArray() { m_argsA = NULL; m_argsW = NULL; }
|
|
|
|
|
|
|
|
template <typename T>
|
2017-04-14 13:00:44 -04:00
|
|
|
void Init(int argc, T **argv)
|
2007-11-12 17:32:23 -05:00
|
|
|
{
|
|
|
|
FreeArgs();
|
|
|
|
|
|
|
|
m_args.clear();
|
2017-04-14 13:00:44 -04:00
|
|
|
m_args.reserve(argc);
|
2007-11-12 17:32:23 -05:00
|
|
|
|
2017-04-14 13:00:44 -04:00
|
|
|
for ( int i = 0; i < argc; i++ )
|
2007-11-12 17:32:23 -05:00
|
|
|
{
|
2017-04-14 13:00:44 -04:00
|
|
|
m_args.push_back(argv[i]);
|
2007-11-12 17:32:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
operator char**() const
|
|
|
|
{
|
|
|
|
if ( !m_argsA )
|
|
|
|
{
|
|
|
|
const size_t count = m_args.size();
|
2016-05-13 09:40:22 -04:00
|
|
|
m_argsA = new char *[count + 1];
|
2007-11-12 17:32:23 -05:00
|
|
|
for ( size_t n = 0; n < count; n++ )
|
|
|
|
m_argsA[n] = wxStrdup(m_args[n].ToAscii());
|
2016-05-13 09:40:22 -04:00
|
|
|
|
|
|
|
m_argsA[count] = NULL;
|
2007-11-12 17:32:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_argsA;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator wchar_t**() const
|
|
|
|
{
|
|
|
|
if ( !m_argsW )
|
|
|
|
{
|
|
|
|
const size_t count = m_args.size();
|
2016-05-13 09:40:22 -04:00
|
|
|
m_argsW = new wchar_t *[count + 1];
|
2007-11-12 17:32:23 -05:00
|
|
|
for ( size_t n = 0; n < count; n++ )
|
|
|
|
m_argsW[n] = wxStrdup(m_args[n].wc_str());
|
2016-05-13 09:40:22 -04:00
|
|
|
|
|
|
|
m_argsW[count] = NULL;
|
2007-11-12 17:32:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_argsW;
|
|
|
|
}
|
|
|
|
|
|
|
|
// existing code does checks like "if ( argv )" and we want it to continue
|
|
|
|
// to compile, so provide this conversion even if it is pretty dangerous
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return !m_args.empty();
|
|
|
|
}
|
|
|
|
|
2007-11-13 08:27:30 -05:00
|
|
|
// and the same for "if ( !argv )" checks
|
|
|
|
bool operator!() const
|
|
|
|
{
|
|
|
|
return m_args.empty();
|
|
|
|
}
|
|
|
|
|
2007-11-12 17:32:23 -05:00
|
|
|
wxString operator[](size_t n) const
|
|
|
|
{
|
|
|
|
return m_args[n];
|
|
|
|
}
|
|
|
|
|
2007-11-12 21:12:09 -05:00
|
|
|
// we must provide this overload for g++ 3.4 which can't choose between
|
|
|
|
// our operator[](size_t) and ::operator[](char**, int) otherwise
|
|
|
|
wxString operator[](int n) const
|
|
|
|
{
|
|
|
|
return m_args[n];
|
|
|
|
}
|
|
|
|
|
2007-11-13 08:27:30 -05:00
|
|
|
|
|
|
|
// convenience methods, i.e. not existing only for backwards compatibility
|
|
|
|
|
|
|
|
// do we have any arguments at all?
|
|
|
|
bool IsEmpty() const { return m_args.empty(); }
|
|
|
|
|
|
|
|
// access the arguments as a convenient array of wxStrings
|
2007-11-12 17:32:23 -05:00
|
|
|
const wxArrayString& GetArguments() const { return m_args; }
|
|
|
|
|
|
|
|
~wxCmdLineArgsArray()
|
|
|
|
{
|
|
|
|
FreeArgs();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
2015-05-17 16:34:18 -04:00
|
|
|
void Free(T**& args)
|
2007-11-12 17:32:23 -05:00
|
|
|
{
|
|
|
|
if ( !args )
|
|
|
|
return;
|
|
|
|
|
|
|
|
const size_t count = m_args.size();
|
|
|
|
for ( size_t n = 0; n < count; n++ )
|
|
|
|
free(args[n]);
|
|
|
|
|
|
|
|
delete [] args;
|
2015-05-17 16:34:18 -04:00
|
|
|
args = NULL;
|
2007-11-12 17:32:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeArgs()
|
|
|
|
{
|
|
|
|
Free(m_argsA);
|
|
|
|
Free(m_argsW);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxArrayString m_args;
|
|
|
|
mutable char **m_argsA;
|
|
|
|
mutable wchar_t **m_argsW;
|
|
|
|
|
2009-02-08 06:45:59 -05:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxCmdLineArgsArray);
|
2007-11-12 17:32:23 -05:00
|
|
|
};
|
|
|
|
|
2007-11-13 08:27:30 -05:00
|
|
|
// provide global operator overload for compatibility with the existing code
|
|
|
|
// doing things like "if ( condition && argv )"
|
2007-11-13 10:11:26 -05:00
|
|
|
inline bool operator&&(bool cond, const wxCmdLineArgsArray& array)
|
2007-11-13 08:27:30 -05:00
|
|
|
{
|
|
|
|
return cond && !array.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2007-11-12 17:32:23 -05:00
|
|
|
#endif // wxUSE_UNICODE
|
|
|
|
|
|
|
|
#endif // _WX_CMDARGS_H_
|
|
|
|
|