#include <xtd/xtd>
class program {
public:
static auto main() -> void {
test_stream_reader_enumerable();
console::write_line("---");
test_reading_file();
}
static void test_stream_reader_enumerable() {
auto memory_before = memory_information::get_used_process_memory();
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.
push_back(line);
console::write_line("Found: {}", strings_found.size());
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 void test_reading_file() {
size memory_before = memory_information::get_used_process_memory();
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());
console::write_line("This example requires a file named {}.", path::combine(path::get_temp_path(), "temp_file.txt"));
return;
}
for (auto line : file_contents)
if (line.contains(
"string to search for")) strings_found.
push_back(line);
console::write_line("Found: {}", strings_found.size());
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} {}
enumerator<string> get_enumerator()
const override {
return {new_ptr<stream_reader_enumerator>(file_path_)};}
};
private:
std::optional<string> current_;
public:
stream_reader_enumerator(const string& file_path) : sr_ {file_path} {}
~stream_reader_enumerator() {sr_.
close();}
const string& current() const override {
return current_.value();
}
bool move_next() override {
return current_.has_value();
}
void reset() override {
sr_.
base_stream()->get().seekg(0, std::ios_base::seekdir::beg);
current_.reset();
}
};
};
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:31
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:35
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:71
virtual void push_back(const type_t &value)
Appends the given element value to the end of the container.
Definition list.hpp:778
The exception that is thrown when the format of an argument does not meet the parameter specification...
Definition invalid_operation_exception.hpp:19
The exception that is thrown when an attempt to access a file that does not exist on disk fails.
Definition file_not_found_exception.hpp:30
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.hpp:28
std::optional< std::reference_wrapper< std::istream > > base_stream() const
Returns the underlying stream.
void close() override
Closes the stream_reader object and the underlying stream, and releases any system resources associat...
bool end_of_stream() const
Gets a value that indicates whether the current stream position is at the end of the stream.
virtual xtd::string read_line()
Reads a line of characters from the current stream and returns the data as a string.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:42
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:175
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:15
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.hpp:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10