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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#!/usr/bin/perl
# PDFedit - free program for PDF document manipulation.
# Copyright (C) 2006-2009 PDFedit team: Michal Hocko,
# Jozef Misutka,
# Martin Petricek
# Former team members: Miroslav Jahoda
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (in doc/LICENSE.GPL); if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
# Project is hosted on http://sourceforge.net/projects/pdfedit
# Program to automatically update .h files from .cc file
# For more info, see headergen.txt
use strict;
my $QTDIR=$ENV{'QTDIR'};
my $cppflags="-I$QTDIR/include -D__GNUC__"; #preprocessor flags
my $file_count=0;
# refresh one header
sub refresh {
my ($name)=@_; # Base name without extension
my $wmessage="Parsing $name";
my @fn=();
my $f_c=$name.".cc"; # Name of .cc file
my $f_h=$name.".h"; # Name of .h file
my $realname=''; # Name of class with proper capitalization
my $mark="__".uc($f_h)."__";
$mark=~s/\./_/g;
# Open C++ file
# open FC, "cpp $f_c $cppflags|"; #use preprocessor to strip comments etc ...
open FC, "cat $f_c|"; #do not use preprocessor
while (my $line=<FC>) {
next if ($line=~/^\s*$/); # Empty line
next if ($line=~/^#/); # Line with preprocessor directive
if ($line=~/\/\*\*/) { # Line contain /** style doxygen comment
while (!($line=~/\*\//)) { # Read until first "*/" comment end
my $lw=<FC>;
$line.=$lw;
last if ($lw eq ''); # End of file
}
$line=~s/\/\*\*[\x00-\xff]*?\*\///mg; # Weed the comment out
}
$line=~s/^\s+//g;
if ($line=~/(^|\s)${name}::/i) { # Line contain class name (assuming filename=classname, except for case)
while (!($line=~/\{/)) { # Read until first "{" bracket
my $lw=<FC>;
$line.=$lw;
last if ($lw eq ''); # End of file
}
$line=~s/\n/ /mg; # Newlines to space
$line=~s/\s*\{.*\s*//g; # strip bracket {
$line.=';'; # Append semicolon ;
$line=~/::(~?\w+)/; # get name of method
my $func=$1; # Name of method
if ($line=~s/(${name}):://i) {
$realname=$1; # Set correct name of class when we found it
}
# Convert commented initializers ( "/*=something*/" --> "=something" )
$line=~s|\s*/\*=\s*(.*?)\*/\s*|=\1|g;
# Strip out calling of superclass constructor(s) "X() : Y();" -> "X();"
# up to one extra level of brackets can occur in calling of constructor, like "X(x):Y(f(x))"
$line=~s/([^:])\s*:\s*[a-zA-Z_][a-zA-Z_0-9]*\s*\(([^()]*|\([^()]*\))*\)(\s*,\s*[a-zA-Z_][a-zA-Z_0-9]*\s*\(([^()]*|\([^()]*\))*\))*\s*;$/\1;/;
# Remove "unused" attribute
$line=~s/__attribute__\(\(unused\)\)\s*//g;
# Add to line with functions found
push @fn,$line;
# print $func." -> ".$line."\n";
}
}
close FC;
$file_count++;
if (!@fn) { #nothing that can be added to headers in this file -> return
print "$wmessage .. no headers\n";
return;
}
$wmessage.="\n";
# Open header file
open FH, "<$f_h";
my $head='';
my $head_h='';
while (my $line=<FH>) {
$head.=$line;
# Look for class definition
if ($line=~/class\s+$name(\s+|:|$)/i) {
#split the content into two pieces ("before class definition" and "inside class definition and further")
$head_h=$head;$head='';
}
}
if ($head_h=~/^$/) {
# No class defined in header -> create one
$head_h="$head\n\nclass $realname {\n";
$head="};\n";
}
close FH;
my $added=0;
my $addedh=0;
# Look for include guards
if (!($head_h=~/$mark/)) {
$head_h="#ifndef $mark\n#define $mark\n".$head_h;
$head.="#endif\n";
$addedh=1;
}
my $out='';
$out.=$head_h;
# For each function found in .cc file
foreach my $i (@fn) {
# Is present in header?
my $idx=0;
my $c='';
for(;;) {
$idx=index($head,$i,$idx);
last if ($idx<=0);
#check for character before definition -> must be whitespace
$c=substr($head,$idx-1,1);
if ($c=~/\S/) { #non-whitespace -> Invalid match
my $idxo=$idx;
$idx=index($head,$i,$idx+1);
last if ($idx<=0);
next;
}
last;
}
if ($idx==-1) {
if (!$added) {$out.="//ADDED functions begin\n";}
$out.=" $i\n";$added++;
}
}
if ($added) {$out.="//ADDED functions end\n";}
$out.=$head;
# Write modifications, if any ...
if ($added || $addedh) {
if ($added) { print "$wmessage (added $added function(s) to $f_h)\n"; $wmessage='';}
if ($addedh) { print "$wmessage (added include guards to $f_h)\n"; $wmessage='';}
# Backup the original file
rename($f_h,$f_h.".old");
# Write modified header file
open FH, ">$f_h";
binmode FH;
print FH $out;
close FH;
}
}
my @ignore=();
open IGN, "<.headergen.ignore";
while (my $l=<IGN>){
$l=~s/^\s+//mg;
$l=~s/\s+$//mg;
push @ignore,$l;
}
my $ign=join('|',@ignore);
#print "Ignoring: $ign\n";
close IGN;
opendir (DH,".") || die "Cannot open current directory";
while (my $s=readdir(DH)) {
if ($ign=~/./ && $s=~/$ign/) {
# print "Ignoring $s\n";
next;
}
next if (!($s=~/(.*)\.cc$/i));
refresh($1);
}
print "Parsed $file_count file(s)\n";
closedir DH;
|