1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#!/bin/sh
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | www.openfoam.com
# \\/ M anipulation |
#------------------------------------------------------------------------------
# Copyright (C) 2011-2016 OpenFOAM Foundation
# Copyright (C) 2018-2019 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM.
#
# OpenFOAM is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
#
# Script
# foamUpdateCaseFileHeader
#
# Description
# Updates the header of application files and removes consecutive
# blank lines.
#
# Uses the API value by default, but the version can also be specified
# with the -version option.
#
#------------------------------------------------------------------------------
usage() {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat<<USAGE
Usage: ${0##*/} [OPTION] <file1> ... <fileN>
options:
-version=VER Specifies version for header (default: $FOAM_API)
-h | -help Print the usage
Updates the header of application files and removes consecutive blank lines.
By default, writes current OpenFOAM API number version in the header.
An alternative version can be specified with the -version option.
USAGE
exit 1
}
#------------------------------------------------------------------------------
# parse options
while [ "$#" -gt 0 ]
do
case "$1" in
-h | -help*)
usage
;;
-version=*)
version="${1#*=}"
;;
-v | -version)
[ "$#" -ge 2 ] || usage "'$1' option requires an argument"
version="$2"
shift
;;
-*)
usage "unknown option: '$1'"
;;
*)
break
;;
esac
shift
done
[ $# -ge 1 ] || usage
#------------------------------------------------------------------------------
# Constant width for version.
# Default to v{FOAM_API}, project version or OPENFOAM
if [ -z "$version" ]
then
if [ -n "$FOAM_API" ]
then
version="v$FOAM_API"
else
version="$WM_PROJECT_VERSION"
fi
fi
version=$(printf %-38s "${version:-OPENFOAM}")
#------------------------------------------------------------------------------
printBanner()
{
cat<<BANNER
/*--------------------------------*- C++ -*----------------------------------*\\
| ========= | |
| \\\\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\\\ / O peration | Version: $version|
| \\\\ / A nd | Website: www.openfoam.com |
| \\\\/ M anipulation | |
\\*---------------------------------------------------------------------------*/
BANNER
}
printFoamFile()
{
echo "FoamFile"
echo "{"
echo " version 2.0;"
echo " format $1;"
echo " class $2;"
echo " object $3;"
echo "}"
}
printFoamFileM4()
{
echo "FoamFile"
echo "{"
echo " version 2.0;"
echo " \`format' $1;"
echo " class $2;"
echo " object $3;"
echo "}"
}
#
# Extract attribute '$1' from file '$2'
#
FoamFileAttribute()
{
sed -n -e 's/[ ;]*$//' -e "s/^ *$1 *//p" "$2"
}
#
# Extract attribute `format' (m4) from file '$1'
#
FoamFileFormatM4()
{
sed -n -e 's/[ ;]*$//' -e "s/^ *.format' *//p" "$1"
}
#
# Main
#
tmpFile="FoamFile.tmp$$"
for caseFile
do
if grep -q FoamFile "$caseFile" 2>/dev/null
then
echo "Updating case file: $caseFile"
sed -n '/FoamFile/,/}/p' "$caseFile" > "$tmpFile"
format=$(FoamFileAttribute format "$tmpFile")
class=$(FoamFileAttribute class "$tmpFile")
object=$(FoamFileAttribute object "$tmpFile")
# extract note? - needs special handling
unset m4format note
if [ -n "$format" ]
then
printBanner > "$tmpFile"
printFoamFile "$format" "$class" "$object" >> "$tmpFile"
else
# No format? Could be an m4 file with `format'
format=$(FoamFileFormatM4 "$tmpFile")
if [ -n "$format" ]
then
printBanner > "$tmpFile"
printFoamFileM4 "$format" "$class" "$object" >> "$tmpFile"
else
echo "Missing format: $caseFile" 1>&2
continue
fi
fi
sed '1,/}/d' "$caseFile" | sed '/./,/^$/!d' >> "$tmpFile"
# Use cat to avoid removing/replace soft-links
[ -s "$tmpFile" ] && cat "$tmpFile" >| "$caseFile"
rm -f "$tmpFile"
if [ "$format" = binary ]
then
echo "Changed binary file? $caseFile" 1>&2
fi
else
echo "Invalid case file: $caseFile" 1>&2
fi
done
#------------------------------------------------------------------------------
|