HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OP_OTLUtils.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: OP_OTLUtils.h (OP Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __OP_OTLUtils__
13 #define __OP_OTLUtils__
14 
15 #include "OP_API.h"
16 #include <UT/UT_Compression.h>
17 #include <UT/UT_Map.h>
18 #include <UT/UT_StringArray.h>
19 #include <UT/UT_StringHolder.h>
20 #include <UT/UT_StringMap.h>
21 #include <UT/UT_UniquePtr.h>
22 #include <SYS/SYS_Types.h>
23 
24 #include <iosfwd>
25 
26 
27 class OP_Network;
28 class OP_OTLLibrary;
29 class FS_IndexFile;
30 class UT_IStream;
31 class UT_String;
32 class UT_WorkBuffer;
33 struct OTLConversionData;
34 
35 
37 {
38 public:
39 
40  OP_OTLUtils();
41 
43  {
44  if (!theInstance)
45  theInstance = new OP_OTLUtils();
46  return theInstance;
47  }
48  // -----------------------------------------------------------------------
49  // High level routines to handle compression/decompression and
50  // conversion to/from MIME
51 
52  // auto decompress Contents and load into buffer
53  static bool getContentsSection(FS_IndexFile &indexFile,
55 
56  // provide stream for auto decompressed Contents stream
57  static UT_UniquePtr<UT_IStream> getContentsStream(
58  FS_IndexFile &deffile,
59  exint *uncompressed_buflen = nullptr);
60  // provide stream for auto decompressed Contents stream
61  static UT_UniquePtr<UT_IStream> getContentsStream(UT_IStream &is,
62  exint nbytes,
63  const UT_StringHolder &file_name,
64  exint *uncompressed_buflen = nullptr);
65 
66  // save the given buffer into the contents section appropriate
67  // for the indexfile given
68  static bool saveContentsSection(FS_IndexFile &indexFile,
69  const UT_WorkBuffer &contents_cpio);
70 
71  // If the contents section for the specified definition needs to be
72  // processed in any way then do so.
73  static bool processContentsSection(FS_IndexFile &indexFile,
74  const UT_String &section_name);
75 
76  // expand an indexfile into a directory and optionally convert
77  // the Contents section to the VCS friendly format if possible.
78  static bool expandToDirectory(OP_OTLLibrary *library,
79  const char *destdir,
80  bool vcsfriendly = false);
81 
82  static bool collapseLibrary(const char *libfile,
83  const char *srcdir,
84  bool createbackup = true);
85  // -----------------------------------------------------------------------
86  // Utility Functions for examining directories and such
87 
88  // expand an indexfile into a directory and optionally convert
89  // the Contents section to the VCS friendly format if possible.
90 
91  // Search all the assets for a filename within them
92  static void findAllSections(const char *srcdir,
93  const char *filename,
94  UT_StringArray &allfiles,
95  bool append = false);
96 
97  // Search all the assets for an existing MIME converted Contents section
98  static void findAllMimeSections(const char *srcdir,
99  UT_StringArray &allfiles,
100  bool append = false);
101 
102  // Determine if this directory is probably a valid HDA.
103  static bool isExpandedOtl(const char *src);
104 
105  // -----------------------------------------------------------------------
106  // Functions to help out doing diffs of assets.
107 
108  // Get the contents as text format regardless of how it's stored.
109  // This isn't guaranteed to be strictly the MIME format..
110  static bool getContentsAsText(FS_IndexFile &indexfile,
111  UT_WorkBuffer &contents_text,
112  bool strip_modtime = true);
113 
114 
115  static bool getNetworkSummary(UT_IStream &is,
116  UT_WorkBuffer &summary,
117  const char *prefix = nullptr,
118  bool isHipFile = false);
119 
120  static bool getNetworkSummary(OP_Network *network,
121  UT_WorkBuffer &summary,
122  const char *prefix = nullptr,
123  const char *root_name = nullptr);
124 
125  // -----------------------------------------------------------------------
126  // Mid level routines to handle compression/decompression and
127  // conversion to/from MIME.
128 
129  // Compress and write the given contents buffer
130  static bool compressContents(const UT_WorkBuffer &contents_cpio,
131  UT_CompressionType compression_type,
132  std::ostream *os);
133 
134  static bool convertContentsFromText(const UT_StringArray &files,
135  UT_StringArray &out_delete_files);
136  static bool convertContentsToText(const UT_StringArray &files,
137  bool preserve_modtime,
138  UT_StringArray &out_delete_files);
139 
140  // -----------------------------------------------------------------------
141  // Node data conversion
142  typedef bool(*ConvertSectionToTextFunc)(const UT_StringHolder &src_path,
143  const UT_WorkBuffer &in_data,
144  UT_WorkBuffer &out_data,
145  bool &store_external);
146 
147  typedef bool(*ConvertSectionFromTextFunc)(const UT_StringHolder &src_path,
148  const UT_WorkBuffer &in_data,
149  UT_WorkBuffer &out_data);
150 
151  typedef bool(*ConvertSectionToTextFuncEx)(const UT_StringHolder &src_path,
152  const UT_WorkBuffer &in_data,
153  UT_WorkBuffer &out_data,
154  bool &store_external,
155  void *conversion_data);
156 
157  typedef bool(*ConvertSectionFromTextFuncEx)(const UT_StringHolder &src_path,
158  const UT_WorkBuffer &in_data,
159  UT_WorkBuffer &out_data,
160  void *conversion_data);
161 
163  {
164  ConvertSectionToTextFunc myToText = nullptr;
165  ConvertSectionFromTextFunc myFromText = nullptr;
166  ConvertSectionToTextFuncEx myExToText = nullptr;
167  ConvertSectionFromTextFuncEx myExFromText = nullptr;
168 
169 
170  ConversionFuncs(ConvertSectionToTextFunc to_text = nullptr,
171  ConvertSectionFromTextFunc from_text = nullptr,
172  ConvertSectionToTextFuncEx to_text_ex = nullptr,
173  ConvertSectionFromTextFuncEx from_text_ex = nullptr) :
174  myToText(to_text), myFromText(from_text),
175  myExToText(to_text_ex), myExFromText(from_text_ex)
176  {}
178  ConvertSectionToTextFuncEx to_text_ex = nullptr,
179  ConvertSectionFromTextFuncEx from_text_ex = nullptr)
180  {
181  return ConversionFuncs(nullptr, nullptr, to_text_ex, from_text_ex);
182  }
183  };
185 
186 
187  static bool registerConversionFuncs(const UT_StringHolder &extension,
188  ConvertSectionToTextFunc to_text,
189  ConvertSectionFromTextFunc from_text);
190 
191  static bool registerConversionFuncsEx(const UT_StringHolder &extension,
192  ConvertSectionToTextFuncEx to_text,
193  ConvertSectionFromTextFuncEx from_text);
194 
196  { return getInstance()->myConversions; }
197 
198  // -----------------------------------------------------------------------
199  // Raw Contents conversions
200 
201  static bool CPIOToMIME(UT_IStream &is,
202  std::ostream &os,
203  bool preserve_modtime,
204  const char *externals_dest = nullptr);
205  static bool MIMEToCPIO(UT_IStream &is,
206  std::ostream &os,
207  const char *externals_dest = nullptr);
208 
209  static bool CPIOToMIME(UT_IStream &is,
210  std::ostream &os,
211  bool preserve_modtime,
212  const char *externals_dest,
213  OTLConversionData *conversion_data);
214  static bool MIMEToCPIO(UT_IStream &is,
215  std::ostream &os,
216  const char *externals_dest,
217  OTLConversionData *conversion_data);
218 
219  static bool _convertContentsToText(UT_IStream &is,
220  const UT_String &dir_name,
221  bool preserve_modtime,
222  std::ostream &os);
223  static bool _convertContentsFromText(UT_IStream &is,
224  const UT_String & dir_name,
225  UT_CompressionType compression_type,
226  std::ostream &os);
227  // -----------------------------------------------------------------------
228  // Parameter conversions
229 
230  static UT_StringHolder getNameFromCompression(
231  UT_CompressionType compression_type);
232  static UT_CompressionType getCompressionFromName(
233  const UT_StringHolder &section_name);
234  static UT_CompressionType getCompressionFromOptionsFile(
235  const UT_StringHolder &options_filename);
236 
237  // -----------------------------------------------------------------------
238  // Public data
239 
241 
244 
245 private:
246  ConversionList myConversions;
247 
248  static OP_OTLUtils *theInstance;
249 
250  static ConversionList &_getConversions()
251  { return getInstance()->myConversions; }
252 
253 };
254 
255 #endif
256 
GT_API const UT_StringHolder filename
UT_StringMap< ConversionFuncs > ConversionList
Definition: OP_OTLUtils.h:184
Unsorted map container.
Definition: UT_Map.h:107
ConversionFuncs(ConvertSectionToTextFunc to_text=nullptr, ConvertSectionFromTextFunc from_text=nullptr, ConvertSectionToTextFuncEx to_text_ex=nullptr, ConvertSectionFromTextFuncEx from_text_ex=nullptr)
Definition: OP_OTLUtils.h:170
int64 exint
Definition: SYS_Types.h:125
static const UT_StringArray allContentsSectionNames
Definition: OP_OTLUtils.h:242
static OP_OTLUtils * getInstance()
Definition: OP_OTLUtils.h:42
static const ConversionList & getConversions()
Definition: OP_OTLUtils.h:195
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
UT_Map< UT_StringHolder, UT_CompressionType > CompressionMap
Definition: OP_OTLUtils.h:240
static ConversionFuncs CreateConversionFuncsEx(ConvertSectionToTextFuncEx to_text_ex=nullptr, ConvertSectionFromTextFuncEx from_text_ex=nullptr)
Definition: OP_OTLUtils.h:177
UT_CompressionType
#define OP_API
Definition: OP_API.h:10
static const CompressionMap contentsCompressionNameMap
Definition: OP_OTLUtils.h:243
OIIO_UTIL_API std::string extension(string_view filepath, bool include_dot=true) noexcept
Definition: format.h:895
GLenum src
Definition: glcorearb.h:1793