Overview
Comment: | Add file. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
95197cb01a0d7dd92b35940c44ead1b9 |
User & Date: | js 2012-09-29 22:12:10 |
Context
2012-09-29
| ||
22:12 | Update corefw.h. check-in: cbf96fa23f user: js tags: trunk | |
22:12 | Add file. check-in: 95197cb01a user: js tags: trunk | |
22:10 | Add stream. check-in: e21a39f7dc user: js tags: trunk | |
Changes
Changes to src/Makefile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | SHARED_LIB = ${LIB_PREFIX}corefw${LIB_SUFFIX} LIB_MAJOR = 0 LIB_MINOR = 0 SRCS = array.c \ bool.c \ box.c \ class.c \ double.c \ int.c \ map.c \ object.c \ range.c \ refpool.c \ stream.c \ string.c | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | SHARED_LIB = ${LIB_PREFIX}corefw${LIB_SUFFIX} LIB_MAJOR = 0 LIB_MINOR = 0 SRCS = array.c \ bool.c \ box.c \ class.c \ double.c \ file.c \ int.c \ map.c \ object.c \ range.c \ refpool.c \ stream.c \ string.c |
︙ | ︙ |
Added src/file.c.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 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 101 102 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 | /* * Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdarg.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include "stream.h" #include "file.h" #ifndef O_BINARY # define O_BINARY 0 #endif #ifndef S_IRGRP # define S_IRGRP 0 #endif #ifndef S_IROTH # define S_IROTH 0 #endif #ifndef S_IWGRP # define S_IWGRP 0 #endif #ifndef S_IWOTH # define S_IWOTH 0 #endif #define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH struct CFWFile { CFWStream stream; int fd; bool eof; }; 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; } static ssize_t file_read(void *ptr, void *buf, size_t len) { CFWFile *file = ptr; ssize_t ret; if ((ret = read(file->fd, buf, len)) == 0) file->eof = true; return ret; } static bool file_write(void *ptr, const void *buf, size_t len) { CFWFile *file = ptr; ssize_t ret; if ((ret = write(file->fd, buf, len)) < len) return false; return true; } static bool file_eof(void *ptr) { CFWFile *file = ptr; return file->eof; } static void file_close(void *ptr) { CFWFile *file = ptr; close(file->fd); } struct cfw_stream_ops stream_ops = { .read = file_read, .write = file_write, .eof = file_eof, .close = file_close }; static bool ctor(void *ptr, va_list args) { CFWFile *file = ptr; const char *path = va_arg(args, const char*); const char *mode = va_arg(args, const char*); int flags; /* Make sure we have a valid pointer in case we error out */ file->stream.ops = NULL; file->eof = false; if ((flags = parse_mode(mode)) == -1) return false; if ((file->fd = open(path, flags, DEFAULT_MODE)) == -1) return false; file->stream.ops = &stream_ops; return true; } static void dtor(void *ptr) { cfw_stream_close(ptr); } static CFWClass class = { .name = "CFWFile", .size = sizeof(CFWFile), .ctor = ctor, .dtor = dtor }; CFWClass *cfw_file = &class; |
Added src/file.h.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /* * Copyright (c) 2012, Jonathan Schleifer <js@webkeks.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef __COREFW_FILE_H__ #define __COREFW_FILE_H__ #include "class.h" typedef struct CFWFile CFWFile; extern CFWClass *cfw_file; #endif |