/* This software carries the following license: Modified BSD license Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Steinar Knutsen */ /* ncmp contains only the function ncmp(). ncmp() compares strings the same way as cmp(), except for numerical substrings, which are compared according to their numerical value. E.g.: >>> import ncmp >>> ncmp.ncmp('program-1.10','program-1.9') 1 >>> cmp('program-1.10','program-1.9') -8 Just copy the source file to the Modules directory of your Python build directory and add something like ncmp ncmpmodule.c to Setup and make. Or whatever the build process you favour is. Remember to check the path for Python.h. Last updated 2000-02-20. */ #include #include "/usr/local/include/python1.5/Python.h" static PyObject * ncmp(PyObject *self, PyObject *args) { const char* x; const char* y; long cc; if (!PyArg_ParseTuple(args, "ss", &x, &y)) { return NULL; } else { cc = numcompare(x, y); if (cc > 0) { return Py_BuildValue("i", 1); } else if ( cc == 0 ) { return Py_BuildValue("i", 0); } else { return Py_BuildValue("i", -1); } } } int numcompare(char* x, char* y) { int i0 = -1, i1 = -1; char* endx; char* endy; int lenx, leny; long X, Y; lenx = strlen(x)-1; leny = strlen(y)-1; while ( i0 < lenx && i1 < leny ) { i0++; i1++; if ( ( x[i0] >= '0' && x[i0] <= '9' ) && ( y[i1] >= '0' && y[i1] <= '9' ) ) { X = strtol(x+i0, &endx, 10); Y = strtol(y+i1, &endy, 10); if ( X != Y ) { return X-Y; } else { /* -1 on cause of increment in start of while-loop */ i0 = (int)(endx - x) - 1; i1 = (int)(endy - y) - 1; continue; } } if ( x[i0] != y[i1] ) { return x[i0]-y[i1]; } } return strcmp(x, y); } static struct PyMethodDef functions[] = { { "ncmp", ncmp, 1}, { NULL, NULL} }; void initncmp() { (void)Py_InitModule("ncmp", functions); }