| 1 |
/* Copyright (c) 2007, Google Inc. |
| 2 |
* All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are |
| 6 |
* met: |
| 7 |
* |
| 8 |
* * Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* * Redistributions in binary form must reproduce the above |
| 11 |
* copyright notice, this list of conditions and the following disclaimer |
| 12 |
* in the documentation and/or other materials provided with the |
| 13 |
* distribution. |
| 14 |
* * Neither the name of Google Inc. nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from |
| 16 |
* this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
* |
| 30 |
* --- |
| 31 |
* Author: Craig Silverstein |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "windows/config.h" |
| 35 |
#ifndef _WIN32 |
| 36 |
# error You should only be including windows/port.cc in a windows environment! |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <stdarg.h> // for va_list, va_start, va_end |
| 40 |
#include <string.h> // for strstr() |
| 41 |
#include <assert.h> |
| 42 |
#include "port.h" |
| 43 |
|
| 44 |
// These call the windows _vsnprintf, but always NUL-terminate. |
| 45 |
#if !defined(__MINGW32__) && !defined(__MINGW64__) && (!defined(_MSC_VER) || _MSC_VER < 1900) /* mingw already defines */ |
| 46 |
int safe_vsnprintf(char *str, size_t size, const char *format, va_list ap) { |
| 47 |
if (size == 0) // not even room for a \0? |
| 48 |
return -1; // not what C99 says to do, but what windows does |
| 49 |
str[size-1] = '\0'; |
| 50 |
return _vsnprintf(str, size-1, format, ap); |
| 51 |
} |
| 52 |
|
| 53 |
int snprintf(char *str, size_t size, const char *format, ...) { |
| 54 |
int r; |
| 55 |
va_list ap; |
| 56 |
va_start(ap, format); |
| 57 |
r = vsnprintf(str, size, format, ap); |
| 58 |
va_end(ap); |
| 59 |
return r; |
| 60 |
} |
| 61 |
#endif /* #if !defined(__MINGW32__) && !defined(__MINGW64__) */ |
| 62 |
|
| 63 |
#ifdef __cplusplus |
| 64 |
#include <string> |
| 65 |
#include <vector> |
| 66 |
#include <ctemplate/template_pathops.h> |
| 67 |
|
| 68 |
using std::string; |
| 69 |
using std::vector; |
| 70 |
|
| 71 |
namespace ctemplate { |
| 72 |
|
| 73 |
// defined (for unix) in template_test_utils.cc |
| 74 |
string TmpFile(const char* basename) { |
| 75 |
char tmppath_buffer[1024]; |
| 76 |
int tmppath_len = GetTempPathA(sizeof(tmppath_buffer), tmppath_buffer); |
| 77 |
if (tmppath_len <= 0 || tmppath_len >= sizeof(tmppath_buffer)) { |
| 78 |
return basename; // an error, so just bail on tmppath |
| 79 |
} |
| 80 |
assert(tmppath_buffer[tmppath_len - 1] == '\\'); // API guarantees it |
| 81 |
return string(tmppath_buffer) + basename; |
| 82 |
} |
| 83 |
|
| 84 |
// A replacement for template_unittest.cc:CleanTestDir() |
| 85 |
void CreateOrCleanTestDir(const string& dirname) { |
| 86 |
string glob(PathJoin(dirname, "*")); |
| 87 |
WIN32_FIND_DATAA found; // that final A is for Ansi (as opposed to Unicode) |
| 88 |
HANDLE hFind = FindFirstFileA(glob.c_str(), &found); // A is for Ansi |
| 89 |
if (hFind == INVALID_HANDLE_VALUE) { // directory doesn't exist or some such |
| 90 |
_mkdir(dirname.c_str()); |
| 91 |
hFind = FindFirstFileA(glob.c_str(), &found); // Try again... |
| 92 |
} |
| 93 |
if (hFind != INVALID_HANDLE_VALUE) { |
| 94 |
do { |
| 95 |
if (strstr(found.cFileName, "template")) |
| 96 |
_unlink(PathJoin(dirname, found.cFileName).c_str()); |
| 97 |
} while (FindNextFileA(hFind, &found) != FALSE); // A is for Ansi |
| 98 |
FindClose(hFind); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
void GetNamelist(const char* testdata_dir, vector<string>* namelist) { |
| 105 |
string glob(ctemplate::PathJoin(testdata_dir, |
| 106 |
"template_unittest_test*")); |
| 107 |
WIN32_FIND_DATAA found; // that final A is for Ansi (as opposed to Unicode) |
| 108 |
HANDLE hFind = FindFirstFileA(glob.c_str(), &found); |
| 109 |
if (hFind == INVALID_HANDLE_VALUE) // no files matching the glob, probably |
| 110 |
return; // if we don't find any files, nothing to add to namelist |
| 111 |
do { |
| 112 |
namelist->push_back(found.cFileName); |
| 113 |
} while (FindNextFileA(hFind, &found) != FALSE); // A is for Ansi |
| 114 |
FindClose(hFind); |
| 115 |
} |
| 116 |
|
| 117 |
#endif /* __cplusplus */ |