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: |
5f4f207266e1626d395e94f30f4cf335 |
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 | @class OFString; /** * \brief A class which provides functions to read, write and manipulate files. */ @interface OFFile: OFStream { | | | > | | | 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 { 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 fd A file descriptor, returned from for example open(). * It is not closed when the OFFile object is deallocated! * \return A new autoreleased OFFile */ + 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 | */ - initWithPath: (OFString*)path mode: (OFString*)mode; /** * Initializes an already allocated OFFile. * | | | | 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 fd A file descriptor, returned from for example open(). * It is not closed when the OFFile object is deallocated! */ - 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 | #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 |
︙ | ︙ |