Dune Core Modules (2.5.0)

checkvtkfile.hh
1#ifndef DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
2#define DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
3
4#include <cstddef>
5#include <cstdlib>
6#include <iostream>
7#include <iomanip>
8#include <ios>
9#include <iostream>
10#include <ostream>
11#include <sstream>
12#include <string>
13
14#include <sys/wait.h>
15
17
18// quote so the result can be used inside '...' in python
19// quotes not included in the result
20inline std::string pyq(const std::string &s)
21{
22 std::ostringstream result;
23 for(std::size_t i = 0; i < s.size(); ++i)
24 {
25 char c = s[i];
26 switch(c) {
27 case '\'': result << "\\'"; break;
28 case '\\': result << "\\\\"; break;
29 case '\n': result << "\\n"; break;
30 default:
31 if(c < 32 || c >= 127)
32 result << "\\x" << std::hex << std::setfill('0') << std::setw(2)
33 << static_cast<int>(c);
34 else
35 result << c;
36 }
37 }
38 return result.str();
39}
40
41// quote so the result can be used inside '...' in the bourne shell
42// quotes not included in the result
43inline std::string shq(const std::string &s)
44{
45 std::ostringstream result;
46 bool pend = false;
47 for(std::size_t i = 0; i < s.size(); ++i)
48 {
49 char c = s[i];
50 switch(c) {
52 "Can't pass \\0 through the shell");
53 case '\'': result << (pend ? "" : "'") << "\\'"; pend = true; break;
54 default: result << (pend ? "'" : "") << c; pend = false; break;
55 }
56 }
57 if(pend) result << "'";
58 return result.str();
59}
60
61inline int runShell(const std::string &code)
62{
63 int result = std::system(code.c_str());
64 // Avoid returning anything that is a multiple of 256, unless the return
65 // value was 0. This way the return value can be directly used as an
66 // argument to exit(), which usually interprets its argument modulo 256.
67 if(WIFEXITED(result))
68 return WEXITSTATUS(result);
69 if(WIFSIGNALED(result))
70 return WTERMSIG(result) + 256;
71 else
72 return 513;
73}
74
75inline int runPython(const std::string &code)
76{
77 return runShell("python -c '"+shq(code)+"'");
78}
79
80inline bool is_suffix(const std::string &s, const std::string &suffix)
81{
82 return s.size() >= suffix.size() &&
83 s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
84}
85
86inline int checkVTKFile(const std::string &name)
87{
88 static const bool havePythonVTK = [] {
89 // This check is invoked only once, even in a multithreading environment,
90 // since it is invoked in the initializer of a static variable.
91 if(runPython("from vtk import *") == 0)
92 return true;
93 std::cerr << "warning: python or python vtk module not available. This "
94 << "will result in skipped tests, since we cannot check that "
95 << "vtk can read the files we wrote." << std::endl;
96 return false;
97 } ();
98 if(!havePythonVTK)
99 {
100 std::cerr << "skip: " << name << std::endl;
101 return 77;
102 }
103
104 std::string reader;
105 if (is_suffix(name, ".vtu")) reader = "vtkXMLUnstructuredGridReader";
106 else if(is_suffix(name, ".pvtu")) reader = "vtkXMLPUnstructuredGridReader";
107 else if(is_suffix(name, ".vtp")) reader = "vtkXMLPolyDataReader";
108 else if(is_suffix(name, ".pvtp")) reader = "vtkXMLPPolyDataReader";
110 "Unknown vtk file extension: " << name);
111
112 std::cout << "Loading " << name << " using python vtk" << std::endl;
113 std::string pycode =
114 "from vtk import *;"
115 "import sys;"
116 "reader = "+reader+"();"
117 "reader.SetFileName('"+pyq(name)+"');"
118 "reader.Update();"
119 // check that the number of of cells is > 0
120 "sys.exit(not (reader.GetOutput().GetNumberOfCells() > 0));";
121 int result = runPython(pycode);
122 std::cout << (result == 0 ? "pass: " : "fail: ") << name << std::endl;
123 return result;
124}
125
126#endif // DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
Default exception for dummy implementations.
Definition: exceptions.hh:261
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)