Changed license from GPL v2+ to FreeBSD (aka BSD 2-clause) license
[libdai.git] / utils / fg2dot.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <iostream>
10 #include <fstream>
11 #include <cstdlib>
12 #include <string>
13 #include <dai/factorgraph.h>
14
15
16 using namespace dai;
17 using namespace std;
18
19
20 int main( int argc, char *argv[] ) {
21 if( argc != 3 ) {
22 // Display help message if number of command line arguments is incorrect
23 cout << "This program is part of libDAI - http://www.libdai.org/" << endl << endl;
24 cout << "Usage: ./fg2dot <in.fg> <out.dot>" << endl << endl;
25 cout << "Converts a libDAI factor graph file to a GraphViz .dot file for visualization." << endl;
26 cout << "The .dot file can be converted to .ps (PostScript) by" << endl;
27 cout << "'neato -T ps out.dot > out.ps' or by 'dot -T ps out.dot > out.ps'" << endl << endl;
28 return 1;
29 } else {
30 // Read factorgraph
31 FactorGraph fg;
32 char *infile = argv[1];
33 fg.ReadFromFile( infile );
34
35 // Open output file for writing (except if filename equals "-")
36 ostream *os = &cout;
37 ofstream outfile;
38 if( string( argv[2] ) != "-" ) {
39 outfile.open( argv[2] );
40 if( !outfile.is_open() ) {
41 cerr << "Cannot open " << argv[2] << " for writing" << endl;
42 return 1;
43 }
44 os = &outfile;
45 } // else, write to cout
46
47 // Write the .dot file
48 fg.printDot( *os );
49
50 return 0;
51 }
52 }