Salome HOME
a3c97f6240bc80cfeec7b840c8472daace897e49
[modules/geom.git] / src / XAO / tests / TestUtils.hxx
1 // Copyright (C) 2013-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef __XAO_TESTUTILS_HXX__
21 #define __XAO_TESTUTILS_HXX__
22
23 #include <fstream>
24 #include <cstdlib>
25
26 namespace XAO
27 {
28     class TestUtils
29     {
30     public:
31         static std::string getTestFilePath(const std::string& fileName)
32         {
33             std::string dataDir = "";
34             char* geomSrcDir = getenv("XAO_DATA_DIR");
35             if (geomSrcDir != NULL)
36             {
37                 dataDir = geomSrcDir;
38 #if defined _WIN32 || defined __CYGWIN__
39                 dataDir += "\\";
40 #else
41                 dataDir += "/";
42 #endif
43                 dataDir += fileName;
44             }
45
46             return dataDir;
47         }
48
49         static char* readTextFile(const std::string& filePath)
50         {
51             std::ifstream rstr;
52             int length;
53             rstr.open(filePath.c_str(), std::ios_base::binary);
54             rstr.seekg(0, rstr.end);        // go to the end
55             length = rstr.tellg();          // report location (this is the length)
56             rstr.seekg(0, rstr.beg);        // go back to the beginning
57             char* txt = new char[length+1]; // allocate memory for a buffer of appropriate dimension
58             rstr.read(txt, length);         // read the whole file into the buffer
59             txt[length] = '\0';
60             rstr.close();
61
62             return txt;
63         }
64     };
65 }
66
67 #endif /* __XAO_TESTUTILS_HXX__ */