Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "../tools/errorhandler.hpp"
00008 #include "../tools/toolbox.hpp"
00009 #include <stdio.h>
00010 #include <string.h>
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
00029
00030 File::~File(void)
00031 {
00032 close();
00033 }
00034
00035
00036
00037
00038
00039 bool File::open(std::string inputFileName, bool beVerbose)
00040 {
00041 m_beVerbose = beVerbose;
00042
00043 close();
00044
00045
00046 m_inputFileName = inputFileName;
00047
00048
00049 m_inputFileStream.open(m_inputFileName.c_str(), std::ios_base::binary | std::ios_base::in);
00050
00051
00052 if (m_inputFileStream.fail() == true)
00053 {
00054 printError("File::open(): Failed to open the file " + m_inputFileName + ", aborting!");
00055 return false;
00056 }
00057
00058 startReadThread();
00059
00060 return true;
00061 }
00062
00063
00064
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
00077
00078 void File::setReadCallbackFunction(File::ReadFunction readFunction, void* obj)
00079 {
00080 m_readFunction = readFunction;
00081 m_readFunctionObjPtr = obj;
00082
00083
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
00102
00103 void File::readThreadFunction(bool& endThread, UINT16& waitTimeMs)
00104 {
00105 INT32 result;
00106
00107
00108 result = readInputData();
00109
00110
00111 if (result < 0)
00112 {
00113
00114 if (m_readThread.m_threadShouldRun == true)
00115 {
00116
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
00124
00125 waitTimeMs = 1;
00126 endThread = true;
00127 }
00128 else
00129 {
00130
00131 waitTimeMs = 10;
00132 }
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 INT32 File::readInputData()
00146 {
00147
00148
00149 const std::streamsize max_length = 8192;
00150 char inBuffer[max_length];
00151 INT32 recvMsgSize = 0;
00152
00153
00154 if (m_inputFileStream.is_open() == false)
00155 {
00156 printError("File::readInputData: File is not open, aborting!");
00157 return -1;
00158 }
00159
00160
00161 m_inputFileStream.read(inBuffer, max_length);
00162 recvMsgSize = m_inputFileStream.gcount();
00163
00164 if (recvMsgSize > 0)
00165 {
00166
00167 printInfoMessage("File::readInputData: Read " + toString(recvMsgSize) + " bytes from the connection.", m_beVerbose);
00168
00169
00170
00171 if (m_readFunction != NULL)
00172 {
00173
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
00185 printInfoMessage("File::readInputData: Read 0 bytes - end of file or file connection is lost!", true);
00186
00187
00188 if (m_disconnectFunction != NULL)
00189 {
00190 m_disconnectFunction(m_disconnectFunctionObjPtr);
00191 }
00192
00193
00194 ScopedLock lock(&m_inputFileMutex);
00195
00196 m_inputFileStream.close();
00197 }
00198
00199 return recvMsgSize;
00200 }
00201
00202
00203
00204
00205 void File::setDisconnectCallbackFunction(DisconnectFunction discFunction, void* obj)
00206 {
00207 m_disconnectFunction = discFunction;
00208 m_disconnectFunctionObjPtr = obj;
00209 }
00210