Reuse define statement from .h file in C# code

前端 未结 7 1888
广开言路
广开言路 2021-01-18 14:08

I have C++ project (VS2005) which includes header file with version number in #define directive. Now I need to include exactly the same number in twin C# project. What is th

7条回答
  •  [愿得一人]
    2021-01-18 14:29

    I wrote a python script that converts #define FOO "bar" into something usable in C# and I'm using it in a pre-build step in my C# project. It works.

    # translate the #defines in messages.h file into consts in MessagesDotH.cs
    
    import re
    import os
    import stat
    
    def convert_h_to_cs(fin, fout):
        for line in fin:
            m = re.match(r"^#define (.*) \"(.*)\"", line)
            if m != None:
                if m.group() != None:
                    fout.write( "public const string " \
                    + m.group(1) \
                    + " = \"" \
                    + m.group(2) \
                    + "\";\n" )
            if re.match(r"^//", line) != None:
                fout.write(line)
    
    fin = open ('..\common_cpp\messages.h')
    fout = open ('..\user_setup\MessagesDotH.cs.tmp','w')
    
    fout.write( 'using System;\n' )
    fout.write( 'namespace xrisk { class MessagesDotH {\n' )
    
    convert_h_to_cs(fin, fout)
    
    fout.write( '}}' )
    
    fout.close()
    
    s1 = open('..\user_setup\MessagesDotH.cs.tmp').read()
    
    s2 = open('..\user_setup\MessagesDotH.cs').read()
    
    if s1 != s2:
        os.chmod('..\user_setup\MessagesDotH.cs', stat.S_IWRITE)
        print 'deleting old MessagesDotH.cs'
        os.remove('..\user_setup\MessagesDotH.cs')
        print 'remaming tmp to MessagesDotH.cs'
        os.rename('..\user_setup\MessagesDotH.cs.tmp','..\user_setup\MessagesDotH.cs')
    else:
        print 'no differences.  using same MessagesDotH.cs'
    

提交回复
热议问题