1 #pragma once
2 
3 /** @file the_Foundation/file.h  File stream.
4 
5 @authors Copyright (c) 2017 Jaakko Keränen <jaakko.keranen@iki.fi>
6 
7 @par License
8 
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11 
12 1. Redistributions of source code must retain the above copyright notice, this
13    list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15    this list of conditions and the following disclaimer in the documentation
16    and/or other materials provided with the distribution.
17 
18 <small>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</small>
28 */
29 
30 #include "stream.h"
31 
32 #include <stdio.h>
33 
34 iBeginPublic
35 
36 typedef iStreamClass iFileClass;
37 
38 iDeclareType(File)
39 iDeclareType(String)
40 
41 enum iFileMode {
42     read_FileMode       = 0x1,
43     readOnly_FileMode   = 0x1,
44     write_FileMode      = 0x2,
45     writeOnly_FileMode  = 0x2,
46     append_FileMode     = 0x4,
47     text_FileMode       = 0x8,
48 
49     readWrite_FileMode  = read_FileMode | write_FileMode,
50 };
51 
52 struct Impl_File {
53     iStream stream;
54     iString *path;
55     int flags;
56     void *file; /* native handle */
57 };
58 
59 iDeclareObjectConstructionArgs(File, const iString *path)
60 
61 iFile *     newCStr_File    (const char *path);
62 
63 iBool       open_File       (iFile *, int mode);
64 void        close_File      (iFile *);
65 iBool       isOpen_File     (const iFile *);
66 
mode_File(const iFile * d)67 iLocalDef int    mode_File   (const iFile *d) { return d->flags ;}
pos_File(const iFile * d)68 iLocalDef size_t pos_File    (const iFile *d) { return pos_Stream(&d->stream); }
size_File(const iFile * d)69 iLocalDef size_t size_File   (const iFile *d) { return size_Stream(&d->stream); }
atEnd_File(const iFile * d)70 iLocalDef iBool  atEnd_File  (const iFile *d) { return atEnd_Stream(&(d)->stream); }
path_File(const iFile * d)71 iLocalDef const  iString *path_File(const iFile *d) { return d->path; }
72 
stream_File(iFile * d)73 iLocalDef iStream *     stream_File     (iFile *d) { return &d->stream; }
seek_File(iFile * d,size_t offset)74 iLocalDef void          seek_File       (iFile *d, size_t offset) { seek_Stream(&d->stream, offset); }
read_File(iFile * d,size_t size)75 iLocalDef iBlock *      read_File       (iFile *d, size_t size) { return read_Stream(&d->stream, size); }
readData_File(iFile * d,size_t size,void * data_out)76 iLocalDef size_t        readData_File   (iFile *d, size_t size, void *data_out) { return readData_Stream(&d->stream, size, data_out); }
readAll_File(iFile * d)77 iLocalDef iBlock *      readAll_File    (iFile *d) { return readAll_Stream(&d->stream); }
readString_File(iFile * d)78 iLocalDef iString *     readString_File (iFile *d) { return readString_Stream(&d->stream); }
readLines_File(iFile * d)79 iLocalDef iStringList * readLines_File  (iFile *d) { return readLines_Stream(&d->stream); }
read8_File(iFile * d)80 iLocalDef int8_t        read8_File      (iFile *d) { return read8_Stream(&d->stream); }
read16_File(iFile * d)81 iLocalDef int16_t       read16_File     (iFile *d) { return read16_Stream(&d->stream); }
read32_File(iFile * d)82 iLocalDef int32_t       read32_File     (iFile *d) { return read32_Stream(&d->stream); }
readU8_File(iFile * d)83 iLocalDef uint8_t       readU8_File     (iFile *d) { return readU8_Stream(&d->stream); }
readU16_File(iFile * d)84 iLocalDef uint16_t      readU16_File    (iFile *d) { return readU16_Stream(&d->stream); }
readU32_File(iFile * d)85 iLocalDef uint32_t      readU32_File    (iFile *d) { return readU32_Stream(&d->stream); }
86 
write_File(iFile * d,const iBlock * data)87 iLocalDef size_t        write_File      (iFile *d, const iBlock *data) { return write_Stream(&d->stream, data); }
writeData_File(iFile * d,const void * data,size_t size)88 iLocalDef size_t        writeData_File  (iFile *d, const void *data, size_t size) { return writeData_Stream(&d->stream, data, size); }
write8_File(iFile * d,int8_t value)89 iLocalDef void          write8_File     (iFile *d, int8_t value) { write8_Stream(&d->stream, value); }
write16_File(iFile * d,int16_t value)90 iLocalDef void          write16_File    (iFile *d, int16_t value) { write16_Stream(&d->stream, value); }
write32_File(iFile * d,int32_t value)91 iLocalDef void          write32_File    (iFile *d, int32_t value) { write32_Stream(&d->stream, value); }
writeU8_File(iFile * d,uint8_t value)92 iLocalDef void          writeU8_File    (iFile *d, uint8_t value) { writeU8_Stream(&d->stream, value); }
writeU16_File(iFile * d,uint16_t value)93 iLocalDef void          writeU16_File   (iFile *d, uint16_t value) { writeU16_Stream(&d->stream, value); }
writeU32_File(iFile * d,uint32_t value)94 iLocalDef void          writeU32_File   (iFile *d, uint32_t value) { writeU32_Stream(&d->stream, value); }
95 
96 iEndPublic
97