HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_CPIO.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: UT library (C++)
7  *
8  * COMMENTS: CPIO packetting for I/O
9  *
10  * USAGE:
11  * To read from a CPIO archive:
12  *
13  * Memory intensive but safe methods
14  *
15  * myclass::readObject(istream &filestream, int binary)
16  * {
17  * UT_CPIO packet;
18  * UT_WorkBuffer type;
19  * istream *is;
20  *
21  * if (packet.open(filestream, type) == UT_CPIO_OK)
22  * {
23  * obj = myclass->addObject(type.buffer());
24  * is = packet.read(); // Read in the contents
25  * obj->readData(*is, binary);
26  * packet.close(filestream); // Correct position
27  * }
28  * else flagError();
29  * }
30  *
31  * myclass::writeObject(object *obj, ostream &filestream, int binary)
32  * {
33  * UT_CPIO packet;
34  * ostream *os;
35  *
36  * if (os = packet.open()) // Open a CPIO packet for writing
37  * {
38  * obj->saveData(*os, binary);
39  * // Now, flush the contents
40  * if (packet.close(filestream, obj->getType()) != UT_CPIO_OK)
41  * flagError();
42  * // If we're at the end, flag the EOF
43  * if (lastObject)
44  * packet.writeTrailer(filestream);
45  * }
46  * }
47  *
48  * Raw methods (not using the memory intensive mechanisms)
49  *
50  * myclass::readCommands(istream &filestream)
51  * {
52  * UT_CPIO packet;
53  * UT_WorkBuffer pathname;
54  * char buffer[SIZE];
55  *
56  * if (packet.open(filestream, pathname) == UT_CPIO_OK)
57  * {
58  * if (pathname == "commands") // Read commands
59  * {
60  * long fileSize = packet.getFileSize();
61  *
62  * while (fileSize > 0)
63  * {
64  * filestream.getline(buffer, SIZE);
65  * fileSize -= strlen(buffer);
66  * execute(buffer);
67  * }
68  * // Now, correct my position in case of overrun
69  * packet.close(filestream);
70  * }
71  * }
72  * }
73  *
74  * myclass::writeStuff(ostream &filestream)
75  * {
76  * UT_CPIO packet;
77  *
78  * if (packet.open(filestream, "commands") == UT_CPIO_OK)
79  * {
80  * for (i = 0; i < ncommands; i++)
81  * os << command[i] << endl;
82  * packet.close(filestream);
83  * }
84  * if (last) packet.writeTrailer(filestream);
85  * }
86  *
87  */
88 
89 #ifndef __UT_CPIO_h__
90 #define __UT_CPIO_h__
91 
92 #include "UT_API.h"
93 #include "UT_String.h"
94 #include "UT_UniquePtr.h"
95 #include "UT_WorkBuffer.h"
96 
97 class UT_IStream;
98 class UT_OStringStream;
99 
100 enum {
102  UT_CPIO_EOF_OK, // End of file reached
103  UT_CPIO_BINARY, // Binary CPIO format not supported yet
104  UT_CPIO_BAD_EOF, // Unexpected EOF
105  UT_CPIO_BAD_HEADER, // Bad header
106  UT_CPIO_BAD_CONTENTS, // Bad contents in CPIO packet
107  UT_CPIO_BAD_STREAM, // Bad stream (i.e. bad calling sequence)
108  UT_CPIO_USER_ABORT // User cancelled reading the stream
109 };
110 
112 public:
113  UT_CPIO();
114  virtual ~UT_CPIO();
115 
116  // This open will read a CPIO header from the stream.
117  virtual int open(UT_IStream &is, UT_WorkBuffer &pathname);
118  int open(UT_IStream &is, UT_String &pathname)
119  {
120  UT_WorkBuffer pathname_buffer;
121  pathname_buffer.append(pathname);
122  int result = open(is, pathname_buffer);
123  pathname_buffer.copyIntoString(pathname);
124  return result;
125  }
126 
127  // This opens after we have verified it is a true CPIO file.
128  int openPostCheckNormal(UT_IStream &is, UT_WorkBuffer &pathname);
129  int openPostCheckExtended(UT_IStream &is, UT_WorkBuffer &pathname);
130 
131  // A safe (but memory intensive) mechanism for reading from the file
132  virtual UT_IStream *read(UT_IStream &is);
133 
134  // Method to skip over the packet that's currently open
135  virtual int skip(UT_IStream &is);
136 
137  // After reading or skipping, closing will move the stream pointer
138  // to the correct position. This is not a manditory method. In fact,
139  // read packets should not be closed if the stream is cin.
140  virtual int close(UT_IStream &is);
141 
142  // Memory intensive method to write a packet, but it works if you're
143  // sending data to cout or cerr streams
144  virtual std::ostream *open();
145  virtual int close(std::ostream &os, const char *path, int bin=0, exint modtime = -1);
146 
147  // Non-memory intensive method to write a packet, it does not work when
148  // writing to cout or cerr streams
149  virtual int open(std::ostream &os, const char *path, int bin=0, exint modtime = -1);
150  virtual int close(std::ostream &os);
151 
152  // When all packets have been written, please write a trailer
153  virtual int writeTrailer(std::ostream &os, exint modtime = -1);
154 
155  exint getFileSize() const { return myFileSize; }
156  exint getModTime() const { return myModTime; }
157 
158 protected:
163  exint myFileSize; // For non-memory intensive read
164  exint myHeaderStart; // For non-memory intensive write
165  exint myHeaderEnd; // End of header
166  bool myPacketTainted; // Is this packet being read tainted?
167 };
168 
169 #endif
int open(UT_IStream &is, UT_String &pathname)
Definition: UT_CPIO.h:118
exint myHeaderEnd
Definition: UT_CPIO.h:165
void skip(T &in, int n)
Definition: ImfXdr.h:613
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
UT_UniquePtr< UT_IStream > myIs
Definition: UT_CPIO.h:160
int64 exint
Definition: SYS_Types.h:125
An output stream object that owns its own string buffer storage.
#define UT_API
Definition: UT_API.h:14
exint getFileSize() const
Definition: UT_CPIO.h:155
void copyIntoString(UT_String &str) const
**But if you need a result
Definition: thread.h:613
void close() override
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
int open(float queuesize) override
exint myModTime
Definition: UT_CPIO.h:162
bool myPacketTainted
Definition: UT_CPIO.h:166
exint getModTime() const
Definition: UT_CPIO.h:156
SYS_FORCE_INLINE void append(char character)
exint myHeaderStart
Definition: UT_CPIO.h:164
exint myFileSize
Definition: UT_CPIO.h:163
UT_WorkBuffer myCurrentFile
Definition: UT_CPIO.h:159
UT_UniquePtr< UT_OStringStream > myOs
Definition: UT_CPIO.h:161