Skip to content

Instantly share code, notes, and snippets.

@diosmosis
Last active December 14, 2015 10:39
Show Gist options
  • Save diosmosis/5073744 to your computer and use it in GitHub Desktop.
Save diosmosis/5073744 to your computer and use it in GitHub Desktop.
perl script to create C++ header files using boost-style header layout. uses following input: project-name path-to-txt-file each line of path-to-txt-file is a path to a header, ie (boost\spirit\whatever\dooble.hpp). TODO: - make it ouput source files and create the config.hpp file (w/ DECL-ing) - only if its not header only - make it create jamf…
#!/usr/bin/perl
# Declare the subroutines
sub trim($);
sub ltrim($);
sub rtrim($);
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Left trim function to remove leading whitespace
sub ltrim($)
{
my $string = shift;
$string =~ s/^\s+//;
return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim($)
{
my $string = shift;
$string =~ s/\s+$//;
return $string;
}
# get command line args
$project = $ARGV[0];
$path = $ARGV[1];
# read file
open INPUT, "<$path" or die $!;
@lines = <INPUT>;
close INPUT;
chomp(@lines);
# for each line, output the file
foreach (@lines)
{
$hpppath = trim($_);
if (length($hpppath) != 0)
{
# get path array and file name
@patharray = split(/[\/\\]/, $hpppath);
$filename = pop(@patharray);
@temp = split(/\./, $filename);
$filename_no_ext = uc($temp[0]);
# go to the specified directory (create if it doesn't exist)
$backtrack = '';
foreach (@patharray)
{
if (!(-d $_))
{
mkdir $_;
}
chdir $_;
$backtrack = $backtrack . "..\\";
}
# create the file text
$UPATH = uc(join('_', @patharray));
$template =
"#if !defined( $UPATH\_$filename_no_ext\_HPP )
#define $UPATH\_$filename_no_ext\_HPP
#include <$project/config.hpp>
namespace $project
{
}
#endif // #if !defined( $UPATH\_$filename_no_ext\_HPP )";
# create file w/ template hpp file
open OUTPUT, ">$filename";
print OUTPUT $template;
close OUTPUT;
# backtrack to root of project includes
chdir $backtrack;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment