#include <xtd/xtd>
class program {
public:
static auto main() {
create_temp_file_text();
test_stream_reader_enumerable();
console::write_line("---");
test_reading_file();
remove_temp_file_text();
}
private:
static auto create_temp_file_text() -> void {
file::write_all_lines(path::combine(path::get_temp_path(), "temp_file.txt"), {"line 1", "string to search for", "line 3", "line 4", "line 5", "line 6", "line 7", "line 8", "line 9", "line 10", "string to search for", "line 12"});
}
static auto remove_temp_file_text() -> void {
file::remove(path::combine(path::get_temp_path(), "temp_file.txt"));
}
static auto test_stream_reader_enumerable() -> void {
auto memory_before = memory_information::get_used_process_memory();
auto strings_found = list<string> {};
try {
for (auto line : stream_reader_enumerable {path::combine(path::get_temp_path(), "temp_file.txt")})
if (line.contains("string to search for")) strings_found.add(line);
console::write_line("Found: {}", strings_found.count());
} catch (const file_not_found_exception&) {
console::write_line("This example requires a file named {}.", path::combine(path::get_temp_path(), "temp_file.txt"));
return;
}
auto memory_after = memory_information::get_used_process_memory();
console::write_line("Memory Used With Iterator = \t{} kb", (memory_after - memory_before) / 1024);
}
static auto test_reading_file() -> void {
auto memory_before = memory_information::get_used_process_memory();
auto file_contents = list<string> {};
try {
auto sr = stream_reader {path::combine(path::get_temp_path(), "temp_file.txt")};
while (!sr.end_of_stream())
file_contents.add(sr.read_line());
sr.close();
} catch (const file_not_found_exception&) {
console::write_line("This example requires a file named {}.", path::combine(path::get_temp_path(), "temp_file.txt"));
return;
}
auto strings_found = list<string> {};
for (auto line : file_contents)
if (line.contains("string to search for")) strings_found.add(line);
console::write_line("Found: {}", strings_found.count());
auto memory_after = memory_information::get_used_process_memory();
console::write_line("Memory Used Without Iterator = \t{} kb", (memory_after - memory_before) / 1024);
}
class stream_reader_enumerable : public ienumerable<string> {
private:
string file_path_;
public:
stream_reader_enumerable(const string& file_path) : file_path_ {file_path} {}
auto get_enumerator() const -> enumerator<string> override {return {new_ptr<stream_reader_enumerator>(file_path_)};}
};
class stream_reader_enumerator : public object, public ienumerator<string> {
private:
stream_reader sr_;
std::optional<string> current_;
public:
stream_reader_enumerator(const string& file_path) : sr_ {file_path} {}
~stream_reader_enumerator() {sr_.close();}
auto current() const -> const string& override {
if (!current_.has_value()) throw invalid_operation_exception {};
return current_.value();
}
auto move_next() -> bool override {
if (sr_.end_of_stream()) current_.reset();
else current_ = sr_.read_line();
return current_.has_value();
}
auto reset() -> void override {
sr_.base_stream()->get().seekg(0, std::ios_base::beg);
current_.reset();
}
};
};
#define startup_(...)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:278