Find And Replace File Names To C Standard
I have a project shared by a friend which includes headers that have case conflicts (eg: #include 'xyz.h' but the actual file is Xyx.h). How should I solve this? I decided to wri
Solution 1:
One single bullet proof solution:
- choose a name convention - all lower case is probably the most common one
- rename all include files to their lower case equivalent - do nothing if they are already lower case
- consistently check all source files and ensure that they actually include the lower case name - this should be done by hand because you do not want to process a litteral string containing
#include
That being said, if the number of files is small, and if files are not too complex, this can possibly be automated. But my opinion is that the gain of time is not worth the development of a tool and the risk of error in it.
Solution 2:
This may be less challenging that I initially thought.
from glob import glob as gg
# read in list of .h files and build dictionary
hfiles = dict([(r, r.lower) for r in gg('srcDir/*.h')])
# read in c filewithopen('srcDir/cfile.c', 'r') as tmp:
cfile = tmp.read().split('\n')
# make include replacements (assuming NO text in file prior to include statments...adjust as needed)for r in arange(len(cfile):
if ('# include'in cfile[r]) and (cfile[r].split()[-1] in hfiles):
key = cfile[r].split()[-1].lower()
cfile[r] = '# include ' + hfiles[key]
# now right out corrected c filewithopen('dst/new_c_file.c', 'w') as fid:
fid.write('\n'.join(cfile))
It's not the prettiest code, and I have not tested it (better to NOT overwrite your files until you have tested this), but the general method should work and be quite flexible.
Post a Comment for "Find And Replace File Names To C Standard"