Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
K
KWSys
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Rolf Eike Beer
KWSys
Commits
2426b57d
Commit
2426b57d
authored
11 years ago
by
Clinton Stimpson
Browse files
Options
Downloads
Patches
Plain Diff
Encoding: Add support for program arguments argc/argv.
Change-Id: Ifda279df9a68872cf16e580274dff534f6cfd855
parent
88165c5e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Encoding.hxx.in
+31
-0
31 additions, 0 deletions
Encoding.hxx.in
EncodingCXX.cxx
+93
-0
93 additions, 0 deletions
EncodingCXX.cxx
testEncoding.cxx
+31
-0
31 additions, 0 deletions
testEncoding.cxx
with
155 additions
and
0 deletions
Encoding.hxx.in
+
31
−
0
View file @
2426b57d
...
...
@@ -14,6 +14,7 @@
#include <@KWSYS_NAMESPACE@/Configure.hxx>
#include <@KWSYS_NAMESPACE@/stl/string>
#include <@KWSYS_NAMESPACE@/stl/vector>
/* Define these macros temporarily to keep the code readable. */
#if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
...
...
@@ -25,6 +26,36 @@ namespace @KWSYS_NAMESPACE@
class @KWSYS_NAMESPACE@_EXPORT Encoding
{
public:
// Container class for argc/argv.
class CommandLineArguments
{
public:
// On Windows, get the program command line arguments
// in this Encoding module's 8 bit encoding.
// On other platforms the given argc/argv is used, and
// to be consistent, should be the argc/argv from main().
static CommandLineArguments Main(int argc, char const* const* argv);
// Construct CommandLineArguments with the given
// argc/argv. It is assumed that the string is already
// in the encoding used by this module.
CommandLineArguments(int argc, char const* const* argv);
// Construct CommandLineArguments with the given
// argc and wide argv. This is useful if wmain() is used.
CommandLineArguments(int argc, wchar_t const* const* argv);
~CommandLineArguments();
CommandLineArguments(const CommandLineArguments&);
CommandLineArguments& operator=(const CommandLineArguments&);
int argc() const;
char const* const* argv() const;
protected:
std::vector<char*> argv_;
};
/**
* Convert between char and wchar_t
*/
...
...
This diff is collapsed.
Click to expand it.
EncodingCXX.cxx
+
93
−
0
View file @
2426b57d
...
...
@@ -29,6 +29,7 @@
#endif
#include
<stdlib.h>
#include
<string.h>
#ifdef _MSC_VER
# pragma warning (disable: 4786)
...
...
@@ -42,6 +43,98 @@
namespace
KWSYS_NAMESPACE
{
Encoding
::
CommandLineArguments
Encoding
::
CommandLineArguments
::
Main
(
int
argc
,
char
const
*
const
*
argv
)
{
#ifdef _WIN32
(
void
)
argc
;
(
void
)
argv
;
int
ac
;
LPWSTR
*
w_av
=
CommandLineToArgvW
(
GetCommandLineW
(),
&
ac
);
std
::
vector
<
std
::
string
>
av1
(
ac
);
std
::
vector
<
char
const
*>
av2
(
ac
);
for
(
int
i
=
0
;
i
<
ac
;
i
++
)
{
av1
[
i
]
=
ToNarrow
(
w_av
[
i
]);
av2
[
i
]
=
av1
[
i
].
c_str
();
}
LocalFree
(
w_av
);
return
CommandLineArguments
(
ac
,
&
av2
[
0
]);
#else
return
CommandLineArguments
(
argc
,
argv
);
#endif
}
Encoding
::
CommandLineArguments
::
CommandLineArguments
(
int
ac
,
char
const
*
const
*
av
)
{
this
->
argv_
.
resize
(
ac
+
1
);
for
(
int
i
=
0
;
i
<
ac
;
i
++
)
{
this
->
argv_
[
i
]
=
strdup
(
av
[
i
]);
}
this
->
argv_
[
ac
]
=
0
;
}
Encoding
::
CommandLineArguments
::
CommandLineArguments
(
int
ac
,
wchar_t
const
*
const
*
av
)
{
this
->
argv_
.
resize
(
ac
+
1
);
for
(
int
i
=
0
;
i
<
ac
;
i
++
)
{
this
->
argv_
[
i
]
=
kwsysEncoding_DupToNarrow
(
av
[
i
]);
}
this
->
argv_
[
ac
]
=
0
;
}
Encoding
::
CommandLineArguments
::~
CommandLineArguments
()
{
for
(
size_t
i
=
0
;
i
<
this
->
argv_
.
size
();
i
++
)
{
free
(
argv_
[
i
]);
}
}
Encoding
::
CommandLineArguments
::
CommandLineArguments
(
const
CommandLineArguments
&
other
)
{
this
->
argv_
.
resize
(
other
.
argv_
.
size
());
for
(
size_t
i
=
0
;
i
<
this
->
argv_
.
size
();
i
++
)
{
this
->
argv_
[
i
]
=
other
.
argv_
[
i
]
?
strdup
(
other
.
argv_
[
i
])
:
0
;
}
}
Encoding
::
CommandLineArguments
&
Encoding
::
CommandLineArguments
::
operator
=
(
const
CommandLineArguments
&
other
)
{
size_t
i
;
for
(
i
=
0
;
i
<
this
->
argv_
.
size
();
i
++
)
{
free
(
this
->
argv_
[
i
]);
}
this
->
argv_
.
resize
(
other
.
argv_
.
size
());
for
(
i
=
0
;
i
<
this
->
argv_
.
size
();
i
++
)
{
this
->
argv_
[
i
]
=
other
.
argv_
[
i
]
?
strdup
(
other
.
argv_
[
i
])
:
0
;
}
return
*
this
;
}
int
Encoding
::
CommandLineArguments
::
argc
()
const
{
return
static_cast
<
int
>
(
this
->
argv_
.
size
()
-
1
);
}
char
const
*
const
*
Encoding
::
CommandLineArguments
::
argv
()
const
{
return
&
this
->
argv_
[
0
];
}
#if KWSYS_STL_HAS_WSTRING
kwsys_stl
::
wstring
Encoding
::
ToWide
(
const
kwsys_stl
::
string
&
str
)
...
...
This diff is collapsed.
Click to expand it.
testEncoding.cxx
+
31
−
0
View file @
2426b57d
...
...
@@ -145,6 +145,36 @@ static int testRobustEncoding()
return
ret
;
}
static
int
testCommandLineArguments
()
{
int
status
=
0
;
char
const
*
argv
[
2
]
=
{
"./app.exe"
,
(
char
const
*
)
helloWorldStrings
[
1
]
};
kwsys
::
Encoding
::
CommandLineArguments
args
(
2
,
argv
);
kwsys
::
Encoding
::
CommandLineArguments
arg2
=
kwsys
::
Encoding
::
CommandLineArguments
(
args
);
char
const
*
const
*
u8_argv
=
args
.
argv
();
for
(
int
i
=
0
;
i
<
args
.
argc
();
i
++
)
{
char
const
*
u8_arg
=
u8_argv
[
i
];
if
(
strcmp
(
argv
[
i
],
u8_arg
)
!=
0
)
{
std
::
cout
<<
"argv["
<<
i
<<
"] "
<<
argv
[
i
]
<<
" != "
<<
u8_arg
<<
std
::
endl
;
status
++
;
}
}
kwsys
::
Encoding
::
CommandLineArguments
args3
=
kwsys
::
Encoding
::
CommandLineArguments
::
Main
(
2
,
argv
);
return
status
;
}
//----------------------------------------------------------------------------
int
testEncoding
(
int
,
char
*
[])
...
...
@@ -163,6 +193,7 @@ int testEncoding(int, char*[])
ret
|=
testHelloWorldEncoding
();
ret
|=
testRobustEncoding
();
ret
|=
testCommandLineArguments
();
return
ret
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment