处理TAP国际漫游话单的几种类库使用方法

来源:互联网 发布:北京哪里有mac专柜 编辑:程序博客网 时间:2024/05/01 08:24

snacc

这个库比较早,最早是1997年,2002年的版本是1.4,后面没有更新。在unix环境使用没问题,在linux环境下编译不过去。下面是unix环境下的使用方法。
- 升级make->gmake

http://ftp.gnu.org/gnu/make/make-3.75.tar.gz
  • 编译snacc
    cd snacc/compiler
    make

    ./snacc

    Usage: ./snacc [-h] [-P] [-t] [-v] [-e] [-d] [-p] [-f]            [-c | -C | -[T|O] <table output file> | -idl ]            [-u <useful types ASN.1 file>]            [-mm] [-mf <max file name length>]            [-l <neg number>]            <ASN.1 file list>
  • 编译c-lib
    cd snacc/c-lib
    make

  • 编译tap031111
    cd snacc/c-examples/tap031111
    make

asn1c

这个是目前使用的比较广泛的一个库
http://lionet.info/asn1c/blog/
http://lionet.info/soft/asn1c-0.9.27.tar.gz

  • 编译asn1c
    cd asn1c-0.9.27
    ./configure & make
    asn1c-0.9.27/asn1c/asn1c即为asn1c编译器

  • 建立自己的TAP转换程序所在目录
    mkdir tap031001
    cd tap031001
    把tap031111.asn1文件放到该目录下
    asn1c-0.9.27/asn1c/asn1c -fnative-types tap031001.asn1
    编译器会在当前目录下生成很多.h,.c文件
    将该该目录下的convert-example.c重命名为自己的文件名,如main.c

#include <stdio.h>#include <sys/types.h>#include <DataInterChange.h>   /* DataInterChange ASN.1 type  *//* * This is a custom function which writes the * encoded output into some FILE stream. */static intwrite_out(const void *buffer, size_t size, void *app_key) {    FILE *out_fp = app_key;    size_t wrote;    wrote = fwrite(buffer, 1, size, out_fp);    return (wrote == size) ? 0 : -1;}static int decode(const char *filename){    char buf[10240];      /* Temporary buffer      */    DataInterChange_t *data_inter_change = 0; /* Type to decode */    asn_dec_rval_t rval; /* Decoder return value  */    FILE *fp;            /* Input file handler    */    size_t size;         /* Number of bytes read  */    /* Open input file as read-only binary */    fp = fopen(filename, "rb");    if(!fp) {      perror(filename);      exit(66); /* better, EX_NOINPUT */    }    /* Read up to the buffer size */    size = fread(buf, 1, sizeof(buf), fp);    fclose(fp);    if(!size) {      fprintf(stderr, "%s: Empty or broken\n", filename);      exit(65); /* better, EX_DATAERR */    }    /* Decode the input buffer as DataInterChange type */    rval = ber_decode(0, &asn_DEF_DataInterChange,      (void **)&data_inter_change, buf, size);    if(rval.code != RC_OK) {      fprintf(stderr,        "%s: Broken DataInterChange encoding at byte %ld\n",        filename, (long)rval.consumed);      exit(65); /* better, EX_DATAERR */    }    /* Print the decoded DataInterChange type as XML */    xer_fprint(stdout, &asn_DEF_DataInterChange, data_inter_change);    return 0;}int main(int ac, char **av) {    const char *filename = av[1];    /* BER encode the data if filename is given */    if(ac < 2) {      fprintf(stderr, "Specify filename for BER output\n");      return -1;    }    decode(filename);    return 0; /* Encoding finished successfully */}

cc -I. -o rdecode *.c
生成执行文件rdecode会直接将tap文件打印成xml文件

后面可以再用php处理这个xml文件

tap3xml

一个perl的转换库,最新版本支持tap0312,这个库是专门转换tap3文件的

  • 安装tap3xml的perl支持库
    http://www.tap3edit.com/
    wget http://tap3edit.com/download/TAP3-Tap3edit-0.33.tar.gz
    wget http://tap3edit.com/download/Convert-ASN1-0.19.tar.gz
    wget http://search.cpan.org/CPAN/authors/id/M/MA/MAKAMAKA/JSON-2.90.tar.gz
    安装上面这三个perl库
    perl Makefile.PL
    make
    make install

  • 写一个tap3话单转换的prl脚本文件

use JSON;use TAP3::Tap3edit;use Data::Dumper;$Data::Dumper::Indent=1;$Data::Dumper::Quotekeys=1;$Data::Dumper::Useqq=1;$filename=shift;if ( ! $filename ) {    die "Usage: $0 tapname\n";}$tap3 = TAP3::Tap3edit->new();$tap3->decode($filename)  or  die $tap3->error;$struct=$tap3->structure;$json = JSON->new->encode($struct);print "$json\n";exit();my $key;# Will scan all the calls for Camel attachments.foreach $key ( @{$struct->{'transferBatch'}->{'callEventDetails'} } ) {    foreach ( keys %{$key} ) {        print "This call is: $_\n";        print Dumper( $key->{$_} );    }}

按自己的需要,也可以输出成json格式

  • 同时他也提供了一个tap3xml的C的源代码
    http://www.tap3edit.com/call.php?url=./download/tap3xml-0.18.zip
    可以直接将tap文件转换成xml文件
0 0
原创粉丝点击