Awk|Bash - C++ automatic generating of new header and implementation file.
Home page Ubuntu C++ Hardware HTML i javascript Java Software SQL

Awk|Bash - C++ automatic generating of new header and implementation file.

Added: 2012-04-27

The generation of certain classes of language C++ as header and implementation from script, it is good, when you work in terminal without possibility use IDE.

The following example generates file header and implementation, whose name defines the user. When you run script, you must insert parameters - name of class.

If you generate the class often in Terminal, create alias (Polish language).

drukowanie kodu zaznaczanie kodu

#!/bin/bash

echo The script that creates the header file (*.h) and source (*.cpp).

if [ "$1" = "" ]; then
	echo "Paramaters not exist!!!";
	exit 1;
fi


if [ -f $1".h" ] || [ -f $1".cpp" ]; then
	echo "File exist!!! Overwrite? [yes]";
	read option;
	if [ "$option" != "yes" ]; then
		exit 0;
	fi
fi


awk -vn=$1 'BEGIN{
	hN = n".h";
	print "Creating header: "n".h ...";
	print "#pragma once \n" > hN;
	print "class "n" {" > hN;
	print "public:" > hN;
	print "	"n"();" > hN;
	print "	~"n"();" > hN;
	print "};" >> hN;

	cN = n".cpp";
	print "Creating file implementation: "n".cpp ...";
	print "#include \""n".h\"\n" > cN;
	print n"::"n"(){" >> cN;
	print "}\n" >> cN;
	print n"::~"n"(){" >> cN;
	print "}" >> cN;
}'


exit 0;