Skip to content

Commit

Permalink
Handle wildcard result filenames properly.
Browse files Browse the repository at this point in the history
This was appending a newline for convenience in the argv stuff, but that
confuses the string parsing when checking extensions - because it had a
null at the end.

Fixes #10.
  • Loading branch information
unknownbrackets committed Mar 24, 2016
1 parent e08a2ed commit c0dc52c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cli/winargs.cpp
Expand Up @@ -15,15 +15,15 @@ static std::vector<std::string> args;
static std::vector<char *> arg_pointers;

// Note: includes the null terminator.
void str_to_utf8(const wchar_t *argw, std::string &arg) {
void str_to_utf8z(const wchar_t *argw, std::string &arg) {
int bytes = WideCharToMultiByte(CP_UTF8, 0, argw, -1, nullptr, 0, nullptr, nullptr);

arg.resize(bytes);
WideCharToMultiByte(CP_UTF8, 0, argw, -1, &arg[0], bytes, nullptr, nullptr);
}

// Note: includes the null terminator.
void str_to_utf16(const char *arg, std::wstring &argw) {
void str_to_utf16z(const char *arg, std::wstring &argw) {
int chars = MultiByteToWideChar(CP_UTF8, 0, arg, -1, nullptr, 0);

argw.resize(chars);
Expand All @@ -39,7 +39,7 @@ char **winargs_get_utf8(int &argc) {
arg_pointers.resize(argc);

for (int i = 0; i < argc; ++i) {
str_to_utf8(argvw[i], args[i]);
str_to_utf8z(argvw[i], args[i]);
// Note: already has a null terminator.
arg_pointers[i] = &args[i][0];
}
Expand Down
4 changes: 2 additions & 2 deletions cli/winargs.h
Expand Up @@ -3,8 +3,8 @@
#include <string>

#ifdef _WIN32
void str_to_utf8(const wchar_t *argw, std::string &arg);
void str_to_utf16(const char *arg, std::wstring &argw);
void str_to_utf8z(const wchar_t *argw, std::string &arg);
void str_to_utf16z(const char *arg, std::wstring &argw);

char **winargs_get_utf8(int &argc);
#endif
8 changes: 6 additions & 2 deletions cli/winglob.cpp
Expand Up @@ -10,7 +10,7 @@

void winargs_get_wildcard(const char *arg, std::vector<std::string> &files) {
std::wstring argw;
str_to_utf16(arg, argw);
str_to_utf16z(arg, argw);

CFileFind finder;
if (!finder.FindFile(argw.c_str())) {
Expand All @@ -26,7 +26,11 @@ void winargs_get_wildcard(const char *arg, std::vector<std::string> &files) {
}

std::string filename;
str_to_utf8(finder.GetFilePath().GetString(), filename);
str_to_utf8z(finder.GetFilePath().GetString(), filename);
// Now truncate off the null - we don't need it here.
if (!filename.empty()) {
filename.resize(filename.size() - 1);
}
files.push_back(filename);
}
}
Expand Down

0 comments on commit c0dc52c

Please sign in to comment.