處理TAP國際漫遊話單的幾種類庫使用方法

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 int
write_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文件的

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格式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章