@@ -667,6 +667,30 @@ static std::string toString(const T &t) {
667667void prepareOpts (int argc, char * argv[]);
668668#endif
669669
670+ FILE* testlib_fopen_ (const char * path, const char * mode) {
671+ #ifdef _MSC_VER
672+ FILE* result = NULL ;
673+ if (fopen_s (&result, path, mode) != 0 )
674+ return NULL ;
675+ else
676+ return result;
677+ #else
678+ return std::fopen (path, mode);
679+ #endif
680+ }
681+
682+ FILE* testlib_freopen_ (const char * path, const char * mode, FILE* file) {
683+ #ifdef _MSC_VER
684+ FILE* result = NULL ;
685+ if (freopen_s (&result, path, mode, file) != 0 )
686+ return NULL ;
687+ else
688+ return result;
689+ #else
690+ return std::freopen (path, mode, file);
691+ #endif
692+ }
693+
670694/*
671695 * Very simple regex-like pattern.
672696 * It used for two purposes: validation and generation.
@@ -1417,7 +1441,11 @@ static void __pattern_scanCounts(const std::string &s, size_t &pos, int &from, i
14171441 if (parts[i].length () == 0 )
14181442 __testlib_fail (" pattern: Illegal pattern (or part) \" " + s + " \" " );
14191443 int number;
1444+ #ifdef _MSC_VER
1445+ if (sscanf_s (parts[i].c_str (), " %d" , &number) != 1 )
1446+ #else
14201447 if (std::sscanf (parts[i].c_str (), " %d" , &number) != 1 )
1448+ #endif
14211449 __testlib_fail (" pattern: Illegal pattern (or part) \" " + s + " \" " );
14221450 numbers.push_back (number);
14231451 }
@@ -2633,7 +2661,7 @@ class Validator {
26332661 else if (fileName == " stderr" )
26342662 f = stderr, standard_file = true ;
26352663 else {
2636- f = fopen (fileName.c_str (), " wb" );
2664+ f = testlib_fopen_ (fileName.c_str (), " wb" );
26372665 if (NULL == f)
26382666 __testlib_fail (" Validator::writeTestOverviewLog: can't write test overview log to (" + fileName + " )" );
26392667 }
@@ -2676,7 +2704,7 @@ class Validator {
26762704 else if (_testMarkupFileName == " stderr" )
26772705 f = stderr, standard_file = true ;
26782706 else {
2679- f = fopen (_testMarkupFileName.c_str (), " wb" );
2707+ f = testlib_fopen_ (_testMarkupFileName.c_str (), " wb" );
26802708 if (NULL == f)
26812709 __testlib_fail (" Validator::writeTestMarkup: can't write test markup to (" + _testMarkupFileName + " )" );
26822710 }
@@ -2723,7 +2751,7 @@ class Validator {
27232751 else if (_testCaseFileName == " stderr" )
27242752 f = stderr, standard_file = true ;
27252753 else {
2726- f = fopen (_testCaseFileName.c_str (), " wb" );
2754+ f = testlib_fopen_ (_testCaseFileName.c_str (), " wb" );
27272755 if (NULL == f)
27282756 __testlib_fail (" Validator::writeTestCase: can't write test case to (" + _testCaseFileName + " )" );
27292757 }
@@ -3129,7 +3157,7 @@ NORETURN void InStream::quit(TResult result, const char *msg) {
31293157 }
31303158
31313159 if (resultName != " " ) {
3132- resultFile = std::fopen (resultName.c_str (), " w" );
3160+ resultFile = testlib_fopen_ (resultName.c_str (), " w" );
31333161 if (resultFile == NULL ) {
31343162 resultName = " " ;
31353163 quit (_fail, " Can not write to the result file" );
@@ -3246,7 +3274,7 @@ void InStream::reset(std::FILE *file) {
32463274 close ();
32473275
32483276 if (!stdfile && NULL == file)
3249- if (NULL == (file = std::fopen (name.c_str (), " rb" ))) {
3277+ if (NULL == (file = testlib_fopen_ (name.c_str (), " rb" ))) {
32503278 if (mode == _output)
32513279 quits (_pe, std::string (" Output file not found: \" " ) + name + " \" " );
32523280
@@ -3651,7 +3679,12 @@ static inline double stringToDouble(InStream &in, const char *buffer) {
36513679
36523680 char *suffix = new char [length + 1 ];
36533681 std::memset (suffix, 0 , length + 1 );
3654- int scanned = std::sscanf (buffer, " %lf%s" , &result, suffix);
3682+ int scanned;
3683+ #ifdef _MSC_VER
3684+ scanned = sscanf_s (buffer, " %lf%s" , &result, suffix, length + 1 );
3685+ #else
3686+ scanned = std::sscanf (buffer, " %lf%s" , &result, suffix);
3687+ #endif
36553688 bool empty = strlen (suffix) == 0 ;
36563689 delete[] suffix;
36573690
@@ -3728,7 +3761,12 @@ static inline double stringToStrictDouble(InStream &in, const char *buffer,
37283761
37293762 char *suffix = new char [length + 1 ];
37303763 std::memset (suffix, 0 , length + 1 );
3731- int scanned = std::sscanf (buffer, " %lf%s" , &result, suffix);
3764+ int scanned;
3765+ #ifdef _MSC_VER
3766+ scanned = sscanf_s (buffer, " %lf%s" , &result, suffix, length + 1 );
3767+ #else
3768+ scanned = std::sscanf (buffer, " %lf%s" , &result, suffix);
3769+ #endif
37323770 bool empty = strlen (suffix) == 0 ;
37333771 delete[] suffix;
37343772
@@ -5007,7 +5045,7 @@ void srand(unsigned int seed) RAND_THROW_STATEMENT
50075045
50085046void startTest (int test) {
50095047 const std::string testFileName = vtos (test);
5010- if (NULL == freopen (testFileName.c_str (), " wt" , stdout))
5048+ if (NULL == testlib_freopen_ (testFileName.c_str (), " wt" , stdout))
50115049 __testlib_fail (" Unable to write file '" + testFileName + " '" );
50125050}
50135051
@@ -6000,7 +6038,11 @@ double deserializePoints(std::string s) {
60006038 return std::numeric_limits<double >::quiet_NaN ();
60016039 else {
60026040 double result;
6003- ensuref (sscanf (s.c_str (), " %lf" , &result) == 1 , " Invalid serialized points" );
6041+ #ifdef _MSC_VER
6042+ ensuref (sscanf_s (s.c_str (), " %lf" , &result) == 1 , " Invalid serialized points" );
6043+ #else
6044+ ensuref (std::sscanf (s.c_str (), " %lf" , &result) == 1 , " Invalid serialized points" );
6045+ #endif
60046046 return result;
60056047 }
60066048}
0 commit comments