ObjFW  Diff

Differences From Artifact [1049054022]:

To Artifact [f28a278b7e]:


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
40
41
42
43
44
45
46


47



48
49
50




51
52
53
54
55
56
57







-
-

-
-
-



-
-
-
-







#import "OFWriteFailedException.h"

#if defined(OF_MORPHOS) && !defined(OF_IXEMUL)
# define BOOL EXEC_BOOL
# include <exec/execbase.h>
# include <proto/dos.h>
# undef BOOL

# define INVALID_FD 0
# define getpid() ((int)SysBase->ThisTask)
# define read(fd, buf, len) Read(fd, buf, len)
# define write(fd, buf, len) Write(fd, buf, len)

extern struct ExecBase *SysBase;
#endif

#ifndef INVALID_FD
# define INVALID_FD -1
#endif

/* References for static linking */
#ifdef OF_WINDOWS
void
_reference_to_OFStdIOStream_Win32Console(void)
{
	[OFStdIOStream_Win32Console class];
}
112
113
114
115
116
117
118
119

120
121
122
123
124

125
126
127
128
129

130
131
132
133
134

135
136
137


138
139
140


141
142

143
144
145
146
147
148
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

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
103
104
105
106
107
108
109

110
111
112
113
114

115
116
117
118
119

120
121
122
123
124

125



126
127



128
129


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
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
278

279
280
281
282
283


284
285

286

287
288
289
290
291
292
293
294
295







-
+




-
+




-
+




-
+
-
-
-
+
+
-
-
-
+
+
-
-
+



















-
-
+
+



-
+















+
-
+

+
+
+
+









+
-
+



-
+




-
+







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











+
-
+



-
+



-
+



-
+







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


















-
+

+
+

-
-
+
+
-

-
+
+







	of_stderr = [[OFStdIOStream alloc] of_initWithFileDescriptor: 2];
# else
	BPTR input = Input(), output = Output();
	BPTR error = ((struct Process *)SysBase->ThisTask)->pr_CES;
	bool inputClosable = false, outputClosable = false,
	    errorClosable = false;

	if (input == INVALID_FD) {
	if (input == 0) {
		input = Open("*", MODE_OLDFILE);
		inputClosable = true;
	}

	if (output == INVALID_FD) {
	if (output == 0) {
		output = Open("*", MODE_OLDFILE);
		outputClosable = true;
	}

	if (error == INVALID_FD) {
	if (error == 0) {
		error = Open("*", MODE_OLDFILE);
		errorClosable = true;
	}

	of_stdin = [[OFStdIOStream alloc]
	of_stdin = [[OFStdIOStream alloc] of_initWithHandle: input
	    of_initWithFileDescriptor: input
			     closable: inputClosable];
	of_stdout = [[OFStdIOStream alloc]
						   closable: inputClosable];
	of_stdout = [[OFStdIOStream alloc] of_initWithHandle: output
	    of_initWithFileDescriptor: output
			     closable: outputClosable];
	of_stderr = [[OFStdIOStream alloc]
						    closable: outputClosable];
	of_stderr = [[OFStdIOStream alloc] of_initWithHandle: error
	    of_initWithFileDescriptor: error
			     closable: errorClosable];
						    closable: errorClosable];
# endif
}
#endif

- init
{
	OF_INVALID_INIT_METHOD
}

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
- (instancetype)of_initWithFileDescriptor: (int)fd
{
	self = [super init];

	_fd = fd;

	return self;
}
#else
- (instancetype)of_initWithFileDescriptor: (long)fd
				 closable: (bool)closable
- (instancetype)of_initWithHandle: (BPTR)handle
			 closable: (bool)closable
{
	self = [super init];

	_fd = fd;
	_handle = handle;
	_closable = closable;

	return self;
}
#endif

- (void)dealloc
{
	[self close];

	[super dealloc];
}

- (bool)lowlevelIsAtEndOfStream
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd == INVALID_FD)
	if (_fd == -1)
		return true;
#else
	if (_handle == 0)
		return true;
#endif

	return _atEndOfStream;
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
			  length: (size_t)length
{
	ssize_t ret;

#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd == INVALID_FD || _atEndOfStream)
	if (_fd == -1 || _atEndOfStream)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];

#ifndef OF_WINDOWS
# ifndef OF_WINDOWS
	if ((ret = read(_fd, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];
#else
# else
	if (length > UINT_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = read(_fd, buffer, (unsigned int)length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length
							    errNo: errno];
# endif
#else
	if (_handle == 0 || _atEndOfStream)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];

	if (length > LONG_MAX)
		@throw [OFOutOfRangeException exception];

	if ((ret = Read(_handle, buffer, length)) < 0)
		@throw [OFReadFailedException exceptionWithObject: self
						  requestedLength: length];
#endif

	if (ret == 0)
		_atEndOfStream = true;

	return ret;
}

- (void)lowlevelWriteBuffer: (const void *)buffer
		     length: (size_t)length
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd == INVALID_FD || _atEndOfStream)
	if (_fd == -1 || _atEndOfStream)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];

#ifndef OF_WINDOWS
# ifndef OF_WINDOWS
	if (length > SSIZE_MAX)
		@throw [OFOutOfRangeException exception];

	if (write(_fd, (void *)buffer, length) != (ssize_t)length)
	if (write(_fd, buffer, length) != (ssize_t)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];
#else
# else
	if (length > INT_MAX)
		@throw [OFOutOfRangeException exception];

	if (write(_fd, buffer, (int)length) != (int)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
							     errNo: errno];
# endif
#else
	if (_handle == 0 || _atEndOfStream)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];

	if (length > SSIZE_MAX)
		@throw [OFOutOfRangeException exception];

	if (Write(_handle, (void *)buffer, length) != (LONG)length)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length];
#endif
}

#if !defined(OF_WINDOWS) && (!defined(OF_MORPHOS) || defined(OF_IXEMUL))
- (int)fileDescriptorForReading
{
	return _fd;
}

- (int)fileDescriptorForWriting
{
	return _fd;
}
#endif

- (void)close
{
#if !defined(OF_MORPHOS) || defined(OF_IXEMUL)
	if (_fd != INVALID_FD)
	if (_fd != -1)
		close(_fd);

	_fd = -1;
#else
	if (_closable && _fd != INVALID_FD)
		Close(_fd);
	if (_closable && _handle != 0)
		Close(_handle);
#endif

	_fd = INVALID_FD;
	_handle = 0;
#endif

	[super close];
}

- autorelease
{
	return self;
286
287
288
289
290
291
292
293


294
295
296
297
298
299
300
301
302
303
304
305
306
307


308
309
310
311
312
313
314
315
316
317
318
307
308
309
310
311
312
313

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

329
330
331
332
333
334
335
336
337
338
339
340
341







-
+
+













-
+
+











- (unsigned int)retainCount
{
	return OF_RETAIN_COUNT_MAX;
}

- (int)columns
{
#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)
#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) && \
    (!defined(OF_MORPHOS) || defined(OF_IXEMUL))
	struct winsize ws;

	if (ioctl(_fd, TIOCGWINSZ, &ws) != 0)
		return -1;

	return ws.ws_col;
#else
	return -1;
#endif
}

- (int)rows
{
#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)
#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) && \
    (!defined(OF_MORPHOS) || defined(OF_IXEMUL))
	struct winsize ws;

	if (ioctl(_fd, TIOCGWINSZ, &ws) != 0)
		return -1;

	return ws.ws_row;
#else
	return -1;
#endif
}
@end