structmodule.c
来自「python s60 1.4.5版本的源代码」· C语言 代码 · 共 1,552 行 · 第 1/3 页
C
1,552 行
static PyObject *
nu_short(const char *p, const formatdef *f)
{
short x;
memcpy((char *)&x, p, sizeof x);
return PyInt_FromLong((long)x);
}
static PyObject *
nu_ushort(const char *p, const formatdef *f)
{
unsigned short x;
memcpy((char *)&x, p, sizeof x);
return PyInt_FromLong((long)x);
}
static PyObject *
nu_int(const char *p, const formatdef *f)
{
int x;
memcpy((char *)&x, p, sizeof x);
return PyInt_FromLong((long)x);
}
static PyObject *
nu_uint(const char *p, const formatdef *f)
{
unsigned int x;
memcpy((char *)&x, p, sizeof x);
return PyLong_FromUnsignedLong((unsigned long)x);
}
static PyObject *
nu_long(const char *p, const formatdef *f)
{
long x;
memcpy((char *)&x, p, sizeof x);
return PyInt_FromLong(x);
}
static PyObject *
nu_ulong(const char *p, const formatdef *f)
{
unsigned long x;
memcpy((char *)&x, p, sizeof x);
return PyLong_FromUnsignedLong(x);
}
/* Native mode doesn't support q or Q unless the platform C supports
long long (or, on Windows, __int64). */
#ifdef HAVE_LONG_LONG
static PyObject *
nu_longlong(const char *p, const formatdef *f)
{
LONG_LONG x;
memcpy((char *)&x, p, sizeof x);
return PyLong_FromLongLong(x);
}
static PyObject *
nu_ulonglong(const char *p, const formatdef *f)
{
unsigned LONG_LONG x;
memcpy((char *)&x, p, sizeof x);
return PyLong_FromUnsignedLongLong(x);
}
#endif
static PyObject *
nu_float(const char *p, const formatdef *f)
{
float x;
memcpy((char *)&x, p, sizeof x);
return PyFloat_FromDouble((double)x);
}
static PyObject *
nu_double(const char *p, const formatdef *f)
{
double x;
memcpy((char *)&x, p, sizeof x);
return PyFloat_FromDouble(x);
}
static PyObject *
nu_void_p(const char *p, const formatdef *f)
{
void *x;
memcpy((char *)&x, p, sizeof x);
return PyLong_FromVoidPtr(x);
}
static int
np_byte(char *p, PyObject *v, const formatdef *f)
{
long x;
if (get_long(v, &x) < 0)
return -1;
if (x < -128 || x > 127){
PyErr_SetString(StructError,
"byte format requires -128<=number<=127");
return -1;
}
*p = (char)x;
return 0;
}
static int
np_ubyte(char *p, PyObject *v, const formatdef *f)
{
long x;
if (get_long(v, &x) < 0)
return -1;
if (x < 0 || x > 255){
PyErr_SetString(StructError,
"ubyte format requires 0<=number<=255");
return -1;
}
*p = (char)x;
return 0;
}
static int
np_char(char *p, PyObject *v, const formatdef *f)
{
if (!PyString_Check(v) || PyString_Size(v) != 1) {
PyErr_SetString(StructError,
"char format require string of length 1");
return -1;
}
*p = *PyString_AsString(v);
return 0;
}
static int
np_short(char *p, PyObject *v, const formatdef *f)
{
long x;
short y;
if (get_long(v, &x) < 0)
return -1;
if (x < SHRT_MIN || x > SHRT_MAX){
PyErr_SetString(StructError,
"short format requires " STRINGIFY(SHRT_MIN)
"<=number<=" STRINGIFY(SHRT_MAX));
return -1;
}
y = (short)x;
memcpy(p, (char *)&y, sizeof y);
return 0;
}
static int
np_ushort(char *p, PyObject *v, const formatdef *f)
{
long x;
unsigned short y;
if (get_long(v, &x) < 0)
return -1;
if (x < 0 || x > USHRT_MAX){
PyErr_SetString(StructError,
"short format requires 0<=number<=" STRINGIFY(USHRT_MAX));
return -1;
}
y = (unsigned short)x;
memcpy(p, (char *)&y, sizeof y);
return 0;
}
static int
np_int(char *p, PyObject *v, const formatdef *f)
{
long x;
int y;
if (get_long(v, &x) < 0)
return -1;
y = (int)x;
memcpy(p, (char *)&y, sizeof y);
return 0;
}
static int
np_uint(char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
unsigned int y;
if (get_ulong(v, &x) < 0)
return -1;
y = (unsigned int)x;
memcpy(p, (char *)&y, sizeof y);
return 0;
}
static int
np_long(char *p, PyObject *v, const formatdef *f)
{
long x;
if (get_long(v, &x) < 0)
return -1;
memcpy(p, (char *)&x, sizeof x);
return 0;
}
static int
np_ulong(char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
if (get_ulong(v, &x) < 0)
return -1;
memcpy(p, (char *)&x, sizeof x);
return 0;
}
#ifdef HAVE_LONG_LONG
static int
np_longlong(char *p, PyObject *v, const formatdef *f)
{
LONG_LONG x;
if (get_longlong(v, &x) < 0)
return -1;
memcpy(p, (char *)&x, sizeof x);
return 0;
}
static int
np_ulonglong(char *p, PyObject *v, const formatdef *f)
{
unsigned LONG_LONG x;
if (get_ulonglong(v, &x) < 0)
return -1;
memcpy(p, (char *)&x, sizeof x);
return 0;
}
#endif
static int
np_float(char *p, PyObject *v, const formatdef *f)
{
float x = (float)PyFloat_AsDouble(v);
if (x == -1 && PyErr_Occurred()) {
PyErr_SetString(StructError,
"required argument is not a float");
return -1;
}
memcpy(p, (char *)&x, sizeof x);
return 0;
}
static int
np_double(char *p, PyObject *v, const formatdef *f)
{
double x = PyFloat_AsDouble(v);
if (x == -1 && PyErr_Occurred()) {
PyErr_SetString(StructError,
"required argument is not a float");
return -1;
}
memcpy(p, (char *)&x, sizeof(double));
return 0;
}
static int
np_void_p(char *p, PyObject *v, const formatdef *f)
{
void *x = PyLong_AsVoidPtr(v);
if (x == NULL && PyErr_Occurred()) {
/* ### hrm. PyLong_AsVoidPtr raises SystemError */
if (PyErr_ExceptionMatches(PyExc_TypeError))
PyErr_SetString(StructError,
"required argument is not an integer");
return -1;
}
memcpy(p, (char *)&x, sizeof x);
return 0;
}
const static formatdef native_table[] = {
{'x', sizeof(char), 0, NULL},
{'b', sizeof(char), 0, nu_byte, np_byte},
{'B', sizeof(char), 0, nu_ubyte, np_ubyte},
{'c', sizeof(char), 0, nu_char, np_char},
{'s', sizeof(char), 0, NULL},
{'p', sizeof(char), 0, NULL},
{'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
{'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
{'i', sizeof(int), INT_ALIGN, nu_int, np_int},
{'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
{'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
{'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
{'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
{'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
{'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
#ifdef HAVE_LONG_LONG
{'q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
{'Q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
#endif
{0}
};
/* Big-endian routines. *****************************************************/
static PyObject *
bu_int(const char *p, const formatdef *f)
{
long x = 0;
int i = f->size;
do {
x = (x<<8) | (*p++ & 0xFF);
} while (--i > 0);
/* Extend the sign bit. */
if (SIZEOF_LONG > f->size)
x |= -(x & (1L << (8*f->size - 1)));
return PyInt_FromLong(x);
}
static PyObject *
bu_uint(const char *p, const formatdef *f)
{
unsigned long x = 0;
int i = f->size;
do {
x = (x<<8) | (*p++ & 0xFF);
} while (--i > 0);
if (f->size >= 4)
return PyLong_FromUnsignedLong(x);
else
return PyInt_FromLong((long)x);
}
static PyObject *
bu_longlong(const char *p, const formatdef *f)
{
return _PyLong_FromByteArray((const unsigned char *)p,
8,
0, /* little-endian */
1 /* signed */);
}
static PyObject *
bu_ulonglong(const char *p, const formatdef *f)
{
return _PyLong_FromByteArray((const unsigned char *)p,
8,
0, /* little-endian */
0 /* signed */);
}
static PyObject *
bu_float(const char *p, const formatdef *f)
{
return unpack_float(p, 1);
}
static PyObject *
bu_double(const char *p, const formatdef *f)
{
return unpack_double(p, 1);
}
static int
bp_int(char *p, PyObject *v, const formatdef *f)
{
long x;
int i;
if (get_long(v, &x) < 0)
return -1;
i = f->size;
do {
p[--i] = (char)x;
x >>= 8;
} while (i > 0);
return 0;
}
static int
bp_uint(char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
int i;
if (get_ulong(v, &x) < 0)
return -1;
i = f->size;
do {
p[--i] = (char)x;
x >>= 8;
} while (i > 0);
return 0;
}
static int
bp_longlong(char *p, PyObject *v, const formatdef *f)
{
int res;
v = get_pylong(v);
if (v == NULL)
return -1;
res = _PyLong_AsByteArray((PyLongObject *)v,
(unsigned char *)p,
8,
0, /* little_endian */
1 /* signed */);
Py_DECREF(v);
return res;
}
static int
bp_ulonglong(char *p, PyObject *v, const formatdef *f)
{
int res;
v = get_pylong(v);
if (v == NULL)
return -1;
res = _PyLong_AsByteArray((PyLongObject *)v,
(unsigned char *)p,
8,
0, /* little_endian */
0 /* signed */);
Py_DECREF(v);
return res;
}
static int
bp_float(char *p, PyObject *v, const formatdef *f)
{
double x = PyFloat_AsDouble(v);
if (x == -1 && PyErr_Occurred()) {
PyErr_SetString(StructError,
"required argument is not a float");
return -1;
}
return pack_float(x, p, 1);
}
static int
bp_double(char *p, PyObject *v, const formatdef *f)
{
double x = PyFloat_AsDouble(v);
if (x == -1 && PyErr_Occurred()) {
PyErr_SetString(StructError,
"required argument is not a float");
return -1;
}
return pack_double(x, p, 1);
}
const static formatdef bigendian_table[] = {
{'x', 1, 0, NULL},
{'b', 1, 0, bu_int, bp_int},
{'B', 1, 0, bu_uint, bp_int},
{'c', 1, 0, nu_char, np_char},
{'s', 1, 0, NULL},
{'p', 1, 0, NULL},
{'h', 2, 0, bu_int, bp_int},
{'H', 2, 0, bu_uint, bp_uint},
{'i', 4, 0, bu_int, bp_int},
{'I', 4, 0, bu_uint, bp_uint},
{'l', 4, 0, bu_int, bp_int},
{'L', 4, 0, bu_uint, bp_uint},
{'q', 8, 0, bu_longlong, bp_longlong},
{'Q', 8, 0, bu_ulonglong, bp_ulonglong},
{'f', 4, 0, bu_float, bp_float},
{'d', 8, 0, bu_double, bp_double},
{0}
};
/* Little-endian routines. *****************************************************/
static PyObject *
lu_int(const char *p, const formatdef *f)
{
long x = 0;
int i = f->size;
do {
x = (x<<8) | (p[--i] & 0xFF);
} while (i > 0);
/* Extend the sign bit. */
if (SIZEOF_LONG > f->size)
x |= -(x & (1L << (8*f->size - 1)));
return PyInt_FromLong(x);
}
static PyObject *
lu_uint(const char *p, const formatdef *f)
{
unsigned long x = 0;
int i = f->size;
do {
x = (x<<8) | (p[--i] & 0xFF);
} while (i > 0);
if (f->size >= 4)
return PyLong_FromUnsignedLong(x);
else
return PyInt_FromLong((long)x);
}
static PyObject *
lu_longlong(const char *p, const formatdef *f)
{
return _PyLong_FromByteArray((const unsigned char *)p,
8,
1, /* little-endian */
1 /* signed */);
}
static PyObject *
lu_ulonglong(const char *p, const formatdef *f)
{
return _PyLong_FromByteArray((const unsigned char *)p,
8,
1, /* little-endian */
0 /* signed */);
}
static PyObject *
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?