testdata.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <string>
  15. #include <stdexcept>
  16. #include <fstream>
  17. #include <vector>
  18. #include "testdata.h"
  19. using namespace std;
  20. namespace {
  21. vector<string>&
  22. getDataPaths() {
  23. static vector<string> data_path;
  24. return (data_path);
  25. }
  26. }
  27. namespace isc {
  28. namespace util {
  29. namespace unittests {
  30. void
  31. addTestDataPath(const string& path) {
  32. getDataPaths().push_back(path);
  33. }
  34. void
  35. openTestData(const char* const datafile, ifstream& ifs) {
  36. vector<string>::const_iterator it = getDataPaths().begin();
  37. for (; it != getDataPaths().end(); ++it) {
  38. string data_path = *it;
  39. if (data_path.empty() || *data_path.rbegin() != '/') {
  40. data_path.push_back('/');
  41. }
  42. ifs.open((data_path + datafile).c_str(), ios_base::in);
  43. if (!ifs.fail()) {
  44. return;
  45. }
  46. }
  47. throw runtime_error("failed to open data file in data paths: " +
  48. string(datafile));
  49. }
  50. }
  51. }
  52. }