Python导入C的头文件

1. 需求

经常有音频转文字的需求,讯飞听见网站效果还不错,赠送5小时免费转写时间,但用完了
想到讯飞有语音开放平台,过去一看,转写SDK还是要买时间,但语音听打接口是免费的,那就试试
费了点劲注册为开发者,下载了LInux C SDK,测了下demo,没问题。为了速度,还是用 Python 调接口吧,看上去 ctypes 实现比较自然。

2. 问题:

SDK 头文件里用enum定义了400+的错误码,为了方便调试,打算导成Python的enum定义(>=3.4)。手动拷贝感觉不爽,而且里面混杂着注释

3. 思路:

SDK库的符号表(被strip过了)
C语言分析器(不考虑)
手动清理复制(算了)

4. 选择:

还是用 gawk(比awk强大)处理下好了,比手动好点,说不准以后还能用...

5. 代码:

BEGIN {                         
    RS = "\r\n|{|}|\n"  # less is good for comment handling
    skip = 0

    print "#!/usr/bin/env python3"
    print "# -*- coding: UTF-8 -*-", "\n"
    print "from enum import Enum", "\n"
}

{
    while ($1 == "")
        next
}

# skip one-line comment, no use?
/^[[:blank:]]*(\/\/|#)/ {
    next
}

# skip comment
/^[[:blank:]]*\/\*/,  /\*\// {
    next
}

# get enum definitions
/^enum/, /;/ {
    subn = 0
    for (i=1; i<=NF; i++)
    {
        switch ($i) {
        case /^enum/: printf("class(Enum):")  #header
            break
        case /^;$/: printf("\n")  #tail
            next
        case /^(\/\/|\/\*)/:  #skip comment
            next
        default:  #body
            if (i == 1) printf("\t")
            subn = sub(/,/, "\n", $i)
            printf("%s ", $i)
            break
        }
    }
    if (subn == 0) printf("\n")
}

END {                           
    print "# end of", FILENAME
}

转载于:https://my.oschina.net/u/1044667/blog/876229

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值