svn / mbed / trunk / FileHandle.h

Revision 29, 2.9 kB (checked in by emilmont, 6 months ago)

New Libraries 11.11

Line 
1/* mbed Microcontroller Library - FileHandler
2 * Copyright (c) 2007-2009 ARM Limited. All rights reserved.
3 */ 
4 
5#ifndef MBED_FILEHANDLE_H
6#define MBED_FILEHANDLE_H
7
8typedef int FILEHANDLE;
9
10#include <stdio.h>
11#ifdef __ARMCC_VERSION
12typedef int ssize_t;
13typedef long off_t;
14#else
15#include <sys/types.h>
16#endif
17
18namespace mbed { 
19
20/* Class FileHandle
21 *  An OO equivalent of the internal FILEHANDLE variable
22 *  and associated _sys_* functions
23 *
24 * FileHandle is an abstract class, needing at least sys_write and
25 * sys_read to be implmented for a simple interactive device
26 *
27 * No one ever directly tals to/instanciates a FileHandle - it gets
28 * created by FileSystem, and wrapped up by stdio
29 */
30class FileHandle {
31
32public:
33
34    /* Function write
35     *  Write the contents of a buffer to the file
36     *
37     * Parameters
38     *  buffer - the buffer to write from
39     *  length - the number of characters to write
40     *
41     * Returns
42     *  The number of characters written (possibly 0) on success, -1 on error.
43     */
44    virtual ssize_t write(const void* buffer, size_t length) = 0;
45
46    /* Function close
47     *  Close the file
48     *
49     * Returns
50     *  Zero on success, -1 on error.
51     */
52    virtual int close() = 0;
53
54    /* Function read
55     *  Reads the contents of the file into a buffer
56     *
57     * Parameters
58     *  buffer - the buffer to read in to
59     *  length - the number of characters to read
60     *
61     * Returns
62     *  The number of characters read (zero at end of file) on success, -1 on error.
63     */
64    virtual ssize_t read(void* buffer, size_t length) = 0;
65
66    /* Function isatty
67     *  Check if the handle is for a interactive terminal device
68     *
69     * If so, line buffered behaviour is used by default
70     *
71     * Returns
72     *  1 if it is a terminal, 0 otherwise
73     */
74    virtual int isatty() = 0 ;
75
76    /* Function lseek
77     *  Move the file position to a given offset from a given location.
78     *
79     * Parameters
80     *  offset - The offset from whence to move to
81     *  whence - SEEK_SET for the start of the file, SEEK_CUR for the
82     *   current file position, or SEEK_END for the end of the file.
83     *
84     * Returns
85     *  New file position on success, -1 on failure or unsupported
86     */
87    virtual off_t lseek(off_t offset, int whence) = 0;
88
89    /* Function fsync
90     *  Flush any buffers associated with the FileHandle, ensuring it
91     *  is up to date on disk
92     *
93     * Returns
94     *  0 on success or un-needed, -1 on error
95     */
96    virtual int fsync() = 0;
97
98    virtual off_t flen() {
99        /* remember our current position */
100        off_t pos = lseek(0, SEEK_CUR);
101        if(pos == -1) return -1;
102        /* seek to the end to get the file length */
103        off_t res = lseek(0, SEEK_END);
104        /* return to our old position */
105        lseek(pos, SEEK_SET);
106        return res;
107    }
108
109};
110
111} // namespace mbed
112
113#endif
114
Note: See TracBrowser for help on using the browser.