|
1 | 1 | #include "graphio.hpp" |
2 | 2 |
|
| 3 | +void GraphIO::do_preactions() const |
| 4 | +{ |
| 5 | + std::string target_path = this->reader.read_property(FileIO::CONFIG_DB_TARGET); |
| 6 | + this->writer.create_required_directories(target_path); |
| 7 | +} |
| 8 | + |
3 | 9 | void GraphIO::convert() const |
4 | 10 | { |
5 | 11 | std::string source_path = this->reader.read_property(FileIO::CONFIG_DB_SOURCE); |
6 | 12 |
|
| 13 | + FILE* file_ptr; |
| 14 | + GraphData graph; |
| 15 | + |
7 | 16 | for (const auto& entry : std::filesystem::recursive_directory_iterator(source_path)) { |
8 | 17 | if (std::filesystem::is_regular_file(entry.path())) { |
9 | 18 |
|
10 | 19 | std::filesystem::path path = entry.path(); |
11 | | - GraphFile gf(path); |
| 20 | + GraphFile ginfo = GraphFile(path); |
| 21 | + |
| 22 | + // read graph file |
| 23 | + graph = read_graph_file(&file_ptr, ginfo.get_source_absolute()); |
| 24 | + |
| 25 | + // save graph adjacency matrix file |
| 26 | + graph.save_adjacency_matrix(ginfo.get_target_absolute()); |
| 27 | + |
| 28 | + // TODO to implement |
| 29 | + // log the success of the operation on console |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +unsigned short GraphIO::read_word(FILE *in) const |
| 35 | +{ |
| 36 | + unsigned char b1, b2; |
| 37 | + b1 = getc(in); |
| 38 | + b2 = getc(in); |
| 39 | + return b1 | (b2 << 8); |
| 40 | +} |
12 | 41 |
|
13 | | - // rest of procedure |
| 42 | +GraphData GraphIO::read_graph(FILE *in) const |
| 43 | +{ |
| 44 | + int nodes; |
| 45 | + int edges; |
| 46 | + int target; |
| 47 | + int i, j; |
| 48 | + |
| 49 | + nodes = read_word(in); |
| 50 | + |
| 51 | + arma::umat matrix(nodes, nodes, arma::fill::zeros); |
| 52 | + |
| 53 | + for (i = 0; i < nodes; i++) |
| 54 | + { |
| 55 | + edges = read_word(in); |
| 56 | + |
| 57 | + for (j = 0; j < edges; j++) |
| 58 | + { |
| 59 | + target = read_word(in); |
| 60 | + matrix.at(i, target) = 1; |
14 | 61 | } |
15 | 62 | } |
| 63 | + |
| 64 | + return GraphData(nodes, matrix); |
| 65 | +} |
| 66 | + |
| 67 | +GraphData GraphIO::read_graph_file(FILE **in, std::string absolute_path) const |
| 68 | +{ |
| 69 | + try { |
| 70 | + *in = fopen(absolute_path.c_str(), "rb"); // file is opening in read binary mode |
| 71 | + if (in == NULL) { |
| 72 | + throw std::runtime_error("file cannot be opened"); |
| 73 | + } |
| 74 | + else { |
| 75 | + return read_graph(*in); |
| 76 | + } |
| 77 | + } |
| 78 | + catch (std::ifstream::failure e) { |
| 79 | + std::cerr << "error during reading the file under path: " << absolute_path << std::endl; |
| 80 | + throw std::runtime_error(e.what()); |
| 81 | + } |
16 | 82 | } |
0 commit comments