Skip to content

Commit b272bf8

Browse files
committedApr 20, 2017
[sipify] read file at once into an array
this will allow accessing other lines while reading
1 parent e5dd936 commit b272bf8

File tree

2 files changed

+88
-71
lines changed

2 files changed

+88
-71
lines changed
 

‎scripts/sipify.pl

Lines changed: 83 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ sub processDoxygenLine
3535
if ( $line =~ m/[\\@]brief (.*)/ ) {
3636
return " $1\n";
3737
}
38-
return $line;
38+
return "$line\n";
3939
}
4040

41-
4241
my $headerfile = $ARGV[0];
4342

44-
open(my $header, "<", $headerfile) || die "Couldn't open '".$headerfile."' for reading because: ".$!;
43+
open(my $handle, "<", $headerfile) || die "Couldn't open '".$headerfile."' for reading because: ".$!;
44+
chomp(my @lines = <$handle>);
45+
close $handle;
4546

4647
# contexts
4748
my $SIP_RUN = 0;
@@ -52,40 +53,44 @@ sub processDoxygenLine
5253
my $comment = '';
5354
my $nesting_index = 0;
5455
my $private_section_line = '';
55-
my $line;
5656
my $classname = '';
5757
my $return_type = '';
5858
my $is_override = 0;
5959
my %qflag_hash;
6060

61-
print "/************************************************************************\n";
62-
print " * This file has been generated automatically from *\n";
63-
print " * *\n";
64-
printf " * %-*s *\n", 68, $headerfile;
65-
print " * *\n";
66-
print " * Do not edit manually ! Edit header and run scripts/sipify.pl again *\n";
67-
print " ************************************************************************/\n";
61+
my $line_count = @lines;
62+
my $line_idx = -1;
63+
my $line;
64+
my @output = ();
6865

66+
push @output, "/************************************************************************\n";
67+
push @output, " * This file has been generated automatically from *\n";
68+
push @output, " * *\n";
69+
push @output, sprintf " * %-*s *\n", 68, $headerfile;
70+
push @output, " * *\n";
71+
push @output, " * Do not edit manually ! Edit header and run scripts/sipify.pl again *\n";
72+
push @output, " ************************************************************************/\n";
6973

70-
while(!eof $header){
71-
$line = readline $header;
72-
#print $line;
74+
while ($line_idx < $line_count){
75+
$line = $lines[$line_idx];
76+
$line_idx++;
77+
#push @output, "$line\n";
7378

7479
if ($line =~ m/^\s*SIP_FEATURE\( (\w+) \)(.*)$/){
75-
print "%Feature $1$2\n";
80+
push @output, "%Feature $1$2\n";
7681
next;
7782
}
7883
if ($line =~ m/^\s*SIP_IF_FEATURE\( (\!?\w+) \)(.*)$/){
79-
print "%If ($1)$2\n";
84+
push @output, "%If ($1)$2\n";
8085
next;
8186
}
8287
if ($line =~ m/^\s*SIP_CONVERT_TO_SUBCLASS_CODE(.*)$/){
83-
print "%ConvertToSubClassCode$1\n";
88+
push @output, "%ConvertToSubClassCode$1\n";
8489
next;
8590
}
8691

8792
if ($line =~ m/^\s*SIP_END(.*)$/){
88-
print "%End$1\n";
93+
push @output, "%End$1\n";
8994
next;
9095
}
9196

@@ -94,7 +99,7 @@ sub processDoxygenLine
9499
if ( $line =~ m/^\s*#ifdef SIP_RUN/){
95100
$SIP_RUN = 1;
96101
if ($ACCESS == PRIVATE){
97-
print $private_section_line
102+
push @output, $private_section_line."\n";
98103
}
99104
next;
100105
}
@@ -114,8 +119,9 @@ sub processDoxygenLine
114119

115120
# if there is an else at this level, code will be ignored i.e. not SIP_RUN
116121
if ( $line =~ m/^\s*#else/ && $nesting_index == 0){
117-
while(!eof $header){
118-
$line = readline $header;
122+
while ($line_idx < $line_count){
123+
$line = $lines[$line_idx];
124+
$line_idx++;
119125
if ( $line =~ m/^\s*#if(def)?\s+/ ){
120126
$nesting_index++;
121127
}
@@ -134,15 +140,16 @@ sub processDoxygenLine
134140
}
135141
elsif ( $line =~ m/^\s*#ifndef SIP_RUN/){
136142
# code is ignored here
137-
while(!eof $header){
138-
$line = readline $header;
143+
while ($line_idx < $line_count){
144+
$line = $lines[$line_idx];
145+
$line_idx++;
139146
if ( $line =~ m/^\s*#if(def)?\s+/ ){
140147
$nesting_index++;
141148
}
142149
elsif ( $line =~ m/^\s*#else/ && $nesting_index == 0 ){
143150
# code here will be printed out
144151
if ($ACCESS == PRIVATE){
145-
print $private_section_line;
152+
push @output, $private_section_line."\n";
146153
}
147154
$SIP_RUN = 1;
148155
last;
@@ -167,7 +174,7 @@ sub processDoxygenLine
167174
# TYPE HEADER CODE
168175
if ( $HEADER_CODE && $SIP_RUN == 0 ){
169176
$HEADER_CODE = 0;
170-
print "%End\n";
177+
push @output, "%End\n";
171178
}
172179

173180
# Skip forward declarations
@@ -205,33 +212,36 @@ sub processDoxygenLine
205212
}
206213
# Skip assignment operator
207214
if ( $line =~ m/operator=\s*\(/ ){
208-
print "// $line";
215+
push @output, "// $line";
209216
next;
210217
}
211218

212219
# Detect comment block
213220
if ($line =~ m/^\s*\/\*/){
214221
do {no warnings 'uninitialized';
215-
$comment = processDoxygenLine( $line =~ s/^\s*\/\*(\*)?(.*)$/$2/r );
222+
$comment = processDoxygenLine( $line =~ s/^\s*\/\*(\*)?(.*?)\n?$/$2/r );
216223
};
217224
$comment =~ s/^\s*$//;
218-
while(!eof $header){
219-
$line = readline $header;
220-
$comment .= processDoxygenLine( $line =~ s/\s*\*?(.*?)(\/)?$/$1/r );
225+
#$comment =~ s/^(\s*\n)*(.+)/$2/;
226+
while ($line_idx < $line_count){
227+
$line = $lines[$line_idx];
228+
$line_idx++;
229+
$comment .= processDoxygenLine( $line =~ s/\s*\*?(.*?)(\/)?\n?$/$1/r );
221230
if ( $line =~ m/\*\/$/ ){
222231
last;
223232
}
224233
}
225-
$comment =~ s/(\n)+$//;
226-
#print $comment;
234+
$comment =~ s/\n+$//;
235+
#push @output, $comment;
227236
next;
228237
}
229238

230239
# save comments and do not print them, except in SIP_RUN
231240
if ( $SIP_RUN == 0 ){
232241
if ( $line =~ m/^\s*\/\// ){
233-
$line =~ s/^\s*\/\/\!*\s*(.*)\n?$/$1/;
242+
$line =~ s/^\s*\/\/\!*\s*(.*?)\n?$/$1/;
234243
$comment = processDoxygenLine( $line );
244+
$comment =~ s/\n+$//;
235245
next;
236246
}
237247
}
@@ -263,14 +273,14 @@ sub processDoxygenLine
263273
if ( $comment !~ m/^\s*$/ ){
264274
$line .= "%Docstring\n$comment\n%End\n";
265275
}
266-
$line .= "\n%TypeHeaderCode\n#include \"" . basename($headerfile) . "\"\n";
276+
$line .= "\n%TypeHeaderCode\n#include \"" . basename($headerfile) . "\"";
267277

268-
print $line;
278+
push @output, "$line\n";
269279

270-
my $skip;
271280
# Skip opening curly bracket, we already added that above
272-
$skip = readline $header;
273-
$skip =~ m/^\s*{\s$/ || die "Unexpected content on line $line";
281+
my $skip = $lines[$line_idx];
282+
$line_idx++;
283+
$skip =~ m/^\s*{\s*$/ || die "Unexpected content on line $skip";
274284

275285
$comment = '';
276286
$HEADER_CODE = 1;
@@ -280,19 +290,21 @@ sub processDoxygenLine
280290

281291
# Enum declaration
282292
if ( $line =~ m/^\s*enum\s+\w+.*?$/ ){
283-
print $line;
284-
$line = readline $header;
293+
push @output, "$line\n";
294+
$line = $lines[$line_idx];
295+
$line_idx++;
285296
$line =~ m/^\s*\{\s*$/ || die 'Unexpected content: enum should be followed by {';
286-
print $line;
287-
while(!eof $header){
288-
$line = readline $header;
297+
push @output, "$line\n";
298+
while ($line_idx < $line_count){
299+
$line = $lines[$line_idx];
300+
$line_idx++;
289301
if ($line =~ m/\};/){
290302
last;
291303
}
292304
$line =~ s/(\s*\w+)(\s*=\s*[\w\s\d<|]+.*?)?(,?).*$/$1$3/;
293-
print $line;
305+
push @output, "$line\n";
294306
}
295-
print $line;
307+
push @output, "$line\n";
296308
# enums don't have Docstring apparently
297309
$comment = '';
298310
next;
@@ -305,7 +317,7 @@ sub processDoxygenLine
305317

306318
# remove struct member assignment
307319
if ( $SIP_RUN != 1 && $ACCESS == PUBLIC && $line =~ m/^(\s*\w+[\w<> *&:,]* \*?\w+) = \w+(\([^()]+\))?;/ ){
308-
$line = "$1;\n";
320+
$line = "$1;";
309321
}
310322

311323
# catch Q_DECLARE_FLAGS
@@ -342,8 +354,9 @@ sub processDoxygenLine
342354
if ( $line =~ m/^(\s*)?(explicit )?(\w+)\([\w\=\(\)\s\,\&\*\<\>]*\)(?!;)$/ ){
343355
my $newline = $line =~ s/\n/;\n/r;
344356
my $nesting_index = 0;
345-
while(!eof $header){
346-
$line = readline $header;
357+
while ($line_idx < $line_count){
358+
$line = $lines[$line_idx];
359+
$line_idx++;
347360
if ( $nesting_index == 0 ){
348361
if ( $line =~ m/^\s*(:|,)/ ){
349362
next;
@@ -368,12 +381,14 @@ sub processDoxygenLine
368381

369382
# remove function bodies
370383
if ( $line =~ m/^(\s*)?(virtual )?(static |const )*(([\w:]+(<.*?>)?\s+(\*|&)?)?(\w+|operator.{1,2})\(.*?(\(.*\))*.*\)( (?:const|SIP_[A-Z_]*?))*)\s*(\{.*\})?(?!;)(\s*\/\/.*)?$/ ){
371-
my $newline = "$1$2$3$4;\n";
384+
my $newline = "$1$2$3$4;";
372385
if ($line !~ m/\{.*?\}$/){
373-
$line = readline $header;
386+
$line = $lines[$line_idx];
387+
$line_idx++;
374388
if ( $line =~ m/^\s*\{\s*$/ ){
375-
while(!eof $header){
376-
$line = readline $header;
389+
while ($line_idx < $line_count){
390+
$line = $lines[$line_idx];
391+
$line_idx++;
377392
if ( $line =~ m/\}\s*$/ ){
378393
last;
379394
}
@@ -385,7 +400,7 @@ sub processDoxygenLine
385400

386401
# remove inline declarations
387402
if ( $line =~ m/^(\s*)?(static |const )*(([\w:]+(<.*?>)?\s+(\*|&)?)?(\w+)( (?:const*?))*)\s*(\{.*\});(\s*\/\/.*)?$/ ){
388-
my $newline = "$1$3;\n";
403+
my $newline = "$1$3;";
389404
$line = $newline;
390405
}
391406

@@ -432,7 +447,7 @@ sub processDoxygenLine
432447
$line =~ s/\s*% (MappedType|TypeHeaderCode|ConvertFromTypeCode|ConvertToTypeCode|MethodCode|End)/%$1/;
433448
$line =~ s/\/\s+GetWrapper\s+\//\/GetWrapper\//;
434449

435-
print $line;
450+
push @output, "$line\n";
436451

437452
# multiline definition (parenthesis left open)
438453
if ( $MULTILINE_DEFINITION == 1 ){
@@ -463,24 +478,27 @@ sub processDoxygenLine
463478
# parent class Docstring
464479
}
465480
else {
466-
print "%Docstring\n";
481+
push @output, "%Docstring\n";
467482
if ( $comment !~ m/^\s*$/ ){
468-
print "$comment\n";
483+
push @output, "$comment\n";
469484
}
470485
if ($return_type ne '' ){
471-
print " :rtype: $return_type\n";
486+
push @output, " :rtype: $return_type\n";
472487
}
473-
print "%End\n";
488+
push @output, "%End\n";
474489
}
475490
$comment = '';
476491
$return_type = '';
477492
$is_override = 0;
478493
}
479494
}
480-
print "/************************************************************************\n";
481-
print " * This file has been generated automatically from *\n";
482-
print " * *\n";
483-
printf " * %-*s *\n", 68, $headerfile;
484-
print " * *\n";
485-
print " * Do not edit manually ! Edit header and run scripts/sipify.pl again *\n";
486-
print " ************************************************************************/\n";
495+
push @output, "/************************************************************************\n";
496+
push @output, " * This file has been generated automatically from *\n";
497+
push @output, " * *\n";
498+
push @output, sprintf " * %-*s *\n", 68, $headerfile;
499+
push @output, " * *\n";
500+
push @output, " * Do not edit manually ! Edit header and run scripts/sipify.pl again *\n";
501+
push @output, " ************************************************************************/\n";
502+
503+
504+
print join('',@output)."\n";

‎tests/scripts/sipifyheader.expected.sip

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class QgsSipifyHeader : QtClass<QVariant>
5151
%TypeHeaderCode
5252
#include "sipifyheader.h"
5353
%End
54+
5455
%ConvertToSubClassCode
5556
if ( sipCpp->headerType() == QgsSipifyHeader::Special )
5657
sipType = sipType_QgsSpecialSipifyHeader;
@@ -68,10 +69,10 @@ class QgsSipifyHeader : QtClass<QVariant>
6869
};
6970
typedef QFlags<QgsSipifyHeader::MyEnum> Flags;
7071

72+
7173
struct Data
7274
{
7375
Data( QgsMapLayer *layer, Qstring name );
74-
7576
QString mName;
7677
int mCount;
7778
QgsMapLayer *mLayer;
@@ -81,7 +82,6 @@ class QgsSipifyHeader : QtClass<QVariant>
8182
%Docstring
8283
A constructor with definition in header
8384
%End
84-
8585
QgsSipifyHeader( QWidget *parent /TransferThis/ = 0 );
8686
%Docstring
8787
A classic constructor with arguments
@@ -91,7 +91,6 @@ A constructor with definition in header
9191
%Docstring
9292
A constructor with no empty `()`
9393
%End
94-
9594
QgsSipifyHeader( QList<Point> a, const Issues &b = Issues::weDontHaveIssues(), QgsClass *b = 0 );
9695
%Docstring
9796
A constructor with some special character types
@@ -104,7 +103,6 @@ Default constructor
104103
%End
105104

106105
// QgsSipifyHeader &operator=( const QgsSipifyHeader other );
107-
108106
bool operator==( const QgsSipifyHeader other );
109107
%Docstring
110108
Comparison operator should be kept
@@ -208,6 +206,7 @@ Removing function body with virtual const reference
208206
virtual int overriddenProperty();
209207
virtual int overrideWithoutVirtual();
210208

209+
211210
QString returnTypeString() const;
212211
%Docstring
213212
:rtype: str
@@ -275,7 +274,6 @@ class ClassWithPrivateInheritanceOnly
275274
%Docstring
276275
A constructor with definition in header on several lines
277276
%End
278-
279277
};
280278

281279

@@ -293,16 +291,17 @@ class AbstractClass /Abstract/
293291
%Docstring
294292
A constructor
295293
%End
296-
297294
};
298295

299296
QFlags<QgsSipifyHeader::MyEnum> operator|(QgsSipifyHeader::MyEnum f1, QFlags<QgsSipifyHeader::MyEnum> f2);
300297

301298

299+
302300
/************************************************************************
303301
* This file has been generated automatically from *
304302
* *
305303
* tests/scripts/sipifyheader.h *
306304
* *
307305
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
308306
************************************************************************/
307+

0 commit comments

Comments
 (0)
Please sign in to comment.