VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkTestUtilities.h 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00030 #ifndef vtkTestUtilities_h 00031 #define vtkTestUtilities_h 00032 00033 #include "vtkSystemIncludes.h" 00034 00035 #if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */ 00036 #pragma warning( disable : 4996 ) // 'function': was declared deprecated 00037 #endif 00038 00039 struct vtkTestUtilities 00040 { 00044 static inline char* GetDataRoot(int argc, char* argv[]); 00045 00047 00053 static inline char* ExpandDataFileName(int argc, char* argv[], 00054 const char* fname, 00055 int slash = 0); 00057 00058 00061 static inline char* GetArgOrEnvOrDefault(const char* arg, 00062 int argc, char* argv[], 00063 const char* env, 00064 const char* def); 00066 00068 00074 static inline char* ExpandFileNameWithArgOrEnvOrDefault(const char* arg, 00075 int argc, char* argv[], 00076 const char* env, 00077 const char* def, 00078 const char* fname, 00079 int slash = 0); 00080 }; 00082 00083 inline 00084 char* vtkTestUtilities::GetDataRoot(int argc, char* argv[]) 00085 { 00086 return vtkTestUtilities::GetArgOrEnvOrDefault( 00087 "-D", argc, argv, 00088 "VTK_DATA_ROOT", 00089 "../../../../VTKData"); 00090 } 00091 00092 inline 00093 char* vtkTestUtilities::ExpandDataFileName(int argc, char* argv[], 00094 const char* fname, 00095 int slash) 00096 { 00097 return vtkTestUtilities::ExpandFileNameWithArgOrEnvOrDefault( 00098 "-D", argc, argv, 00099 "VTK_DATA_ROOT", 00100 "../../../../VTKData", 00101 fname, 00102 slash); 00103 } 00104 00105 inline 00106 char* vtkTestUtilities::GetArgOrEnvOrDefault(const char* arg, 00107 int argc, char* argv[], 00108 const char* env, 00109 const char *def) 00110 { 00111 int index = -1; 00112 00113 for (int i = 0; i < argc; i++) 00114 { 00115 if (strcmp(arg, argv[i]) == 0 && i < argc - 1) 00116 { 00117 index = i + 1; 00118 } 00119 } 00120 00121 char* value; 00122 00123 if (index != -1) 00124 { 00125 value = new char[strlen(argv[index]) + 1]; 00126 strcpy(value, argv[index]); 00127 } 00128 else 00129 { 00130 char *foundenv = getenv(env); 00131 if (foundenv) 00132 { 00133 value = new char[strlen(foundenv) + 1]; 00134 strcpy(value, foundenv); 00135 } 00136 else if (def) 00137 { 00138 value = new char[strlen(def) + 1]; 00139 strcpy(value, def); 00140 } 00141 else 00142 { 00143 value = NULL; 00144 } 00145 } 00146 00147 return value; 00148 } 00149 00150 inline 00151 char* vtkTestUtilities::ExpandFileNameWithArgOrEnvOrDefault(const char* arg, 00152 int argc, 00153 char* argv[], 00154 const char* env, 00155 const char *def, 00156 const char* fname, 00157 int slash) 00158 { 00159 char* fullName; 00160 00161 char* value = vtkTestUtilities::GetArgOrEnvOrDefault(arg, argc, argv, 00162 env, 00163 def); 00164 if (value) 00165 { 00166 fullName = new char[strlen(value) + strlen(fname) + 2 + 00167 static_cast<size_t>(slash ? 1 : 0)]; 00168 fullName[0] = 0; 00169 strcat(fullName, value); 00170 size_t len = strlen(fullName); 00171 fullName[len] = '/'; 00172 fullName[len+1] = 0; 00173 strcat(fullName, fname); 00174 } 00175 else 00176 { 00177 fullName = new char[strlen(fname) + 1 + static_cast<size_t>(slash ? 1 : 0)]; 00178 strcpy(fullName, fname); 00179 } 00180 00181 if (slash) 00182 { 00183 strcat(fullName, "/"); 00184 } 00185 00186 delete[] value; 00187 00188 return fullName; 00189 } 00190 00191 #endif // vtkTestUtilities_h