1 /** @file file.c File stream.
2
3 @authors Copyright (c) 2017 Jaakko Keränen <jaakko.keranen@iki.fi>
4
5 @par License
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 <small>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</small>
26 */
27
28 #include "the_Foundation/file.h"
29 #include "the_Foundation/fileinfo.h"
30 #include "the_Foundation/path.h"
31 #include "the_Foundation/string.h"
32
33 static iFileClass Class_File;
34
new_File(const iString * path)35 iFile *new_File(const iString *path) {
36 iFile *d = new_Object(&Class_File);
37 init_File(d, path);
38 return d;
39 }
40
newCStr_File(const char * path)41 iFile *newCStr_File(const char *path) {
42 iString str;
43 initCStr_String(&str, path);
44 clean_Path(&str);
45 iFile *d = new_File(&str);
46 deinit_String(&str);
47 return d;
48 }
49
init_File(iFile * d,const iString * path)50 void init_File(iFile *d, const iString *path) {
51 iAssertIsObject(d);
52 init_Stream(&d->stream);
53 d->path = copy_String(path);
54 clean_Path(d->path);
55 d->flags = readOnly_FileMode;
56 d->file = NULL;
57 }
58
deinit_File(iFile * d)59 void deinit_File(iFile *d) {
60 if (isOpen_File(d)) {
61 close_File(d);
62 }
63 delete_String(d->path);
64 }
65
open_File(iFile * d,int modeFlags)66 iBool open_File(iFile *d, int modeFlags) {
67 if (isOpen_File(d)) return iFalse;
68 d->stream.pos = 0;
69 d->flags = modeFlags;
70 if ((d->flags & (readWrite_FileMode | append_FileMode)) == 0) {
71 /* Default to read. */
72 d->flags |= read_FileMode;
73 }
74 if (d->flags & (read_FileMode | append_FileMode)) {
75 setSize_Stream(&d->stream, fileSize_FileInfo(d->path));
76 }
77 char mode[4], *m = mode;
78 if (d->flags & append_FileMode) {
79 *m++ = 'a';
80 d->stream.pos = d->stream.size;
81 if (d->flags & read_FileMode) { *m++ = '+'; }
82 }
83 else {
84 if (d->flags & read_FileMode) { *m++ = 'r'; }
85 if (d->flags & write_FileMode) {
86 if (d->flags & read_FileMode) { *m++ = '+'; }
87 else { *m++ = 'w'; }
88 }
89 }
90 if (d->flags & text_FileMode) { *m++ = 't'; } else { *m++ = 'b'; }
91 *m = 0;
92 d->file = fopen(cstr_String(d->path), mode);
93 return isOpen_File(d);
94 }
95
close_File(iFile * d)96 void close_File(iFile *d) {
97 if (isOpen_File(d)) {
98 fclose(d->file);
99 d->file = NULL;
100 }
101 }
102
isOpen_File(const iFile * d)103 iBool isOpen_File(const iFile *d) {
104 return d->file != NULL;
105 }
106
seek_File_(iFile * d,size_t offset)107 static size_t seek_File_(iFile *d, size_t offset) {
108 if (isOpen_File(d)) {
109 fseek(d->file, offset, SEEK_SET);
110 return ftell(d->file);
111 }
112 return pos_Stream(&d->stream);
113 }
114
read_File_(iFile * d,size_t size,void * data_out)115 static size_t read_File_(iFile *d, size_t size, void *data_out) {
116 if (isOpen_File(d)) {
117 const size_t oldPos = d->stream.pos;
118 size_t numRead = fread(data_out, 1, size, d->file);
119 #if defined (iPlatformWindows) || defined (iPlatformMsys)
120 if (d->flags & text_FileMode) {
121 /* We may have skipped over some carriage returns. */
122 const size_t numActualRead = ftell(d->file) - oldPos;
123 d->stream.pos += numActualRead - numRead;
124 }
125 #else
126 iUnused(oldPos);
127 #endif
128 return numRead;
129 }
130 return 0;
131 }
132
write_File_(iFile * d,const void * data,size_t size)133 static size_t write_File_(iFile *d, const void *data, size_t size) {
134 if (isOpen_File(d)) {
135 return fwrite(data, 1, size, d->file);
136 }
137 return 0;
138 }
139
flush_File_(iFile * d)140 static void flush_File_(iFile *d) {
141 if (isOpen_File(d)) {
142 fflush(d->file);
143 }
144 }
145
146 static iBeginDefineSubclass(File, Stream)
147 .seek = (size_t (*)(iStream *, size_t)) seek_File_,
148 .read = (size_t (*)(iStream *, size_t, void *)) read_File_,
149 .write = (size_t (*)(iStream *, const void *, size_t)) write_File_,
150 .flush = (void (*)(iStream *)) flush_File_,
151 iEndDefineClass(File)
152