ObjFW  Diff

Differences From Artifact [778d1b4971]:

To Artifact [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