HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 <UT/UT_ArrayStringMap.h>
18 #include <UT/UT_Defines.h>
19 #include <UT/UT_Lock.h>
20 #include <UT/UT_SharedPtr.h>
21 #include <UT/UT_StringHolder.h>
22 #include <UT/UT_ValArray.h>
23 #include <SYS/SYS_Types.h>
24 #include <iosfwd>
25 
26 class FS_IndexFileHandle;
28 class FS_Reader;
29 class FS_ReaderStream;
30 class FS_Section;
32 class UT_Options;
33 class UT_String;
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=nullptr);
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  FS_IndexFileHandle 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_StringHolder &getSourceFile() const { return mySourceFile; }
144 
145  // Access functions for our description string.
146  const UT_StringHolder &getDescription() const;
147  void setDescription(const UT_StringHolder &description);
148 
149 
150  // If we are going to be written to a strstream, this function returns
151  // a sensible guess about the buffer size that should be allocated.
152  exint guessStreamSize() const;
153 
154  // Write us out to a stream (with all modifications).
155  // Note: Don't write out an index file to the same file it was loaded
156  // from, since writing implicitly reads the file. Instead, write it to a
157  // separate file and then copy it over. This will also require this index
158  // file to be reloaded.
159  virtual void writeFile(std::ostream &os) const;
160 
161  // This functions are for expanding and collapsing an index file,
162  // much like hexpand and hcollapse do for cpio archives.
163  virtual bool expandToDirectory(const char *destdir);
164  virtual bool collapseFromDirectory(const char *srcdir);
165 
166  // Get the current time, used to set the section modification time.
167  static int getCurrentTime();
168 
169  /// Tests if the index file is copy-protected.
170  bool isCopyProtected() const;
171 
172  /// Tests if the index file is black boxed.
173  bool isBlackBoxed() const;
174 
175  /// Tests if the index file is stored as expanded.
176  bool isExpanded() const
177  { return myIsExpanded; }
178 
179  static bool canWriteFile(const char *filename);
180 
181  bool getSectionFileName(const char *section,
183 
184  // -----------------------------------------------------------------------
185  // Section data conversion to/from text
186 
187  typedef bool(*ConvertFunc)(const UT_StringHolder &src_path,
188  const UT_WorkBuffer &in_data,
189  UT_WorkBuffer &out_data);
190 
192  {
193  ConversionFuncs(const char *bin_file_pattern = "",
194  const char *text_file_pattern = "",
195  ConvertFunc to_text = nullptr,
196  ConvertFunc from_text = nullptr)
197  : myToText(to_text),
198  myFromText(from_text),
199  myBinFilePattern(bin_file_pattern),
200  myTextFilePattern(text_file_pattern)
201  {}
202 
203  void clear()
204  {
205  myToText = nullptr;
206  myFromText = nullptr;
207  myBinFilePattern.clear();
208  myTextFilePattern.clear();
209  }
210 
211  bool isClear() const
212  {
213  // sufficient to just text first data member
214  return myToText == nullptr;
215  }
216 
217  ConvertFunc myToText;
218  ConvertFunc myFromText;
221  };
223 
224 
225  static bool registerConversionFuncs(const char *bin_file_pattern,
226  const char *text_file_pattern,
227  ConvertFunc to_text,
228  ConvertFunc from_text);
229 
230  static const ConversionList &getConversionsToText();
231  static const ConversionList &getConversionsFromText();
232 
233 
234 protected:
235  void setModified() { myModified = true; }
236 
237  exint writeHeader(std::ostream &os) const;
238 
239  static ConversionList &_getConversionsToText();
240  static ConversionList &_getConversionsFromText();
241 
242  mutable UT_Lock myLock;
243 
244 private:
246 
247  // Functions for opening and closing our stream.
248  void openStream(const UT_Options *options = nullptr) const;
249  void closeStream() const;
250 
251  // Helper functions for reading and writing ourselves to a directory.
252  static void makeValidFileName(UT_String &filename);
253  bool writeSectionListFile(const char *sectionfile);
254 
255  // Writes out a section to the output stream. If the filter factory
256  // has been set, the filter will be applied to the section data before
257  // it is written out.
258  void writeSection(FS_Section *section,
259  std::ostream &os,
260  bool as_text) const;
261 
262  // Get an FS_Reader for reading a section of the index.
263  FS_Reader *getSectionReader(FS_Section *section) const;
264 
265  // These utility functions should only be used by FS_ReaderHelpers
266  FS_ReaderStream *getSectionStream(FS_Section *section) const;
267 
268  // Read in our index and use it to create our section data structures.
269  void readIndex();
270  void readDirectoryIndex(
271  const UT_String &source_file_path,
272  const UT_String &sections_file_path);
273  void readStreamIndex();
274 
275  // Obtains a size of the i-th section after it has been filtered (if at all)
276  int getWriteFilteredSectionSize( int index ) const;
277 
278  // Recalculates the data sizes of our sections when the read filter is set.
279  void recalculateFilteredSectionSizes();
280 
282 
283  // Data:
284  mutable FS_Reader *myReader;
285  FS_SectionArray mySections;
286  SectionMap mySectionTable;
287  FS_SectionArray myTempSections;
288  SectionMap myTempSectionTable;
289  UT_StringHolder myDescription;
290  UT_StringHolder mySourceFile;
291  FS_WriteFilterFactory *myWriteFilter;
292  FS_IStreamFilterFactory *myReadFilter;
293  int myModTime;
294  int64 mySize;
295  int myStreamStart;
296  bool myModified;
297  bool myIsExpanded;
298 
299  friend class FS_Section;
300  friend class FS_EmbeddedSection;
301  friend class FS_DiskSection;
302  friend class FS_IndexFileHandle;
303 };
304 
305 class FS_API FS_IndexFileHandle : public UT_SharedPtr<FS_IndexFile>
306 {
307 public:
310  virtual ~FS_IndexFileHandle();
311 
312 private:
313 
314  friend class FS_Section;
315  friend class FS_EmbeddedSection;
316  friend class FS_DiskSection;
317  friend class FS_IndexFile;
318 };
319 
320 // For UT::ArraySet.
321 namespace UT
322 {
323 template <typename T>
324 struct DefaultClearer;
325 
326 template <>
327 struct DefaultClearer<FS_IndexFile::ConversionFuncs>
328 {
330  static void clear(ValueT &v) { v.clear(); }
331  static bool isClear(const ValueT &v) { return v.isClear(); }
332  static void clearConstruct(ValueT *p)
333  {
334  new ((void *)p) ValueT();
335  }
336  static const bool clearNeedsDestruction = false;
337 };
338 } // namespace UT
339 
340 #endif
341 
Class for reading files.
Definition: FS_Reader.h:33
GT_API const UT_StringHolder filename
UT_StringHolder myTextFilePattern
Definition: FS_IndexFile.h:220
UT_Lock myLock
Definition: FS_IndexFile.h:242
GLuint index
Definition: glew.h:1814
ConversionFuncs(const char *bin_file_pattern="", const char *text_file_pattern="", ConvertFunc to_text=nullptr, ConvertFunc from_text=nullptr)
Definition: FS_IndexFile.h:193
int64 exint
Definition: SYS_Types.h:125
GLsizei GLsizei GLchar * source
Definition: glew.h:1832
const GLint * first
Definition: glew.h:1528
bool hasRegularSection(const UT_StringRef &section) const
Definition: FS_IndexFile.h:76
const GLdouble * v
Definition: glew.h:1391
bool isExpanded() const
Tests if the index file is stored as expanded.
Definition: FS_IndexFile.h:176
UT_ArrayStringMap< ConversionFuncs > ConversionList
Definition: FS_IndexFile.h:222
void setModified()
Definition: FS_IndexFile.h:235
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:28
GLuint buffer
Definition: glew.h:1680
bool hasTempSection(const UT_StringRef &section) const
Definition: FS_IndexFile.h:74
long long int64
Definition: SYS_Types.h:116
virtual bool writeFile(const GA_Detail &g, const char *filename, const GA_SaveOptions *opts, UT_StringArray *errors) const
GLfloat GLfloat p
Definition: glew.h:16321
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:84
const UT_StringHolder & getSourceFile() const
Definition: FS_IndexFile.h:143
GLenum GLuint GLsizei const GLchar * buf
Definition: glew.h:2580
#define FS_API
Definition: FS_API.h:10
UT_StringHolder myBinFilePattern
Definition: FS_IndexFile.h:219
GLenum GLsizei len
Definition: glew.h:7752
GLintptr offset
Definition: glew.h:1682