windows 将 .Glade(或 xml)文件转换为 C 源代码的工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2787202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:25:21  来源:igfitidea点击:

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

cwindowsgtkglade

提问by User7723337

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.

我正在寻找可以将 .Glade(或 xml)文件转换为 C 源文件的工具。
我尝试过 g2c(Glade To C Translator),但我正在寻找 Windows 二进制文件。

Any one does know any good tool for window.

任何人都知道窗口的任何好工具。

Thanks,
PP.

谢谢,
PP。

回答by ptomato

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:

你不需要工具。只需用您最喜欢的脚本语言编写一个脚本,将您的 Glade 文件格式化为 C 字符串文字:

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

例如,让我们称之为glade_file.c

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

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

编译glade_file.c到您的程序中,然后在构建界面时执行以下操作:

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

回答by Alexey Yakovenko

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

glade2 对我来说有效。但是,如果您使用的是新的 Glade3 功能,它对您没有帮助..

回答by Gearoid Murphy

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

我不得不写一个小python脚本来做到这一点,希望这对其他人有帮助:

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()