Debian Codegraph @

packages/seacas/applications/exodiff/util.C

raw
1 // Copyright(C) 1999-2021, 2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "fmt/color.h"
8 #include "fmt/ostream.h"
9 #include "util.h"
10 #include <cstring> // for nullptr, memset
11 #include <iostream>
12 #include <stringx.h>
13 #include <unistd.h>
14
15 #include "ED_SystemInterface.h" // for SystemInterface, interFace
16
17 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
18 defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
19 #include <io.h>
20 #define isatty _isatty
21 #endif
22
23 int name_length()
24 {
25 static int max_name_length = -1;
26 if (max_name_length < 0) {
27 max_name_length = std::max(max_name_length, max_string_length(interFace.glob_var_names));
28 max_name_length = std::max(max_name_length, max_string_length(interFace.node_var_names));
29 max_name_length = std::max(max_name_length, max_string_length(interFace.elmt_var_names));
30 max_name_length = std::max(max_name_length, max_string_length(interFace.elmt_att_names));
31 max_name_length = std::max(max_name_length, max_string_length(interFace.ns_var_names));
32 max_name_length = std::max(max_name_length, max_string_length(interFace.ss_var_names));
33 max_name_length = std::max(max_name_length, max_string_length(interFace.eb_var_names));
34 max_name_length = std::max(max_name_length, max_string_length(interFace.fb_var_names));
35 max_name_length++;
36 }
37 return max_name_length;
38 }
39
40 char **get_name_array(int size, int length)
41 {
42 char **names = nullptr;
43 if (size > 0) {
44 names = new char *[size];
45 for (int i = 0; i < size; i++) {
46 names[i] = new char[length + 1];
47 std::memset(names[i], '\0', length + 1);
48 }
49 }
50 return names;
51 }
52
53 void free_name_array(char **names, int size)
54 {
55 for (int i = 0; i < size; i++) {
56 delete[] names[i];
57 }
58 delete[] names;
59 }
60
61 namespace {
62 bool term_out()
63 {
64 static bool is_term = isatty(fileno(stdout));
65 return is_term;
66 }
67
68 bool cerr_out()
69 {
70 static bool is_term = isatty(fileno(stderr));
71 return is_term;
72 }
73 } // namespace
74
75 void Error(std::ostringstream &x)
76 {
77 std::ostringstream out;
78 fmt::print(out, "exodiff: ERROR: {}", x.str());
79 ERR_OUT(out);
80 exit(EXIT_FAILURE);
81 }
82
83 void Error(const std::string &x)
84 {
85 std::ostringstream out;
86 fmt::print(out, "exodiff: ERROR: {}", x);
87 ERR_OUT(out);
88 exit(EXIT_FAILURE);
89 }
90
91 void Warning(const std::string &x)
92 {
93 std::ostringstream out;
94 fmt::print(out, "exodiff: WARNING: {}", x);
95 WARN_OUT(out);
96 }
97
98 void ERR_OUT(std::ostringstream &buf)
99 {
100 if (cerr_out()) {
101 fmt::print(stderr, fmt::fg(fmt::color::red), "{}", buf.str());
102 }
103 else {
104 fmt::print(stderr, "{}", buf.str());
105 }
106 }
107
108 void WARN_OUT(std::ostringstream &buf)
109 {
110 if (cerr_out()) {
111 fmt::print(stderr, fmt::fg(fmt::color::yellow), "{}", buf.str());
112 }
113 else {
114 fmt::print(stderr, "{}", buf.str());
115 }
116 }
117
118 void DIFF_OUT(std::ostringstream &buf, fmt::detail::color_type color)
119 {
120 if (term_out()) {
121 fmt::print(fmt::fg(color), "{}\n", buf.str());
122 }
123 else {
124 fmt::print("{}\n", buf.str());
125 }
126 }
127
128 void DIFF_OUT(const std::string &buf, fmt::detail::color_type color)
129 {
130 if (term_out()) {
131 fmt::print(fmt::fg(color), "{}\n", buf);
132 }
133 else {
134 fmt::print("{}\n", buf);
135 }
136 }