Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 7503

C/C++ • Little BASH file For Adding New Source File to Multi-File Project

$
0
0
Guys,

I've been fiddling with an on-going multi source file project for years now. Every now and again lifting a group of functions out of an old source file into a newer more appropriate file or just add new features and needing a new source file.

Every time I did this I needed to access all the current header files in the project and get them listed at the top of the new program source file. A recurring pain. Madness is doing the same thing over and over. I needed a BASH file but I needed to learn a lot more about BASH to get it to work.

Below is where I am and having just deployed it again, quite successfully I've copied it below:

Code:

#!/bin/bashechoecho "I attempt to create a new almost empty .cpp and .h file given an embryo path/filenname."echo "I expect a header or source file name only."echo "If base filename omits an extension both '.cpp' and '.h. files are created."echo "Else include either '.h', '.c' or '.cpp' for single output file."echoif [[ -z "$1" ]] ;thenecho "Oops! I need a 'path/filename{ <no ext> | <.c> | <.cpp> }'"echoecho "Done."exit 1fi#=======================================================================# Call as "write_include_clauses PathFilename Ext{ cpp | c }"write_include_clauses() {WriteFile="$1.$2"search_dir="$(dirname "${1}")" ;echo "Writing local directory Header files as '#includes <headerfiles.h' to $WriteFile"echo "// List of \"header.$3\" header file(s) from your target directory follow ..." >> $WriteFilefor entry in "$search_dir"/*doif [ "${entry##*.}" == "$3" ] ;thenHeader="$(basename "${entry}")"echo "#include \"$Header\"" >> $WriteFilefidoneecho >> $WriteFileecho "//============================================================" >> $WriteFileecho >> $WriteFile}#=======================================================================# Call as "write_header_prologue PathFilename Ext{ h | hpp }"write_header_prologue() {Define="$(basename "${1}")"Time=$(date +%T)Date=$(date +%F\(%A\))File="$(basename "${1}")"echo "Writing Header Prologue to $1.$2"echo "//============================================================" > $1.$2echo "// Auto created Header file follows" >> $1.$2echo "// File: $Define.$2" >> $1.$2echo "// Created: $Date at $Time" >> $1.$2echo "// Use: '#include \"$File.$3\"'" >> $1.$2echo "//============================================================" >> $1.$2echo >> $1.$2echo "#ifndef ${Define^^}" >> $1.$2echo >> $1.$2echo "#define ${Define^^}" >> $1.$2echo >> $1.$2echo "//============================================================" >> $1.$2echo >> $1.$2}#=======================================================================# Call as "write_header_epilogue PathFilename Ext{ h | hpp }"write_header_epilogue() {Define="$(basename "${1}")"echo "Writing Header EPilogue to $1.$2"echo >> $1.$2echo >> $1.$2echo "#endif   // end #ifndef ${Define^^}" >> $1.$2echo >> $1.$2echo "//============================================================" >> $1.$2echo "// End $1.$2" >> $1.$2echo "//============================================================" >> $1.$2echo >> $1.$2}#=======================================================================# Call as "write_source_prologue PathFilename Ext{ c | cpp }"write_source_prologue() {Define="$(basename "${1}")"Time=$(date +%T)Date=$(date +%F\(%A\))echo "Writing Source Prologue to $1.$2"echo "//============================================================" > $1.$2echo "// Auto created Source file follows" >> $1.$2echo "// File: $Define.$2" >> $1.$2echo "// Created: $Date at $Time" >> $1.$2echo "//============================================================" >> $1.$2echo >> $1.$2echo "#include <stdio.h>" >> $1.$2echo "#include <stdlib.h>" >> $1.$2echo "#include <string.h>" >> $1.$2echo "#include <stdint.h>" >> $1.$2echo >> $1.$2echo "//============================================================" >> $1.$2echo >> $1.$2}#=======================================================================# Call as "write_source_epilogue PathFilename Ext{ c | cpp }"write_source_epilogue() {echo "Writing Source Epilogue to $1.$2"echo >> $1.$2echo "//============================================================" >> $1.$2echo "// End $1.$2." >> $1.$2echo "//============================================================" >> $1.$2echo >> $1.$2}#=======================================================================#=======================================================================#filename=$(basename -- "$1")filename=$1extension="${filename##*.}"filename="${filename%.*}"#echo "Filename '$filename', Extension '$extension'"if [ $extension == "h" ] ;thenecho "Writing embryo Header File: '$filename.h'"write_header_prologue $filename "h" "h"write_header_epilogue $filename "h"elif [ $extension == "hpp" ] ;thenecho "Writing embryo Header File: '$filename.hpp'"write_header_prologue $filename "hpp" "hpp"write_header_epilogue $filename "hpp"elif [ $extension == "c" ] ;thenecho "Writing embryo C Source File: '$filename.c'"write_source_prologue $filename "c"write_include_clauses $filename "c" "h"write_source_epilogue $filename "c"elif [ $extension == "cpp" ] ;thenecho "Writing embryo CPP Source File: '$filename.cpp'"write_source_prologue $filename "cpp"write_include_clauses $filename "cpp" "hpp"write_source_epilogue $filename "cpp"elseecho "No file extension specified File: Creating both '$filename.cpp' and '$filename.h/hpp' files"write_header_prologue $filename "h" "h"write_header_epilogue $filename "h"write_source_prologue $filename "cpp"write_include_clauses $filename "cpp" "h"write_include_clauses $filename "cpp" "hpp"write_source_epilogue $filename "cpp"fiecho "Done."
I run it as :

Code:

./new_C_Source_file.sh src/Offhook_Controller
But if used without parameters it offers a few reminders on usage.

There's always a better way of doing this stuff so feel free to improve/build on it.

Statistics: Posted by RaspISteve — Mon Feb 02, 2026 8:22 pm — Replies 0 — Views 19



Viewing all articles
Browse latest Browse all 7503

Trending Articles