clsSimpleTxtData.h
Go to the documentation of this file.
1/*!
2 * \file clsSimpleTxtData.h
3 * \brief A simple text read class
4 * \author Junzhi Liu, Liangjun Zhu
5 * \version 1.1
6 * \date Aug., 2022
7 */
8#ifndef SEIMS_SIMPLE_TEXT_H
9#define SEIMS_SIMPLE_TEXT_H
10#include <fstream>
11
12#include "utils_array.h"
13#include "utils_string.h"
14#include "utils_filesystem.h"
15#include "basic.h"
16#include <seims.h>
17
18using namespace ccgl;
19using namespace utils_array;
20using namespace utils_string;
21using namespace utils_filesystem;
22
23/*!
24 * \ingroup data
25 * \class clsSimpleTxtData
26 * \brief read string line from text file
27 *
28 */
29template <typename T>
31public:
32 //! Constructor, from text file read lines data
33 explicit clsSimpleTxtData(const string& filename);
34
35 //! Destructor
37
38 //! Get line number and data
39 void GetData(int* n_row, T** data);
40
41 //! Output lines data to \a ostream
42 void Dump(std::ostream* fs);
43
44private:
45 //! line number
46 int row_;
47 //! lines data
48 T* data_;
49};
50
51template <typename T>
52clsSimpleTxtData<T>::clsSimpleTxtData(const string& filename) : row_(0), data_(nullptr) {
53 if (!FileExists(filename)) {
54 throw ModelException("clsSimpleTxtData", "ReadFile", "The file " + filename +
55 " does not exist or has not read permission.");
56 }
57 std::ifstream myfile;
58 myfile.open(filename.c_str(), std::ifstream::in);
59 char* end = nullptr;
60 //get number of lines
61 if (myfile.is_open()) {
62 string line;
63 vector<T> data;
64 while (!myfile.eof()) {
65 if (!myfile.good()) {
66 continue;
67 }
68 getline(myfile, line);
69 TrimSpaces(line);
70 if (line.empty() || line[0] == '#') {
71 continue; // ignore comments and empty lines
72 }
73 vector<string> tokens = SplitString(line, '|');
74 if (!tokens.empty()) {
75 TrimSpaces(tokens[0]);
76 if (tokens[0].find_first_of("0123456789") == string::npos) {
77 continue;
78 }
79 data.emplace_back(T(strtod(tokens[0].c_str(), &end))); // add data
80 }
81 }
82 myfile.close();
83
84 row_ = CVT_INT(data.size());
85 if (row_ > 0) {
86 data_ = new(nothrow) T[row_];
87 int i = 0;
88 for (auto it = data.begin(); it < data.end(); ++it) {
89 data_[i] = *it;
90 i++;
91 }
92 }
93 }
94}
95
96template <typename T>
98 if (data_ != nullptr) { Release1DArray(data_); }
99}
100
101template <typename T>
102void clsSimpleTxtData<T>::Dump(std::ostream* fs) {
103 if (nullptr == fs) return;
104 if (nullptr == data_) return;
105 for (int i = 0; i < row_; i++) {
106 *fs << data_[i] << endl;
107 }
108}
109
110template <typename T>
111void clsSimpleTxtData<T>::GetData(int* n_row, T** data) {
112 *n_row = row_;
113 *data = data_;
114}
115
116#endif /* SEIMS_SIMPLE_TEXT_H */
Basic definitions.
#define CVT_INT(param)
A reference to the postfix of executable file for RELWITHDEBINFO mode.
Definition: basic.h:325
Base type of all interfaces.
Definition: basic.h:407
Print the exception message.
Definition: basic.h:416
void GetData(int *n_row, T **data)
Get line number and data.
Definition: clsSimpleTxtData.h:111
clsSimpleTxtData(const string &filename)
Constructor, from text file read lines data.
Definition: clsSimpleTxtData.h:52
void Dump(std::ostream *fs)
Output lines data to ostream.
Definition: clsSimpleTxtData.h:102
~clsSimpleTxtData()
Destructor.
Definition: clsSimpleTxtData.h:97
read string line from text file
Definition: clsSimpleTxtData.h:30
void Release1DArray(T *&data)
Release DT_Array1D data.
Definition: utils_array.h:460
bool FileExists(string const &filename)
Return a flag indicating if the given file exists.
vector< string > SplitString(const string &item)
Splits the given string by spaces.
void TrimSpaces(string &str)
Trim Both leading and trailing spaces.
Common Cross-platform Geographic Library (CCGL)
The SEIMS related definitions and utilities header.
Template functions to initialize and release arrays.
File system related functions in CCGL.
Handling string related issues in CCGL.