From af6dbf5ecdcc80dd805faac5bd66f94cbc984bfe Mon Sep 17 00:00:00 2001 From: maxb Date: Fri, 11 Feb 2005 23:53:52 +0000 Subject: [PATCH] tparse: Enable compilation on gcc 3.x, sacrificing compatibility with gcc 2.x. * tparse/tparse.h: #include , not , and add "using namespace std;". * tparse/tparsemodule.cpp: Do not #include . If a version of GNU C++ with the stdio_filebuf extension to the STL is detected, then use it to allow the passing of a filehandle from Python to the parser. Otherwise, throw a NotImplementedError when a filehandle is passed. git-svn-id: http://viewvc.tigris.org/svn/viewvc/trunk@1033 8cb11bc2-c004-0410-86c3-e597b4017df7 --- tparse/tparse.h | 4 +++- tparse/tparsemodule.cpp | 29 +++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/tparse/tparse.h b/tparse/tparse.h index 785ce6e3..55319f7c 100644 --- a/tparse/tparse.h +++ b/tparse/tparse.h @@ -34,13 +34,15 @@ #ifndef __PARSE_H #define __PARSE_H #include -#include +#include #include #include #include #include #define delstr(a) if (a != NULL) { delete [] a; a=NULL; }; +using namespace std; + /* This class represents a exception that occured during the parsing of a file */ class RCSParseError diff --git a/tparse/tparsemodule.cpp b/tparse/tparsemodule.cpp index 4cd8ce37..a11d9b24 100644 --- a/tparse/tparsemodule.cpp +++ b/tparse/tparsemodule.cpp @@ -31,10 +31,18 @@ Version: $Id$ */ #include -#include #include "tparsemodule.h" #include "tparse.cpp" +#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) +#include // for auto_ptr +#include +typedef __gnu_cxx::stdio_filebuf stdio_filebuf; +#define GNUC_STDIO_FILEBUF_AVAILABLE +#endif + +using namespace std; + static PyMethodDef tparseMethods[] = { {"parse", tparse, METH_VARARGS, tparse__doc__}, {NULL, NULL} /* Sentinel */ @@ -270,17 +278,26 @@ static PyObject * tparse( PyObject *self, PyObject *args) istream *input; PyObject *file = NULL; PyObject *hsink; +#ifdef GNUC_STDIO_FILEBUF_AVAILABLE + auto_ptr rdbuf; +#endif if (PyArg_ParseTuple(args, "sO!", &filename, &PyInstance_Type, &hsink)) - input = new ifstream(filename, ios::nocreate | ios::in); + input = new ifstream(filename, ios::in); else if (PyArg_ParseTuple(args, "O!O!", &PyFile_Type, &file, &PyInstance_Type, &hsink)) { PyErr_Clear(); // Reset the exception PyArg_ParseTuple has raised. - input = new istdiostream(PyFile_AsFile(file)); - ((istdiostream *)input)->buffered(1); - // We need buffering as because otherwise, it reads the file 4096 - // bytes at a time, with no readahead. +#ifdef GNUC_STDIO_FILEBUF_AVAILABLE + rdbuf.reset(new stdio_filebuf(PyFile_AsFile(file), ios::in | ios::binary)); + input = new istream(rdbuf.get()); +#else + PyErr_SetString(PyExc_NotImplementedError, + "tparse only implements the parsing of filehandles " + "when compiled with GNU C++ version 3.1 or later - " + "please pass a filename instead"); + return NULL; +#endif } else return NULL;