Tool to convert .Glade (or xml) file to C source

不想你离开。 提交于 2019-12-06 06:49:48

问题


I am looking for tool that can convert .Glade (or xml) file to C source.
I have tried g2c (Glade To C Translator) but i am looking for windows binary.

Any one does know any good tool for window.

Thanks,
PP.


回答1:


glade2 works for me. though if you are using new glade3 features it won't help you..




回答2:


You don't need a tool. Just write a script in your favorite scripting language to format your glade file as a C string literal:

For example, let's call it glade_file.c:

const gchar *my_glade_file = 
    "<interface>"
        "<object class=\"GtkDialog\">"
            "<et-cetera />"
        "</object>"
    "</interface>";

Compile glade_file.c into your program, then do this when you build your interface:

extern const gchar *my_glade_file;
result = gtk_builder_add_from_string(builder, my_glade_file, -1, &error);



回答3:


I had to write a little python script to do this, hope this helps others:

import xml.dom.minidom
class XmlWriter:
    def __init__(self):
        self.snippets = []
    def write(self, data):
        if data.isspace(): return
        self.snippets.append(data)
    def __str__(self):
        return ''.join(self.snippets)

if __name__ == "__main__":
    writer = XmlWriter()
    xml = xml.dom.minidom.parse("example.glade")
    xml.writexml(writer)
    strippedXml = ("%s" % (writer)).replace('"', '\\"')

    byteCount = len(strippedXml)
    baseOffset=0
    stripSize=64

    output = open("example.h", 'w')
    output.write("static const char gladestring [] = \n{\n")
    while baseOffset < byteCount:
        skipTrailingQuote = 0
        if baseOffset+stripSize < byteCount and strippedXml[baseOffset+stripSize] == '"':
            skipTrailingQuote = 1
        output.write('  "%s"\n' % (strippedXml[baseOffset:baseOffset+stripSize+skipTrailingQuote]))
        baseOffset += stripSize + skipTrailingQuote

    output.write("};\n")
    output.close()


来源:https://stackoverflow.com/questions/2787202/tool-to-convert-glade-or-xml-file-to-c-source

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!