1 #ifndef E57EXCEPTION_H
2 #define E57EXCEPTION_H
3 
4 /*
5  * Copyright 2009 - 2010 Kevin Ackley (kackley@gwi.net)
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29 
30 #include <exception>
31 #include <iostream>
32 #include <string>
33 
34 namespace e57 {
35 
36 //! @brief Numeric error identifiers used in E57Exception
37 enum ErrorCode {
38    // N.B.  *** When changing error strings here, remember to update the error strings in E57Exception.cpp ****
39    E57_SUCCESS                                 = 0,  //!< operation was successful
40    E57_ERROR_BAD_CV_HEADER                     = 1,  //!< a CompressedVector binary header was bad
41    E57_ERROR_BAD_CV_PACKET                     = 2,  //!< a CompressedVector binary packet was bad
42    E57_ERROR_CHILD_INDEX_OUT_OF_BOUNDS         = 3,  //!< a numerical index identifying a child was out of bounds
43    E57_ERROR_SET_TWICE                         = 4,  //!< attempted to set an existing child element to a new value
44    E57_ERROR_HOMOGENEOUS_VIOLATION             = 5,  //!< attempted to add an E57 Element that would have made the children of a homogenous Vector have different types
45    E57_ERROR_VALUE_NOT_REPRESENTABLE           = 6,  //!< a value could not be represented in the requested type
46    E57_ERROR_SCALED_VALUE_NOT_REPRESENTABLE    = 7,  //!< after scaling the result could not be represented in the requested type
47    E57_ERROR_REAL64_TOO_LARGE                  = 8,  //!< a 64 bit IEEE float was too large to store in a 32 bit IEEE float
48    E57_ERROR_EXPECTING_NUMERIC                 = 9,  //!< Expecting numeric representation in user's buffer, found ustring
49    E57_ERROR_EXPECTING_USTRING                 = 10, //!< Expecting string representation in user's buffer, found numeric
50    E57_ERROR_INTERNAL                          = 11, //!< An unrecoverable inconsistent internal state was detected
51    E57_ERROR_BAD_XML_FORMAT                    = 12, //!< E57 primitive not encoded in XML correctly
52    E57_ERROR_XML_PARSER                        = 13, //!< XML not well formed
53    E57_ERROR_BAD_API_ARGUMENT                  = 14, //!< bad API function argument provided by user
54    E57_ERROR_FILE_IS_READ_ONLY                 = 15, //!< can't modify read only file
55    E57_ERROR_BAD_CHECKSUM                      = 16, //!< checksum mismatch, file is corrupted
56    E57_ERROR_OPEN_FAILED                       = 17, //!< open() failed
57    E57_ERROR_CLOSE_FAILED                      = 18, //!< close() failed
58    E57_ERROR_READ_FAILED                       = 19, //!< read() failed
59    E57_ERROR_WRITE_FAILED                      = 20, //!< write() failed
60    E57_ERROR_LSEEK_FAILED                      = 21, //!< lseek() failed
61    E57_ERROR_PATH_UNDEFINED                    = 22, //!< E57 element path well formed but not defined
62    E57_ERROR_BAD_BUFFER                        = 23, //!< bad SourceDestBuffer
63    E57_ERROR_NO_BUFFER_FOR_ELEMENT             = 24, //!< no buffer specified for an element in CompressedVectorNode during write
64    E57_ERROR_BUFFER_SIZE_MISMATCH              = 25, //!< SourceDestBuffers not all same size
65    E57_ERROR_BUFFER_DUPLICATE_PATHNAME         = 26, //!< duplicate pathname in CompressedVectorNode read/write
66    E57_ERROR_BAD_FILE_SIGNATURE                = 27, //!< file signature not "ASTM-E57"
67    E57_ERROR_UNKNOWN_FILE_VERSION              = 28, //!< incompatible file version
68    E57_ERROR_BAD_FILE_LENGTH                   = 29, //!< size in file header not same as actual
69    E57_ERROR_XML_PARSER_INIT                   = 30, //!< XML parser failed to initialize
70    E57_ERROR_DUPLICATE_NAMESPACE_PREFIX        = 31, //!< namespace prefix already defined
71    E57_ERROR_DUPLICATE_NAMESPACE_URI           = 32, //!< namespace URI already defined
72    E57_ERROR_BAD_PROTOTYPE                     = 33, //!< bad prototype in CompressedVectorNode
73    E57_ERROR_BAD_CODECS                        = 34, //!< bad codecs in CompressedVectorNode
74    E57_ERROR_VALUE_OUT_OF_BOUNDS               = 35, //!< element value out of min/max bounds
75    E57_ERROR_CONVERSION_REQUIRED               = 36, //!< conversion required to assign element value, but not requested
76    E57_ERROR_BAD_PATH_NAME                     = 37, //!< E57 path name is not well formed
77    E57_ERROR_NOT_IMPLEMENTED                   = 38, //!< functionality not implemented
78    E57_ERROR_BAD_NODE_DOWNCAST                 = 39, //!< bad downcast from Node to specific node type
79    E57_ERROR_WRITER_NOT_OPEN                   = 40, //!< CompressedVectorWriter is no longer open
80    E57_ERROR_READER_NOT_OPEN                   = 41, //!< CompressedVectorReader is no longer open
81    E57_ERROR_NODE_UNATTACHED                   = 42, //!< node is not yet attached to tree of ImageFile
82    E57_ERROR_ALREADY_HAS_PARENT                = 43, //!< node already has a parent
83    E57_ERROR_DIFFERENT_DEST_IMAGEFILE          = 44, //!< nodes were constructed with different destImageFiles
84    E57_ERROR_IMAGEFILE_NOT_OPEN                = 45, //!< destImageFile is no longer open
85    E57_ERROR_BUFFERS_NOT_COMPATIBLE            = 46, //!< SourceDestBuffers not compatible with previously given ones
86    E57_ERROR_TOO_MANY_WRITERS                  = 47, //!< too many open CompressedVectorWriters of an ImageFile
87    E57_ERROR_TOO_MANY_READERS                  = 48, //!< too many open CompressedVectorReaders of an ImageFile
88    E57_ERROR_BAD_CONFIGURATION                 = 49, //!< bad configuration string
89    E57_ERROR_INVARIANCE_VIOLATION              = 50  //!< class invariance constraint violation in debug mode
90 };
91 
92 class E57Exception : public std::exception
93 {
94    public:
95       void        report(const char* reportingFileName=nullptr, int reportingLineNumber=0, const char* reportingFunctionName=nullptr, std::ostream& os = std::cout) const;
96       ErrorCode   errorCode() const;
97       std::string context() const;
98       const char* what() const noexcept override;
99 
100       // For debugging purposes:
101       const char* sourceFileName() const;
102       const char* sourceFunctionName() const;
103       int         sourceLineNumber() const;
104 
105       //! \cond documentNonPublic   The following isn't part of the API, and isn't documented.
106       E57Exception() = delete;
107       E57Exception(ErrorCode ecode, const std::string &context,
108                    const std::string &srcFileName = nullptr, int srcLineNumber = 0,
109                    const char* srcFunctionName = nullptr);
110       ~E57Exception() noexcept override = default;
111 
112    protected:
113       ErrorCode   errorCode_;
114       std::string context_;
115       std::string sourceFileName_;
116       const char* sourceFunctionName_;
117       int         sourceLineNumber_;
118       //! \endcond
119 };
120 
121 
122 namespace Utilities {
123    // Get latest version of ASTM standard supported, and library id string
124    void    getVersions(int& astmMajor, int& astmMinor, std::string& libraryId);
125 
126    std::string errorCodeToString(ErrorCode ecode);
127 }
128 }
129 
130 #endif
131