java2html  1.7
About: produces from a given a source java file an html source with syntax highlighting.
  Fossies Dox: java2html-1.7.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

 All Classes Files Functions Variables Typedefs Enumerator Macros
main.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999, 2000 Lorenzo Bettini, lorenzo.bettini@penteres.it
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  */
19 
20 #include <stdio.h>
21 #include <string.h>
22 #include <iostream.h>
23 #include <fstream.h>
24 
25 #include "version.h"
26 #include "main.h"
27 #include "colors.h"
28 #include "tags.h"
29 #include "keys.h"
30 #include "textgen.h"
31 #include "decorators.h"
32 #include "generators.h"
33 #include "messages.h"
34 
35 #include "cmdline.h"
36 #include "copyright.h"
37 
38 #define OUTPUT_EXTENSION ".html"
39 
40 /* global symbols */
41 
42 char *inputFileName, *outputFileName ; /* what we're reading */
43 ostream* sout ;
44 
45 int tabSpaces = 0 ; // space to substitue to tabs
46 
47 int entire_doc = 0 ; // we want a real html doc
48 int otherArgs ;
49 short verbose = 0 ;
50 char *cssUrl = 0 ;
51 int use_css = 0 ; // Use CSS instead of font-tags
52 
53 char *programName = 0 ;
54 char *programVersion = 0 ;
55 
56 extern int yylex() ;
57 extern int parseTags() ;
58 
59 static char *read_file(char *fileName);
60 static void file_error(const char *error, char *fileName);
61 static void internal_error(const char *error);
62 
63 int
64 main( int argc, char * argv[] )
65 {
66  char *docTitle;
67  char *docHeader; // the buffer with the header
68  char *docFooter; // the buffer with the footer
69  char *header_fileName = 0;
70  char *footer_fileName = 0;
71  gengetopt_args_info args_info ; // command line structure
72  unsigned i;
73  int v;
74 
75  if((v = cmdline_parser(argc, argv, &args_info)) != 0)
76  // calls cmdline parser. The user gived bag args if it doesn't return -1
77  return 1;
78 
81 
82  /* initialization of global symbols */
84  sout = 0 ;
85  docTitle = 0 ;
86  docHeader = 0 ;
87  docFooter = 0 ;
88 
89  // adjust flags for command line parameters
90  otherArgs = 1;
91 
92  docTitle = args_info.title_arg ;
93  header_fileName = args_info.header_arg ;
94  footer_fileName = args_info.footer_arg ;
95  verbose = args_info.verbose_given ;
96 
97  if ( args_info.tab_given > 0 )
98  tabSpaces = args_info.tab_arg ;
99 
100  if (header_fileName)
101  docHeader = read_file (header_fileName);
102 
103  if (footer_fileName)
104  docFooter = read_file (footer_fileName);
105 
106  cssUrl = args_info.css_arg ;
107  use_css = ( cssUrl != 0 ) ;
108 
109  entire_doc = ( args_info.doc_given || (docTitle != 0) || use_css ) ;
110 
111  inputFileName = args_info.input_arg ;
112  if ( inputFileName ) {
113  outputFileName = args_info.output_arg ;
114  if ( ! outputFileName ) {
116  }
117  }
118 
119  if ( verbose )
121 
122  printMessage( PACKAGE " " VERSION ) ;
123 
124  parseTags() ;
125 
126  if( use_css ) {
128  }
129  else {
130  createGenerators() ;
131  }
132 
133  // let's start the translation :-)
134 
135  // first the --input file
136  if ( ! args_info.inputs_num )
137  processFile( inputFileName, outputFileName, docTitle, docHeader, docFooter ) ;
138 
139  // let's process other files, if there are any
140  if ( args_info.inputs_num ) {
141  for ( i = 0 ; i < (args_info.inputs_num) ; ++i ) {
142  processFile( args_info.inputs[i],
143  createOutputFileName( args_info.inputs[i] ),
144  docTitle, docHeader, docFooter ) ;
145  cerr << "Processed " << args_info.inputs[i] << endl ;
146  }
147  }
148 
149  return (0 );
150 }
151 
152 char *
153 read_file(char *fileName)
154 {
155  FILE *file;
156  char *buffer = 0;
157  long int char_count;
158 
159  // we open it as binary otherwise we may experience problems under
160  // Windows system: when we fread, the number of char read can be
161  // less then char_count, and thus we'd get an error...
162  if ( (file = fopen(fileName,"rb") ) == 0 ) // The file does not exist :(
163  file_error ("Error operning", fileName);
164  else
165  {
166  // let's go to the end of the file...
167  if (fseek (file, 0, SEEK_END) != 0)
168  file_error ("Error positioning", fileName);
169 
170  // ...to read the dimension
171  char_count = ftell (file);
172  if (char_count < 0)
173  file_error ("Error reading position", fileName);
174 
175  buffer = (char *) malloc (char_count +1);
176  if (! buffer)
177  internal_error ("Memory allocation failed");
178 
179  // let's go back to the start
180  rewind (file);
181 
182  if (fread ((void *) buffer, 1, char_count, file) < (size_t) char_count)
183  file_error ("read error", fileName);
184  buffer[char_count] = '\0';
185 
186  fclose (file);
187  }
188 
189  return buffer;
190 }
191 
192 void
193 file_error(const char *error, char *file)
194 {
195  fprintf (stderr, "%s: %s, file %s\n", PACKAGE, error, file);
196  exit (1);
197 }
198 
199 void
200 internal_error(const char *error)
201 {
202  fprintf (stderr, "%s: Internal error: %s\n", PACKAGE, error);
203  exit (1);
204 }
205 
206 // output file name = input file name + ".html"
208  char *outputFileName = new char[ strlen(inputFileName) +
209  strlen(OUTPUT_EXTENSION) + 1 ] ;
210  strcpy( outputFileName, inputFileName ) ;
211  strcat( outputFileName, OUTPUT_EXTENSION ) ;
212 
213  return outputFileName ;
214 }
215 
216 void processFile( char *inputFileName, char *outputFileName, char *docTitle,
217  const char *docHeader, const char *docFooter) {
218  FILE *in = 0;
219  short deleteOStream = 1 ;
220 
221  if ( outputFileName ) {
222  sout = new ofstream(outputFileName) ;
223  if ( ! sout ) {
224  cerr << "Error in creating " << outputFileName << " for output" << endl ;
225  exit(1) ;
226  }
227  }
228 
229  if ( inputFileName ) {
230  in = freopen (inputFileName, "r", stdin);
231  if (!in) {
232  cerr << "Error in opening " << inputFileName
233  << " for input" << endl ;
234  exit(1) ;
235  }
236  }
237 
238  /*
239  * Use default values for any options not provided
240  */
241  if (sout == 0) {
242  sout = &cout;
243  deleteOStream = 0 ; // we can't delete cout !!!
244  }
245  if (in == 0) {
246  ; /* Well stdin already points to stdin so, .... */
247  }
248  if (docTitle == 0) {
249  docTitle = inputFileName; /* inputFileName may also be 0,
250  this is OK. */
251  }
252 
253  if ( entire_doc ) {
254  print_top( docTitle, cssUrl, docHeader );
255  }
256 
257  printMessage( "translating source code... ", cerr ) ;
258 
259  generateln( "<pre>" ) ;
260  generateln( "<tt>" ) ;
261  yylex() ;
262  generateln( "</tt>" ) ;
263  generateln( "</pre>" ) ;
264 
265  printMessage( "done !", cerr ) ;
266 
267  if ( entire_doc )
268  print_bottom( docFooter ) ;
269 
270  if ( deleteOStream )
271  delete sout ;
272 }
273 
274 void
275 print_top( char *docTitle, char *cssUrl , const char *docHeader)
276 {
277  if( cssUrl == 0 ) {
278  generateln( "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">" ) ;
279  }
280  else {
281  generateln( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\"");
282  generateln( " \"http://www.w3.org/TR/REC-html40/strict.dtd\">");
283  }
284  generateln( "<html>" ) ;
285  generateln( "<head>" ) ;
286  generateln( "<meta http-equiv=\"Content-Type\"" ) ;
287  generateln( "content=\"text/html; charset=iso-8859-1\">" ) ;
288  generate( "<meta name=\"GENERATOR\" content=\"" ) ;
289  generate( PACKAGE " " VERSION ) ;
290  generate( "\nby Lorenzo Bettini, bettini@gnu.org" ) ;
291  generate( "\nhttp://w3.newnet.it/bettini" ) ;
292  generate( "\nhttp://www.gnu.org/software/" PACKAGE "/" PACKAGE ".html" ) ;
293  generateln( "\">" ) ;
294  generate( "<title>" ) ;
295  generate( ( docTitle ? docTitle :
296  ( inputFileName ? inputFileName : "source file" ) ) ) ;
297  generateln( "</title>" ) ;
298  if( cssUrl != 0 ) {
299  generate( "<link rel=\"stylesheet\" href=\"" );
300  generate( cssUrl );
301  generateln( "\" type=\"text/css\">");
302  }
303  generateln( "</head>" ) ;
304  if( cssUrl == 0 && docHeader == 0) {
305  generate ("<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#0000EE\" ");
306  generateln ( "vlink=\"#551A8B\" alink=\"#FF0000\">" );
307  }
308  else {
309  generateln( "<body>" ) ;
310  }
311  if (docHeader)
312  generateln (docHeader) ;
313 }
314 
315 void print_bottom( const char *docFooter) {
316  if ( docFooter ) generateln( docFooter ) ;
317  generateln( "</body>" ) ;
318  generateln( "</html>" ) ;
319 }
320 
321 void generate( const char *s ) {
323 }
324 
325 void generateln( const char *s ) {
327 }
328 
330  generateln( "" ) ;
331 }
332 
333 void generateTab() {
334  if ( tabSpaces )
335  for ( register int i = 0 ; i < tabSpaces ; ++i )
336  generate( SPACE_CHAR ) ;
337  else
338  generate( "\t" ) ;
339 }
340 
341 void startComment( const char *s )
342 {
344 }
345 
346 void endComment( const char *s )
347 {
349 }
350 
351 void generateComment( const char *s ) {
353 }
354 
355 void startString( const char *s )
356 {
358 }
359 
360 void endString( const char *s )
361 {
363 }
364 
365 void generateString( const char *s ) {
367 }
368 
369 void generateKeyWord( const char *s ) {
371 }
372 
373 void generateBaseType( const char *s ) {
375 }
376 
377 void generateNumber( const char *s ) {
379 }
380 
381 void startTAG( const char *tag, const char *attr, const char *val ) {
382  (*sout) << "<" << tag ;
383  if ( attr && val )
384  (*sout) << " " << attr << "=" << val ;
385  (*sout) << ">" ;
386 }
387 
388 void endTAG( const char *tag ) {
389  (*sout) << "</" << tag << ">" ;
390 }
391 
392 void startColor( const char *color ) {
393  startTAG( FONT_TAG, COLOR_TAG, color ) ;
394 }
395 
396 void endColor() {
397  endTAG( FONT_TAG ) ;
398 }