file.cpp
Go to the documentation of this file.
1 //
2 // File.cpp
3 //
4 // Read from a binary file.
5 //
6 
7 #include "../tools/errorhandler.hpp"
8 #include "../tools/toolbox.hpp"
9 #include <stdio.h> // for sprintf()
10 #include <string.h> // for memset()
11 #include "file.hpp"
12 
13 
15 {
16  m_beVerbose = false;
17 
19 
20  m_inputFileName = "";
21  m_readFunction = NULL;
22  m_readFunctionObjPtr = NULL;
23  m_disconnectFunction = NULL;
25 }
26 
27 //
28 // Destruktor.
29 //
31 {
32  close();
33 }
34 
35 
36 //
37 // Open the input file for reading.
38 //
39 bool File::open(std::string inputFileName, bool beVerbose)
40 {
41  m_beVerbose = beVerbose;
42 
43  close();
44 
45  // Store the file name for later use
46  m_inputFileName = inputFileName;
47 
48  // Open the file
49  m_inputFileStream.open(m_inputFileName.c_str(), std::ios_base::binary | std::ios_base::in);
50 
51  // Check if the file was opened
52  if (m_inputFileStream.fail() == true)
53  {
54  printError("File::open(): Failed to open the file " + m_inputFileName + ", aborting!");
55  return false; // Exit here
56  }
57 
59 
60  return true;
61 }
62 
63 //
64 // Close the input file, if it was open.
65 //
67 {
68  if (m_inputFileStream.is_open() == true)
69  {
70  m_inputFileStream.close();
71  }
72  m_inputFileName = "";
73 }
74 
75 //
76 // Define the Read-Callback-Funktion.
77 //
79 {
80  m_readFunction = readFunction;
82 
83  // Start the read thread
85 }
86 
87 //
88 //
89 //
91 {
92  if ((m_readThread.isRunning() == false) &&
93  (m_readFunctionObjPtr != NULL) &&
94  (m_inputFileName != ""))
95  {
96  m_readThread.run(this);
97  }
98 }
99 
100 //
101 // Lese-Thread (Main function).
102 //
103 void File::readThreadFunction(bool& endThread, UINT16& waitTimeMs)
104 {
105  INT32 result;
106 
107  // Read
108  result = readInputData();
109 
110  // Result
111  if (result < 0)
112  {
113  // Verbindung wurde abgebrochen
114  if (m_readThread.m_threadShouldRun == true)
115  {
116  // Wir sollten eigentlich noch laufen!
117  printInfoMessage("File::readThreadMain: End of file reached! Read thread terminates now.", m_beVerbose);
118  }
119  waitTimeMs = 0;
120  }
121  else if (result == 0)
122  {
123  // No data. We may have reached the end of the file. In any case, there is nothing
124  // more we can do!
125  waitTimeMs = 1;
126  endThread = true;
127  }
128  else
129  {
130  // Wir haben etwas empfangen, also nicht schlafen
131  waitTimeMs = 10;
132  }
133 }
134 
135 
136 //
137 // Read some data from the file.
138 // Called from the read thread!
139 //
140 // Return value:
141 // > 0: Size of the data
142 // = 0: No data available
143 // < 0: Failed to read data, e.g. end of file
144 //
146 {
147  // Prepare the input buffer
148 // const UINT16 max_length = 8192;
149  const std::streamsize max_length = 8192;
150  char inBuffer[max_length];
151  INT32 recvMsgSize = 0;
152 
153  // Ist die Verbindung offen?
154  if (m_inputFileStream.is_open() == false)
155  {
156  printError("File::readInputData: File is not open, aborting!");
157  return -1;
158  }
159 
160  // Read some data, if any
161  m_inputFileStream.read(inBuffer, max_length); // Read
162  recvMsgSize = m_inputFileStream.gcount(); // Get number of read bytes
163 
164  if (recvMsgSize > 0)
165  {
166  // Success
167  printInfoMessage("File::readInputData: Read " + toString(recvMsgSize) + " bytes from the connection.", m_beVerbose);
168 
169  // Falls eine Callback-Funktion definiert ist, rufe sie auf mit den
170  // empfangenen Daten. If not, discard the data!
171  if (m_readFunction != NULL)
172  {
173  // Die Daten an die Callback-Funktion uebergeben
174  UINT32 length_uint32 = (UINT32)recvMsgSize;
175  m_readFunction(m_readFunctionObjPtr, (UINT8*)inBuffer, length_uint32);
176  }
177  else
178  {
179  printWarning("File::readInputData: Discarding data because there is no callback function!");
180  }
181  }
182  else if (recvMsgSize == 0)
183  {
184  // Verbindungsabbruch
185  printInfoMessage("File::readInputData: Read 0 bytes - end of file or file connection is lost!", true);
186 
187  // Informieren?
188  if (m_disconnectFunction != NULL)
189  {
191  }
192 
193  // Mutex setzen
195 
196  m_inputFileStream.close();
197  }
198 
199  return recvMsgSize;
200 }
201 
202 //
203 // Setzt die Funktion, die bei einem Disconnect-Ereignis aufgerufen wird.
204 //
206 {
207  m_disconnectFunction = discFunction;
209 }
210 
INT32 readInputData()
Definition: file.cpp:145
void printError(std::string message)
std::string toString(const PositionWGS84::PositionWGS84SourceType &type)
void * m_readFunctionObjPtr
Definition: file.hpp:67
std::string m_inputFileName
Definition: file.hpp:61
DisconnectFunction m_disconnectFunction
Definition: file.hpp:68
uint16_t UINT16
void setReadCallbackFunction(ReadFunction readFunction, void *obj)
Definition: file.cpp:78
SickThread< File,&File::readThreadFunction > m_readThread
Definition: file.hpp:56
#define printInfoMessage(a, b)
uint32_t UINT32
bool m_threadShouldRun
Definition: SickThread.hpp:106
void run(void *classptr)
Definition: SickThread.hpp:26
void close()
Definition: file.cpp:66
Mutex m_inputFileMutex
Definition: file.hpp:63
File()
Definition: file.cpp:14
void setDisconnectCallbackFunction(DisconnectFunction discFunction, void *obj)
Definition: file.cpp:205
ReadFunction m_readFunction
Definition: file.hpp:66
int32_t INT32
void(* DisconnectFunction)(void *obj)
Definition: file.hpp:46
void startReadThread()
Definition: file.cpp:90
void(* ReadFunction)(void *obj, UINT8 *inputBuffer, UINT32 &numBytes)
Definition: file.hpp:42
std::ifstream m_inputFileStream
Definition: file.hpp:62
~File()
Definition: file.cpp:30
void readThreadFunction(bool &endThread, UINT16 &waitTimeMs)
Definition: file.cpp:103
bool m_beVerbose
Definition: file.hpp:51
void printWarning(std::string message)
bool open(std::string inputFileName, bool beVerbose=false)
Definition: file.cpp:39
void * m_disconnectFunctionObjPtr
Definition: file.hpp:69
uint8_t UINT8


libsick_ldmrs
Author(s): SICK AG , Martin Günther , Jochen Sprickerhof
autogenerated on Sat Jun 8 2019 17:57:33