riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
FileConnector.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2021 Thomas Friedrich, Chu-Ping Yu,
2  * University of Antwerp - All Rights Reserved.
3  * You may use, distribute and modify
4  * this code under the terms of the GPL3 license.
5  * You should have received a copy of the GPL3 license with
6  * this file. If not, please visit:
7  * https://www.gnu.org/licenses/gpl-3.0.en.html
8  *
9  * Authors:
10  * Thomas Friedrich <thomas.friedrich@uantwerpen.be>
11  * Chu-Ping Yu <chu-ping.yu@uantwerpen.be>
12  */
13 
14 #include "FileConnector.h"
15 
17 {
18  if (!path.empty())
19  {
20  file_size = std::filesystem::file_size(path);
21  stream.open(path, std::ios::in | std::ios::binary);
22  if (stream.is_open())
23  {
24  reset_file();
25  }
26  else
27  {
28  std::cout << "FileConnector::open_file(): Error opening file!" << std::endl;
29  }
30  }
31  else
32  {
33  std::cout << "FileConnector::open_file(): Path argument is empty!" << std::endl;
34  }
35 }
36 
38 {
39  if (stream.is_open())
40  {
41  stream.close();
42  }
43 }
44 
45 // Reading data stream from File
46 void FileConnector::read_data(char *buffer, size_t data_size)
47 {
48  stream.read(buffer, data_size);
49 
50  pos += data_size;
51  // Reset file to the beginning for repeat reading
52  if (file_size - pos < data_size)
53  {
54  reset_file();
55  }
56 };
57 
58 void FileConnector::reset_file()
59 {
60  pos = 0;
61  stream.clear();
62  stream.seekg(0, std::ios::beg);
63 }
64 
65 FileConnector::FileConnector(): path(), stream(), file_size(0), pos(0) {};
void read_data(char *buffer, size_t data_size)
std::filesystem::path path
Definition: FileConnector.h:26