]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/api/FileHandle.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / api / FileHandle.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MBED_FILEHANDLE_H
17 #define MBED_FILEHANDLE_H
18
19 typedef int FILEHANDLE;
20
21 #include <stdio.h>
22
23 #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
24 typedef int ssize_t;
25 typedef long off_t;
26
27 #else
28 #   include <sys/types.h>
29 #endif
30
31 namespace mbed {
32
33 /** An OO equivalent of the internal FILEHANDLE variable
34  *  and associated _sys_* functions.
35  *
36  * FileHandle is an abstract class, needing at least sys_write and
37  *  sys_read to be implmented for a simple interactive device.
38  *
39  * No one ever directly tals to/instanciates a FileHandle - it gets
40  *  created by FileSystem, and wrapped up by stdio.
41  */
42 class FileHandle {
43
44 public:
45     /** Write the contents of a buffer to the file
46      *
47      *  @param buffer the buffer to write from
48      *  @param length the number of characters to write
49      *
50      *  @returns
51      *  The number of characters written (possibly 0) on success, -1 on error.
52      */
53     virtual ssize_t write(const void* buffer, size_t length) = 0;
54
55     /** Close the file
56      *
57      *  @returns
58      *  Zero on success, -1 on error.
59      */
60     virtual int close() = 0;
61
62     /** Function read
63      *  Reads the contents of the file into a buffer
64      *
65      *  @param buffer the buffer to read in to
66      *  @param length the number of characters to read
67      *
68      *  @returns
69      *  The number of characters read (zero at end of file) on success, -1 on error.
70      */
71     virtual ssize_t read(void* buffer, size_t length) = 0;
72
73     /** Check if the handle is for a interactive terminal device.
74      * If so, line buffered behaviour is used by default
75      *
76      *  @returns
77      *    1 if it is a terminal,
78      *    0 otherwise
79      */
80     virtual int isatty() = 0;
81
82     /** Move the file position to a given offset from a given location.
83      *
84      *  @param offset The offset from whence to move to
85      *  @param whence SEEK_SET for the start of the file, SEEK_CUR for the
86      *   current file position, or SEEK_END for the end of the file.
87      *
88      *  @returns
89      *    new file position on success,
90      *    -1 on failure or unsupported
91      */
92     virtual off_t lseek(off_t offset, int whence) = 0;
93
94     /** Flush any buffers associated with the FileHandle, ensuring it
95      *  is up to date on disk
96      *
97      *  @returns
98      *    0 on success or un-needed,
99      *   -1 on error
100      */
101     virtual int fsync() = 0;
102
103     virtual off_t flen() {
104         /* remember our current position */
105         off_t pos = lseek(0, SEEK_CUR);
106         if(pos == -1) return -1;
107         /* seek to the end to get the file length */
108         off_t res = lseek(0, SEEK_END);
109         /* return to our old position */
110         lseek(pos, SEEK_SET);
111         return res;
112     }
113
114     virtual ~FileHandle();
115 };
116
117 } // namespace mbed
118
119 #endif