File: _nib.pyx

package info (click to toggle)
python-bx 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,000 kB
  • sloc: python: 17,136; ansic: 2,326; makefile: 24; sh: 8
file content (59 lines) | stat: -rw-r--r-- 2,180 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from cpython.version cimport PY_MAJOR_VERSION


cdef extern from "Python.h":
    char * PyBytes_AsString( object )
    object PyBytes_FromStringAndSize( char *, Py_ssize_t )

import struct
import sys


cdef char * NIB_I2C_TABLE 
cdef char * NIB_I2C_TABLE_FIRST 
cdef char * NIB_I2C_TABLE_SECOND
#NIB_I2C_TABLE        = "TCAGNXXXtcagnxxx"
NIB_I2C_TABLE_FIRST  = "TTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAGGGGGGGGGGGGGGGGNNNNNNNNNNNNNNNNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXttttttttttttttttccccccccccccccccaaaaaaaaaaaaaaaaggggggggggggggggnnnnnnnnnnnnnnnnxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
NIB_I2C_TABLE_SECOND = "TCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxxTCAGNXXXtcagnxxx"

def translate_raw_data( data, int start, int length ):
    """
    Data is a block read from the file that needs to be unpacked, dealing
    with end conditions based on start/length.
    """
    cdef int i, j
    cdef char * p_rval
    cdef unsigned char * p_data

    if length == 0 :
      return ""

    # Allocate string to write into
    rval = PyBytes_FromStringAndSize( NULL, length )

    # Get char pointer access to strings
    p_rval = PyBytes_AsString( rval )
    p_data = <unsigned char *> PyBytes_AsString( data )
    i = 0
    # Odd start
    if start & 1: 
        #p_rval[i] = NIB_I2C_TABLE[ p_data[0] & 0xF ]
        p_rval[i] = NIB_I2C_TABLE_SECOND[ p_data[0] ]
        p_data = p_data + 1
        i = 1
    # Two output values for each input value
    for j from 0 <= j < (length-i) // 2:
        #p_rval[i]   = NIB_I2C_TABLE[ ( p_data[0] >> 4 ) & 0xF ];
        #p_rval[i+1] = NIB_I2C_TABLE[ ( p_data[0] >> 0 ) & 0xF ];
        p_rval[i]   = NIB_I2C_TABLE_FIRST [ p_data[0] ]
        p_rval[i+1] = NIB_I2C_TABLE_SECOND[ p_data[0] ]
        i = i + 2
        p_data = p_data + 1
    # Odd end
    if i < length: 
        p_rval[i] = NIB_I2C_TABLE_FIRST[ p_data[0] ]

    if PY_MAJOR_VERSION >= 3:
        return rval.decode()
    else:
        return rval