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
|
/* $Id: _chararraymodule.c,v 1.13.2.1 2004/11/16 23:02:47 jaytmiller Exp $ */
#include <Python.h>
#include <stdio.h>
#include <math.h>
#include <signal.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)
{
int 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)
{
int i;
for(i=strnlen(s, n); i<n; i++)
s[i] = ' ';
}
/* 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;
if (!PyArg_ParseTuple(args, "s#i:Pad", &s, &slen, &rsize))
return NULL;
rsize = max(slen, rsize);
if ((t = mystrdup(s, rsize)))
{
padw(t, rsize);
obj = Py_BuildValue("s#", t, strnlen(t, rsize));
PyMem_Free(t);
return obj;
}
else
return NULL;
}
static int
Concat(PyObject *aux, long nnumarray, PyArrayObject *numarray[])
{
int rused, bused;
PyArrayObject *ai = numarray[0];
PyArrayObject *bi = numarray[1];
PyArrayObject *ri = numarray[2];
char *a = (char *) ai->data;
char *b = (char *) bi->data;
char *r = (char *) ri->data;
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);
static int
StrCmp(PyObject *aux, long nnumarray, PyArrayObject *numarray[])
{
PyArrayObject *ai = numarray[0];
PyArrayObject *bi = numarray[1];
PyArrayObject *ri = numarray[2];
char *a = (char *) ai->data;
char *b = (char *) bi->data;
Int8 *r = (Int8 *) ri->data;
int nc = min(ai->itemsize, bi->itemsize);
if (!PyInt_Check(aux) || (nnumarray != 3)) {
PyErr_SetString(_Error, "Bad call to StrCmp.");
return -1;
}
if (PyInt_AsLong(aux))
*r = memcmp(a, b, nc);
else
*r = strncmp(a, b, nc);
return 0;
}
NSTRIDING_DESCR3(StrCmp);
/* 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[])
{
PyArrayObject *ai = numarray[0];
char *a = (char *) ai->data;
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[])
{
PyArrayObject *ai = numarray[0];
char *a = (char *) ai->data;
if (nnumarray != 1) {
PyErr_Format(_Error, "PadAll: invalid parameters.");
return -1;
}
if (!PyArray_ISWRITABLE(ai)) {
PyErr_Format(_Error, "PadAll: result array not writeable.");
return -1;
}
padw(a, ai->itemsize);
return 0;
}
NSTRIDING_DESCR1(PadAll);
/* MaxLen computes the maximum length of all stripped elements in a CharArray.
This information can be used to reduce the itemsize of the array.
*/
static PyObject *
MaxLen(PyObject *obj, PyObject *args)
{
int i, len=0;
PyArrayObject *ao;
char *a;
if (!PyArg_ParseTuple(args, "O:MaxLen", &ao))
return NULL;
if (!NA_NDArrayCheck((PyObject *) ao))
return PyErr_Format(PyExc_TypeError,
"MaxLen: object not an NDArray");
if (!NA_updateDataPtr(ao))
return NULL;
a = (char *) ao->data;
for(i=0; i<NA_elements(ao); i++, a+=ao->bytestride)
{
rstripw(a, ao->itemsize);
len = max(len, strnlen(a, ao->itemsize));
}
return Py_BuildValue("i", len);
}
static int
Eval(PyObject *args, int nnumarray, PyArrayObject *numarray[])
{
PyArrayObject *ai = numarray[0],
*ni = numarray[1];
char *a = (char *) ai->data;
Float64 *n = (Float64 *) ni->data;
char buffer[64], *ptr;
int len;
if (!PyArg_ParseTuple(args, ":Eval"))
return -1;
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[])
{
PyArrayObject *ni = numarray[0];
PyArrayObject *ai = numarray[1];
char *a = (char *) ai->data;
Float64 n = NA_get1_Float64(ni, 0);
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},
{"MaxLen", MaxLen, 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("_chararray.error", NULL, NULL);
PyDict_SetItemString(d, "error", _Error);
import_libnumarray();
NA_add_cfunc(d, "Concat", &Concat_descr);
NA_add_cfunc(d, "StrCmp", &StrCmp_descr);
NA_add_cfunc(d, "Format", &Format_descr);
NA_add_cfunc(d, "Eval", &Eval_descr);
NA_add_cfunc(d, "StripAll", &StripAll_descr);
NA_add_cfunc(d, "PadAll", &PadAll_descr);
}
|