1 // Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <string>
10 #include <stdexcept>
11 #include <fstream>
12 #include <vector>
13 
14 #include <util/unittests/testdata.h>
15 
16 using namespace std;
17 
18 namespace {
19 vector<string>&
getDataPaths()20 getDataPaths() {
21     static vector<string> data_path;
22     return (data_path);
23 }
24 }
25 
26 namespace isc {
27 namespace util {
28 namespace unittests {
29 
30 void
addTestDataPath(const string & path)31 addTestDataPath(const string& path) {
32     getDataPaths().push_back(path);
33 }
34 
35 void
openTestData(const char * const datafile,ifstream & ifs)36 openTestData(const char* const datafile, ifstream& ifs) {
37     vector<string>::const_iterator it = getDataPaths().begin();
38     for (; it != getDataPaths().end(); ++it) {
39         string data_path = *it;
40         if (data_path.empty() || *data_path.rbegin() != '/') {
41             data_path.push_back('/');
42         }
43         ifs.open((data_path + datafile).c_str(), ios_base::in);
44         if (!ifs.fail()) {
45             return;
46         }
47     }
48 
49     throw runtime_error("failed to open data file in data paths: " +
50                         string(datafile));
51 }
52 
53 }
54 }
55 }
56