summaryrefslogtreecommitdiff
path: root/misc/doxystar.cxx
blob: f5fd269745b09562c6ac62aa9c43a76baa430e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// "$Id$"
//
// Doxygen pre-formatting program for the Fast Light Tool Kit (FLTK).
//
// Copyright 2010 by Matthias Melcher.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems on the following page:
//
//     http://www.fltk.org/str.php
//

#include <stdio.h>
#include <string.h>

char linebuf[1024];



int main(int argc, char **argv) {
  if (argc!=1) {
    puts("Add stars (*) in front of multi-line doxygen comments");
    puts("to protect comment indentation from code beautifiers.");
    puts("usage: cat file | doxystar");
    return 0;
  }

  int state = 0;
  char *commentStart;
  int i, commentCol;
  for (;;) {
    if (!fgets(linebuf, 1020, stdin)) break; // EOF or error
    switch (state) {
      case 0: // line start is source code
        commentStart = strstr(linebuf, "/*"); 
        if (commentStart) {
          // check if this comment spans multiple lines
          if (strstr(commentStart, "*/")==0) {
            if ((commentStart[2]=='*' || commentStart[2]=='!') && commentStart[3]!='*') {
              state = 2; // Doxygen multiline comment
              commentCol = commentStart - linebuf;
            } else {
              state = 1; // regular multiline comment
            }
          } else { 
            // single line comment, do nothing
          }
        }
        fputs(linebuf, stdout);
        break;
      case 1: // line start is inside a regular multiline comment
        if (strstr(linebuf, "*/")) {
          state = 0;
        } else {
          // still inside comment
        }
        fputs(linebuf, stdout);
        break;
      case 2: // line start is inside a doxygen  multiline comment
        for (i=0; i<commentCol; i++) fputc(' ', stdout);
        fputs(" *", stdout); 
        if (strstr(linebuf, "*/")) {
          state = 0;
        } else {
          // still inside comment
        }
        for (i=0; i<commentCol+1; i++) 
          if (linebuf[i]!=' ')
            break;
        if (linebuf[i]=='*') {
          if (linebuf[i+1]==' ') {
            i+=2;
          } else {
            i+=1;
          }
        }
        fputs(linebuf+i, stdout);
        break;
    }
  }

  return 0;
}

//
// End of "$Id$".
//