Differences From Artifact [778d1b4971]:
- File src/OFFile.m — part of check-in [08769cce4e] at 2010-03-15 14:10:03 on branch trunk — Prevent double-closing in OFFile. (user: js, size: 4914) [annotate] [blame] [check-ins using]
To Artifact [cdb8ca685b]:
- File
src/OFFile.m
— part of check-in
[5f4f207266]
at
2010-04-02 15:58:39
on branch trunk
— Use open() / read() / write() instead of fopen() / fread() / fwrite().
This fixes reading from stdin. (user: js, size: 5948) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
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 | #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #import "OFFile.h" #import "OFString.h" #import "OFExceptions.h" #ifdef _WIN32 # import <windows.h> #endif OFFile *of_stdin = nil; OFFile *of_stdout = nil; OFFile *of_stderr = nil; /// \cond internal @interface OFFileSingleton: OFFile @end /// \endcond @implementation OFFile + (void)load { if (self != [OFFile class]) return; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | | | | 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] 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]; } + fileWithFileDescriptor: (int)fd_ { 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 | selector: _cmd]; } - initWithPath: (OFString*)path mode: (OFString*)mode { Class c; self = [super init]; | > > > > > > > > | | | | | | | | | < > > < | | | | | 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 ((fd = open([path cString], flags)) == -1) { c = isa; [super dealloc]; @throw [OFOpenFileFailedException newWithClass: c path: path mode: mode]; } closable = YES; return self; } - initWithFileDescriptor: (int)fd_ { self = [super init]; fd = fd_; return self; } - (void)dealloc { if (closable && fd != -1) close(fd); [super dealloc]; } - (BOOL)atEndOfStreamWithoutCache { if (fd == -1) return YES; return eos; } - (size_t)readNBytesWithoutCache: (size_t)size intoBuffer: (char*)buf { size_t ret; if (fd == -1 || eos) @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 (fd == -1 || eos || (ret = write(fd, buf, size)) < size) @throw [OFWriteFailedException newWithClass: isa size: size]; return ret; } - close { if (fd != -1) close(fd); fd = -1; return self; } @end /// \cond internal @implementation OFFileSingleton |
︙ | ︙ |