bes  Updated for version 3.20.6
read_test_baseline.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of the BES
4 
5 // Copyright (c) 2018 OPeNDAP, Inc.
6 // Author: James Gallagher <jgallagher@opendap.org>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 //
22 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
23 
24 #include "config.h"
25 
26 #include <fstream>
27 #include <string>
28 #include <vector>
29 
30 #include <cstdlib> // for system
31 
32 #include "BESInternalError.h"
33 
34 #include "read_test_baseline.h"
35 
36 using namespace std;
37 
38 namespace bes {
48 string
49 read_test_baseline(const string &fn)
50 {
51  int length;
52 
53  ifstream is;
54  is.open (fn.c_str(), ios::binary );
55 
56  if (!is)
57  return "Could not read baseline file: "+fn;
58 
59  // get length of file:
60  is.seekg (0, ios::end);
61  length = is.tellg();
62 
63  // back to start
64  is.seekg (0, ios::beg);
65 
66  // allocate memory:
67  vector<char> buffer(length+1);
68 
69  // read data as a block:
70  is.read (&buffer[0], length);
71  is.close();
72  buffer[length] = '\0';
73 
74  return string(&buffer[0]);
75 }
76 
77 void clean_cache_dir(const string &cache)
78 {
79  string cache_dir = cache + "/*";
80 
81  string command = string("rm ") + cache_dir + " 2>/dev/null";
82 
83  int status = system(command.c_str());
84 
85  // it's normal for this to 'fail' because the clean operation has already
86  // been run or because it's the first run of the tests. But, fork and waitpid
87  // should not return an error and the shell should be found.
88  if (status == -1 || status == 127)
89  throw BESInternalError("Failed to clean cache dir: " + cache_dir, __FILE__, __LINE__);
90 }
91 
92 } // namespace bes
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43