0

In current directory, All C header files (*.h) don't include the preprocessor macro

#ifndef FILENAME_H
#define FILENAME_H
...
#endif

, it's too tedious to add them in each header manually. How to do that automatically via python or shell?

BugRepairMan
  • 221
  • 2
  • 9

4 Answers4

2

Based on Cody's answer, I implemented the guardHeader.py:

  1 #!/usr/bin/python3
  2
  3 import glob,os,sys
  4
  5 global search_dir
  6
  7 def clearContent(pfile):
  8     pfile.seek(0)
  9     pfile.truncate()
 10
 11 def guard(fileName):
 12     file = open(fileName, 'r+')
 13     content = file.read()
 14
 15     fileNameUp = fileName.split(".")[0].upper() + '_H'
 16     guardBegin = '#ifndef ' + fileNameUp + '\n'    \
 17             '#define ' + fileNameUp + '\n\n'
 18     guardEnd = '\n#endif'
 19     newContent = guardBegin + content + guardEnd
 20
 21     clearContent(file)
 22     file.write(newContent)
 23
 24 if __name__ == '__main__':
 25     if len(sys.argv) == 1:
 26         print('Please provide a directory')
 27     else:
 28         search_dir = sys.argv[1]
 29
 30     # enter search directory
 31     os.chdir(search_dir)
 32
 33     for file in glob.glob("*.h"):
 34         guard(file)
BugRepairMan
  • 221
  • 2
  • 9
1

Assuming you are in a unix shell with find, cut, and sed available. You can get each filename using find, then use sed to change those files.

You can save the below script in a file called addifndef.sh.

  for fn in $(find . -maxdepth 1 -type f -regex '.*\.h$' | cut -f 2 -d '/'); 
  do 
     dn=$(echo $fn | cut -f 1 -d '.');
     sed -i  -e "1 i#ifndef ${dn}_H\n#define ${dn}_H" -e "$ a#endif" "$fn";  
  done

And, run that script as [prompt $] sh addifndef.sh in your shell prompt.

Alternatively you can directly use this command in command line.

For more info you have to look at man pages of find, cut, and sed.

sps
  • 2,720
  • 2
  • 19
  • 38
1

I'm going to second @kaylum's advice and also not give you the whole thing, but here is some pseudocode that might set you on the right path

for each file in the directory
   if filename doesn't end with .h
      continue
   open the file
   store its contents in a variable
   create the header guard by taking the filename, removing the '.', and replacing it with a '_'
   create new contents = headerGuard + contents + "\n#endif"
   write file back out to the same name

Each of these things should be answerable with a quick google/stack overflow search, and if you can't figure out any of those parts, a specific stack overflow question about that bit would be better suited for this site. And here is one link to a relevant question to get you started.

Community
  • 1
  • 1
Cody
  • 2,643
  • 2
  • 10
  • 24
0

Here’s a small script that gets the job done: https://gist.github.com/rumpeltux/9e4a3e6e8ccd4c0c86770ca4f2afc1d0

wget https://gist.github.com/rumpeltux/9e4a3e6e8ccd4c0c86770ca4f2afc1d0/raw/auto_add_headers.sh

find -name \*.h | xargs sh auto_add_headers.sh
rumpel
  • 7,870
  • 2
  • 38
  • 39