HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FS_IndexFile.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: FS_IndexFile.h ( FS Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __FS_IndexFile__
13 #define __FS_IndexFile__
14 
15 #include "FS_API.h"
16 
17 #include "FS_Reader.h"
18 #include "FS_ReaderStream.h"
19 
20 #include <UT/UT_Defines.h>
21 #include <UT/UT_SharedPtr.h>
22 #include <UT/UT_String.h>
23 #include <UT/UT_StringHolder.h>
24 #include <UT/UT_SymbolTable.h>
25 #include <UT/UT_ValArray.h>
26 #include <iosfwd>
27 
28 class UT_Options;
29 class FS_Section;
30 class FS_EmbeddedSection;
31 class FS_DiskSection;
34 class UT_WorkBuffer;
35 
36 #define FS_SECTION_SEPARATOR UT_SECTION_SEPARATOR_CHAR
37 #define FS_SECTION_SEPARATOR_STRING UT_SECTION_SEPARATOR_STRING
38 
39 // This class implements a file that contains several sections that can be found
40 // at certain offsets of a main file.
42 {
43 public:
44  /// Default constructor.
45  FS_IndexFile();
46 
47  /// This constructor takes a path of some sort (e.g. a file path, an oplib:
48  /// path, an opdef: path, an http:// path, etc) to the index file.
49  FS_IndexFile(const char *source);
50 
51  /// This constructor takes a reader that will be used to retrieve section
52  /// data. The index file takes ownership and will delete the reader in the
53  /// destructor. (If null is passed in, this object creates its own default
54  /// FS_Reader, but unless a source is given nothing can be read from such
55  /// an index file.)
56  ///
57  /// If no source is given, closing the index file and reopening it again
58  /// will have no effect on the underlying reader. The reader will only be
59  /// closed and deleted when the index file object is deleted. If a source
60  /// is passed in, though, the reader will be deleted when the index file is
61  /// closed and subsequent openings will use the source path to construct a
62  /// new reader.
63  FS_IndexFile(FS_Reader *reader, const char *source=NULL);
64 
65  virtual ~FS_IndexFile();
66 
67  // Some basic operations on sections (add, remove, modify).
68  // These functions don't affect the source file. The changes
69  // are only evident when we call writeFile.
70  // Reading is NOT currently threadsafe.
71  inline bool hasSection(const UT_StringRef &section) const
72  { return mySectionTable.contains(section) ||
73  myTempSectionTable.contains(section); }
74  inline bool hasTempSection(const UT_StringRef &section) const
75  { return myTempSectionTable.contains(section); }
76  inline bool hasRegularSection(const UT_StringRef &section) const
77  { return mySectionTable.contains(section); }
78  int getSectionModTime(const UT_StringRef &section) const;
79  int getSectionDataSize(const UT_StringRef &section) const;
80  bool readSection(const UT_StringRef &section,
81  char *buffer) const;
82  bool readSection(const UT_StringRef &section,
83  UT_WorkBuffer &buffer) const;
84  bool readOptionsSection(const UT_StringRef &section,
85  UT_Options &options) const;
86  void addSection(const UT_StringRef &section,
87  const char *filename);
88  void addSection(const UT_StringRef &section,
89  const UT_WorkBuffer &buf,
90  int modtime = -1);
91  void addSection(const UT_StringRef &section,
92  const char *buffer,
93  int len, int modtime = -1);
94  void addSection(const UT_StringRef &section,
95  const FS_IndexFile &file);
96  void modifySection(const UT_StringRef &section,
97  const char *buffer,
98  int len, int modtime = -1);
99  void modifySection(const UT_StringRef &section,
100  const UT_WorkBuffer &buf,
101  int modtime = -1);
102  void removeSection(const UT_StringRef &section);
103  void mergeIndexFile(const FS_IndexFile &file, bool overwrite);
104 
105  // Change the ordering of some sections. This is basically a book-keeping
106  // change, and only affects things when this index file is then saved.
107  void moveSections(int first, int last, int offset);
108 
109  // Get the modification time for the index file as a whole.
110  int getModTime() const;
111 
112  // Get the size of the whole index file
113  int64 getFileDataSize() const;
114 
115  // Sets the stream filters used for processing the data when it is read
116  // in or written out.
117  void setFilters(FS_WriteFilterFactory *encrypt_factory,
118  FS_IStreamFilterFactory *decrypt_factory);
119 
120  // Gets the decryption and encryption filter factories.
121  FS_WriteFilterFactory *getEncryptionFilter() const;
122  FS_IStreamFilterFactory *getDecryptionFilter() const;
123 
124  // Get an opaque pointer to a section.
125  FS_Section *getSection(const UT_StringRef &section) const;
126 
127  // Get an FS_Reader for reading a section of the index.
128  FS_Reader *getSectionReader(const UT_StringRef &section) const;
129 
130  // These utility functions should only be used by FS_ReaderHelpers
131  FS_ReaderStream *getSectionStream(const UT_StringRef &section) const;
132  FS_ReaderStream *getStreamCopy() const;
133 
134  // Given an opaque section pointer get an FS_IndexFile it represents.
135  UT_SharedPtr<FS_IndexFile> getIndexFileFromSection(const UT_StringRef &section,
136  const char *source = nullptr) const;
137 
138  // Simple access functions to find out about our sections.
139  int getNumSections() const;
140  const UT_StringHolder &getSectionName(int index) const;
141 
142  // Access our source file name (which may be empty).
143  const UT_String &getSourceFile() const;
144 
145  // Access functions for our description string.
146  const UT_String &getDescription() const;
147  void setDescription(const char *description);
148 
149  // If we are going to be written to a strstream, this function returns
150  // a sensible guess about the buffer size that should be allocated.
151  exint guessStreamSize() const;
152 
153  // Write us out to a stream (with all modifications).
154  // Note: Don't write out an index file to the same file it was loaded
155  // from, since writing implicitly reads the file. Instead, write it to a
156  // separate file and then copy it over. This will also require this index
157  // file to be reloaded.
158  virtual void writeFile(std::ostream &os) const;
159 
160  // This functions are for expanding and collapsing an index file,
161  // much like hexpand and hcollapse do for cpio archives.
162  virtual bool expandToDirectory(const char *destdir);
163  virtual bool collapseFromDirectory(const char *srcdir);
164 
165  // Get the current time, used to set the section modification time.
166  static int getCurrentTime();
167 
168  /// Tests if the index file is copy-protected.
169  bool isCopyProtected() const;
170 
171  /// Tests if the index file is black boxed.
172  bool isBlackBoxed() const;
173 
174  /// Tests if the index file is stored as expanded.
175  bool isExpanded() const
176  { return myIsExpanded; }
177 
178  static bool canWriteFile(const char *filename);
179 
180  bool getSectionFileName(const char *section,
181  UT_String &filename);
182 
183  // -----------------------------------------------------------------------
184  // Section data conversion to/from text
185 
186  typedef bool(*ConvertFunc)(const UT_StringHolder &src_path,
187  const UT_WorkBuffer &in_data,
188  UT_WorkBuffer &out_data);
189 
191  {
192  ConversionFuncs(const char *bin_file_pattern = "",
193  const char *text_file_pattern = "",
194  ConvertFunc to_text = nullptr,
195  ConvertFunc from_text = nullptr)
196  : myToText(to_text),
197  myFromText(from_text),
198  myBinFilePattern(bin_file_pattern),
199  myTextFilePattern(text_file_pattern)
200  {}
201 
202  ConvertFunc myToText;
203  ConvertFunc myFromText;
206  };
208 
209 
210  static bool registerConversionFuncs(const char *bin_file_pattern,
211  const char *text_file_pattern,
212  ConvertFunc to_text,
213  ConvertFunc from_text);
214 
215  static const ConversionList &getConversionsToText();
216  static const ConversionList &getConversionsFromText();
217 
218 
219 protected:
220  void setModified();
221 
222  exint writeHeader(std::ostream &os) const;
223 
224  static ConversionList &_getConversionsToText();
225  static ConversionList &_getConversionsFromText();
226 
227 private:
229 
230  // Functions for opening and closing our stream.
231  void openStream(exint buffer_size = -1) const;
232  void closeStream() const;
233 
234  // Helper functions for reading and writing ourselves to a directory.
235  void makeValidFileName(UT_String &filename);
236  bool writeSectionListFile(const char *sectionfile);
237 
238  // Writes out a section to the output stream. If the filter factory
239  // has been set, the filter will be applied to the section data before
240  // it is written out.
241  void writeSection(FS_Section *section,
242  std::ostream &os,
243  bool as_text) const;
244 
245  // Get an FS_Reader for reading a section of the index.
246  FS_Reader *getSectionReader(FS_Section *section) const;
247 
248  // These utility functions should only be used by FS_ReaderHelpers
249  FS_ReaderStream *getSectionStream(FS_Section *section) const;
250 
251  // Read in our index and use it to create our section data structures.
252  void readIndex();
253  void readDirectoryIndex(
254  const UT_String &source_file_path,
255  const UT_String &sections_file_path);
256  void readStreamIndex();
257 
258  // Obtains a size of the i-th section after it has been filtered (if at all)
259  int getWriteFilteredSectionSize( int index ) const;
260 
261  // Recalculates the data sizes of our sections when the read filter is set.
262  void recalculateFilteredSectionSizes();
263 
264  // Data:
265  mutable FS_Reader *myReader;
266  FS_SectionArray mySections;
267  UT_SymbolMap<FS_Section *> mySectionTable;
268  FS_SectionArray myTempSections;
269  UT_SymbolMap<FS_Section *> myTempSectionTable;
270  UT_String myDescription;
271  UT_String mySourceFile;
272  FS_WriteFilterFactory *myWriteFilter;
273  FS_IStreamFilterFactory *myReadFilter;
274  int myModTime;
275  int64 mySize;
276  int myStreamStart;
277  bool myModified;
278  bool myIsExpanded;
279 
280  friend class FS_Section;
281  friend class FS_EmbeddedSection;
282  friend class FS_DiskSection;
283 };
284 
285 #endif
286 
GLint first
Definition: glcorearb.h:404
Class for reading files.
Definition: FS_Reader.h:30
UT_StringHolder myTextFilePattern
Definition: FS_IndexFile.h:205
ConversionFuncs(const char *bin_file_pattern="", const char *text_file_pattern="", ConvertFunc to_text=nullptr, ConvertFunc from_text=nullptr)
Definition: FS_IndexFile.h:192
bool hasRegularSection(const UT_StringRef &section) const
Definition: FS_IndexFile.h:76
GLuint buffer
Definition: glcorearb.h:659
bool isExpanded() const
Tests if the index file is stored as expanded.
Definition: FS_IndexFile.h:175
long long int64
Definition: SYS_Types.h:100
int64 exint
Definition: SYS_Types.h:109
bool hasTempSection(const UT_StringRef &section) const
Definition: FS_IndexFile.h:74
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:802
GLintptr offset
Definition: glcorearb.h:664
virtual bool writeFile(const GA_Detail &g, const char *filename, const GA_SaveOptions *opts, UT_StringArray *errors) const
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
bool hasSection(const UT_StringRef &section) const
Definition: FS_IndexFile.h:71
A map of string to various well defined value types.
Definition: UT_Options.h:42
GLuint index
Definition: glcorearb.h:785
png_infop png_bytep png_size_t buffer_size
Definition: png.h:2124
UT_StringMap< ConversionFuncs > ConversionList
Definition: FS_IndexFile.h:207
#define FS_API
Definition: FS_API.h:10
UT_StringHolder myBinFilePattern
Definition: FS_IndexFile.h:204