Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / disembl / biopython-1.50 / Bio / cstringfnsmodule.c
1 /* Copyright 2000 by Jeffrey Chang.  All rights reserved.
2  * This code is part of the Biopython distribution and governed by its
3  * license.  Please see the LICENSE file that should have been included
4  * as part of this package.
5  *
6  * cstringfnsmodule.c
7  * Created 7 Jun 2000
8  */
9
10 #include "Python.h"
11 #include <string.h>  /* memset */
12
13
14 /* Functions in this module. */
15
16 static char cstringfns_splitany__doc__[] = 
17 "splitany(str [,sep [,maxsplit [,negate]]]) -> list of strings\n\
18 \n\
19 Split a string.  Similar to string.split, except that this considers\n\
20 any one of the characters in sep to be a delimiter.  If negate is\n\
21 true, then everything but sep will be a separator.\n\
22 \n\
23 ";
24
25 static PyObject *cstringfns_splitany(
26      PyObject *self, PyObject *args, PyObject *keywds)
27 {
28     int i, prev;
29     int nsplit, maxsplit=0;
30     /*int negate=0;*/
31     PyObject *py_negate=NULL;
32     PyObject *strlist, *newstr;
33     unsigned char *str, 
34         *sep=" \011\012\013\014\015";  /* whitespace */
35     char tosplit[256];
36     static char *kwlist[] = {"str", "sep", "maxsplit", "negate", NULL};
37
38     if(!PyArg_ParseTupleAndKeywords(args, keywds, "s|siO", kwlist, 
39                                     &str, &sep, &maxsplit, &py_negate))
40         return NULL;
41     if(maxsplit < 0)
42         maxsplit = 1;
43     /* negate = (py_negate && PyObject_IsTrue(py_negate));*/
44     /* XXX NO MORE NEGATE */
45
46     /* Set the tosplit array to 1 for characters to split on. */
47     memset(tosplit, 0, 256);
48     while(*sep) {
49         tosplit[(unsigned char)*sep++] = 1;
50         }
51     if(py_negate && PyObject_IsTrue(py_negate)) {
52         for(i=0; i<256; i++)
53             tosplit[i] = !tosplit[i];
54     }
55
56     /* Create a new list to store the variables. */
57     if(!(strlist = PyList_New(0))) {
58         PyErr_SetString(PyExc_SystemError, "I could not create a new list");
59         return NULL;
60     }
61
62     prev = 0;
63     nsplit = 0;
64     for(i=0; str[i] && (maxsplit == 0 || nsplit < maxsplit); i++) {
65         /*if(!(tosplit[(int)str[i]] == !negate))
66           continue; */
67         if(!tosplit[(int)str[i]])
68             continue;
69
70         /* Split the string here. */
71         if(!(newstr = PyString_FromStringAndSize(&str[prev], i-prev))) {
72             PyErr_SetString(PyExc_SystemError, 
73                             "I could not create a new string");
74             break;
75         }
76         if(PyList_Append(strlist, newstr) == -1) {
77             Py_DECREF(newstr);
78             break;
79         }
80         Py_DECREF(newstr);
81         prev = i+1;
82         nsplit++;
83     }
84     if(!PyErr_Occurred()) {
85         i = strlen(str);
86         /* Add the last one. */
87         if(!(newstr = PyString_FromStringAndSize(&str[prev], i-prev))) {
88             PyErr_SetString(PyExc_SystemError, 
89                             "I could not create a new string");
90         } else {
91             PyList_Append(strlist, newstr);
92             Py_DECREF(newstr);
93         }
94     } else {
95         Py_DECREF(strlist);
96         return NULL;
97     }
98
99
100     return strlist;
101 }
102
103
104
105 /* Module definition stuff */
106
107 static PyMethodDef cstringfnsMethods[] = {
108   {"splitany", (PyCFunction)cstringfns_splitany, METH_VARARGS|METH_KEYWORDS, 
109    cstringfns_splitany__doc__},
110   {NULL, NULL}
111 };
112
113 static char cstringfns__doc__[] =
114 "This provides helper functions for the stringfns module.\n\
115 You should never import this module on its own.\n\
116 \n\
117 ";
118
119 void initcstringfns(void)
120 {
121   (void) Py_InitModule3("cstringfns", cstringfnsMethods, cstringfns__doc__);
122 }