Crombie Tools
bcalstrings.pl
Go to the documentation of this file.
1 #! /usr/bin/env perl
2 
3 use strict;
4 use warnings;
5 
6 # Check arguments and print help function if needed
7 sub print_use {
8  print "Usage: $0 NAME=SOURCE [NAME=SOURCE] ... OUTPUTHEAD\n\n";
9  print "NAME Name of the string variable to create in the header\n";
10  print "SOURCE Source file(s) that you would like to analyze\n";
11  print "OUTPUTHEAD Place where you would like to put the output header\n";
12  return "\nYou did something wrong with arguments\n";
13 }
14 
15 # Check presence of arguments
16 my $out_file = pop @ARGV or die print_use;
17 if (not @ARGV) {
18  die print_use;
19 }
20 
21 # Check that each source file exists
22 my %out_vars;
23 foreach my $arg (@ARGV) {
24  my @holder = split(/=/, $arg);
25 
26  if (not -e $holder[1]) {
27  print "File $holder[1] does not exist!\n\n";
28  die print_use;
29  }
30 
31  $out_vars{$holder[0]} = $holder[1];
32 }
33 
34 # Check first line of header
35 if (-e $out_file) {
36  open (my $handle, '<', $out_file);
37  chomp (my $first = <$handle>);
38  close $handle;
39  if ($first ne '#ifndef CROMBIE_BCALSTRINGS_H') {
40  print "First line of $out_file looks suspicious! I don't want to overwrite:\n";
41  print "$first\n";
42  die print_use;
43  }
44 }
45 
46 
47 # Start output file
48 open (my $out, '>', $out_file);
49 
50 print $out <<HEAD;
51 #ifndef CROMBIE_BCALSTRINGS_H
52 #define CROMBIE_BCALSTRINGS_H 1
53 
54 #include <string>
55 
56 namespace bcalstrings {
57 HEAD
58 
59 while ((my $varname, my $csvfile) = each(%out_vars)) {
60  print $out "\n const std::string $varname = \"\"\n";
61  open(my $input, '<', $csvfile);
62  while(<$input>) {
63  chomp;
64  s/\r//g;
65  s/"/\\"/g;
66  print $out " \"$_\\n\"\n";
67  }
68  print $out " ;\n";
69  close $input;
70 }
71 
72 print $out <<HEAD;
73 }
74 
75 #endif
76 HEAD
77 
78 close $out;