blob: 1b540f902f1a74226b2eb60a104b9d6ae5271ab0 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* File object interface */
3
Fred Drakeea9cb5a2000-07-09 00:20:36 +00004#ifndef Py_FILEOBJECT_H
5#define Py_FILEOBJECT_H
6#ifdef __cplusplus
7extern "C" {
8#endif
9
Martin v. Löwisf6eebbb2002-03-15 17:42:16 +000010typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000011 PyObject_HEAD
12 FILE *f_fp;
13 PyObject *f_name;
14 PyObject *f_mode;
15 int (*f_close)(FILE *);
16 int f_softspace; /* Flag used by 'print' command */
17 int f_binary; /* Flag which indicates whether the file is
18 open in binary (1) or text (0) mode */
19 char* f_buf; /* Allocated readahead buffer */
20 char* f_bufend; /* Points after last occupied position */
21 char* f_bufptr; /* Current buffer position */
22 char *f_setbuf; /* Buffer for setbuf(3) and setvbuf(3) */
23 int f_univ_newline; /* Handle any newline convention */
24 int f_newlinetypes; /* Types of newlines seen */
25 int f_skipnextlf; /* Skip next \n */
26 PyObject *f_encoding;
27 PyObject *f_errors;
28 PyObject *weakreflist; /* List of weak references */
29 int unlocked_count; /* Num. currently running sections of code
30 using f_fp with the GIL released. */
31 int readable;
32 int writable;
Martin v. Löwisf6eebbb2002-03-15 17:42:16 +000033} PyFileObject;
34
Mark Hammond91a681d2002-08-12 07:21:58 +000035PyAPI_DATA(PyTypeObject) PyFile_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
Tim Peters59c9a642001-09-13 05:38:56 +000037#define PyFile_Check(op) PyObject_TypeCheck(op, &PyFile_Type)
Christian Heimese93237d2007-12-19 02:37:44 +000038#define PyFile_CheckExact(op) (Py_TYPE(op) == &PyFile_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039
Mark Hammond91a681d2002-08-12 07:21:58 +000040PyAPI_FUNC(PyObject *) PyFile_FromString(char *, char *);
41PyAPI_FUNC(void) PyFile_SetBufSize(PyObject *, int);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +000042PyAPI_FUNC(int) PyFile_SetEncoding(PyObject *, const char *);
Martin v. Löwis99815892008-06-01 07:20:46 +000043PyAPI_FUNC(int) PyFile_SetEncodingAndErrors(PyObject *, const char *, char *errors);
Mark Hammond91a681d2002-08-12 07:21:58 +000044PyAPI_FUNC(PyObject *) PyFile_FromFile(FILE *, char *, char *,
Fred Drakeea9cb5a2000-07-09 00:20:36 +000045 int (*)(FILE *));
Mark Hammond91a681d2002-08-12 07:21:58 +000046PyAPI_FUNC(FILE *) PyFile_AsFile(PyObject *);
Gregory P. Smithaa63d0d2008-04-06 23:11:17 +000047PyAPI_FUNC(void) PyFile_IncUseCount(PyFileObject *);
48PyAPI_FUNC(void) PyFile_DecUseCount(PyFileObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000049PyAPI_FUNC(PyObject *) PyFile_Name(PyObject *);
50PyAPI_FUNC(PyObject *) PyFile_GetLine(PyObject *, int);
51PyAPI_FUNC(int) PyFile_WriteObject(PyObject *, PyObject *, int);
52PyAPI_FUNC(int) PyFile_SoftSpace(PyObject *, int);
53PyAPI_FUNC(int) PyFile_WriteString(const char *, PyObject *);
54PyAPI_FUNC(int) PyObject_AsFileDescriptor(PyObject *);
Guido van Rossuma3309961993-07-28 09:05:47 +000055
Mark Hammond26cffde2001-05-14 12:17:34 +000056/* The default encoding used by the platform file system APIs
Tim Peters058b1412002-04-21 07:29:14 +000057 If non-NULL, this is different than the default encoding for strings
Mark Hammond26cffde2001-05-14 12:17:34 +000058*/
Mark Hammond91a681d2002-08-12 07:21:58 +000059PyAPI_DATA(const char *) Py_FileSystemDefaultEncoding;
Mark Hammond26cffde2001-05-14 12:17:34 +000060
Jack Jansen7b8c7542002-04-14 20:12:41 +000061/* Routines to replace fread() and fgets() which accept any of \r, \n
62 or \r\n as line terminators.
63*/
64#define PY_STDIOTEXTMODE "b"
65char *Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *);
Tim Peters058b1412002-04-21 07:29:14 +000066size_t Py_UniversalNewlineFread(char *, size_t, FILE *, PyObject *);
Skip Montanarodb608052004-02-07 13:53:46 +000067
Kristján Valur Jónsson0a440d42007-04-26 09:15:08 +000068/* A routine to do sanity checking on the file mode string. returns
69 non-zero on if an exception occurred
70*/
71int _PyFile_SanitizeMode(char *mode);
72
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +000073#if defined _MSC_VER && _MSC_VER >= 1400
74/* A routine to check if a file descriptor is valid on Windows. Returns 0
75 * and sets errno to EBADF if it isn't. This is to avoid Assertions
76 * from various functions in the Windows CRT beginning with
77 * Visual Studio 2005
78 */
79int _PyVerify_fd(int fd);
Hirokazu Yamamotobcd3ea82009-02-11 04:13:06 +000080#elif defined _MSC_VER && _MSC_VER >= 1200
81/* fdopen doesn't set errno EBADF and crashes for large fd on debug build */
82#define _PyVerify_fd(fd) (_get_osfhandle(fd) >= 0)
Kristján Valur Jónsson6a743d32009-02-10 13:32:24 +000083#else
84#define _PyVerify_fd(A) (1) /* dummy */
85#endif
86
Charles-François Natalifda7b372011-08-28 16:22:33 +020087/* A routine to check if a file descriptor can be select()-ed. */
Charles-François Natali605ee242011-08-28 16:43:24 +020088#ifdef HAVE_SELECT
Charles-François Natalifda7b372011-08-28 16:22:33 +020089 #define _PyIsSelectable_fd(FD) (((FD) >= 0) && ((FD) < FD_SETSIZE))
Charles-François Natali605ee242011-08-28 16:43:24 +020090#else
91 #define _PyIsSelectable_fd(FD) (1)
Charles-François Natalifda7b372011-08-28 16:22:33 +020092#endif /* HAVE_SELECT */
93
Guido van Rossuma3309961993-07-28 09:05:47 +000094#ifdef __cplusplus
95}
96#endif
97#endif /* !Py_FILEOBJECT_H */