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_SharedPtr.h>
20 #include <UT/UT_StringHolder.h>
21 #include <UT/UT_ValArray.h>
22 #include <SYS/SYS_Types.h>
23 #include <iosfwd>
24 
25 class FS_IndexFileHandle;
27 class FS_Reader;
28 class FS_ReaderStream;
29 class FS_Section;
31 class UT_Options;
32 class UT_String;
33 class UT_WorkBuffer;
34 
35 #define FS_SECTION_SEPARATOR UT_SECTION_SEPARATOR_CHAR
36 #define FS_SECTION_SEPARATOR_STRING UT_SECTION_SEPARATOR_STRING
37 
38 // This class implements a file that contains several sections that can be found
39 // at certain offsets of a main file.
41 {
42 public:
43  /// Default constructor.
44  FS_IndexFile();
45 
46  /// This constructor takes a path of some sort (e.g. a file path, an oplib:
47  /// path, an opdef: path, an http:// path, etc) to the index file.
48  FS_IndexFile(const char *source);
49 
50  /// This constructor takes a reader that will be used to retrieve section
51  /// data. The index file takes ownership and will delete the reader in the
52  /// destructor. (If null is passed in, this object creates its own default
53  /// FS_Reader, but unless a source is given nothing can be read from such
54  /// an index file.)
55  ///
56  /// If no source is given, closing the index file and reopening it again
57  /// will have no effect on the underlying reader. The reader will only be
58  /// closed and deleted when the index file object is deleted. If a source
59  /// is passed in, though, the reader will be deleted when the index file is
60  /// closed and subsequent openings will use the source path to construct a
61  /// new reader.
62  FS_IndexFile(FS_Reader *reader, const char *source=nullptr);
63 
64  virtual ~FS_IndexFile();
65 
66  // Some basic operations on sections (add, remove, modify).
67  // These functions don't affect the source file. The changes
68  // are only evident when we call writeFile.
69  // Reading is NOT currently threadsafe.
70  inline bool hasSection(const UT_StringRef &section) const
71  { return mySectionTable.contains(section) ||
72  myTempSectionTable.contains(section); }
73  inline bool hasTempSection(const UT_StringRef &section) const
74  { return myTempSectionTable.contains(section); }
75  inline bool hasRegularSection(const UT_StringRef &section) const
76  { return mySectionTable.contains(section); }
77  int getSectionModTime(const UT_StringRef &section) const;
78  int getSectionDataSize(const UT_StringRef &section) const;
79  bool readSection(const UT_StringRef &section,
80  char *buffer) const;
81  bool readSection(const UT_StringRef &section,
82  UT_WorkBuffer &buffer) const;
83  bool readOptionsSection(const UT_StringRef &section,
84  UT_Options &options) const;
85  void addSection(const UT_StringRef &section,
86  const char *filename);
87  void addSection(const UT_StringRef &section,
88  const UT_WorkBuffer &buf,
89  int modtime = -1);
90  void addSection(const UT_StringRef &section,
91  const char *buffer,
92  int len, int modtime = -1);
93  void addSection(const UT_StringRef &section,
94  const FS_IndexFile &file);
95  void modifySection(const UT_StringRef &section,
96  const char *buffer,
97  int len, int modtime = -1);
98  void modifySection(const UT_StringRef &section,
99  const UT_WorkBuffer &buf,
100  int modtime = -1);
101  void removeSection(const UT_StringRef &section);
102  void mergeIndexFile(const FS_IndexFile &file, bool overwrite);
103 
104  // Change the ordering of some sections. This is basically a book-keeping
105  // change, and only affects things when this index file is then saved.
106  void moveSections(int first, int last, int offset);
107 
108  // Get the modification time for the index file as a whole.
109  int getModTime() const;
110 
111  // Get the size of the whole index file
112  int64 getFileDataSize() const;
113 
114  // Sets the stream filters used for processing the data when it is read
115  // in or written out.
116  void setFilters(FS_WriteFilterFactory *encrypt_factory,
117  FS_IStreamFilterFactory *decrypt_factory);
118 
119  // Gets the decryption and encryption filter factories.
120  FS_WriteFilterFactory *getEncryptionFilter() const;
121  FS_IStreamFilterFactory *getDecryptionFilter() const;
122 
123  // Get an opaque pointer to a section.
124  FS_Section *getSection(const UT_StringRef &section) const;
125 
126  // Get an FS_Reader for reading a section of the index.
127  FS_Reader *getSectionReader(const UT_StringRef &section) const;
128 
129  // These utility functions should only be used by FS_ReaderHelpers
130  FS_ReaderStream *getSectionStream(const UT_StringRef &section) const;
131  FS_ReaderStream *getStreamCopy() const;
132 
133  // Given an opaque section pointer get an FS_IndexFile it represents.
134  FS_IndexFileHandle getIndexFileFromSection(const UT_StringRef &section,
135  const char *source = nullptr) const;
136 
137  // Simple access functions to find out about our sections.
138  int getNumSections() const { return mySections.size(); }
139  const UT_StringHolder &getSectionName(int index) const;
140 
141  // Access our source file name (which may be empty).
142  const UT_StringHolder &getSourceFile() const { return mySourceFile; }
143 
144  // Access functions for our description string.
145  const UT_StringHolder &getDescription() const { return myDescription; }
146  void setDescription(const UT_StringHolder &description)
147  { myDescription = description; setModified(); }
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,
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  void clear()
203  {
204  myToText = nullptr;
205  myFromText = nullptr;
206  myBinFilePattern.clear();
207  myTextFilePattern.clear();
208  }
209 
210  bool isClear() const
211  {
212  // sufficient to just text first data member
213  return myToText == nullptr;
214  }
215 
216  ConvertFunc myToText;
217  ConvertFunc myFromText;
220  };
222 
223 
224  static bool registerConversionFuncs(const char *bin_file_pattern,
225  const char *text_file_pattern,
226  ConvertFunc to_text,
227  ConvertFunc from_text);
228 
229  static const ConversionList &getConversionsToText();
230  static const ConversionList &getConversionsFromText();
231 
232 
233 protected:
234  void setModified() { myModified = true; }
235 
236  exint writeHeader(std::ostream &os) const;
237 
238  static ConversionList &_getConversionsToText();
239  static ConversionList &_getConversionsFromText();
240 
241 private:
242  typedef UT_ValArray<FS_Section *> FS_SectionArray;
243 
244  // Functions for opening and closing our stream.
245  void openStream(const UT_Options *options = nullptr) const;
246  void closeStream() const;
247 
248  // Helper functions for reading and writing ourselves to a directory.
249  void makeValidFileName(UT_String &filename);
250  bool writeSectionListFile(const char *sectionfile);
251 
252  // Writes out a section to the output stream. If the filter factory
253  // has been set, the filter will be applied to the section data before
254  // it is written out.
255  void writeSection(FS_Section *section,
256  std::ostream &os,
257  bool as_text) const;
258 
259  // Get an FS_Reader for reading a section of the index.
260  FS_Reader *getSectionReader(FS_Section *section) const;
261 
262  // These utility functions should only be used by FS_ReaderHelpers
263  FS_ReaderStream *getSectionStream(FS_Section *section) const;
264 
265  // Read in our index and use it to create our section data structures.
266  void readIndex();
267  void readDirectoryIndex(
268  const UT_String &source_file_path,
269  const UT_String &sections_file_path);
270  void readStreamIndex();
271 
272  // Obtains a size of the i-th section after it has been filtered (if at all)
273  int getWriteFilteredSectionSize( int index ) const;
274 
275  // Recalculates the data sizes of our sections when the read filter is set.
276  void recalculateFilteredSectionSizes();
277 
278  using SectionMap = UT_ArrayStringMap<FS_Section*>;
279 
280  // Data:
281  mutable FS_Reader *myReader;
282  FS_SectionArray mySections;
283  SectionMap mySectionTable;
284  FS_SectionArray myTempSections;
285  SectionMap myTempSectionTable;
286  UT_StringHolder myDescription;
287  UT_StringHolder mySourceFile;
288  FS_WriteFilterFactory *myWriteFilter;
289  FS_IStreamFilterFactory *myReadFilter;
290  int myModTime;
291  int64 mySize;
292  int myStreamStart;
293  bool myModified;
294  bool myIsExpanded;
295 
296  friend class FS_Section;
297  friend class FS_EmbeddedSection;
298  friend class FS_DiskSection;
299  friend class FS_IndexFileHandle;
300 };
301 
302 class FS_API FS_IndexFileHandle : public UT_SharedPtr<FS_IndexFile>
303 {
304 public:
307  virtual ~FS_IndexFileHandle();
308 
309 private:
310 
311  friend class FS_Section;
312  friend class FS_EmbeddedSection;
313  friend class FS_DiskSection;
314  friend class FS_IndexFile;
315 };
316 
317 // For UT::ArraySet.
318 namespace UT
319 {
320 template <typename T>
321 struct DefaultClearer;
322 
323 template <>
324 struct DefaultClearer<FS_IndexFile::ConversionFuncs>
325 {
327  static void clear(ValueT &v) { v.clear(); }
328  static bool isClear(const ValueT &v) { return v.isClear(); }
329  static void clearConstruct(ValueT *p)
330  {
331  new ((void *)p) ValueT();
332  }
333  static const bool clearNeedsDestruction = false;
334 };
335 } // namespace UT
336 
337 #endif
338 
GLint first
Definition: glcorearb.h:404
Class for reading files.
Definition: FS_Reader.h:33
GT_API const UT_StringHolder filename
UT_StringHolder myTextFilePattern
Definition: FS_IndexFile.h:219
const GLdouble * v
Definition: glcorearb.h:836
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:75
void setDescription(const UT_StringHolder &description)
Definition: FS_IndexFile.h:146
GLuint buffer
Definition: glcorearb.h:659
bool isExpanded() const
Tests if the index file is stored as expanded.
Definition: FS_IndexFile.h:175
UT_ArrayStringMap< ConversionFuncs > ConversionList
Definition: FS_IndexFile.h:221
void setModified()
Definition: FS_IndexFile.h:234
const UT_StringHolder & getDescription() const
Definition: FS_IndexFile.h:145
long long int64
Definition: SYS_Types.h:107
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:28
int64 exint
Definition: SYS_Types.h:116
bool hasTempSection(const UT_StringRef &section) const
Definition: FS_IndexFile.h:73
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:70
A map of string to various well defined value types.
Definition: UT_Options.h:42
const UT_StringHolder & getSourceFile() const
Definition: FS_IndexFile.h:142
GLuint index
Definition: glcorearb.h:785
WARNING: This class DOES NOT ALLOW EMPTY STRING as a key!
int getNumSections() const
Definition: FS_IndexFile.h:138
#define FS_API
Definition: FS_API.h:10
UT_StringHolder myBinFilePattern
Definition: FS_IndexFile.h:218