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