Skip to content

Commit 5dfd5cf

Browse files
committed
next parts and refactorization
1 parent 3b8c92e commit 5dfd5cf

File tree

10 files changed

+152
-12
lines changed

10 files changed

+152
-12
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ find_package(Boost 1.85 REQUIRED COMPONENTS
2929

3030
set(SOURCES
3131
src/main.cpp
32+
src/application.cpp
3233
src/graphio.cpp
3334
src/graphfilebase.cpp
3435
src/graphfile.cpp
36+
src/graphdata.cpp
3537
src/fileio.cpp
3638
src/filereader.cpp
3739
src/filewriter.cpp

include/application.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#ifndef APPLICATION_H
4+
#define APPLICATION_H
5+
6+
#include "graphio.hpp"
7+
8+
class Application {
9+
10+
public:
11+
static void run();
12+
};
13+
14+
#endif //APPLICATION_H

include/filewriter.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ class FileWriter : public FileIO {
1010
private:
1111

1212
public:
13-
void create_directory(const std::string &parent_path, const std::string &directory_name);
14-
void create_required_directories(const std::string& target_path);
13+
void create_required_directories(const std::string& target_path) const;
14+
15+
private:
16+
void create_directory(const std::string &parent_path, const std::string &directory_name) const;
1517
};
1618

1719
#endif //FILE_WRITER_H

include/graphdata.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#ifndef GRAPH_DATA_H
4+
#define GRAPH_DATA_H
5+
6+
#include <armadillo>
7+
#include <string>
8+
9+
class GraphData {
10+
11+
private:
12+
int nodes;
13+
arma::umat amatrix;
14+
15+
public:
16+
GraphData() {};
17+
GraphData(int nodes, arma::umat amatrix) : nodes(nodes), amatrix(amatrix) {};
18+
void save_adjacency_matrix(const std::string target_path) const;
19+
20+
// getters
21+
const int get_nodes() const;
22+
const arma::umat get_amatrix() const;
23+
24+
};
25+
26+
#endif //GRAPH_DATA_H

include/graphio.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#pragma once
2+
#pragma warning(disable:4996)
23

34
#ifndef GRAPH_IO_H
45
#define GRAPH_IO_H
56

67
#include <armadillo>
8+
#include <exception>
79
#include "filereader.hpp"
810
#include "filewriter.hpp"
911
#include "graphfile.hpp"
12+
#include "graphdata.hpp"
1013

1114
class GraphIO {
1215

@@ -15,7 +18,13 @@ class GraphIO {
1518
FileWriter writer;
1619

1720
public:
21+
void do_preactions() const;
1822
void convert() const;
23+
24+
private:
25+
unsigned short read_word(FILE* in) const;
26+
GraphData read_graph(FILE* in) const;
27+
GraphData read_graph_file(FILE** in, std::string absolute_path) const;
1928
};
2029

2130
#endif //GRAPH_IO_H

src/application.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "application.hpp"
2+
3+
void Application::run()
4+
{
5+
GraphIO graphio;
6+
graphio.do_preactions();
7+
graphio.convert();
8+
}

src/filewriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "filewriter.hpp"
22

3-
void FileWriter::create_directory(const std::string &parent_path, const std::string &directory_name)
3+
void FileWriter::create_directory(const std::string &parent_path, const std::string &directory_name) const
44
{
55
std::filesystem::path target_path(
66
parent_path
@@ -15,7 +15,7 @@ void FileWriter::create_directory(const std::string &parent_path, const std::str
1515
std::filesystem::create_directories(target_path);
1616
}
1717

18-
void FileWriter::create_required_directories(const std::string &target_path)
18+
void FileWriter::create_required_directories(const std::string &target_path) const
1919
{
2020
std::vector<std::string> bvg_vec {
2121
"b03", "b03m", "b06", "b06m", "b09", "b09m"

src/graphdata.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "graphdata.hpp"
2+
3+
void GraphData::save_adjacency_matrix(const std::string target_path) const
4+
{
5+
this->amatrix.save(arma::csv_name(target_path));
6+
}
7+
8+
const int GraphData::get_nodes() const
9+
{
10+
return this->nodes;
11+
}
12+
13+
const arma::umat GraphData::get_amatrix() const
14+
{
15+
return this->amatrix;
16+
}

src/graphio.cpp

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,82 @@
11
#include "graphio.hpp"
22

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+
39
void GraphIO::convert() const
410
{
511
std::string source_path = this->reader.read_property(FileIO::CONFIG_DB_SOURCE);
612

13+
FILE* file_ptr;
14+
GraphData graph;
15+
716
for (const auto& entry : std::filesystem::recursive_directory_iterator(source_path)) {
817
if (std::filesystem::is_regular_file(entry.path())) {
918

1019
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+
}
1241

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;
1461
}
1562
}
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+
}
1682
}

src/main.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
#include "graphio.hpp"
2-
3-
#include <boost/algorithm/string.hpp>
1+
#include "application.hpp"
42

53
int main() {
64

7-
// temporary playground
8-
GraphIO graphio;
9-
graphio.convert();
5+
Application::run();
6+
return 0;
107
}

0 commit comments

Comments
 (0)