[vlc-commits] commit: Parser: generate the header file (not perfect but not too bad). ( Rémi Duraffort )

git at videolan.org git at videolan.org
Sun Apr 25 15:17:09 CEST 2010


vlc/libvlcpp | branch: master | Rémi Duraffort <ivoire at videolan.org> | Sun Apr 25 15:15:15 2010 +0200| [8e99e0e2101736bad7835cc253967b0020e6d1c5] | committer: Rémi Duraffort 

Parser: generate the header file (not perfect but not too bad).

> http://git.videolan.org/gitweb.cgi/vlc/libvlcpp.git/?a=commit;h=8e99e0e2101736bad7835cc253967b0020e6d1c5
---

 parser.py |   41 +++++++++++++++++++++++++++++++++++++++--
 1 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/parser.py b/parser.py
index 3960b30..6d338dc 100755
--- a/parser.py
+++ b/parser.py
@@ -2,7 +2,39 @@
 import sys
 
 def parse_headers(base_path):
-    parse(base_path + 'libvlc_media.h', 'libvlc_media_t *p_md')
+    dico = parse(base_path + 'libvlc_media.h', 'libvlc_media_t *p_md')
+    generate_class('LIBVLCPP_MEDIA_HPP', 'libvlc_media', 'Media', 'media', dico)
+
+
+def generate_class(define, base_name, class_name, filename, dico):
+    f_hpp = open(filename + '.hpp', 'w')
+    f_cpp = open(filename + '.cpp', 'w')
+
+    # Headers
+    f_cpp.write('#include "' + filename + '.hpp"')
+    f_cpp.write('\n\nusing namespace libvlc;\n\n')
+    f_hpp.write('#ifndef ' + define + '\n#define ' + define)
+    f_hpp.write('\n\nnamespace libvlc\n{\n\nclass ' + class_name + '\n{\npublic:\n')
+
+    for k,v in dico.items():
+        f_hpp.write('    ' + v[2].replace('\n', '\n    '))
+        f_hpp.write(v[0] + ' ' + k.replace(base_name + '_', '', 1) + v[1] + ';\n\n')
+
+    # Footers
+    f_hpp.write('};\n\n};\n\n#endif // ' + define + '\n')
+    f_hpp.close()
+    f_cpp.close()
+
+
+def parse_declaration(declaration):
+    delim = declaration.find('(')
+    space = declaration.rfind(' ', 0, delim) + 1
+    if(declaration[space] == '*'):
+        space = space + 1
+    else:
+        space = space - 1
+
+    return (declaration[0:space], declaration[space:delim].lstrip(), declaration[delim:])
 
 def parse(filename, removed_arg):
     # Open the file
@@ -11,6 +43,7 @@ def parse(filename, removed_arg):
     # Usefull informations
     doxygen = ''
     declaration = ''
+    dico = {}
 
     # Read each line of the file
     line = f_header.readline()
@@ -39,10 +72,14 @@ def parse(filename, removed_arg):
                 declaration = declaration.replace('( ' + removed_arg + ' )', '()')
             else:
                 declaration = declaration.replace('( ' + removed_arg + ', ', '( ')
-            print declaration
+            parsed = parse_declaration(declaration)
+            dico[parsed[1]] = (parsed[0], parsed[2], doxygen)
 
         line = f_header.readline()
 
+    f_header.close()
+    return dico
+
 # Begining of the script
 if __name__ == "__main__":
     if(len(sys.argv) != 2):



More information about the vlc-commits mailing list