diff --git a/.gitignore b/.gitignore index bce41f311..140db3a05 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ p /*.txt .idea/* *.ipynb_checkpoint* +tools/.format/* diff --git a/src/Factory.h b/src/Factory.h index bead2abcc..c356dd8b9 100644 --- a/src/Factory.h +++ b/src/Factory.h @@ -47,7 +47,7 @@ class Factory { ret = (*it)->Produce(input); if (ret) return ret; } - return NULL; + return nullptr; }; // Template class for registration of the worker functions template diff --git a/src/Geometry.h b/src/Geometry.h index 408f44129..9e1f99b86 100644 --- a/src/Geometry.h +++ b/src/Geometry.h @@ -2,6 +2,7 @@ #define GEOMETRY_H #include "unit.h" +#include "pugixml.hpp" #include /// STL triangle structure #ifdef _WIN32 diff --git a/src/Region.h b/src/Region.h index 4ab72f2df..5b151503b 100644 --- a/src/Region.h +++ b/src/Region.h @@ -1,6 +1,7 @@ #ifndef REGION_H #include +#include "cross.h" class lbRegion { public: int dx,dy,dz; diff --git a/src/SolidGrid.h b/src/SolidGrid.h index fa345b74d..893517b27 100644 --- a/src/SolidGrid.h +++ b/src/SolidGrid.h @@ -7,6 +7,7 @@ #include #include #include +#include "types.h" typedef int gr_addr_t; diff --git a/src/SolidTree.h b/src/SolidTree.h index 079ecb8bf..fbeb9b28b 100644 --- a/src/SolidTree.h +++ b/src/SolidTree.h @@ -7,10 +7,7 @@ #include #include #include - -typedef char tr_flag_t; -typedef int tr_addr_t; -typedef double tr_real_t; +#include "types.h" struct tr_elem { tr_flag_t flag; diff --git a/src/def.h b/src/def.h index 20bf73026..ff75c04bd 100644 --- a/src/def.h +++ b/src/def.h @@ -1,2 +1,3 @@ +#include "pugixml.hpp" extern pugi::xml_document xml_def; int xml_def_init(); diff --git a/src/types.h b/src/types.h index 84ebeb1e9..486f6a99e 100644 --- a/src/types.h +++ b/src/types.h @@ -28,6 +28,11 @@ #define CUT_MAX 65000 #define CUT_LEN(x__) (0.005f * (x__)) + typedef char tr_flag_t; + typedef int tr_addr_t; + typedef double tr_real_t; + + /* struct vector_t { real_t x,y,z; diff --git a/tools/.clang-format b/tools/.clang-format new file mode 100644 index 000000000..848316a23 --- /dev/null +++ b/tools/.clang-format @@ -0,0 +1,12 @@ +BasedOnStyle: Google +ColumnLimit: 160 +IndentWidth: 4 +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: InlineOnly +AllowShortIfStatementsOnASingleLine: Always +DerivePointerAlignment: false +PointerAlignment: Left +BinPackArguments: false +BinPackParameters: false +ReflowComments: false diff --git a/tools/format.sh b/tools/format.sh new file mode 100755 index 000000000..e4bb8ff6f --- /dev/null +++ b/tools/format.sh @@ -0,0 +1,144 @@ +#!/bin/bash + +set -o pipefail + +PP=$(dirname $0) +FORMATFILE="$PP/.clang-format" +PRINTSKIP=true + +function format { + clang-format --style=file:$FORMATFILE \ + | sed -E 's/for[[:blank:]]*[(]([[:alpha:]_]*[[:blank:]]|)[[:blank:]]*([[:alnum:]_]+)[[:blank:]]*=[[:blank:]]*([[:alnum:]_]+)[[:blank:]]*;[[:blank:]]*([[:alnum:]_]+)[[:blank:]]*([<=]*)[[:blank:]]*([[:alnum:]_]+)[[:blank:]]*;[[:blank:]]*([[:alnum:]_]+)[+][+][[:blank:]]*\)/for (\1\2=\3; \4\5\6; \7++)/g' \ + | sed -E 's| ([*/]) |\1|g' +} + +function formatRT { + R -s -e 'rtemplate::RTtokenize()' | format | R -s -e 'rtemplate::RTtokenize(inv=TRUE)' +} + +function format_sel { + if [[ -z "$1" || "$1" =~ \.[rR][tT]$ ]] + then + echo formatRT + else + echo format + fi +} + +function format_to { + F="$(format_sel "$1")" + if test -z "$1" || test -z "$2" + then + echo "Something went wrong in format_to" + exit 5 + fi + mkdir -p $PP/.format + NAMESUM=$(echo "$1 $2" | sha256sum | cut -c 1-30) + SUMFILE="$PP/.format/$NAMESUM" + if test -f "$SUMFILE" + then + if sha256sum --status --check "$SUMFILE" + then + $PRINTSKIP && echo "Skipping $1" + return 0 + fi + fi + if [[ "$1" == "$2" ]] + then + echo "Running $F on $1" + else + echo "Running $F on $1 -> $2" + fi + cat "$1" | $F >tmp + mv tmp "$2" + sha256sum "$1" "$2" "$FORMATFILE" >"$SUMFILE" +} + +OUTFILE="" +OUTSAME=false +while test -n "$1" +do + case "$1" in + --help) + echo "" + echo "$0 [--all] [-o output] [-x] [files]" + echo "" + echo " -o (--output) OUTPUT : put the formated output in OUTPUT" + echo " -x (--overwrite) : put the formated output in the same file" + echo " --all [FROM_DIR] [TO_DIR] : format all source files in DIR" + echo "" + exit 0; + ;; + -o|--output) + shift + OUTFILE="$1" + ;; + -x|--overwrite) + OUTSAME=true + ;; + -a|--all) + shift + FROM_DIR="src" + if ! test -z "$1" + then + if ! test -d "$1" + then + echo "$1: not a directory" + exit 3 + fi + FROM_DIR="$1" + shift + fi + TO_DIR="$FROM_DIR" + if ! test -z "$1" + then + if ! test -d "$1" + then + echo "$1: not a directory" + exit 3 + fi + TO_DIR="$1" + shift + fi + if test "$FROM_DIR" == "$TO_DIR" && ! $OUTSAME + then + echo "Trying to format all file into the same directory. If you want to overwrite say '-x'" + exit 4 + fi + PRINTSKIP=false + find "$FROM_DIR" | grep -E '[.](cpp|h|hpp)([.]Rt|)$' | while read i + do + if test "$FROM_DIR" == "$TO_DIR" + then + j="$i" + else + j="$TO_DIR/${i#$FROM_DIR}" + fi + format_to "$i" "$j" + done + ;; + -p|--pipe) + formatRT + ;; + -*) + echo "Uknown option $1" + exit 1; + ;; + *) + INFILE="$1" + if $OUTSAME && test -z "$OUTFILE" + then + OUTFILE="$INFILE" + fi + if test -z $OUTFILE + then + cat "$INFILE" | $(format_sel "$INFILE") + else + format_to "$INFILE" "$OUTFILE" + OUTFILE="" + fi + esac + shift +done + +exit 0