Backend main functions are now factored out.
[external/ragel.git] / rlgen-dot / main.cpp
1 /*
2  *  Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
3  */
4
5 /*  This file is part of Ragel.
6  *
7  *  Ragel is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  Ragel is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  * 
17  *  You should have received a copy of the GNU General Public License
18  *  along with Ragel; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <iostream>
26 #include <fstream>
27 #include <unistd.h>
28
29 #include "common.h"
30 #include "rlgen-dot.h"
31 #include "xmlparse.h"
32 #include "pcheck.h"
33 #include "vector.h"
34 #include "version.h"
35 #include "gvdotgen.h"
36
37 using std::istream;
38 using std::ifstream;
39 using std::ostream;
40 using std::ios;
41 using std::cin;
42 using std::cout;
43 using std::cerr;
44 using std::endl;
45
46 /* Io globals. */
47 extern istream *inStream;
48 extern ostream *outStream;
49 extern output_filter *outFilter;
50 extern const char *outputFileName;
51
52 /* Graphviz dot file generation. */
53 extern bool graphvizDone;
54 extern bool displayPrintables;
55
56 extern int numSplitPartitions;
57
58 /* Print a summary of the options. */
59
60 /*
61  * Callbacks invoked by the XML data parser.
62  */
63
64 /* Invoked by the parser when the root element is opened. */
65 ostream *dotOpenOutput( char *inputFile )
66 {
67         /* Make sure we are not writing to the same file as the input file. */
68         if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
69                 error() << "output file \"" << outputFileName  << 
70                                 "\" is the same as the input file" << endl;
71         }
72
73         if ( outputFileName != 0 ) {
74                 /* Create the filter on the output and open it. */
75                 outFilter = new output_filter( outputFileName );
76                 outFilter->open( outputFileName, ios::out|ios::trunc );
77                 if ( !outFilter->is_open() ) {
78                         error() << "error opening " << outputFileName << " for writing" << endl;
79                         exit(1);
80                 }
81
82                 /* Open the output stream, attaching it to the filter. */
83                 outStream = new ostream( outFilter );
84         }
85         else {
86                 /* Writing out ot std out. */
87                 outStream = &cout;
88         }
89         return outStream;
90 }
91
92 /* Invoked by the parser when a ragel definition is opened. */
93 CodeGenData *dotMakeCodeGen( char *sourceFileName, char *fsmName, 
94                 ostream &out, bool wantComplete )
95 {
96         CodeGenData *codeGen = new GraphvizDotGen(out);
97
98         codeGen->sourceFileName = sourceFileName;
99         codeGen->fsmName = fsmName;
100         codeGen->wantComplete = wantComplete;
101
102         return codeGen;
103 }