1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkErrorCode.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 #include "vtkErrorCode.h"
16
17 #include <cstring>
18 #include <cctype>
19 #include <cerrno>
20
21 // this list should only contain the initial, contiguous
22 // set of error codes and should not include UserError
23 static const char *vtkErrorCodeErrorStrings[] = {
24 "NoError",
25 "FileNotFoundError",
26 "CannotOpenFileError",
27 "UnrecognizedFileTypeError",
28 "PrematureEndOfFileError",
29 "FileFormatError",
30 "NoFileNameError",
31 "OutOfDiskSpaceError",
32 "UnknownError",
33 "UserError",
34 nullptr
35 };
36
GetStringFromErrorCode(unsigned long error)37 const char *vtkErrorCode::GetStringFromErrorCode(unsigned long error)
38 {
39 static unsigned long numerrors = 0;
40 if(error < FirstVTKErrorCode)
41 {
42 return strerror(static_cast<int>(error));
43 }
44 else
45 {
46 error -= FirstVTKErrorCode;
47 }
48
49 // find length of table
50 if (!numerrors)
51 {
52 while (vtkErrorCodeErrorStrings[numerrors] != nullptr)
53 {
54 numerrors++;
55 }
56 }
57 if (error < numerrors)
58 {
59 return vtkErrorCodeErrorStrings[error];
60 }
61 else if (error == vtkErrorCode::UserError)
62 {
63 return "UserError";
64 }
65 else
66 {
67 return "NoError";
68 }
69 }
70
GetErrorCodeFromString(const char * error)71 unsigned long vtkErrorCode::GetErrorCodeFromString(const char *error)
72 {
73 unsigned long i;
74
75 for (i = 0; vtkErrorCodeErrorStrings[i] != nullptr; i++)
76 {
77 if (!strcmp(vtkErrorCodeErrorStrings[i],error))
78 {
79 return i;
80 }
81 }
82 if (!strcmp("UserError",error))
83 {
84 return vtkErrorCode::UserError;
85 }
86 return vtkErrorCode::NoError;
87 }
88
89
GetLastSystemError()90 unsigned long vtkErrorCode::GetLastSystemError()
91 {
92 return static_cast<unsigned long>(errno);
93 }
94
95
96
97