File: process_C_header.pl

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (65 lines) | stat: -rwxr-xr-x 1,896 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl
use Data::Dumper;

$|=1;
my $debug = 0;

if (scalar @ARGV < 2) {
  &usage;
}

# Open grib_api.h and extract all macros of the form
#  #define GRIB.*  number
#
my $grib_header_file = $ARGV[0];
my $ecco_header_file = $ARGV[1];

open GRIB_HEADER_FILE, $grib_header_file or die $!;
my @grib_lines = <GRIB_HEADER_FILE>;
close GRIB_HEADER_FILE;

# Store each GRIB_ macro and its value
foreach (@grib_lines) {
    if (/^\s*#define\s+(GRIB[_A-z0-9]+)\s+(.+)/) {
        my $grib_macro = $1;
        my $grib_value = $2;
        #print "m=|$grib_macro| \t\t v=|$grib_value|\n";
        $macro_map{$grib_macro} = $grib_value;
    }
}
#print Data::Dumper->Dump([\%macro_map], ["macro_map"]), $/ if ($debug);

open ECCO_HEADER_FILE, $ecco_header_file or die $!;
my @ecco_lines = <ECCO_HEADER_FILE>;
close ECCO_HEADER_FILE;
# Apply the value from our map to the equivalent CODES_ macros
foreach (@ecco_lines) {
    if (/^\s*#define\s+(CODES[_A-z0-9]+)\s+(GRIB.+)/) {
        # Replace macro lines like:
        #   #define CODES_OUT_OF_RANGE   GRIB_OUT_OF_RANGE
        # with
        #   #define CODES_OUT_OF_RANGE   -65
        my $ecco_macro = $1;
        my $grib_macro = $2;
        print "#define $ecco_macro $macro_map{$grib_macro}\n";
    }
    elsif (/^\s*typedef\s+struct\s+(grib_.*)\s+(codes_.*) *;/) {
        # The struct documentation is placed before declarations like:
        #   typedef struct grib_iterator codes_iterator;
        # This is not useful for users so instead we add just the struct
        # declaration beforehand e.g.
        #   struct codes_iterator;
        my $grib_struct = $1;
        my $ecco_struct = $2;
        print "struct $ecco_struct;\n$&\n";  # Note: $& references the matched string
    }
    else {
        print;
    }
}

###################################################
sub usage {
   print "$0 grib_api.h eccodes.h\n";
   exit 1
}