#include <stdlib.h>
#include <itksys/SystemTools.hxx>
const int NO_OUTPUT_OPTION=-1;
+const int TEST_EXITED=1;
int getOutputOptionIndex(int argc, char** argv){
for(int i=1; i<argc; i++){
std::string s = argv[i];
}
return NO_OUTPUT_OPTION;
}
-inline char* getTmpFileName(){
- char buffer [L_tmpnam];
- char* back = tmpnam (buffer);
- if(back==NULL){
- exit(1);
+
+std::string getTmpFileName(){
+ char fileName[] = "/tmp/vvTempXXXXXX";
+
+ #ifdef _WIN32
+ errno_t err = tmpfile_s(&fileName, strlen(fileName));
+ #else
+ int err=0;
+ int fd = mkstemp(fileName);
+ if(fd==-1) err=1;
+ #endif
+ if(err){
+ std::cout<<"couldnot create file. Exiting"<<std::endl;
+ exit(TEST_EXITED);
}
- return back;
+ return std::string(fileName);
}
+
+
/**
* argv
* [1] executable
std::ostringstream cmd_line;
cmd_line<<CLITK_TEST_TOOLS_PATH;
for(int i=1; i<argc-1; i++){
- //we should ensure the file exists, find an -i index or a long file name maybe?
+ //we should ensure the file exists, find an -i index or a long file name maybe?
cmd_line<<argv[i]<<" ";
}
//look for the need of generating an output file
int outputOptionIndex = getOutputOptionIndex(argc, argv);
- char* outFile;
+ std::string outFile;
if(NO_OUTPUT_OPTION==outputOptionIndex){
outFile = getTmpFileName();
- cmd_line<<" > "<<outFile;
+ std::cout<<outFile<<std::endl;
+ cmd_line<<">"<<outFile;
}else{
//todo test this else branch
- std::string s = std::string(CLITK_TEST_DATA_PATH);
- s+=argv[outputOptionIndex];
- //DO NOT MODIFY outFile
- outFile = (char*)s.c_str();
+ outFile = std::string(CLITK_TEST_DATA_PATH);
+ outFile += argv[outputOptionIndex];
}
-
//run the command line
system(cmd_line.str().c_str());
//files should be equal, so if this is the case return success=0
- int fail = (itksys::SystemTools::FilesDiffer(outFile, refFile))?1:0;
- remove(outFile);
+ int fail = (itksys::SystemTools::FilesDiffer(outFile.c_str(), refFile))?1:0;
+ remove(outFile.c_str());
return fail;
}