HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 <iostream>
17 #include <UT/UT_Compression.h>
18 #include <UT/UT_Defines.h>
19 #include <UT/UT_IStream.h>
20 #include <UT/UT_StringArray.h>
21 #include <UT/UT_StringMap.h>
22 #include <UT/UT_UniquePtr.h>
23 #include <UT/UT_WorkBuffer.h>
24 #include <FS/FS_IndexFile.h>
25 
26 class OP_Operator;
27 class OP_Network;
28 class OP_OTLLibrary;
29 
31 {
32 public:
33 
34  OP_OTLUtils();
35 
37  {
38  if (!theInstance)
39  theInstance = new OP_OTLUtils();
40  return theInstance;
41  }
42  // -----------------------------------------------------------------------
43  // High level routines to handle compression/decompression and
44  // conversion to/from MIME
45 
46  // auto decompress Contents and load into buffer
47  static bool getContentsSection(FS_IndexFile &indexFile,
49 
50  // provide stream for auto decompressed Contents stream
51  static UT_UniquePtr<UT_IStream> getContentsStream(
52  FS_IndexFile &deffile,
53  exint *uncompressed_buflen = nullptr);
54  // provide stream for auto decompressed Contents stream
55  static UT_UniquePtr<UT_IStream> getContentsStream(UT_IStream &is,
56  exint nbytes,
57  const UT_StringHolder &file_name,
58  exint *uncompressed_buflen = nullptr);
59 
60  // save the given buffer into the contents section appropriate
61  // for the indexfile given
62  static bool saveContentsSection(FS_IndexFile &indexFile,
63  const UT_WorkBuffer &contents_cpio);
64 
65  // If the contents section for the specified definition needs to be
66  // processed in any way then do so.
67  static bool processContentsSection(FS_IndexFile &indexFile,
68  const UT_String &section_name);
69 
70  // expand an indexfile into a directory and optionally convert
71  // the Contents section to the VCS friendly format if possible.
72  static bool expandToDirectory(OP_OTLLibrary *library,
73  const char *destdir,
74  bool vcsfriendly = false);
75 
76  static bool collapseLibrary(const char *libfile,
77  const char *srcdir,
78  bool createbackup = true);
79  // -----------------------------------------------------------------------
80  // Utility Functions for examining directories and such
81 
82  // expand an indexfile into a directory and optionally convert
83  // the Contents section to the VCS friendly format if possible.
84 
85  // Search all the assets for a filename within them
86  static void findAllSections(const char *srcdir,
87  const char *filename,
88  UT_StringArray &allfiles,
89  bool append = false);
90 
91  // Search all the assets for an existing MIME converted Contents section
92  static void findAllMimeSections(const char *srcdir,
93  UT_StringArray &allfiles,
94  bool append = false);
95 
96  // Determine if this directory is probably a valid HDA.
97  static bool isExpandedOtl(const char *src);
98 
99  // -----------------------------------------------------------------------
100  // Functions to help out doing diffs of assets.
101 
102  // Get the contents as text format regardless of how it's stored.
103  // This isn't guaranteed to be strictly the MIME format..
104  static bool getContentsAsText(FS_IndexFile &indexfile,
105  UT_WorkBuffer &contents_text,
106  bool strip_modtime = true);
107 
108 
109  static bool getNetworkSummary(UT_IStream &is,
110  UT_WorkBuffer &summary,
111  const char *prefix = nullptr,
112  bool isHipFile = false);
113 
114  static bool getNetworkSummary(OP_Network *network,
115  UT_WorkBuffer &summary,
116  const char *prefix = nullptr,
117  const char *root_name = nullptr);
118 
119  // -----------------------------------------------------------------------
120  // Mid level routines to handle compression/decompression and
121  // conversion to/from MIME.
122 
123  // Compress and write the given contents buffer
124  static bool compressContents(const UT_WorkBuffer &contents_cpio,
126  std::ostream *os);
127 
128  static bool convertContentsFromText(const UT_StringArray &files,
129  UT_StringArray &out_delete_files);
130  static bool convertContentsToText(UT_StringArray files,
131  bool preserve_modtime,
132  UT_StringArray &out_delete_files);
133 
134  // -----------------------------------------------------------------------
135  // Node data conversion
136 
137  typedef bool(*ConvertSectionToTextFunc)(const UT_StringHolder &src_path,
138  const UT_WorkBuffer &in_data,
139  UT_WorkBuffer &out_data,
140  bool &store_external);
141 
142  typedef bool(*ConvertSectionFromTextFunc)(const UT_StringHolder &src_path,
143  const UT_WorkBuffer &in_data,
144  UT_WorkBuffer &out_data);
145 
147  {
148  ConvertSectionToTextFunc myToText;
149  ConvertSectionFromTextFunc myFromText;
150  ConversionFuncs(ConvertSectionToTextFunc to_text = nullptr,
151  ConvertSectionFromTextFunc from_text = nullptr)
152  : myToText(to_text), myFromText(from_text)
153  {}
154  };
156 
157 
158  static bool registerConversionFuncs(const UT_StringHolder &extension,
159  ConvertSectionToTextFunc to_text,
160  ConvertSectionFromTextFunc from_text);
161 
163  { return getInstance()->myConversions; }
164 
165  // -----------------------------------------------------------------------
166  // Raw Contents conversions
167 
168  static bool CPIOToMIME(UT_IStream &is,
169  std::ostream &os,
170  bool preserve_modtime,
171  const char *externals_dest = nullptr);
172  static bool MIMEToCPIO(UT_IStream &is,
173  std::ostream &os,
174  const char *externals_dest = nullptr);
175 
176  // -----------------------------------------------------------------------
177  // Parameter conversions
178 
179  static UT_StringHolder getNameFromCompression(
181  static UT_CompressionType getCompressionFromName(
182  const UT_StringHolder &section_name);
183 
184  // -----------------------------------------------------------------------
185  // Public data
186 
188 
191 
192 private:
193  ConversionList myConversions;
194 
195  static OP_OTLUtils *theInstance;
196 
197  static ConversionList &_getConversions()
198  { return getInstance()->myConversions; }
199 
200 };
201 
202 #endif
203 
UT_StringMap< ConversionFuncs > ConversionList
Definition: OP_OTLUtils.h:155
Unsorted map container.
Definition: UT_Map.h:83
static const UT_StringArray allContentsSectionNames
Definition: OP_OTLUtils.h:189
static OP_OTLUtils * getInstance()
Definition: OP_OTLUtils.h:36
static const ConversionList & getConversions()
Definition: OP_OTLUtils.h:162
png_infop png_charpp int * compression_type
Definition: png.h:2467
UT_Map< UT_StringHolder, UT_CompressionType > CompressionMap
Definition: OP_OTLUtils.h:187
ConvertSectionFromTextFunc myFromText
Definition: OP_OTLUtils.h:149
int64 exint
Definition: SYS_Types.h:115
ConversionFuncs(ConvertSectionToTextFunc to_text=nullptr, ConvertSectionFromTextFunc from_text=nullptr)
Definition: OP_OTLUtils.h:150
GLboolean * data
Definition: glcorearb.h:130
UT_CompressionType
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:47
#define OP_API
Definition: OP_API.h:10
static const CompressionMap contentsCompressionNameMap
Definition: OP_OTLUtils.h:190
ConvertSectionToTextFunc myToText
Definition: OP_OTLUtils.h:148
GLenum src
Definition: glcorearb.h:1792