STORMM Source Documentation
Loading...
Searching...
No Matches
textfile.h
1// -*-c++-*-
2#ifndef STORMM_TEXTFILE_H
3#define STORMM_TEXTFILE_H
4
5#include <string>
6#include <vector>
7#include "copyright.h"
8#include "DataTypes/stormm_vector_types.h"
9#include "FileManagement/file_enumerators.h"
10#include "Reporting/reporting_enumerators.h"
11#include "parsing_enumerators.h"
12
13namespace stormm {
14namespace parse {
15
16using diskutil::PrintSituation;
17using review::TextEnds;
18
21
23 TextFileReader(int line_count_in, const int* line_lengths_in, const size_t* line_limits_in,
24 const char* text_in, const std::string file_name_in);
25
29 TextFileReader(const TextFileReader &original) = default;
30 TextFileReader(TextFileReader &&original) = default;
32
33 const int line_count;
34 const int* line_lengths;
35 const size_t* line_limits;
36 const char* text;
37 const std::string file_name;
38};
39
45class TextFile {
46public:
47
61 TextFile();
62
63 TextFile(const std::string &file_name, TextOrigin source = TextOrigin::DISK,
64 const std::string &content = std::string(""),
65 const std::string &caller = std::string(""));
66
67 TextFile(const char* content, const size_t length, const std::string &caller = std::string(""));
69
72 TextFile(const TextFile &original) = default;
73 TextFile(TextFile &&original) = default;
74 TextFile& operator=(const TextFile &other) = default;
75 TextFile& operator=(TextFile &&other) = default;
77
79 ~TextFile() = default;
80
82 std::string getFileName() const;
83
85 int getLineCount() const;
86
91 int getLineLimits(int index) const;
92
97 int getLineLength(int index) const;
98
100 int getLongestLineLength() const;
101
104 size_t getTextSize() const;
105
109 char getChar(int index) const;
110
114 std::string getLineAsString(int line_index) const;
115
119 const char* getTextPointer(int index) const;
120
124 const char* getLinePointer(int line_index) const;
125
127 const TextFileReader data() const;
128
136 std::string extractString(int line_number, int start_pos = 0, int string_length = -1) const;
137
146 char4 extractChar4(int line_number, int start_pos, int string_length) const;
147
153 std::string toString(TextEnds line_endings = TextEnds::NEWLINE) const;
154
168 void write(std::ofstream *foutp) const;
169 void write(const std::string &new_filename,
170 PrintSituation expectation = PrintSituation::OPEN_NEW) const;
171 void write(PrintSituation expectation = PrintSituation::OPEN_NEW) const;
173
174private:
175
177 std::string orig_file;
178
180 int line_count;
181
183 std::vector<int> line_lengths;
184
186 std::vector<size_t> line_limits;
187
189 std::vector<char> text;
190
196 void validateLineIndex(int index, const char* caller) const;
197
206 std::string setFileName(const std::string &file_name, TextOrigin source,
207 const std::string &content);
208
220 void linesFromString(const char* text_in, const size_t n_char);
221 void linesFromString(const std::string &text_in);
223
232 int checkAvailableLength(int line_number, int start_pos = 0, int string_length = -1) const;
233};
234
235} // namespace parse
236} // namespace stormm
237
238#endif
TextFile()
Constructor for taking an ascii file or a very long, formatted string and transforming it into a std:...
Definition textfile.cpp:22
std::string getLineAsString(int line_index) const
Get a line from the file as a string.
Definition textfile.cpp:160
char getChar(int index) const
Get one character of a text file after converting it to a character vector in memory.
Definition textfile.cpp:155
std::string getFileName() const
Get the name of the original file.
Definition textfile.cpp:106
int getLineCount() const
Get the line count of a text file after converting it to a character vector in memory.
Definition textfile.cpp:121
std::string extractString(int line_number, int start_pos=0, int string_length=-1) const
Extract a string based on a line number, starting position, and length.
Definition textfile.cpp:190
const char * getLinePointer(int line_index) const
Get a char pointer to a specific line in the object.
Definition textfile.cpp:178
char4 extractChar4(int line_number, int start_pos, int string_length) const
Extract a char4 tuple based on a line number, starting position, and length.
Definition textfile.cpp:203
~TextFile()=default
Default destructor.
int getLineLength(int index) const
Get the length of a line. A bounds check will be applied to the line index number.
Definition textfile.cpp:135
void write(std::ofstream *foutp) const
Write the contents of the object to disk.
Definition textfile.cpp:319
int getLineLimits(int index) const
Get one of the line limits of a text file converted to a character vector in memory.
Definition textfile.cpp:126
const char * getTextPointer(int index) const
Get a char pointer to a specific index in the object.
Definition textfile.cpp:173
TextFile(const TextFile &original)=default
The default copy and move constructors as well as assignment operators are acceptable.
const TextFileReader data() const
Get an abstract of a text file's CPU-RAM representation, for ease of use.
Definition textfile.cpp:184
std::string toString(TextEnds line_endings=TextEnds::NEWLINE) const
Convert all content to a string, with the option of making line endings carriage returns or simple sp...
Definition textfile.cpp:285
int getLongestLineLength() const
Get the length of the longest line in the file.
Definition textfile.cpp:141
size_t getTextSize() const
Get the number of text characters in the object's buffer (this will not count implicit carriage retur...
Definition textfile.cpp:150
TextFile()
Constructor for taking an ascii file or a very long, formatted string and transforming it into a std:...
Definition textfile.cpp:22
Definition stormm_vector_types.h:141
TextFileReader(int line_count_in, const int *line_lengths_in, const size_t *line_limits_in, const char *text_in, const std::string file_name_in)
The constructor takes length constants and constant pointers.
Definition textfile.cpp:14
Abstract for the TextFile object, providing read-only access.
Definition textfile.h:20
TextFileReader(int line_count_in, const int *line_lengths_in, const size_t *line_limits_in, const char *text_in, const std::string file_name_in)
The constructor takes length constants and constant pointers.
Definition textfile.cpp:14
TextFileReader(const TextFileReader &original)=default
Take the default copy and move constructors. The assignment operators will get implicitly deleted as ...