diff options
author | Nathan Binkert <nate@binkert.org> | 2009-05-11 10:38:43 -0700 |
---|---|---|
committer | Nathan Binkert <nate@binkert.org> | 2009-05-11 10:38:43 -0700 |
commit | 2f30950143cc70bc42a3c8a4111d7cf8198ec881 (patch) | |
tree | 708f6c22edb3c6feb31dd82866c26623a5329580 /src/mem/gems_common/ioutil/embedtext.py | |
parent | c70241810d4e4f523f173c1646b008dc40faad8e (diff) | |
download | gem5-2f30950143cc70bc42a3c8a4111d7cf8198ec881.tar.xz |
ruby: Import ruby and slicc from GEMS
We eventually plan to replace the m5 cache hierarchy with the GEMS
hierarchy, but for now we will make both live alongside eachother.
Diffstat (limited to 'src/mem/gems_common/ioutil/embedtext.py')
-rw-r--r-- | src/mem/gems_common/ioutil/embedtext.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/mem/gems_common/ioutil/embedtext.py b/src/mem/gems_common/ioutil/embedtext.py new file mode 100644 index 000000000..64e1c97f3 --- /dev/null +++ b/src/mem/gems_common/ioutil/embedtext.py @@ -0,0 +1,54 @@ + +import sys + +#--------------------------------------------------------------------------- + +class embedText: + """ + embedText converts a text file into a file that can be embedded in C + using an #include statement, that defines a \"const char *\" pointing + to the same text. + + This is useful to embed scripts and configuration files in object files. + """ + def __init__(self, filename): + self.filename = filename + self.escape = [ "\'", "\"", "\\", "\?" ] + + def write(self, outputfile, varname): + # reads the text file in, line by line, converting it to a C string + fin = open( self.filename, 'r' ) + fout= open( outputfile, 'w' ) + fout.write("static const char *%s =\n" % varname); + l = " " + while l != "": + l = fin.readline() + + # add escape sequences for the characters in escape + fout.write("\"") + for char in l: + if char == "\n": + break + if char in self.escape: + fout.write( "\\" ) + fout.write( char ) + else: + fout.write( char ) + fout.write("\\n\"\n"); + fout.write(";\n"); + fin.close() + fout.close() + +#--------------------------------------------------------------------------- + +if __name__ == "__main__": + if len(sys.argv) != 4: + print len(sys.argv) + print "usage:", sys.argv[0], " input-file output-file varname" + sys.exit(1) + inputfile = sys.argv[1] + outputfile = sys.argv[2] + varname = sys.argv[3] + print "generating embedded text file: %s from %s\n" % (outputfile, inputfile) + inc = embedText( inputfile ) + inc.write( outputfile, varname ) |