Salome HOME
fix bug of invalid file read - a garbage is in the end of string data
[modules/geom.git] / src / XAO / tests / TestUtils.hxx
1 #ifndef __XAO_TESTUTILS_HXX__
2 #define __XAO_TESTUTILS_HXX__
3
4 #include <fstream>
5 #include <cstdlib>
6
7 namespace XAO
8 {
9     class TestUtils
10     {
11     public:
12         static std::string getTestFilePath(const std::string& fileName)
13         {
14             std::string dataDir = getenv("GEOM_SRC_DIR");
15             dataDir += "/src/XAO/tests/data/";
16             dataDir += fileName;
17             return dataDir;
18         }
19
20         static char* readTextFile(const std::string& filePath)
21         {
22             std::ifstream rstr;
23             int length;
24             rstr.open(filePath.c_str());
25             rstr.seekg(0, rstr.end);        // go to the end
26             length = rstr.tellg();          // report location (this is the length)
27             printf("---------------------------VSR: length=%ld\n", length);
28             rstr.seekg(0, rstr.beg);        // go back to the beginning
29             char* txt = new char[length+1]; // allocate memory for a buffer of appropriate dimension
30             rstr.read(txt, length);         // read the whole file into the buffer
31             txt[length] = '\0';
32             rstr.close();
33
34             return txt;
35         }
36     };
37 }
38
39 #endif /* __XAO_TESTUTILS_HXX__ */