1 #ifndef IMAGEFILEIMPL_H
2 #define IMAGEFILEIMPL_H
3 /*
4  * Copyright 2009 - 2010 Kevin Ackley (kackley@gwi.net)
5  *
6  * Permission is hereby granted, free of charge, to any person or organization
7  * obtaining a copy of the software and accompanying documentation covered by
8  * this license (the "Software") to use, reproduce, display, distribute,
9  * execute, and transmit the Software, and to prepare derivative works of the
10  * Software, and to permit third-parties to whom the Software is furnished to
11  * do so, all subject to the following:
12  *
13  * The copyright notices in the Software and this entire statement, including
14  * the above license grant, this restriction and the following disclaimer,
15  * must be included in all copies of the Software, in whole or in part, and
16  * all derivative works of the Software, unless such copies or derivative
17  * works are solely in the form of machine-executable object code generated by
18  * a source language processor.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28 
29 #include <memory>
30 
31 #include "Common.h"
32 
33 
34 namespace e57
35 {
36    class CheckedFile;
37 
38    struct E57FileHeader;
39    struct NameSpace;
40 
41    class ImageFileImpl : public std::enable_shared_from_this<ImageFileImpl>
42    {
43       public:
44          ImageFileImpl( ReadChecksumPolicy policy );
45          void            construct2(const ustring& fileName, const ustring& mode);
46          std::shared_ptr<StructureNodeImpl> root();
47          void            close();
48          void            cancel();
49          bool            isOpen() const;
50          bool            isWriter() const;
51          int             writerCount() const;
52          int             readerCount() const;
53          ~ImageFileImpl();
54 
55          uint64_t        allocateSpace(uint64_t byteCount, bool doExtendNow);
56          CheckedFile*    file() const;
57          ustring         fileName() const;
58 
59          /// Manipulate registered extensions in the file
60          void            extensionsAdd(const ustring& prefix, const ustring& uri);
61          bool            extensionsLookupPrefix(const ustring& prefix, ustring& uri) const;
62          bool            extensionsLookupUri(const ustring& uri, ustring& prefix) const;
63          size_t          extensionsCount() const;
64          ustring         extensionsPrefix(const size_t index) const;
65          ustring         extensionsUri(const size_t index) const;
66 
67          /// Utility functions:
68          bool            isElementNameExtended(const ustring& elementName);
69          bool            isElementNameLegal(const ustring& elementName, bool allowNumber = true);
70          bool            isPathNameLegal(const ustring& pathName);
71          void            checkElementNameLegal(const ustring& elementName, bool allowNumber = true);
72          void            elementNameParse(const ustring& elementName, ustring& prefix, ustring& localPart, bool allowNumber = true);
73 
74          void            pathNameCheckWellFormed(const ustring& pathName);
75          void            pathNameParse(const ustring &pathName, bool &isRelative, StringList &fields);
76          ustring         pathNameUnparse(bool isRelative, const StringList &fields);
77 
78          unsigned        bitsNeeded(int64_t minimum, int64_t maximum);
79          void            incrWriterCount();
80          void            decrWriterCount();
81          void            incrReaderCount();
82          void            decrReaderCount();
83 
84          /// Diagnostic functions:
85 #ifdef E57_DEBUG
86          void            dump(int indent = 0, std::ostream& os = std::cout) const;
87 #endif
88 
89       private:
90          friend class E57XmlParser;
91          friend class BlobNodeImpl;
92          friend class CompressedVectorWriterImpl;
93          friend class CompressedVectorReaderImpl; //??? add file() instead of accessing file_, others friends too
94 
95          static void     readFileHeader(CheckedFile* file, E57FileHeader& header);
96 
97          void checkImageFileOpen(const char* srcFileName, int srcLineNumber, const char* srcFunctionName) const;
98 
99          ustring         fileName_;
100          bool            isWriter_;
101          int             writerCount_;
102          int             readerCount_;
103 
104          ReadChecksumPolicy   checksumPolicy;
105 
106          CheckedFile*    file_;
107 
108          /// Read file attributes
109          uint64_t        xmlLogicalOffset_;
110          uint64_t        xmlLogicalLength_;
111 
112          /// Write file attributes
113          uint64_t        unusedLogicalStart_;
114 
115          /// Bidirectional map from namespace prefix to uri
116          std::vector<NameSpace>  nameSpaces_;
117 
118          /// Smart pointer to metadata tree
119          std::shared_ptr<StructureNodeImpl> root_;
120    };
121 }
122 
123 #endif
124