File: vtkTestUtilities.h

package info (click to toggle)
vtk9 9.3.0%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 267,116 kB
  • sloc: cpp: 2,195,914; ansic: 285,452; python: 104,858; sh: 4,061; yacc: 4,035; java: 3,977; xml: 2,771; perl: 2,189; lex: 1,762; objc: 153; makefile: 150; javascript: 90; tcl: 59
file content (151 lines) | stat: -rw-r--r-- 4,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
/**
 * @class   vtkTestUtilities
 * @brief   Utility functions used for regression testing.
 *
 * vtkTestUtilities provides methods to perform common testing operations.
 * These include getting a command line argument or an environment variable,
 * or a default value. Particularly, there are specialized methods to get the
 * root directory for VTK Data, expanding a filename with this root directory.
 */

#ifndef vtkTestUtilities_h
#define vtkTestUtilities_h

#include "vtkSystemIncludes.h"

#if defined(_MSC_VER)           /* Visual C++ (and Intel C++) */
#pragma warning(disable : 4996) // 'function': was declared deprecated
#endif

VTK_ABI_NAMESPACE_BEGIN
struct vtkTestUtilities
{
  /**
   * Function necessary for accessing the root directory for VTK data. Try the
   * -D command line argument or VTK_DATA_ROOT or a default value. The returned
   * string has to be deleted (with delete[]) by the user.
   */
  static inline char* GetDataRoot(int argc, char* argv[]);

  /**
   * Given a file name, this function returns a new string which is (in theory)
   * the full path. This path is constructed by prepending the file name with a
   * command line argument (-D path) or VTK_DATA_ROOT env. variable. If slash
   * is true, appends a slash to the resulting string. The returned string has
   * to be deleted (with delete[]) by the user.
   */
  static inline VTK_FILEPATH char* ExpandDataFileName(
    int argc, char* argv[], VTK_FILEPATH const char* fname, int slash = 0);
  /**
   * Function returning either a command line argument, an environment variable
   * or a default value. The returned string has to be deleted (with delete[])
   * by the user.
   */
  static inline char* GetArgOrEnvOrDefault(
    const char* arg, int argc, char* argv[], const char* env, const char* def);

  ///@{
  /**
   * Given a file name, this function returns a new string which is (in theory)
   * the full path. This path is constructed by prepending the file name with a
   * command line argument, an environment variable or a default value. If
   * slash is true, appends a slash to the resulting string. The returned
   * string has to be deleted (with delete[]) by the user.
   */
  static inline VTK_FILEPATH char* ExpandFileNameWithArgOrEnvOrDefault(const char* arg, int argc,
    char* argv[], const char* env, const char* def, VTK_FILEPATH const char* fname, int slash = 0);
  ///@}
};

inline char* vtkTestUtilities::GetDataRoot(int argc, char* argv[])
{
  return vtkTestUtilities::GetArgOrEnvOrDefault(
    "-D", argc, argv, "VTK_DATA_ROOT", "../../../../VTKData");
}

inline char* vtkTestUtilities::ExpandDataFileName(
  int argc, char* argv[], const char* fname, int slash)
{
  return vtkTestUtilities::ExpandFileNameWithArgOrEnvOrDefault(
    "-D", argc, argv, "VTK_DATA_ROOT", "../../../../VTKData", fname, slash);
}

inline char* vtkTestUtilities::GetArgOrEnvOrDefault(
  const char* arg, int argc, char* argv[], const char* env, const char* def)
{
  int index = -1;

  for (int i = 0; i < argc; i++)
  {
    if (strcmp(arg, argv[i]) == 0 && i < argc - 1)
    {
      index = i + 1;
    }
  }

  char* value;

  if (index != -1)
  {
    value = new char[strlen(argv[index]) + 1];
    strcpy(value, argv[index]);
  }
  else
  {
    char* foundenv = getenv(env);
    if (foundenv)
    {
      value = new char[strlen(foundenv) + 1];
      strcpy(value, foundenv);
    }
    else if (def)
    {
      value = new char[strlen(def) + 1];
      strcpy(value, def);
    }
    else
    {
      value = nullptr;
    }
  }

  return value;
}

inline char* vtkTestUtilities::ExpandFileNameWithArgOrEnvOrDefault(const char* arg, int argc,
  char* argv[], const char* env, const char* def, const char* fname, int slash)
{
  char* fullName;

  char* value = vtkTestUtilities::GetArgOrEnvOrDefault(arg, argc, argv, env, def);
  if (value)
  {
    fullName = new char[strlen(value) + strlen(fname) + 2 + static_cast<size_t>(slash ? 1 : 0)];
    fullName[0] = 0;
    strcat(fullName, value);
    size_t len = strlen(fullName);
    fullName[len] = '/';
    fullName[len + 1] = 0;
    strcat(fullName, fname);
  }
  else
  {
    fullName = new char[strlen(fname) + 1 + static_cast<size_t>(slash ? 1 : 0)];
    strcpy(fullName, fname);
  }

  if (slash)
  {
    strcat(fullName, "/");
  }

  delete[] value;

  return fullName;
}

VTK_ABI_NAMESPACE_END
#endif // vtkTestUtilities_h
// VTK-HeaderTest-Exclude: vtkTestUtilities.h