ObjFW  Check-in [5f4f207266]

Overview
Comment:Use open() / read() / write() instead of fopen() / fread() / fwrite().

This fixes reading from stdin.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5f4f207266e1626d395e94f30f4cf33555fa8ed5217d7b8327dabae730701eca
User & Date: js on 2010-04-02 15:58:39
Other Links: manifest | tags
Context
2010-04-02
16:33
-[hexadecimalValueAsInteger] returns an unsigned value now. check-in: 59dd873fd0 user: js tags: trunk
15:58
Use open() / read() / write() instead of fopen() / fread() / fwrite(). check-in: 5f4f207266 user: js tags: trunk
14:18
Close socket before throwing an exception when bind fails. check-in: ebf57f4891 user: js tags: trunk
Changes

Modified src/OFFile.h from [5e8cf7bcda] to [e16ab950d2].

18
19
20
21
22
23
24
25
26



27
28
29
30
31
32
33
34
35
36
37
38

39
40
41
42

43
44
45
46
47
48
49
18
19
20
21
22
23
24


25
26
27
28
29
30
31
32
33
34
35
36
37
38

39
40
41
42

43
44
45
46
47
48
49
50







-
-
+
+
+











-
+



-
+







@class OFString;

/**
 * \brief A class which provides functions to read, write and manipulate files.
 */
@interface OFFile: OFStream
{
	FILE *fp;
	BOOL close;
	int fd;
	BOOL closable;
	BOOL eos;
}

/**
 * \param path The path to the file to open as a string
 * \param mode The mode in which the file should be opened as a string
 * \return A new autoreleased OFFile
 */
+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode;

/**
 * \param fp A file pointer, returned from for example fopen().
 * \param fd A file descriptor, returned from for example open().
 *	     It is not closed when the OFFile object is deallocated!
 * \return A new autoreleased OFFile
 */
+ fileWithFilePointer: (FILE*)fp;
+ fileWithFileDescriptor: (int)fd;

/**
 * Changes the mode of a file.
 *
 * Only changes read-only flag on Windows.
 *
 * \param path The path to the file of which the mode should be changed as a
118
119
120
121
122
123
124
125

126
127
128

129
130
131
132
133
134
135
136
119
120
121
122
123
124
125

126
127
128

129
130
131
132
133
134
135
136
137







-
+


-
+








 */
- initWithPath: (OFString*)path
	  mode: (OFString*)mode;

/**
 * Initializes an already allocated OFFile.
 *
 * \param fp A file pointer, returned from for example fopen().
 * \param fd A file descriptor, returned from for example open().
 *	     It is not closed when the OFFile object is deallocated!
 */
- initWithFilePointer: (FILE*)fp;
- initWithFileDescriptor: (int)fd;
@end

/// An OFFile object for stdin
extern OFFile *of_stdin;
/// An OFFile object for stdout
extern OFFile *of_stdout;
/// An OFFile object for stderr
extern OFFile *of_stderr;

Modified src/OFFile.m from [778d1b4971] to [cdb8ca685b].

13
14
15
16
17
18
19

20
21
22
23
24
25
26
27
28




29
30
31






























32
33
34
35
36
37
38
39
40
41
42
43
44
45
46



47
48
49
50
51
52
53
54
55
56

57
58

59
60
61
62
63
64
65
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78



79
80
81
82
83
84
85
86
87
88
89
90

91
92

93
94
95
96
97
98
99
100







+









+
+
+
+



+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+












-
-
-
+
+
+









-
+

-
+








#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#import "OFFile.h"
#import "OFString.h"
#import "OFExceptions.h"

#ifdef _WIN32
# import <windows.h>
#endif

#ifndef O_BINARY
# define O_BINARY 0
#endif

OFFile *of_stdin = nil;
OFFile *of_stdout = nil;
OFFile *of_stderr = nil;

static int parse_mode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;
	if (!strcmp(mode, "rb"))
		return O_RDONLY | O_BINARY;
	if (!strcmp(mode, "r+"))
		return O_RDWR;
	if (!strcmp(mode, "rb+") || !strcmp(mode, "r+b"))
		return O_RDWR | O_BINARY;
	if (!strcmp(mode, "w"))
		return O_WRONLY | O_CREAT | O_TRUNC;
	if (!strcmp(mode, "wb"))
		return O_WRONLY | O_CREAT | O_TRUNC | O_BINARY;
	if (!strcmp(mode, "w+"))
		return O_RDWR | O_CREAT | O_TRUNC;
	if (!strcmp(mode, "wb+") || !strcmp(mode, "w+b"))
		return O_RDWR | O_CREAT | O_TRUNC | O_BINARY;
	if (!strcmp(mode, "a"))
		return O_WRONLY | O_CREAT | O_APPEND;
	if (!strcmp(mode, "ab"))
		return O_WRONLY | O_CREAT | O_APPEND | O_BINARY;
	if (!strcmp(mode, "a+"))
		return O_RDWR | O_CREAT | O_APPEND;
	if (!strcmp(mode, "ab+") || !strcmp(mode, "a+b"))
		return O_RDWR | O_CREAT | O_APPEND | O_BINARY;

	return -1;
}

/// \cond internal
@interface OFFileSingleton: OFFile
@end
/// \endcond

@implementation OFFile
+ (void)load
{
	if (self != [OFFile class])
		return;

	of_stdin = [[OFFileSingleton alloc] initWithFilePointer: stdin];
	of_stdout = [[OFFileSingleton alloc] initWithFilePointer: stdout];
	of_stderr = [[OFFileSingleton alloc] initWithFilePointer: stderr];
	of_stdin = [[OFFileSingleton alloc] initWithFileDescriptor: 0];
	of_stdout = [[OFFileSingleton alloc] initWithFileDescriptor: 1];
	of_stderr = [[OFFileSingleton alloc] initWithFileDescriptor: 2];
}

+ fileWithPath: (OFString*)path
	  mode: (OFString*)mode
{
	return [[[self alloc] initWithPath: path
				      mode: mode] autorelease];
}

+ fileWithFilePointer: (FILE*)fp_
+ fileWithFileDescriptor: (int)fd_
{
	return [[[self alloc] initWithFilePointer: fp_] autorelease];
	return [[[self alloc] initWithFileDescriptor: fd_] autorelease];
}

+ (void)changeModeOfFile: (OFString*)path
		  toMode: (mode_t)mode
{
#ifndef _WIN32
	if (chmod([path cString], mode))
149
150
151
152
153
154
155

156
157
158







159

160
161
162
163
164
165
166
167

168
169
170
171
172

173
174
175
176

177
178
179
180
181
182
183
184


185
186
187
188
189
190
191

192
193
194

195
196
197
198
199
200
201
202

203
204
205


206
207
208
209
210
211
212
213
214
215
216

217
218
219
220
221
222
223
224
225
226
227



228
229
230
231
232
233
234
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

202
203
204
205
206
207
208
209

210
211
212
213
214

215
216
217
218

219
220
221
222
223
224
225


226
227
228
229
230
231
232
233

234
235
236

237
238
239
240
241
242
243
244

245

246
247
248
249
250
251
252
253
254
255
256
257
258


259
260
261
262
263
264
265
266
267



268
269
270
271
272
273
274
275
276
277







+



+
+
+
+
+
+
+
-
+







-
+




-
+



-
+






-
-
+
+






-
+


-
+







-
+
-


+
+









-
-
+








-
-
-
+
+
+







					      selector: _cmd];
}

- initWithPath: (OFString*)path
	  mode: (OFString*)mode
{
	Class c;
	int flags;

	self = [super init];

	if ((flags = parse_mode([mode cString])) == -1) {
		c = isa;
		[super dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}

	if ((fp = fopen([path cString], [mode cString])) == NULL) {
	if ((fd = open([path cString], flags)) == -1) {
		c = isa;
		[super dealloc];
		@throw [OFOpenFileFailedException newWithClass: c
							  path: path
							  mode: mode];
	}

	close = YES;
	closable = YES;

	return self;
}

- initWithFilePointer: (FILE*)fp_
- initWithFileDescriptor: (int)fd_
{
	self = [super init];

	fp = fp_;
	fd = fd_;

	return self;
}

- (void)dealloc
{
	if (close && fp != NULL)
		fclose(fp);
	if (closable && fd != -1)
		close(fd);

	[super dealloc];
}

- (BOOL)atEndOfStreamWithoutCache
{
	if (fp == NULL)
	if (fd == -1)
		return YES;

	return (feof(fp) == 0 ? NO : YES);
	return eos;
}

- (size_t)readNBytesWithoutCache: (size_t)size
		      intoBuffer: (char*)buf
{
	size_t ret;

	if (fp == NULL || feof(fp) || ((ret = fread(buf, 1, size, fp)) == 0 &&
	if (fd == -1 || eos)
	    size != 0 && !feof(fp)))
		@throw [OFReadFailedException newWithClass: isa
						      size: size];
	if ((ret = read(fd, buf, size)) == 0)
		eos = YES;

	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	size_t ret;

	if (fp == NULL || feof(fp) ||
	    ((ret = fwrite(buf, 1, size, fp)) < size && size != 0))
	if (fd == -1 || eos || (ret = write(fd, buf, size)) < size)
		@throw [OFWriteFailedException newWithClass: isa
						       size: size];

	return ret;
}

- close
{
	if (fp != NULL)
		fclose(fp);
	fp = NULL;
	if (fd != -1)
		close(fd);
	fd = -1;

	return self;
}
@end

/// \cond internal
@implementation OFFileSingleton