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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
/* $Id: _chararraymodule.c,v 1.24 2005/10/27 21:06:35 jaytmiller Exp $ */
#include <Python.h>
#include <stdio.h>
#include <math.h>
#include <signal.h>
#include <ctype.h>
#include <libnumarray.h>
static PyObject *_Error;
/* returns length of string 's', or 'maxlen', whichever is smaller. */
#if !defined(strnlen)
size_t strnlen(const char *s, size_t maxlen)
{
size_t i;
for(i=0; i<maxlen; i++)
if (s[i] == 0)
return i;
return maxlen;
}
#endif
#if !defined(min)
static int min(int x, int y)
{
if (x < y)
return x;
else
return y;
}
#endif
#if !defined(max)
static int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
#endif
/* mystrdup duplicates the given string 's', copying up to 'size' bytes.
This still works when 's' is not terminated within 'size' bytes.
Also uses python memory allocation.
*/
static char *mystrdup(char *s, int size)
{
char *u = PyMem_Malloc(size), *v = u;
if (!u)
return (char *) PyErr_Format(_Error,
"mycat: Error allocating memory.");
while(size-- && (*u++ = *s++));
return v;
}
/* rstripw strips whitespace from the right side of 's', which can be up to
'n' characters long. rstripw interprets 0 as terminating 's'.
rstripw never strips a string down to length 0; the last whitespace is always
spared.
*/
#if defined(isspace)
#undef isspace
#define isspace(c) ((c==' ')||(c=='\t')||(c=='\n')||(c=='\r')||(c=='\v')||(c=='\f'))
#endif
static void rstripw(char *s, int n)
{
int i;
for(i=strnlen(s,n)-1; i>=1; i--) /* Never strip to length 0. */
{
int c = s[i];
if (!c || isspace(c))
s[i] = 0;
else
break;
}
}
/* padw pads string 's' with blanks, for a total length of 'n' characters. */
static void
padw(char *s, int n, char padc)
{
int i;
for(i=strnlen(s, n); i<n; i++)
s[i] = padc;
}
/* Strip executes rstripw on a python string. */
static PyObject *
Strip(PyObject *obj, PyObject *args)
{
char *s, *t;
int slen;
if (!PyArg_ParseTuple(args, "s#:Strip", &s, &slen))
return NULL;
if ((t = mystrdup(s, slen)))
{
rstripw(t, slen);
obj = Py_BuildValue("s#", t, strnlen(t, slen));
PyMem_Free(t);
return obj;
}
else
return NULL;
}
/* Pad executes padw on a python string. */
static PyObject *
Pad(PyObject *obj, PyObject *args)
{
char *s, *t;
int slen, rsize;
char padc;
if (!PyArg_ParseTuple(args, "s#ib:Pad", &s, &slen, &rsize, &padc))
return NULL;
rsize = max(slen, rsize);
if ((t = mystrdup(s, rsize)))
{
padw(t, rsize, padc);
obj = Py_BuildValue("s#", t, rsize);
PyMem_Free(t);
return obj;
}
else
return NULL;
}
static int
Concat(PyObject *aux, long nnumarray, PyArrayObject *numarray[], char *data[])
{
size_t rused, bused;
PyArrayObject *ai = numarray[0];
PyArrayObject *bi = numarray[1];
PyArrayObject *ri = numarray[2];
char *a = data[0];
char *b = data[1];
char *r = data[2];
if (!PyInt_Check(aux)) {
PyErr_SetString(_Error, "Bad call to Concat.");
return -1;
}
if (PyInt_AsLong(aux)) {
memcpy(r, a, ai->itemsize);
memcpy(r+ai->itemsize, b, bi->itemsize);
} else {
strncpy(r, a, ai->itemsize);
rused = strnlen(r, ai->itemsize);
bused = min(strnlen(b, bi->itemsize), ri->itemsize-rused);
strncpy(r+rused, b, bused);
memset(r+rused+bused, 0, ri->itemsize-rused-bused);
}
return 0;
}
NSTRIDING_DESCR3(Concat);
#define SMALL_STRING 4096
static char *
_chararray_copy_and_strip(char *original, char *temp, int nc, int is_raw)
{
if (nc > SMALL_STRING) {
temp = malloc(nc);
if (!temp) {
PyErr_SetString(_Error, "Couldn't malloc memory for CharArray string comparison.");
return NULL;
}
}
memcpy(temp, original, nc);
if (!is_raw) {
rstripw(temp, nc);
}
return temp;
}
static void
_chararray_release(char *ptr, int nc)
{
if (nc > SMALL_STRING) {
free(ptr);
}
}
enum {
STR_EQ,
STR_NE,
STR_LT,
STR_GT,
STR_LE,
STR_GE,
RAWSTR_EQ,
RAWSTR_NE,
RAWSTR_LT,
RAWSTR_GT,
RAWSTR_LE,
RAWSTR_GE,
};
static int
StrCmp(PyObject *aux, long nnumarray, PyArrayObject *numarray[], char *data[])
{
PyArrayObject *ai = numarray[0];
PyArrayObject *bi = numarray[1];
char *a = data[0];
char *b = data[1];
Bool *r = (Bool *) data[2];
int nc = min(ai->itemsize, bi->itemsize);
char atemp[SMALL_STRING], btemp[SMALL_STRING];
char *aptr, *bptr;
long mode = (long) aux;
int is_raw = mode > RAWSTR_EQ;
aptr = _chararray_copy_and_strip(a, atemp, nc, is_raw);
bptr = _chararray_copy_and_strip(b, btemp, nc, is_raw);
if (!aptr || !bptr) return -1;
switch(mode)
{
case STR_EQ: *r = strncmp(aptr, bptr, nc) == 0; break;
case STR_NE: *r = strncmp(aptr, bptr, nc) != 0; break;
case STR_LT: *r = strncmp(aptr, bptr, nc) < 0; break;
case STR_GT: *r = strncmp(aptr, bptr, nc) > 0; break;
case STR_LE: *r = strncmp(aptr, bptr, nc) <= 0; break;
case STR_GE: *r = strncmp(aptr, bptr, nc) >= 0; break;
case RAWSTR_EQ: *r = memcmp(aptr, bptr, nc) == 0; break;
case RAWSTR_NE: *r = memcmp(aptr, bptr, nc) != 0; break;
case RAWSTR_LT: *r = memcmp(aptr, bptr, nc) < 0; break;
case RAWSTR_GT: *r = memcmp(aptr, bptr, nc) > 0; break;
case RAWSTR_LE: *r = memcmp(aptr, bptr, nc) <= 0; break;
case RAWSTR_GE: *r = memcmp(aptr, bptr, nc) >= 0; break;
default:
PyErr_Format(PyExc_ValueError,
"StrCmp: invalid comparison mode.");
return -1;
}
_chararray_release(aptr, nc);
_chararray_release(bptr, nc);
return 0;
}
PyObject *
_Py_StrCmp(PyObject *module, PyObject *args)
{
long i, op, raw, mode;
PyArrayObject *arrays[3] = {0, 0, 0};
char *data[3];
if (!PyArg_ParseTuple( args, "OllO:StrCmp", arrays, &op, &raw, arrays+1))
return NULL;
if (!NA_NDArrayCheck((PyObject *) arrays[0]))
return PyErr_Format(PyExc_TypeError,
"StrCmp: Invalid 1st parameter type.");
if (!NA_NDArrayCheck((PyObject *) arrays[1])
|| (arrays[0]->ob_type != arrays[1]->ob_type)
|| (arrays[0]->itemsize != arrays[1]->itemsize)
|| !NA_ShapeEqual(arrays[0], arrays[1])) {
return PyObject_CallMethod(
(PyObject *) arrays[0], "_StrCmp", "llO",
op, raw, arrays[1]);
}
if (!NA_updateDataPtr(arrays[0]))
return NULL;
if (!NA_updateDataPtr(arrays[1]))
return NULL;
mode = op + RAWSTR_EQ*raw;
arrays[2] = NA_vNewArray( NULL, tBool,
arrays[0]->nd,
arrays[0]->dimensions);
if (!arrays[2]) return NULL;
for(i=0; i<3; i++)
data[i] = arrays[i]->data;
if (_NA_callStridingHelper(
(PyObject *) mode, arrays[0]->nd, 3, arrays, data, StrCmp) < 0) {
Py_DECREF(arrays[2]);
return NULL;
}
return (PyObject *) arrays[2];
}
/* StripAll applies rstripw to every element of a CharArray. This can be used
to normalize charnumarray initialized from a binary source.
*/
static int
StripAll( PyObject *aux, int nnumarray, PyArrayObject *numarray[], char *data[])
{
PyArrayObject *ai = numarray[0];
char *a = data[0];
if (nnumarray != 1) {
PyErr_Format(_Error, "StripAll: invalid parameters.");
return -1;
}
if (!PyArray_ISWRITABLE(ai)) {
PyErr_Format(_Error, "StripAll: result array not writeable.");
return -1;
}
rstripw(a, ai->itemsize);
return 0;
}
NSTRIDING_DESCR1(StripAll);
static int
PadAll( PyObject *aux, int nnumarray, PyArrayObject *numarray[], char *data[])
{
PyArrayObject *ai = numarray[0];
char *a = data[0];
if (nnumarray != 1) {
PyErr_Format(PyExc_ValueError,
"PadAll: invalid parameters.");
return -1;
}
if (!PyArray_ISWRITABLE(ai)) {
PyErr_Format(PyExc_ValueError,
"PadAll: result array not writeable.");
return -1;
}
if (!PyString_Check(aux) || PyString_Size(aux) != 1) {
PyErr_Format(PyExc_ValueError,
"aux parameter must be a len-1-padding-string");
return -1;
}
padw(a, ai->itemsize, *PyString_AsString(aux));
return 0;
}
NSTRIDING_DESCR1(PadAll);
static int
ToUpper( PyObject *aux, int nnumarray, PyArrayObject *numarray[], char *data[])
{
PyArrayObject *ai = numarray[0];
char *a = data[0];
long i;
if (nnumarray != 1) {
PyErr_Format(_Error, "ToUpper: invalid parameters.");
return -1;
}
if (!PyArray_ISWRITABLE(ai)) {
PyErr_Format(_Error, "ToUpper: result array not writeable.");
return -1;
}
for(i=0; (a[i]!=0) && (i<ai->itemsize); i++)
a[i] = toupper(a[i]);
return 0;
}
NSTRIDING_DESCR1(ToUpper);
static int
ToLower( PyObject *aux, int nnumarray, PyArrayObject *numarray[], char *data[])
{
PyArrayObject *ai = numarray[0];
char *a = data[0];
long i;
if (nnumarray != 1) {
PyErr_Format(_Error, "ToLower: invalid parameters.");
return -1;
}
if (!PyArray_ISWRITABLE(ai)) {
PyErr_Format(_Error, "ToLower: result array not writeable.");
return -1;
}
for(i=0; (a[i]!=0) && (i<ai->itemsize); i++)
a[i] = tolower(a[i]);
return 0;
}
NSTRIDING_DESCR1(ToLower);
/* StrLen computes the lengths of all the items in a chararray. */
static int
StrLen(PyObject *args, int nnumarray, PyArrayObject *numarray[], char *data[])
{
PyArrayObject *ai = numarray[0];
PyArrayObject *ni = numarray[1];
char *a;
Int32 *n;
if ((nnumarray < 2) ||
!NA_NDArrayCheck((PyObject *)ai) ||
!NA_NumArrayCheck((PyObject *)ni)) {
PyErr_Format(PyExc_ValueError,
"StrLen requires one string and one numerical array");
return -1;
}
a = data[0];
n = (Int32 *) data[1];
rstripw(a, ai->itemsize);
*n = strnlen(a, ai->itemsize);
return 0;
}
NSTRIDING_DESCR2(StrLen);
static int
Eval(PyObject *args, int nnumarray, PyArrayObject *numarray[], char *data[])
{
PyArrayObject *ai = numarray[0];
char *a = data[0];
Float64 *n = (Float64 *) data[1];
char buffer[64], *ptr;
int len;
len = strnlen(a, ai->itemsize);
if (len > ELEM(buffer)-1) {
PyErr_Format(PyExc_ValueError,
"string too long to convert.");
return -1;
}
memcpy(buffer, a, len);
buffer[len] = 0;
*(double *) n = strtod(buffer, &ptr);
if ((ptr == buffer) && (*n == 0)) {
PyErr_Format(_Error, "Eval: error evaluating string.");
return -1;
}
return 0;
}
NSTRIDING_DESCR2(Eval);
static int
Format(PyObject *format, int nnumarray, PyArrayObject *numarray[], char *data[])
{
PyArrayObject *ni = numarray[0];
PyArrayObject *ai = numarray[1];
char *np = data[0];
char *a = data[1];
maybelong offset = np - ni->data;
Float64 n = NA_get_Float64(ni, offset);
PyObject *args, *astr;
char *s;
if (!(args = Py_BuildValue("(d)", n))) {
PyErr_Format(_Error,
"Format: error building args tuple.");
return -1;
}
if (!(astr = PyString_Format(format, args)))
return -1;
s = PyString_AsString(astr);
if (strlen(s) > ai->itemsize) {
PyErr_Warn(PyExc_RuntimeWarning,
"formatted value too large"
" for CharArray itemsize.");
}
strncpy(a, s, ai->itemsize);
Py_DECREF(astr);
Py_DECREF(args);
return 0;
}
NSTRIDING_DESCR2(Format);
static PyMethodDef _chararrayMethods[] = {
{"Strip", Strip, METH_VARARGS},
{"Pad", Pad, METH_VARARGS},
{"StrCmp", _Py_StrCmp, METH_VARARGS},
{NULL, NULL} /* Sentinel */
};
/* platform independent*/
#ifdef MS_WIN32
__declspec(dllexport)
#endif
void init_chararray(void) {
PyObject *m, *d;
m = Py_InitModule("_chararray", _chararrayMethods);
d = PyModule_GetDict(m);
_Error = PyErr_NewException("numarray._chararray.error", NULL, NULL);
PyDict_SetItemString(d, "error", _Error);
ADD_VERSION(m);
import_libnumarray();
NA_add_cfunc(d, "Concat", &Concat_descr);
NA_add_cfunc(d, "Format", &Format_descr);
NA_add_cfunc(d, "Eval", &Eval_descr);
NA_add_cfunc(d, "StrLen", &StrLen_descr);
NA_add_cfunc(d, "StripAll", &StripAll_descr);
NA_add_cfunc(d, "PadAll", &PadAll_descr);
NA_add_cfunc(d, "ToUpper", &ToUpper_descr);
NA_add_cfunc(d, "ToLower", &ToLower_descr);
}
|