Files
ce85939bb0 Fix add_test calls and change temporary file creation. (#1522)
* * Fix add_test in cmake files. This notation allows it to understand that the command portion is a target and to translate it to the proper executable.
* Remove starting backslash if present from temporary filename. A starting backslash mean that it is safe to create it in the current working directory.
* Implement a public TempFile class.
* Change relevant tests to use TempFile class.

Signed-off-by: Patrick Northon <northon_patrick3@yahoo.ca>

* * Revert back changes for TempFile class.
* Add comment on temporary file functions fix.

Signed-off-by: Patrick Northon <northon_patrick3@yahoo.ca>

* Add FIXME comments.

Signed-off-by: Patrick Northon <northon_patrick3@yahoo.ca>

* Add -C flag to ctest commands.

Signed-off-by: Patrick Northon <northon_patrick3@yahoo.ca>

* Add `-C Release` to ctest commands in `analysis_workflow.yml`.

Signed-off-by: Patrick Northon <northon_patrick3@yahoo.ca>

Co-authored-by: Patrick Hodoul <patrick.hodoul@autodesk.com>
Co-authored-by: Rémi Achard <remiachard@gmail.com>
2021-11-04 12:58:38 -04:00

59 lines
1.3 KiB
C++

// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.
#include <stdio.h>
#include <fstream>
#if !defined(_WIN32)
#include <sstream>
#include <stdlib.h>
#include <time.h>
#endif
#include <OpenColorIO/OpenColorIO.h>
#include "GPUHelpers.h"
namespace OCIO = OCIO_NAMESPACE;
// TODO: Make OCIO::Platform::CreateTempFilename() public so it could be used here.
std::string createTempFile(const std::string& fileExt, const std::string& fileContent)
{
// Note: because of security issue, tmpnam could not be used
std::string filename;
#ifdef _WIN32
char tmpFilename[L_tmpnam];
if(tmpnam_s(tmpFilename))
{
throw OCIO::Exception("Could not create a temporary file");
}
// Note that when a file name is pre-pended with a backslash and no path information, such as \fname21, this
// indicates that the name is valid for the current working directory.
filename = tmpFilename[0] == '\\' ? tmpFilename + 1 : tmpFilename;
filename += fileExt;
#else
std::stringstream ss;
ss << "/tmp/ocio";
ss << std::rand();
ss << fileExt;
filename = ss.str();
#endif
std::ofstream ofs(filename.c_str(), std::ios_base::out);
ofs << fileContent;
ofs.close();
return filename;
}