| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Copyright 2001 Google Inc. |
| 4 |
# All rights reserved. |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are |
| 8 |
# met: |
| 9 |
# |
| 10 |
# * Redistributions of source code must retain the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer. |
| 12 |
# * Redistributions in binary form must reproduce the above |
| 13 |
# copyright notice, this list of conditions and the following disclaimer |
| 14 |
# in the documentation and/or other materials provided with the |
| 15 |
# distribution. |
| 16 |
# * Neither the name of Google Inc. nor the names of its |
| 17 |
# contributors may be used to endorse or promote products derived from |
| 18 |
# this software without specific prior written permission. |
| 19 |
# |
| 20 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
|
| 32 |
# --- |
| 33 |
# Author: Andrew Fikes |
| 34 |
# |
| 35 |
# Tool to convert a template file (.tpl) into a C++ header |
| 36 |
# file with a const string defining the same template. This string |
| 37 |
# can then be used to create/retrieve a template using |
| 38 |
# Template::StringToTemplate()/StringToTemplateCache() (see template.h). |
| 39 |
# |
| 40 |
# Usage: template-converter <template_name> < infile > outfile |
| 41 |
# |
| 42 |
# template_name is the name of the variable we export. A good choice |
| 43 |
# is to pass in the outfile name. To make that easier, we treat |
| 44 |
# <template_name> as a pathname, and take the basename, strip the |
| 45 |
# suffix if it's .h, and sanitize the rest of the name to be a legal |
| 46 |
# C variable name. |
| 47 |
# MOE:insert # NOTE: See doc/index.html for a general description of Google ctemplate. |
| 48 |
|
| 49 |
|
| 50 |
# Store the input argv. |
| 51 |
my $argv = join(" ", $0, @ARGV); |
| 52 |
|
| 53 |
# Open template file |
| 54 |
(my $template_name = shift) || usage("Need to specify template variable name."); |
| 55 |
|
| 56 |
# If a second argument is supplied, treat it as an input filename. |
| 57 |
if (my $infile = shift) { |
| 58 |
open(STDIN, "<", $infile) or usage("Can't open $infile for reading."); |
| 59 |
} |
| 60 |
|
| 61 |
# If a third argument is supplied, treat it as an output filename. |
| 62 |
if (my $outfile = shift) { |
| 63 |
open(STDOUT, ">", $outfile) or usage("Can't open $outfile for writing."); |
| 64 |
} |
| 65 |
|
| 66 |
# Get base name of template file |
| 67 |
$base_name = $template_name; |
| 68 |
$base_name =~ s|^.*/([^/]*)$|$1|; # Strip out directory name |
| 69 |
$base_name =~ s|\.h$||; # Strip out suffix, if it's .h |
| 70 |
$base_name =~ tr|A-Za-z0-9_|_|c; # Sanitize name to remove non-letters/nums |
| 71 |
|
| 72 |
# Print header |
| 73 |
print "// This file automatically generated by template-converter:\n"; |
| 74 |
print "// $argv\n"; |
| 75 |
print "//\n"; |
| 76 |
print "// DO NOT EDIT!\n\n"; |
| 77 |
print "#ifndef " . uc($base_name) . "_H_\n"; |
| 78 |
print "#define " . uc($base_name) . "_H_\n\n"; |
| 79 |
print "#include <string>\n\n"; |
| 80 |
|
| 81 |
# Read in template file and print template as a string |
| 82 |
# MOE:begin_strip |
| 83 |
print "const string ${base_name} (\n"; |
| 84 |
# MOE:end_strip_and_replace print "const std::string ${base_name} (\n"; |
| 85 |
while (<>) { |
| 86 |
chomp; |
| 87 |
my $escaped_line = escape_line($_); |
| 88 |
print "\"$escaped_line\\n\"\n"; |
| 89 |
} |
| 90 |
print ");\n\n"; |
| 91 |
|
| 92 |
# Print footer and exit |
| 93 |
print "#endif /* " . uc($base_name) . "_H_ */\n"; |
| 94 |
exit(0); |
| 95 |
|
| 96 |
# Prints usage message |
| 97 |
sub usage { |
| 98 |
my $msg = shift; |
| 99 |
print STDERR "\n$msg\n"; |
| 100 |
print STDERR "Usage: template-converter <template-varname>", |
| 101 |
" [infile] [outfile]\n\n"; |
| 102 |
exit(1); |
| 103 |
} |
| 104 |
|
| 105 |
# Escapes line (adds a '\' to quotes and possible control characters) |
| 106 |
sub escape_line { |
| 107 |
(my $line) = (@_); |
| 108 |
$line =~ s|\\|\\\\|g; |
| 109 |
$line =~ s|\"|\\"|g; |
| 110 |
return $line; |
| 111 |
} |