File: FileUtils.pm

package info (click to toggle)
geogram 1.9.6-1
  • links: PTS, VCS
  • area: contrib
  • in suites: sid
  • size: 15,436 kB
  • sloc: cpp: 143,890; ansic: 10,098; perl: 1,430; sh: 1,199; yacc: 522; lex: 182; python: 157; javascript: 149; makefile: 17
file content (491 lines) | stat: -rwxr-xr-x 11,143 bytes parent folder | download
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long();
use FindBin();
use File::Find();
use File::Copy();
use Data::Dumper();

package FileUtils;

#############################################################################
# Globals
#############################################################################

#! Result suffix
my $RESULT_SUFFIX = '.mod';

#! Backup suffix
my $BACKUP_SUFFIX = '.mod-backup';

#! List of regular expressions for file exclusion
my @EXCLUDE_REGEXPS;

#! User-supplied file acceptance patterns
my $FILE_PATTERN = qr/\.(?:c|cpp|cxx|C|h|hpp|hxx|H)$/;

#! User-supplied file reordering routine
my $FILE_REORDER;

#! User-supplied handler for processing a file
my $FILE_HANDLER;

#! User-supplied handler for processing the text of a file
my $TEXT_HANDLER;

#! Options values
my %OPTIONS = (
    debug => 0,
    indent => 1,
    backup => 1,
);

#! Debug flags
my $DEBUG;

#!
# \brief Starts the processing
#
sub update_files {
    my(%attrs) = @_;

    # Check user supplied attributes

    if( my $id = $attrs{id} ) {
        $RESULT_SUFFIX = ".$id";
        $BACKUP_SUFFIX = ".$id-backup";
    } else {
        die "Error: must specify an 'id' attribute'\n";
    }

    if( $attrs{file_handler} ) {
        $FILE_HANDLER = $attrs{file_handler};
        if( ref($FILE_HANDLER) ne 'CODE' ) {
            die "Error: attribute 'file_handler' must be a function\n";
        }
    }
    elsif( $attrs{text_handler} ) {
        $TEXT_HANDLER = $attrs{text_handler};
        if( ref($TEXT_HANDLER) ne 'CODE' ) {
            die "Error: attribute 'text_handler' must be a function\n";
        }
    }
    else {
        die "Error: must specify either a 'file_handler' or a 'text_handler' attribute\n";
    }

    if( $attrs{file_reorder} ) {
        $FILE_REORDER = $attrs{file_reorder};
        if( ref($FILE_REORDER) ne 'CODE' ) {
            die "Error: attribute 'file_reorder' must be a function\n";
        }
    }

    if( $attrs{file_pattern} ) {
        $FILE_PATTERN = $attrs{file_pattern};
        if( ref($FILE_PATTERN) ne 'Regexp' ) {
            die "Error: attribute 'file_pattern' must be a Perl Regexp\n";
        }
    }

    my $options;
    if( $options = $attrs{options} ) {
        if( ref($options) ne 'ARRAY' ) {
            die "Error: attribute 'options' must be a Perl ARRAY\n";
        }
    } else {
        $options = [];
    }

    my $option_values;
    if( $option_values = $attrs{option_values} ) {
        if( ref($option_values) ne 'HASH' ) {
            die "Error: attribute 'option_values' must be a Perl HASH\n";
        }
        # Copy local default option values
        while(my($name, $value) = each %OPTIONS) {
            $option_values->{$name} = $value;
        }
    } else {
        $option_values = \%OPTIONS;
    }

    # Decode the command line argument in the user-supplied
    # table of option values

    Getopt::Long::GetOptions(
        $option_values,
        'help',
        'debug:+',
        'verbose',
        'stdout',
        'dry_run|dry-run|n',
        'replace',
        'backup!',
        'exclude=s@',
        @$options
    ) or exit(1);

    # Copy the option values to the local option table
    %OPTIONS = %$option_values;

    # Setup the debug flag
    $DEBUG = $OPTIONS{debug};

    # Check if the help option is specified
    if( $OPTIONS{help}) {
        if( my $help = $attrs{help} ) {
            if( ref($help) ne 'HASH' ) {
                die "Error: attribute 'help' must be a Perl HASH\n";
            }
            help($help);
            exit(0);
        } else {
            die "No help available\n";
        }
    }

    # Call the user supplied init function
    if( my $init = $attrs{init}) {
        if( ref($init) ne 'CODE' ) {
            die "Error: attribute 'init' must be a Perl function\n";
        }
        $init->();
    }

    # Check the validity of the exclude patterns
    check_exclude_patterns();

    # Process input
    if(@ARGV) {
        # Process given input files
        process_files(@ARGV)
    } else {
        # Process standard input
        process_file(undef);
    }
}

#!
# \brief Check the validity of the exclude patterns
# \details Exclude patterns are specified in command line option 'exclude'
#
sub check_exclude_patterns {

    foreach my $pattern (@{$OPTIONS{exclude}}) {

        if( $DEBUG ) {
            print "DEBUG: checking exclude pattern: $pattern\n";
        }

        my $re = eval { qr{$pattern}; };
        if( $@ ) {
            die <<END;
Error: exclude pattern is not a valid regular expression: $pattern
Supported patterns are Perl compatible regular expressions.
END
        }

        push(@EXCLUDE_REGEXPS, $re);
    }

    return;
}

#!
# \brief Collect multiple files or directories
# \details Directories are searched recursively for C and C++ files to
# process. Indivual files are processed if they match the configured file
# patterns (By default C/C++ files extensions)
# \param[in] files list of files or directories to process
# \return list of collected files
#
sub collect_files {
    my(@files) = @_;

    my @collected_files;

    # Filter for the recursive file search

    my $wanted = sub {
        my $file = $_;

        if( not -f $file ) {
            return;
        }

        if( $file !~ $FILE_PATTERN ) {
            return;
        }

        foreach my $re (@EXCLUDE_REGEXPS) {
            $DEBUG > 1 and print "DEBUG: checking $file for pattern: $re\n";
            if( $file =~ $re ) {
                return;
            }
        }

        push(@collected_files, $file);
        return;
    };

    File::Find::find(
        { wanted => $wanted, no_chdir => 1 },
        @files
    );

    return @collected_files;
}

#!
# \brief Process multiple files or directories
# \details Directories are searched recursively for C and C++ files to
# process. Indivual files are processed if they match the configured file
# patterns (By default C/C++ files extensions)
# \param[in] files list of files or directories to process
#
sub process_files {
    my(@files) = @_;

    my @collected_files = collect_files(@files);

    if( $FILE_REORDER ) {
        @collected_files = $FILE_REORDER->(@collected_files);
    }

    foreach my $file (@collected_files) {
        process_file($file);
    }

    return;
}

#!
# \brief Process a single file
# \param[in] file path to the input file
#
sub process_file {
    my($file) = @_;

    if( defined($file) and $OPTIONS{verbose} ) {
        print "Processing: $file\n";
    }

    if( $OPTIONS{dry_run} ) {
        return;
    }

    my $text;
    if( $FILE_HANDLER ) {
        $text = $FILE_HANDLER->($file);
        if( not defined($text) ) {
            return;
        }
    }
    elsif( $TEXT_HANDLER ) {
        $text = defined($file) ? read_file($file) : read_stdin();
        if( not defined($text) ) {
            return;
        }

        my $original_text = $text;

        $text = $TEXT_HANDLER->($file, $text);
        if( not defined($text) ) {
            return;
        }

        if( $text eq $original_text ) {
            if( $OPTIONS{verbose} ) {
                print "File is up to date\n";
            }
            if( not $OPTIONS{stdout} ) {
                return;
            }
        }
    }

    save_result($file, $text);
    return;
}

#!
# \brief Save the result
# \param[in] file path to the output file
# \param[in] text the text to save
#
sub save_result {
    my($file, $text) = @_;

    if( not defined($file) or $OPTIONS{stdout} ) {
        print $text;
        return;
    }

    my $output_file;

    if( not $OPTIONS{replace} ) {
        $output_file = "$file$RESULT_SUFFIX";
    } else {
        $output_file = $file;
        if( $OPTIONS{backup} ) {
            File::Copy::copy($file, "$file$BACKUP_SUFFIX");
        }
    }

    if( $OPTIONS{verbose} ) {
        print "Output: $output_file\n";
    }

    save_file($output_file, $text);
    return;
}


#!
# \brief Reads the contents of a file as an array of lines
# \param[in] file path to the file to read
# \return the contents of the file as an array of lines
#
sub read_file_as_lines {
    my($file) = @_;

    my $fh;
    if( not open($fh, '<', $file) ) {
        print "Error: could not open file $file: $!\n";
        return;
    }

    my @lines = <$fh>;
    close($fh);
    return @lines;
}

#!
# \brief Reads the contents of a file as a single string
# \param[in] file path to the file to read
# \return the contents of the file as a string
#
sub read_file {
    my($file) = @_;

    my $fh;
    if( not open($fh, '<', $file) ) {
        print "Error: could not open file $file: $!\n";
        return;
    }

    local($/);
    my $text = <$fh>;
    close($fh);
    return $text;
}

#!
# \brief Reads standard input as a single string
# \return the contents of the file as a string
#
sub read_stdin {
    local($/);
    my $text = <>;
    return $text;
}

#!
# \brief Reads the contents of a file as a single string
# \param[in] file path to the file to read
# \return the contents of the file as a string
#
sub save_file {
    my($file, @text) = @_;

    my $fh;
    if( not open($fh, '>', $file) ) {
        print "Error: could not open file for writing: $file: $!\n";
        return 0;
    }

    print $fh @text;
    close($fh);
    return 1;
}

#!
# \brief Help
# \param[in] sections map of textual information for the sections NAME
# SYNOPSIS DESCRIPTION OPTIONS and FILES.
#
sub help {
    my($sections) = @_;

    # Fix missing sections

    $sections->{NAME} ||= <<END;
    $FindBin::Script - Undocumented
END

    $sections->{SYNOPSIS} ||= <<END;
    $FindBin::Script [options] [files...]
END

    $sections->{DESCRIPTION} ||= <<END;
    Undocumented
END

    $sections->{OPTIONS} ||= '';
    $sections->{FILES} ||= '';

    print <<END;

NAME
$sections->{NAME}
SYNOPSIS
$sections->{SYNOPSIS}
DESCRIPTION
$sections->{DESCRIPTION}
OPTIONS
    --help
        Print this help and exit.

    --verbose
        Print extra information

    --debug
        Print extra debug output

    --replace
          Replace source files (creates a backup F.indent-backup).

    --no-backup
        In combination with option --replace, --no-backup do not create any
        backup of the input file. This is useful if files are under source
        control.

    --exclude=pattern
        When a directory is searched recursively, excludes the files matching
        the specified pattern (Perl compatible regular expressions). Option
        --exclude can be repeated any number of times to add multiple
        exclusion patterns.

    --dry-run, -n
        Only prints the list of files that would be processed, without
        actually processing them.
$sections->{OPTIONS}
FILES
    List of files and directories to update.
    - File arguments are processed only if they have the requested extension.
    - Directory arguments are searched recursively and all files having the
      requested extension are processed.

    If no input files are specified, the input is read from stdin and
    dumped to standard output

$sections->{FILES}
END
    return;
}

1;