Commit 1fafc38e authored by Milo Yip's avatar Milo Yip

Merge pull request #122 from miloyip/issue114tmpnam

Replace tmpname() by mkstemp() on non-MSVC compilers
parents e27f7e71 6f4c83ad
......@@ -158,9 +158,7 @@ protected:
// Test FileWriteStream
{
char filename[L_tmpnam];
TempFilename(filename);
FILE *fp = fopen(filename, "wb");
FILE* fp = TempFile(filename);
char buffer[16];
FileWriteStream os(fp, buffer, sizeof(buffer));
EncodedOutputStream<FileEncoding, FileWriteStream> eos(os, putBOM);
......@@ -193,9 +191,8 @@ protected:
// Test FileWriteStream
{
char filename[L_tmpnam];
TempFilename(filename);
FILE* fp = TempFile(filename);
FILE *fp = fopen(filename, "wb");
char buffer[16];
FileWriteStream os(fp, buffer, sizeof(buffer));
AutoUTFOutputStream<unsigned, FileWriteStream> eos(os, type, putBOM);
......
......@@ -98,9 +98,8 @@ TEST_F(FileStreamTest, FileReadStream) {
TEST_F(FileStreamTest, FileWriteStream) {
char filename[L_tmpnam];
TempFilename(filename);
FILE* fp = TempFile(filename);
FILE *fp = fopen(filename, "wb");
char buffer[65536];
FileWriteStream os(fp, buffer, sizeof(buffer));
for (size_t i = 0; i < length_; i++)
......
......@@ -67,13 +67,21 @@ inline Ch* StrDup(const Ch* str) {
return buffer;
}
inline void TempFilename(char *filename) {
inline FILE* TempFile(char *filename) {
#if _MSC_VER
filename = tmpnam(filename);
// For Visual Studio, tmpnam() adds a backslash in front. Remove it.
if (filename[0] == '\\')
for (int i = 0; filename[i] != '\0'; i++)
filename[i] = filename[i + 1];
return fopen(filename, "wb");
#else
strcpy(filename, "/tmp/fileXXXXXX");
int fd = mkstemp(filename);
return fdopen(fd, "w");
#endif
}
// Use exception for catching assert
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment