1 /* 2 * Copyright 2010, The Android Open Source Project 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 17 #ifndef BCC_FILEHANDLE_H 18 #define BCC_FILEHANDLE_H 19 20 #include <sys/types.h> 21 22 #include <stddef.h> 23 #include <stdint.h> 24 25 namespace bcc { 26 namespace OpenMode { 27 enum ModeType { 28 Read = 0, 29 Write = 1, 30 }; 31 } 32 33 class FileHandle { 34 private: 35 int mFD; 36 37 public: 38 FileHandle() : mFD(-1) { 39 } 40 41 ~FileHandle() { 42 if (mFD >= 0) { 43 close(); 44 } 45 } 46 47 int open(char const *filename, OpenMode::ModeType mode); 48 49 void close(); 50 51 int getFD() { 52 // Note: This function is designed not being qualified by const. 53 // Because once the file descriptor is given, the user can do every 54 // thing on file descriptor. 55 56 return mFD; 57 } 58 59 off_t seek(off_t offset, int whence); 60 61 ssize_t read(char *buf, size_t count); 62 63 ssize_t write(char const *buf, size_t count); 64 65 void truncate(); 66 67 }; 68 69 } // namespace bcc 70 71 #endif // BCC_FILEHANDLE_H 72