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  * NOTE: Because some UT_CPIO methods seek back when writing the stream,
89  * this class should be used with UT_OStringRefStream class, which
90  * may discard previoulsy written data when auto-terminating the string.
91  * Please, use UT_OStringStream instead, use its string length,
92  * and watch out for null characters in the middle of the CPIO stream.
93  */
94 
95 #ifndef __UT_CPIO_h__
96 #define __UT_CPIO_h__
97 
98 #include "UT_API.h"
99 #include "UT_String.h"
100 #include "UT_UniquePtr.h"
101 #include "UT_WorkBuffer.h"
102 
103 class UT_IStream;
104 class UT_OStringStream;
105 
106 enum {
108  UT_CPIO_EOF_OK, // End of file reached
109  UT_CPIO_BINARY, // Binary CPIO format not supported yet
110  UT_CPIO_BAD_EOF, // Unexpected EOF
111  UT_CPIO_BAD_HEADER, // Bad header
112  UT_CPIO_BAD_CONTENTS, // Bad contents in CPIO packet
113  UT_CPIO_BAD_STREAM, // Bad stream (i.e. bad calling sequence)
114  UT_CPIO_USER_ABORT // User cancelled reading the stream
115 };
116 
118 public:
119  UT_CPIO();
120  virtual ~UT_CPIO();
121 
122  // This open will read a CPIO header from the stream.
123  virtual int open(UT_IStream &is, UT_WorkBuffer &pathname);
124  int open(UT_IStream &is, UT_String &pathname)
125  {
126  UT_WorkBuffer pathname_buffer;
127  pathname_buffer.append(pathname);
128  int result = open(is, pathname_buffer);
129  pathname_buffer.copyIntoString(pathname);
130  return result;
131  }
132 
133  // This opens after we have verified it is a true CPIO file.
134  int openPostCheckNormal(UT_IStream &is, UT_WorkBuffer &pathname);
135  int openPostCheckExtended(UT_IStream &is, UT_WorkBuffer &pathname);
136 
137  // A safe (but memory intensive) mechanism for reading from the file
138  virtual UT_IStream *read(UT_IStream &is);
139 
140  // Method to skip over the packet that's currently open
141  virtual int skip(UT_IStream &is);
142 
143  // After reading or skipping, closing will move the stream pointer
144  // to the correct position. This is not a manditory method. In fact,
145  // read packets should not be closed if the stream is cin.
146  virtual int close(UT_IStream &is);
147 
148  // Memory intensive method to write a packet, but it works if you're
149  // sending data to cout or cerr streams
150  virtual std::ostream *open();
151  virtual int close(std::ostream &os, const char *path, int bin=0, exint modtime = -1);
152 
153  // Non-memory intensive method to write a packet, it does not work when
154  // writing to cout or cerr streams
155  virtual int open(std::ostream &os, const char *path, int bin=0, exint modtime = -1);
156  virtual int close(std::ostream &os);
157 
158  // When all packets have been written, please write a trailer
159  virtual int writeTrailer(std::ostream &os, exint modtime = -1);
160 
161  exint getFileSize() const { return myFileSize; }
162  exint getModTime() const { return myModTime; }
163 
164 protected:
169  exint myFileSize; // For non-memory intensive read
170  exint myHeaderStart; // For non-memory intensive write
171  exint myHeaderEnd; // End of header
172  bool myPacketTainted; // Is this packet being read tainted?
173 };
174 
175 #endif
int open(UT_IStream &is, UT_String &pathname)
Definition: UT_CPIO.h:124
exint myHeaderEnd
Definition: UT_CPIO.h:171
void skip(T &in, int n)
Definition: ImfXdr.h:885
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
UT_UniquePtr< UT_IStream > myIs
Definition: UT_CPIO.h:166
An output stream object that owns its own string buffer storage.
#define UT_API
Definition: UT_API.h:13
exint getFileSize() const
Definition: UT_CPIO.h:161
void copyIntoString(UT_String &str) const
void read(T &in, bool &v)
Definition: ImfXdr.h:611
int64 exint
Definition: SYS_Types.h:116
exint myModTime
Definition: UT_CPIO.h:168
bool myPacketTainted
Definition: UT_CPIO.h:172
virtual int open(float queuesize)
exint getModTime() const
Definition: UT_CPIO.h:162
virtual void close()
SYS_FORCE_INLINE void append(char character)
exint myHeaderStart
Definition: UT_CPIO.h:170
exint myFileSize
Definition: UT_CPIO.h:169
UT_WorkBuffer myCurrentFile
Definition: UT_CPIO.h:165
UT_UniquePtr< UT_OStringStream > myOs
Definition: UT_CPIO.h:167