INTRODUCTION Overview Download and Install Documentation Publications REPOSITORY Libraries DEVELOPER Dev Guide Dashboard PEOPLE Contributors Users Project Download Mailing lists
|
logfile.h00001 /* 00002 * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics 00003 * http://gearbox.sf.net/ 00004 * Copyright (c) 2008 Geoffrey Biggs 00005 * 00006 * flexiport flexible hardware data communications library. 00007 * 00008 * This distribution is licensed to you under the terms described in the LICENSE file included in 00009 * this distribution. 00010 * 00011 * This work is a product of the National Institute of Advanced Industrial Science and Technology, 00012 * Japan. Registration number: H20PRO-881 00013 * 00014 * This file is part of flexiport. 00015 * 00016 * flexiport is free software: you can redistribute it and/or modify it under the terms of the GNU 00017 * Lesser General Public License as published by the Free Software Foundation, either version 3 of 00018 * the License, or (at your option) any later version. 00019 * 00020 * flexiport is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 00021 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00022 * Lesser General Public License for more details. 00023 * 00024 * You should have received a copy of the GNU Lesser General Public License along with flexiport. 00025 * If not, see <http://www.gnu.org/licenses/>. 00026 */ 00027 00028 #ifndef __LOGFILE_H 00029 #define __LOGFILE_H 00030 00031 #if defined (WIN32) 00032 #include <winsock2.h> // For timeval 00033 #else 00034 #include <unistd.h> 00035 #include <sys/time.h> 00036 #endif 00037 #include <string> 00038 #include <vector> 00039 00040 #include "timeout.h" 00041 #include "flexiport_types.h" 00042 00043 namespace flexiport 00044 { 00045 00046 // Class for managing a log file pair 00047 class LogFile 00048 { 00049 public: 00050 LogFile (unsigned int debug); 00051 ~LogFile (); 00052 00053 void Open (std::string fileName, bool read, bool ignoreTimes = false); 00054 void Close (); 00055 bool IsOpen () const; 00056 void ResetFile (); 00057 00058 // File reading 00059 ssize_t Read (void *data, size_t count, Timeout &timeout); 00060 ssize_t BytesAvailable (const Timeout &timeout); 00061 bool CheckWrite (const void * const data, const size_t count, size_t * const numWritten, 00062 const Timeout * const timeout = NULL); 00063 void Flush (); 00064 void Drain (); 00065 00066 // File writing 00067 void WriteRead (const void * const data, size_t count); 00068 void WriteWrite (const void * const data, size_t count); 00069 00070 private: 00071 std::string _fileName; 00072 bool _read; 00073 FILE *_readFile, *_writeFile; 00074 long _readFileSize, _writeFileSize; 00075 // When writing, this is the time the file was opened. When reading, it's the reset time. 00076 struct timeval _openTime; 00077 unsigned int _debug; 00078 size_t _readUsage, _writeUsage; 00079 size_t _readSize, _writeSize; 00080 uint8_t *_readBuffer, *_writeBuffer; 00081 bool _ignoreTimes; 00082 00083 void AllocateReadBuffer (unsigned int size = 0); 00084 void AllocateWriteBuffer (unsigned int size = 0); 00085 void DeallocateReadBuffer (); 00086 void DeallocateWriteBuffer (); 00087 00088 void GetCurrentFileTime (struct timeval &dest); 00089 bool DataAvailableWithinLimit (FILE * const file, const struct timeval &limit); 00090 void GetNextChunkInfo (FILE * const file, struct timeval &timeStamp, size_t &size); 00091 size_t GetChunksToTimeLimit (FILE * const file, void *data, size_t count, 00092 const struct timeval &limit); 00093 size_t GetChunkSizesToTimeLimit (FILE * const file, const struct timeval &limit); 00094 size_t GetSingleChunk (FILE * const file, void *data, size_t count, 00095 struct timeval &timeStamp, size_t &size); 00096 size_t GetFileSize (FILE * const file); 00097 00098 void ReadFromFile (FILE * const file, void * const dest, size_t count); 00099 void WriteToFile (FILE * const file, const void * const data, size_t count); 00100 void WriteTimeStamp (FILE * const file); 00101 }; 00102 00103 } // namespace flexiport 00104 00105 #endif // __LOGFILE_H |