mbed library sources. Supersedes mbed-src.
Dependents: LPCXpresso1769_blinky
Fork of mbed-dev by
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 * @Note Synchronization level: Set by subclass 00043 */ 00044 class FileHandle { 00045 00046 public: 00047 /** Write the contents of a buffer to the file 00048 * 00049 * @param buffer the buffer to write from 00050 * @param length the number of characters to write 00051 * 00052 * @returns 00053 * The number of characters written (possibly 0) on success, -1 on error. 00054 */ 00055 virtual ssize_t write(const void* buffer, size_t length) = 0; 00056 00057 /** Close the file 00058 * 00059 * @returns 00060 * Zero on success, -1 on error. 00061 */ 00062 virtual int close() = 0; 00063 00064 /** Function read 00065 * Reads the contents of the file into a buffer 00066 * 00067 * @param buffer the buffer to read in to 00068 * @param length the number of characters to read 00069 * 00070 * @returns 00071 * The number of characters read (zero at end of file) on success, -1 on error. 00072 */ 00073 virtual ssize_t read(void* buffer, size_t length) = 0; 00074 00075 /** Check if the handle is for a interactive terminal device. 00076 * If so, line buffered behaviour is used by default 00077 * 00078 * @returns 00079 * 1 if it is a terminal, 00080 * 0 otherwise 00081 */ 00082 virtual int isatty() = 0; 00083 00084 /** Move the file position to a given offset from a given location. 00085 * 00086 * @param offset The offset from whence to move to 00087 * @param whence SEEK_SET for the start of the file, SEEK_CUR for the 00088 * current file position, or SEEK_END for the end of the file. 00089 * 00090 * @returns 00091 * new file position on success, 00092 * -1 on failure or unsupported 00093 */ 00094 virtual off_t lseek(off_t offset, int whence) = 0; 00095 00096 /** Flush any buffers associated with the FileHandle, ensuring it 00097 * is up to date on disk 00098 * 00099 * @returns 00100 * 0 on success or un-needed, 00101 * -1 on error 00102 */ 00103 virtual int fsync() = 0; 00104 00105 virtual off_t flen() { 00106 lock(); 00107 /* remember our current position */ 00108 off_t pos = lseek(0, SEEK_CUR); 00109 if(pos == -1) { 00110 unlock(); 00111 return -1; 00112 } 00113 /* seek to the end to get the file length */ 00114 off_t res = lseek(0, SEEK_END); 00115 /* return to our old position */ 00116 lseek(pos, SEEK_SET); 00117 unlock(); 00118 return res; 00119 } 00120 00121 virtual ~FileHandle(); 00122 00123 protected: 00124 00125 /** Acquire exclusive access to this object. 00126 */ 00127 virtual void lock() { 00128 // Stub 00129 } 00130 00131 /** Release exclusive access to this object. 00132 */ 00133 virtual void unlock() { 00134 // Stub 00135 } 00136 }; 00137 00138 } // namespace mbed 00139 00140 #endif
Generated on Tue Jul 12 2022 16:04:25 by 1.7.2