Dune Core Modules (2.4.1)

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 = (runPython("from vtk import *") == 0);
89 if(!havePythonVTK)
90 {
91 std::cerr << "warning: python or python vtk module not available"
92 << std::endl;
93 std::cerr << "skip: " << name << std::endl;
94 return 77;
95 }
96
97 std::string reader;
98 if (is_suffix(name, ".vtu")) reader = "vtkXMLUnstructuredGridReader";
99 else if(is_suffix(name, ".pvtu")) reader = "vtkXMLPUnstructuredGridReader";
100 else if(is_suffix(name, ".vtp")) reader = "vtkXMLPolyDataReader";
101 else if(is_suffix(name, ".pvtp")) reader = "vtkXMLPPolyDataReader";
103 "Unknown vtk file extension: " << name);
104
105 std::cout << "Loading " << name << " using python vtk" << std::endl;
106 std::string pycode =
107 "from vtk import *;"
108 "import sys;"
109 "reader = "+reader+"();"
110 "reader.SetFileName('"+pyq(name)+"');"
111 "reader.Update();"
112 // check that the number of of cells is > 0
113 "sys.exit(not (reader.GetOutput().GetNumberOfCells() > 0));";
114 int result = runPython(pycode);
115 std::cout << (result == 0 ? "pass: " : "fail: ") << name << std::endl;
116 return result;
117}
118
119#endif // DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
Default exception for dummy implementations.
Definition: exceptions.hh:288
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 22, 23:30, 2024)