Crossings Minimization  1.0
dot_and_ord_to_sgf.c
Go to the documentation of this file.
1 /**
2  * @file dot_and_ord_to_sgf.c
3  * @brief Program to convert a dot and ord file to an equivalent sgf file
4  * @author Matt Stallmann
5  * @date 2011/06/16
6  *
7  * sgf format is as follows (blank lines are ignored):
8  * c comment line 1
9  * ...
10  * c comment line k
11  *
12  * t graph_name nodes edges layers
13  *
14  * n id_1 layer_1 position_1
15  * n id_2 layer_2 position_2
16  * ...
17  * n id_n layer_n position_n
18  *
19  * e source_1 target_1
20  * ...
21  * e source_m target_m
22  *
23  * edges are directed so that the nodes appearing earlier in the input are
24  * sources
25  *
26  * @todo comments from the original dot file are not preserved and should be
27  * added by an external script for now.
28  *
29  * Deviations (not relevant here):
30  * - edge direction may not be correct yet, but can be fixed using
31  * sgfDirect.awk
32  * - nodes, edges, and layers may be missing in the title (t) line, but can
33  * be deduced
34  * - the 'n' lines may be missing, but can be deduced from the title
35  * - layers and positions may be missing in the 'n' lines but can be filled
36  * in by existing programs and/or scripts
37  * - the 'e' lines may have additional information, e.g., to identify edges
38  * as 'favored' (details not worked out yet)
39  *
40  * $Id: dot_and_ord_to_sgf.c 73 2014-07-17 20:36:15Z mfms $
41  */
42 
43 
44 #include<stdio.h>
45 #include<stdlib.h>
46 #include<string.h>
47 #include<limits.h>
48 #include<assert.h>
49 
50 #include"defs.h"
51 #include"graph_io.h"
52 #include"graph.h"
53 
54 /**
55  * prints usage message
56  */
57 static void printUsage( void ) {
58  printf( "Usage: dot_and_ord_to_sgf DOT_FILE_NAME ORD_FILE_NAME\n" );
59  printf( " reads files DOT_FILE_NAME and ORD_FILE_NAME to produce an sgf file\n" );
60  printf( " printing it on standard output\n");
61 }
62 
63 /**
64  * Writes an sgf file based on the current graph to standard output
65  */
66 static void write_sgf( void ) {
67  printf( "t %s %d %d %d\n",
68  graph_name,
72  );
73 
74  // add lines for the nodes
75  for( int layer = 0; layer < number_of_layers; layer++ ) {
76  for( int position = 0;
77  position < layers[ layer ]->number_of_nodes;
78  position++ ) {
79  Nodeptr node = layers[ layer ]->nodes[ position ];
80  printf( "n %d %d %d\n", node->id, layer, position );
81  }
82  }
83 
84  // add lines for the edges
85  for( int layer = 0; layer < number_of_layers - 1; layer++ ) {
86  for(
87  int node_position = 0;
88  node_position < layers[ layer ]->number_of_nodes;
89  node_position++ ) {
90  Nodeptr node = layers[ layer ]->nodes[ node_position ];
91  for( int edge_position = 0;
92  edge_position < node->up_degree;
93  edge_position++ ) {
94  Edgeptr edge = node->up_edges[ edge_position ];
95  printf( "e %d %d\n", node->id, edge->up_node->id );
96  }
97  }
98  }
99 }
100 
101 int main( int argc, char * argv[] )
102 {
103  if( argc != 3 ) {
104  printUsage();
105  return EXIT_FAILURE;
106  }
107  const char * dot_file_name = argv[1];
108  const char * ord_file_name = argv[2];
109 
110  readGraph( dot_file_name, ord_file_name );
111  write_sgf();
112 
113  return EXIT_SUCCESS;
114 }
115 
116 /* [Last modified: 2014 07 17 at 20:14:41 GMT] */
Nodeptr up_node
Definition: graph.h:98
int id
Definition: graph.h:64
Layerptr * layers
Definition: graph_io.c:31
int number_of_nodes
Definition: graph.h:115
int main(int argc, char *argv[])
Definitions common to all edge crossing heuristic source files.
static void write_sgf(void)
void readGraph(const char *dot_file, const char *ord_file)
Definition: graph_io.c:363
Edgeptr * up_edges
Definition: graph.h:77
int up_degree
Definition: graph.h:74
int number_of_nodes
Definition: graph_io.c:27
Definition of functions for reading and writing graphs.
static void printUsage(void)
Definition of data structures and access functions for a layered graph.
static char graph_name[MAX_NAME_LENGTH]
Definition: dot.c:42
int number_of_edges
Definition: graph_io.c:29
Nodeptr * nodes
Definition: graph.h:116
int number_of_layers
Definition: graph_io.c:28