@@ -21,21 +21,40 @@ limitations under the License.
2121#include < string>
2222
2323#include " osp/auxiliary/io/DotFileWriter.hpp"
24- #include " osp/auxiliary/io/dot_graph_file_reader.hpp"
25- #include " osp/auxiliary/io/hdag_graph_file_reader.hpp"
26- #include " osp/auxiliary/io/mtx_graph_file_reader.hpp"
24+ #include " osp/auxiliary/io/general_file_reader.hpp"
25+ #include " osp/auxiliary/io/hdag_graph_file_writer.hpp"
2726#include " osp/graph_implementations/adj_list_impl/computational_dag_edge_idx_vector_impl.hpp"
2827
2928using namespace osp ;
3029
3130using ComputationalDag = computational_dag_edge_idx_vector_impl_def_int_t ;
3231
3332void print_usage (const char *prog_name) {
34- std::cerr << " Usage: " << prog_name << " <input_file> <output_file>" << std::endl;
35- std::cerr << " Converts a graph from one file format to another." << std::endl;
36- std::cerr << " If <output_file> is '.dot', the output file will be named after the input file with a .dot extension." << std::endl;
37- std::cerr << " Supported input formats (by extension): .hdag, .mtx, .dot" << std::endl;
38- std::cerr << " Supported output formats (by extension): .dot" << std::endl;
33+ std::cerr << " Graph Format Converter" << std::endl;
34+ std::cerr << " ----------------------" << std::endl;
35+ std::cerr << " This tool converts a directed graph from one file format to another. The desired output" << std::endl;
36+ std::cerr << " format is determined by the file extension of the output file." << std::endl
37+ << std::endl;
38+ std::cerr << " Usage: " << prog_name << " <input_file> <output_file>" << std::endl << std::endl;
39+ std::cerr << " Arguments:" << std::endl;
40+ std::cerr << " <input_file> Path to the input graph file." << std::endl
41+ << std::endl;
42+ std::cerr << " <output_file> Path for the output graph file. Special values of '.dot' or '.hdag' can be" << std::endl;
43+ std::cerr << " used to automatically generate the output filename by replacing the input" << std::endl;
44+ std::cerr << " file's extension with the specified one." << std::endl;
45+ std::cerr << std::endl;
46+ std::cerr << " Supported Formats:" << std::endl;
47+ std::cerr << " Input (by extension): .hdag, .mtx, .dot" << std::endl;
48+ std::cerr << " Output (by extension): .hdag, .dot" << std::endl
49+ << std::endl;
50+ std::cerr << " The .hdag format is the HyperdagDB format. A detailed description can be found at:" << std::endl;
51+ std::cerr << " https://github.com/Algebraic-Programming/HyperDAG_DB" << std::endl
52+ << std::endl;
53+ std::cerr << " Examples:" << std::endl;
54+ std::cerr << " " << prog_name << " my_graph.mtx my_graph.hdag" << std::endl;
55+ std::cerr << " " << prog_name << " my_graph.hdag my_graph.dot" << std::endl;
56+ std::cerr << " " << prog_name << " my_graph.mtx .dot # Creates my_graph.dot" << std::endl;
57+ std::cerr << " " << prog_name << " my_graph.dot .hdag # Creates my_graph.hdag" << std::endl;
3958}
4059
4160int main (int argc, char *argv[]) {
@@ -59,30 +78,23 @@ int main(int argc, char *argv[]) {
5978 return 1 ;
6079 }
6180 output_filename = std::filesystem::path (input_filename).replace_extension (" .dot" ).string ();
81+ } else if (output_filename_arg == " .hdag" ) {
82+ if (input_ext == " .hdag" ) {
83+ std::cerr << " Error: Input file is already a .hdag file. Cannot use '.hdag' as the output file argument in "
84+ " this case."
85+ << std::endl;
86+ return 1 ;
87+ }
88+ output_filename = std::filesystem::path (input_filename).replace_extension (" .hdag" ).string ();
6289 } else {
6390 output_filename = output_filename_arg;
6491 }
6592
6693 ComputationalDag graph;
67- bool read_success = false ;
68-
6994 std::cout << " Attempting to read graph from " << input_filename << " ..." << std::endl;
70-
71- if (input_ext == " .hdag" ) {
72- read_success = file_reader::readComputationalDagHyperdagFormat (input_filename, graph);
73- } else if (input_ext == " .txt" ) {
74- read_success = file_reader::readComputationalDagHyperdagFormatDB (input_filename, graph);
75- } else if (input_ext == " .mtx" ) {
76- read_success = file_reader::readComputationalDagMartixMarketFormat (input_filename, graph);
77- } else if (input_ext == " .dot" ) {
78- read_success = file_reader::readComputationalDagDotFormat (input_filename, graph);
79- } else {
80- std::cerr << " Unknown input file extension: " << input_ext << " . Assuming .hdag format." << std::endl;
81- read_success = file_reader::readComputationalDagHyperdagFormat (input_filename, graph);
82- }
83-
84- if (!read_success) {
85- std::cerr << " Error: Failed to read graph from " << input_filename << std::endl;
95+ bool status = file_reader::readGraph (input_filename, graph);
96+ if (!status) {
97+ std::cout << " Failed to read graph\n " ;
8698 return 1 ;
8799 }
88100
@@ -95,6 +107,8 @@ int main(int argc, char *argv[]) {
95107 if (output_ext == " .dot" ) {
96108 DotFileWriter writer;
97109 writer.write_graph (output_filename, graph);
110+ } else if (output_ext == " .hdag" ) {
111+ file_writer::writeComputationalDagHyperdagFormatDB (output_filename, graph);
98112 } else {
99113 std::cerr << " Error: Unsupported output file format: " << output_ext << std::endl;
100114 print_usage (argv[0 ]);
0 commit comments