mbed library sources

Dependents:   RPC_Serial_V_mac

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileHandle.h Source File

FileHandle.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_FILEHANDLE_H
00017 #define MBED_FILEHANDLE_H
00018 
00019 typedef int FILEHANDLE;
00020 
00021 #include <stdio.h>
00022 
00023 #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
00024 typedef int ssize_t;
00025 typedef long off_t;
00026 
00027 #else
00028 #   include <sys/types.h>
00029 #endif
00030 
00031 namespace mbed {
00032 
00033 /** An OO equivalent of the internal FILEHANDLE variable
00034  *  and associated _sys_* functions.
00035  *
00036  * FileHandle is an abstract class, needing at least sys_write and
00037  *  sys_read to be implmented for a simple interactive device.
00038  *
00039  * No one ever directly tals to/instanciates a FileHandle - it gets
00040  *  created by FileSystem, and wrapped up by stdio.
00041  */
00042 class FileHandle {
00043 
00044 public:
00045     /** Write the contents of a buffer to the file
00046      *
00047      *  @param buffer the buffer to write from
00048      *  @param length the number of characters to write
00049      *
00050      *  @returns
00051      *  The number of characters written (possibly 0) on success, -1 on error.
00052      */
00053     virtual ssize_t write(const void* buffer, size_t length) = 0;
00054 
00055     /** Close the file
00056      *
00057      *  @returns
00058      *  Zero on success, -1 on error.
00059      */
00060     virtual int close() = 0;
00061 
00062     /** Function read
00063      *  Reads the contents of the file into a buffer
00064      *
00065      *  @param buffer the buffer to read in to
00066      *  @param length the number of characters to read
00067      *
00068      *  @returns
00069      *  The number of characters read (zero at end of file) on success, -1 on error.
00070      */
00071     virtual ssize_t read(void* buffer, size_t length) = 0;
00072 
00073     /** Check if the handle is for a interactive terminal device.
00074      * If so, line buffered behaviour is used by default
00075      *
00076      *  @returns
00077      *    1 if it is a terminal,
00078      *    0 otherwise
00079      */
00080     virtual int isatty() = 0;
00081 
00082     /** Move the file position to a given offset from a given location.
00083      *
00084      *  @param offset The offset from whence to move to
00085      *  @param whence SEEK_SET for the start of the file, SEEK_CUR for the
00086      *   current file position, or SEEK_END for the end of the file.
00087      *
00088      *  @returns
00089      *    new file position on success,
00090      *    -1 on failure or unsupported
00091      */
00092     virtual off_t lseek(off_t offset, int whence) = 0;
00093 
00094     /** Flush any buffers associated with the FileHandle, ensuring it
00095      *  is up to date on disk
00096      *
00097      *  @returns
00098      *    0 on success or un-needed,
00099      *   -1 on error
00100      */
00101     virtual int fsync() = 0;
00102 
00103     virtual off_t flen() {
00104         /* remember our current position */
00105         off_t pos = lseek(0, SEEK_CUR);
00106         if(pos == -1) return -1;
00107         /* seek to the end to get the file length */
00108         off_t res = lseek(0, SEEK_END);
00109         /* return to our old position */
00110         lseek(pos, SEEK_SET);
00111         return res;
00112     }
00113 
00114     virtual ~FileHandle();
00115 };
00116 
00117 } // namespace mbed
00118 
00119 #endif