h2pair.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. # http://tipstricks.itmatrix.eu/?p=305
  3. tac () {
  4. awk '1 { last = NR; line[last] = $0; } END { for (i = last; i > 0; i--) { print line[i]; } }'
  5. }
  6. function replace_inline
  7. {
  8. result="$1"
  9. # replace inlines with IGL_INLINEs, first those that begin lines
  10. result=`echo "$result" | sed -e 's/^inline/IGL_INLINE/g'`
  11. # then also those that begin words
  12. result=`echo "$result" | sed -e 's/ inline/ IGL_INLINE/g'`
  13. echo "$result"
  14. }
  15. # Convert well organized .h file to a .h/.cpp pair
  16. for i in $*
  17. do
  18. # Only operate on .h files
  19. filename=$(basename $i)
  20. extension=${filename##*.}
  21. filename=${filename%.*}
  22. FILENAME=`echo $filename | tr '[a-z]' '[A-Z]'`
  23. if [ ! "$extension" = "h" ];
  24. then
  25. echo "Skipping $i because it is not a .h file"
  26. continue;
  27. fi
  28. if ! grep -q "^\/\/ Implementation *$" "$i"
  29. then
  30. echo "Skipping $i because it does not match ^\/\/ Implementation *$ "
  31. continue;
  32. fi
  33. if [[ `grep -c "^\#endif" "$i"` > 1 ]];
  34. then
  35. echo "Warning $i contains multple matches to ^#endif"
  36. fi
  37. before=`sed -n '/^\/\/ Implementation$/q;p' "$i"`;
  38. if ! echo "$before" | grep -q "^\#ifndef IGL_${FILENAME}_H"
  39. then
  40. echo "Skipping $i because it does not match ^#ifndef IGL_${FILENAME}_H "
  41. continue;
  42. fi
  43. if ! echo "$before" | grep -q "^\#define IGL_${FILENAME}_H"
  44. then
  45. echo "Skipping $i because it does not match ^#define IGL_${FILENAME}_H "
  46. continue;
  47. fi
  48. before=`replace_inline "$before"`
  49. # prepend #include "igl_inline.h"
  50. before=`echo "$before" | sed -e 's/^\(#define IGL_'${FILENAME}'_H\)/\1\'$'\n''#include \"igl_inline.h\"'/`
  51. after=`sed '1,/^\/\/ Implementation$/d' $i`;
  52. after=`replace_inline "$after"`
  53. # reverse file
  54. after=`echo "$after" | tac`
  55. # everything after first (last) endif
  56. after=`echo "
  57. $after" | sed '1,/endif/d'`;
  58. # reverse file
  59. after=`echo "$after" | tac`
  60. # append empty template code
  61. if grep -q "template" "$i"
  62. then
  63. after=`echo "$after
  64. #ifdef IGL_STATIC_LIBRARY
  65. // Explicit template specialization
  66. #endif"`
  67. fi
  68. echo "$before
  69. #ifndef IGL_STATIC_LIBRARY
  70. # include \"$filename.cpp\"
  71. #endif
  72. #endif"> "$filename".h
  73. echo "#include \"$filename.h\"
  74. $after"> "$filename".cpp
  75. done