blob: 235f777f5fefe7aefade2f4d78287d2f535d71b9 [file] [log] [blame]
Guido van Rossum049cd901996-12-05 23:30:48 +00001#ifndef CSTRINGIO_INCLUDED
2#define CSTRINGIO_INCLUDED
Martin v. Löwis522cf1f2002-03-30 08:57:12 +00003#ifdef __cplusplus
4extern "C" {
5#endif
Guido van Rossum049cd901996-12-05 23:30:48 +00006/*
7
Guido van Rossum049cd901996-12-05 23:30:48 +00008 This header provides access to cStringIO objects from C.
9 Functions are provided for calling cStringIO objects and
10 macros are provided for testing whether you have cStringIO
11 objects.
12
13 Before calling any of the functions or macros, you must initialize
14 the routines with:
15
Guido van Rossum7999bfb1999-01-25 21:36:13 +000016 PycString_IMPORT
Guido van Rossum049cd901996-12-05 23:30:48 +000017
18 This would typically be done in your init function.
19
Guido van Rossum049cd901996-12-05 23:30:48 +000020*/
21
Thomas Wouters7e474022000-07-16 12:04:32 +000022/* Basic functions to manipulate cStringIO objects from C */
Guido van Rossum049cd901996-12-05 23:30:48 +000023
Guido van Rossum0a73dd51997-04-09 17:34:28 +000024static struct PycStringIO_CAPI {
25
26 /* Read a string. If the last argument is -1, the remainder will be read. */
Tim Petersdbd9ba62000-07-09 03:09:57 +000027 int(*cread)(PyObject *, char **, int);
Guido van Rossum049cd901996-12-05 23:30:48 +000028
Guido van Rossum0a73dd51997-04-09 17:34:28 +000029 /* Read a line */
Tim Petersdbd9ba62000-07-09 03:09:57 +000030 int(*creadline)(PyObject *, char **);
Guido van Rossum049cd901996-12-05 23:30:48 +000031
Guido van Rossum0a73dd51997-04-09 17:34:28 +000032 /* Write a string */
Tim Petersdbd9ba62000-07-09 03:09:57 +000033 int(*cwrite)(PyObject *, char *, int);
Guido van Rossum049cd901996-12-05 23:30:48 +000034
Guido van Rossum0a73dd51997-04-09 17:34:28 +000035 /* Get the cStringIO object as a Python string */
Tim Petersdbd9ba62000-07-09 03:09:57 +000036 PyObject *(*cgetvalue)(PyObject *);
Guido van Rossum049cd901996-12-05 23:30:48 +000037
Guido van Rossum0a73dd51997-04-09 17:34:28 +000038 /* Create a new output object */
Tim Petersdbd9ba62000-07-09 03:09:57 +000039 PyObject *(*NewOutput)(int);
Guido van Rossum049cd901996-12-05 23:30:48 +000040
Guido van Rossum0a73dd51997-04-09 17:34:28 +000041 /* Create an input object from a Python string */
Tim Petersdbd9ba62000-07-09 03:09:57 +000042 PyObject *(*NewInput)(PyObject *);
Guido van Rossum049cd901996-12-05 23:30:48 +000043
Guido van Rossum0a73dd51997-04-09 17:34:28 +000044 /* The Python types for cStringIO input and output objects.
45 Note that you can do input on an output object.
46 */
47 PyTypeObject *InputType, *OutputType;
48
49} * PycStringIO = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +000050
51/* These can be used to test if you have one */
Guido van Rossumd81a1ba1997-01-06 22:50:12 +000052#define PycStringIO_InputCheck(O) \
Guido van Rossum0a73dd51997-04-09 17:34:28 +000053 ((O)->ob_type==PycStringIO->InputType)
Guido van Rossumd81a1ba1997-01-06 22:50:12 +000054#define PycStringIO_OutputCheck(O) \
Guido van Rossum0a73dd51997-04-09 17:34:28 +000055 ((O)->ob_type==PycStringIO->OutputType)
Guido van Rossum049cd901996-12-05 23:30:48 +000056
Guido van Rossumf0f36001998-12-08 13:23:22 +000057static void *
Thomas Wouters78890102000-07-22 19:25:51 +000058xxxPyCObject_Import(char *module_name, char *name)
Guido van Rossum142eeb81997-08-13 03:14:41 +000059{
60 PyObject *m, *c;
61 void *r=NULL;
62
63 if((m=PyImport_ImportModule(module_name)))
64 {
65 if((c=PyObject_GetAttrString(m,name)))
66 {
67 r=PyCObject_AsVoidPtr(c);
68 Py_DECREF(c);
69 }
70 Py_DECREF(m);
71 }
72
73 return r;
74}
75
Guido van Rossum049cd901996-12-05 23:30:48 +000076#define PycString_IMPORT \
Martin v. Löwiseefa9642001-06-09 07:59:43 +000077 PycStringIO=(struct PycStringIO_CAPI*)xxxPyCObject_Import("cStringIO", "cStringIO_CAPI")
Guido van Rossum049cd901996-12-05 23:30:48 +000078
Martin v. Löwis522cf1f2002-03-30 08:57:12 +000079#ifdef __cplusplus
80}
81#endif
Guido van Rossum049cd901996-12-05 23:30:48 +000082#endif /* CSTRINGIO_INCLUDED */