~jeanfrancois.roy/mpqkit/1.0b1

« back to all changes in this revision

Viewing changes to MPQFile.h

  • Committer: bahamut
  • Date: 2007-04-03 01:12:37 UTC
  • Revision ID: svn-v4:08a27de9-96f8-0310-9aec-cd9f9b5b01a8:trunk:158
- svn refactor phase 2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  MPQFile.h
 
3
//  MPQKit
 
4
//
 
5
//  Created by Jean-Francois Roy on Mon Sep 30 2002.
 
6
//  Copyright (c) 2002 MacStorm. All rights reserved.
 
7
//
 
8
 
 
9
#import <Foundation/Foundation.h>
 
10
#import <MPQKit/MPQSharedConstants.h>
 
11
 
 
12
/*!
 
13
  @header MPQFile.h
 
14
  This header is automatically included by MPQKit.h and provides the definition of MPQFile.
 
15
*/
 
16
 
 
17
@class MPQArchive;
 
18
 
 
19
/*!
 
20
    @class MPQFile
 
21
    @abstract MPQFile is the root class of a class cluster responsible for reading MPQ files. 
 
22
        It can be used for simple one-time extraction or for data streaming.
 
23
    @discussion MPQFile is both designed and fast enough to be used for file streaming. Indeed, 
 
24
        MPQFile objects maintain an internal file pointer, much like NSFileHandle objects. 
 
25
        Whenever you read some data from an MPQFile object, the file pointer is increased by the 
 
26
        number of bytes read. As such, you may simply repeatedly call the readDataOfLength: 
 
27
        method to stream a file. MPQFile offer other facilities, such as seeking and file 
 
28
        information.
 
29
        
 
30
        You should not create MPQFile objects on your own, since MPQFile is the root class of a class cluster.
 
31
        Rather, use MPQArchive's openFile methods to get an MPQFile object for a given MPQ file.
 
32
*/
 
33
@interface MPQFile : NSObject {
 
34
    NSString *filename;
 
35
    MPQArchive *parent;
 
36
    uint32_t file_pointer;
 
37
    
 
38
    uint32_t hash_position;
 
39
    mpq_hash_table_entry_t hash_entry;
 
40
    mpq_block_table_entry_t block_entry;
 
41
}
 
42
 
 
43
/*! 
 
44
    @method name
 
45
    @abstract Returns the file's MPQ path.
 
46
    @discussion Note that the path separator is \.
 
47
    @result An NSString containing the file's path.
 
48
*/
 
49
- (NSString *)name;
 
50
 
 
51
/*! 
 
52
    @method length
 
53
    @abstract Returns the file's uncompressed length.
 
54
    @discussion If you are going to use atomical writing, you'll need twice that amount of 
 
55
        space on the target disk to extract the file.
 
56
    @result The file's uncompressed length as an integer.
 
57
*/
 
58
- (uint32_t)length;
 
59
 
 
60
/*! 
 
61
    @method fileInfo
 
62
    @abstract Returns the information dictionary for the file.
 
63
    @discussion For details on the keys that will be in the information dictionary, 
 
64
        see the documentation for fileInfoForPosition: in MPQArchive.
 
65
    @result An NSDictionary object containing the file's information. nil on failure.
 
66
*/
 
67
- (NSDictionary *)fileInfo;
 
68
- (NSDictionary *)fileInfo:(NSError **)error;
 
69
 
 
70
/*! 
 
71
    @method seekToFileOffset:
 
72
    @abstract Seeks to the specified file offset.
 
73
    @discussion Note that this method simply calls seekToFileOffset:withMode: with the MPQFileBegin mode.
 
74
    @param offset The number of bytes to move from the beginning of the file as an unsigned integer.
 
75
    @result The new file position, or -1 on error.
 
76
    
 
77
*/
 
78
- (uint32_t)seekToFileOffset:(uint32_t)offset;
 
79
- (uint32_t)seekToFileOffset:(uint32_t)offset error:(NSError **)error;
 
80
 
 
81
/*! 
 
82
    @method seekToFileOffset:mode:
 
83
    @abstract Seeks the number of specified bytes from the specified starting point.
 
84
    @discussion The valid displacement modes are: 
 
85
    
 
86
        * MPQFileStart: Seeking is done with respect to the beginning of the file 
 
87
        and toward the end of file. In effect, this makes nDistanceToMove an absolute 
 
88
        file offset to seek to.
 
89
        
 
90
        * MPQFileCurrent: Seeking is done with respect to the current file pointer 
 
91
        and toward the end of file. If nDistanceToMove will move the file pointer 
 
92
        beyond the end of file, the file pointer is moved to the end of file.
 
93
        
 
94
        * MPQFileEnd: Seeking is done with respect to the end of file and toward 
 
95
        the beginning of the file. If nDistanceToMove will move the file pointer 
 
96
        to a negative position, the file pointer is moved to the beginning of the 
 
97
        file.
 
98
    @param offset The number of bytes to move from the beginning of the file as an unsigned integer.
 
99
    @param mode The displacement method. Must be a valid MPQFileDisplacementMode constant. 
 
100
        Will affect the interpretation of distanceToMove.
 
101
    @result The new file position, or -1 on error.
 
102
*/
 
103
- (uint32_t)seekToFileOffset:(uint32_t)offset mode:(MPQFileDisplacementMode)mode;
 
104
- (uint32_t)seekToFileOffset:(uint32_t)offset mode:(MPQFileDisplacementMode)mode error:(NSError **)error;
 
105
 
 
106
/*! 
 
107
    @method offsetInFile
 
108
    @abstract Returns position of the file pointer with respect to the beginning of the file.
 
109
    @discussion This should always return 0 for newly created MPQFile instances.
 
110
    @result The position of the file pointer as an integer.
 
111
*/
 
112
- (uint32_t)offsetInFile;
 
113
 
 
114
/*! 
 
115
    @method eof
 
116
    @abstract Returns the end of file state of the file.
 
117
    @discussion Returns YES when bytes were read up to or past the end of file, or when the file 
 
118
        cursor is moved past the last byte.
 
119
    @result YES if the file pointer is at the end of file, NO otherwise.
 
120
*/
 
121
- (BOOL)eof;
 
122
 
 
123
/*! 
 
124
    @method copyDataOfLength:
 
125
    @abstract Reads the specified number of bytes in the file starting at 
 
126
        the position of the file pointer and returns an NSData container.
 
127
    @discussion IMPORTANT: The size of the returned data may actually be smaller than the number of 
 
128
        requested bytes, if the number of requested bytes would cause the file pointer 
 
129
        to go beyond the end of file.
 
130
        
 
131
        The file pointer is incremented by the number of bytes returned.
 
132
        
 
133
        Note that the returned NSData object is not autoreleased and so its ownership is transferred
 
134
        to the caller. Because the NSData object is not autoreleased, this method will offer much 
 
135
        better performances in tight loops due to the large overhead of the autorelease mechanism.
 
136
    @param length The number of bytes to read.
 
137
    @result An NSData object containing the requested bytes (or less). nil on failure.
 
138
*/
 
139
- (NSData *)copyDataOfLength:(uint32_t)length;
 
140
- (NSData *)copyDataOfLength:(uint32_t)length error:(NSError **)error;
 
141
 
 
142
/*! 
 
143
    @method copyDataToEndOfFile
 
144
    @abstract Reads the file starting at the position of the file pointer down to
 
145
        the end of file.
 
146
    @discussion IMPORTANT: The size of the returned data may actually be smaller than the number of 
 
147
        requested bytes, if the number of requested bytes would cause the file pointer 
 
148
        to go beyond the end of file.
 
149
        
 
150
        The file pointer is incremented by the number of bytes returned.
 
151
        
 
152
        Note that this method simply calls the copyDataOfLength: method with the number 
 
153
        of bytes to read set to whatever value will move the file pointer to the end of file.
 
154
        
 
155
        Note that the returned NSData object is not autoreleased and so its ownership is transferred
 
156
        to the caller. Because the NSData object is not autoreleased, this method will offer much 
 
157
        better performances in tight loops due to the large overhead of the autorelease mechanism.
 
158
    @result An NSData object containing the bytes from the old file pointer down to the
 
159
        end of file. nil on failure.
 
160
*/
 
161
- (NSData *)copyDataToEndOfFile;
 
162
- (NSData *)copyDataToEndOfFile:(NSError **)error;
 
163
 
 
164
/*! 
 
165
    @method getDataOfLength:
 
166
    @abstract Reads the specified number of bytes in the file starting at 
 
167
        the position of the file pointer and returns an autoreleased NSData container.
 
168
    @discussion IMPORTANT: The size of the returned data may actually be smaller than the number of 
 
169
        requested bytes, if the number of requested bytes would cause the file pointer 
 
170
        to go beyond the end of file.
 
171
        
 
172
        The file pointer is incremented by the number of bytes returned.
 
173
    @param length The number of bytes to read.
 
174
    @result An autoreleased NSData object containing the requested bytes (or less). nil on failure.
 
175
*/
 
176
- (NSData *)getDataOfLength:(uint32_t)length;
 
177
- (NSData *)getDataOfLength:(uint32_t)length error:(NSError **)error;
 
178
 
 
179
/*! 
 
180
    @method getDataToEndOfFile
 
181
    @abstract Reads the file starting at the position of the file pointer down to
 
182
        the end of file.
 
183
    @discussion IMPORTANT: The size of the returned data may actually be smaller than the number of 
 
184
        requested bytes, if the number of requested bytes would cause the file pointer 
 
185
        to go beyond the end of file.
 
186
        
 
187
        The file pointer is incremented by the number of bytes returned.
 
188
        
 
189
        Note that this method simply calls the getDataOfLength: method with the number 
 
190
        of bytes to read set to whatever value will move the file pointer to the end of file.
 
191
    @result An autoreleased NSData object containing the bytes from the old file pointer down to the
 
192
        end of file. nil on failure.
 
193
*/
 
194
- (NSData *)getDataToEndOfFile;
 
195
- (NSData *)getDataToEndOfFile:(NSError **)error;
 
196
 
 
197
- (ssize_t)read:(void *)buffer size:(size_t)size error:(NSError **)error;
 
198
 
 
199
/*! 
 
200
    @method writeToFile:atomically:
 
201
    @abstract Writes the entire content of the file to the specified file on disk.
 
202
    @discussion Note that this method simply calls the readDataOfLength: method with the number 
 
203
        of bytes to read set to whatever value will move the file pointer to the end of file.
 
204
        
 
205
        Note that this method simply calls NSData's writeToFile:atomically: method to write 
 
206
        the data to the disk.
 
207
    @param path Path at which the file's content should be written.
 
208
    @param atomically Set to YES to write the data to a temporary file and move it to path after.
 
209
    @result YES on success and NO on failure.
 
210
*/
 
211
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)atomically;
 
212
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)atomically error:(NSError **)error;
 
213
 
 
214
@end
 
215