file.cpp
Go to the documentation of this file.
00001 //
00002 // File.cpp
00003 //
00004 // Read from a binary file.
00005 //
00006 
00007 #include "../tools/errorhandler.hpp"
00008 #include "../tools/toolbox.hpp"
00009 #include <stdio.h>      // for sprintf()
00010 #include <string.h>     // for memset()
00011 #include "file.hpp"
00012 
00013 
00014 File::File()
00015 {
00016         m_beVerbose = false;
00017         
00018         m_readThread.m_threadShouldRun = false;
00019         
00020         m_inputFileName = "";
00021         m_readFunction = NULL;
00022         m_readFunctionObjPtr = NULL;
00023         m_disconnectFunction = NULL;
00024         m_disconnectFunctionObjPtr = NULL;
00025 }
00026 
00027 //
00028 // Destruktor.
00029 //
00030 File::~File(void)
00031 {
00032         close();
00033 }
00034 
00035 
00036 //
00037 // Open the input file for reading.
00038 //
00039 bool File::open(std::string inputFileName, bool beVerbose)
00040 {
00041         m_beVerbose = beVerbose;
00042         
00043         close();
00044 
00045         // Store the file name for later use
00046         m_inputFileName = inputFileName;
00047         
00048         // Open the file
00049         m_inputFileStream.open(m_inputFileName.c_str(), std::ios_base::binary | std::ios_base::in);
00050         
00051         // Check if the file was opened
00052         if (m_inputFileStream.fail() == true)
00053         {
00054                 printError("File::open(): Failed to open the file " + m_inputFileName + ", aborting!");
00055                 return false;   // Exit here
00056         }
00057         
00058         startReadThread();
00059         
00060         return true;
00061 }
00062 
00063 //
00064 // Close the input file, if it was open.
00065 //
00066 void File::close()
00067 {
00068         if  (m_inputFileStream.is_open() == true)
00069         {
00070                 m_inputFileStream.close();
00071         }
00072         m_inputFileName = "";
00073 }
00074 
00075 //
00076 // Define the Read-Callback-Funktion.
00077 //
00078 void File::setReadCallbackFunction(File::ReadFunction readFunction, void* obj)
00079 {
00080         m_readFunction = readFunction;
00081         m_readFunctionObjPtr = obj;
00082         
00083         // Start the read thread
00084         startReadThread();
00085 }
00086 
00087 //
00088 //
00089 //
00090 void File::startReadThread()
00091 {
00092         if ((m_readThread.isRunning() == false) &&
00093                 (m_readFunctionObjPtr != NULL) &&
00094                 (m_inputFileName != ""))
00095         {
00096                 m_readThread.run(this);
00097         }
00098 }
00099 
00100 //
00101 // Lese-Thread (Main function).
00102 //
00103 void File::readThreadFunction(bool& endThread, UINT16& waitTimeMs)
00104 {
00105         INT32 result;
00106 
00107         // Read
00108         result = readInputData();
00109 
00110         // Result
00111         if (result < 0)
00112         {
00113                 // Verbindung wurde abgebrochen
00114                 if (m_readThread.m_threadShouldRun == true)
00115                 {
00116                         // Wir sollten eigentlich noch laufen!
00117                         printInfoMessage("File::readThreadMain: End of file reached! Read thread terminates now.", m_beVerbose);
00118                 }
00119                 waitTimeMs = 0;
00120         }
00121         else if (result == 0)
00122         {
00123                 // No data. We may have reached the end of the file. In any case, there is nothing
00124                 // more we can do!
00125                 waitTimeMs = 1;
00126                 endThread = true;
00127         }
00128         else
00129         {
00130                 // Wir haben etwas empfangen, also nicht schlafen
00131                 waitTimeMs = 10;
00132         }
00133 }
00134 
00135 
00136 //
00137 // Read some data from the file.
00138 // Called from the read thread!
00139 //
00140 // Return value:
00141 // > 0: Size of the data
00142 // = 0: No data available
00143 // < 0: Failed to read data, e.g. end of file
00144 //
00145 INT32 File::readInputData()
00146 {
00147         // Prepare the input buffer
00148 //      const UINT16 max_length = 8192;
00149         const std::streamsize max_length = 8192;
00150         char inBuffer[max_length];
00151         INT32 recvMsgSize = 0;
00152         
00153         // Ist die Verbindung offen?
00154         if (m_inputFileStream.is_open() == false)
00155         {
00156                 printError("File::readInputData: File is not open, aborting!");
00157                 return -1;
00158         }
00159                 
00160         // Read some data, if any
00161         m_inputFileStream.read(inBuffer, max_length);   // Read
00162         recvMsgSize = m_inputFileStream.gcount();               // Get number of read bytes
00163         
00164         if (recvMsgSize > 0)
00165         {
00166                 // Success
00167                 printInfoMessage("File::readInputData: Read " + toString(recvMsgSize) + " bytes from the connection.", m_beVerbose);
00168                 
00169                 // Falls eine Callback-Funktion definiert ist, rufe sie auf mit den
00170                 // empfangenen Daten. If not, discard the data!
00171                 if (m_readFunction != NULL)
00172                 {
00173                         // Die Daten an die Callback-Funktion uebergeben
00174                         UINT32 length_uint32 = (UINT32)recvMsgSize;
00175                         m_readFunction(m_readFunctionObjPtr, (UINT8*)inBuffer, length_uint32);
00176                 }
00177                 else
00178                 {
00179                         printWarning("File::readInputData: Discarding data because there is no callback function!");
00180                 }
00181         }
00182         else if (recvMsgSize == 0)
00183         {
00184                 // Verbindungsabbruch
00185                 printInfoMessage("File::readInputData: Read 0 bytes - end of file or file connection is lost!", true);
00186                 
00187                 // Informieren?
00188                 if (m_disconnectFunction != NULL)
00189                 {
00190                         m_disconnectFunction(m_disconnectFunctionObjPtr);
00191                 }
00192                 
00193                 // Mutex setzen
00194                 ScopedLock lock(&m_inputFileMutex);
00195 
00196                 m_inputFileStream.close();
00197         }
00198         
00199         return recvMsgSize;
00200 }
00201 
00202 //
00203 // Setzt die Funktion, die bei einem Disconnect-Ereignis aufgerufen wird.
00204 //
00205 void File::setDisconnectCallbackFunction(DisconnectFunction discFunction, void* obj)
00206 {
00207         m_disconnectFunction = discFunction;
00208         m_disconnectFunctionObjPtr = obj;
00209 }
00210 


libsick_ldmrs
Author(s): SICK AG , Martin Günther , Jochen Sprickerhof
autogenerated on Thu Jun 6 2019 21:02:36