1 /** @file
2     File object interface.
3 
4     Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5     This program and the accompanying materials are licensed and made available under
6     the terms and conditions of the BSD License that accompanies this distribution.
7     The full text of the license may be found at
8     http://opensource.org/licenses/bsd-license.
9 
10     THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11     WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13 
14 /* File object interface */
15 
16 #ifndef Py_FILEOBJECT_H
17 #define Py_FILEOBJECT_H
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 typedef struct {
23     PyObject_HEAD
24     FILE *f_fp;
25     PyObject *f_name;
26     PyObject *f_mode;
27     int (*f_close)(FILE *);
28     int f_softspace;            /* Flag used by 'print' command */
29     int f_binary;               /* Flag which indicates whether the file is
30                                open in binary (1) or text (0) mode */
31     char* f_buf;                /* Allocated readahead buffer */
32     char* f_bufend;             /* Points after last occupied position */
33     char* f_bufptr;             /* Current buffer position */
34     char *f_setbuf;             /* Buffer for setbuf(3) and setvbuf(3) */
35     int f_univ_newline;         /* Handle any newline convention */
36     int f_newlinetypes;         /* Types of newlines seen */
37     int f_skipnextlf;           /* Skip next \n */
38     PyObject *f_encoding;
39     PyObject *f_errors;
40     PyObject *weakreflist; /* List of weak references */
41     int unlocked_count;         /* Num. currently running sections of code
42                                using f_fp with the GIL released. */
43     int readable;
44     int writable;
45 } PyFileObject;
46 
47 PyAPI_DATA(PyTypeObject) PyFile_Type;
48 
49 #define PyFile_Check(op) PyObject_TypeCheck(op, &PyFile_Type)
50 #define PyFile_CheckExact(op) (Py_TYPE(op) == &PyFile_Type)
51 
52 PyAPI_FUNC(PyObject *) PyFile_FromString(char *, char *);
53 PyAPI_FUNC(void) PyFile_SetBufSize(PyObject *, int);
54 PyAPI_FUNC(int) PyFile_SetEncoding(PyObject *, const char *);
55 PyAPI_FUNC(int) PyFile_SetEncodingAndErrors(PyObject *, const char *, char *errors);
56 PyAPI_FUNC(PyObject *) PyFile_FromFile(FILE *, char *, char *,
57                                              int (*)(FILE *));
58 PyAPI_FUNC(FILE *) PyFile_AsFile(PyObject *);
59 PyAPI_FUNC(void) PyFile_IncUseCount(PyFileObject *);
60 PyAPI_FUNC(void) PyFile_DecUseCount(PyFileObject *);
61 PyAPI_FUNC(PyObject *) PyFile_Name(PyObject *);
62 PyAPI_FUNC(PyObject *) PyFile_GetLine(PyObject *, int);
63 PyAPI_FUNC(int) PyFile_WriteObject(PyObject *, PyObject *, int);
64 PyAPI_FUNC(int) PyFile_SoftSpace(PyObject *, int);
65 PyAPI_FUNC(int) PyFile_WriteString(const char *, PyObject *);
66 PyAPI_FUNC(int) PyObject_AsFileDescriptor(PyObject *);
67 
68 /* The default encoding used by the platform file system APIs
69    If non-NULL, this is different than the default encoding for strings
70 */
71 PyAPI_DATA(const char *) Py_FileSystemDefaultEncoding;
72 
73 /* Routines to replace fread() and fgets() which accept any of \r, \n
74    or \r\n as line terminators.
75 */
76 #define PY_STDIOTEXTMODE "b"
77 char *Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *);
78 size_t Py_UniversalNewlineFread(char *, size_t, FILE *, PyObject *);
79 
80 /* A routine to do sanity checking on the file mode string.  returns
81    non-zero on if an exception occurred
82 */
83 int _PyFile_SanitizeMode(char *mode);
84 
85 //#if defined _MSC_VER && _MSC_VER >= 1400
86 /* A routine to check if a file descriptor is valid on Windows.  Returns 0
87  * and sets errno to EBADF if it isn't.  This is to avoid Assertions
88  * from various functions in the Windows CRT beginning with
89  * Visual Studio 2005
90  */
91 //int _PyVerify_fd(int fd);
92 //#elif defined _MSC_VER && _MSC_VER >= 1200
93 /* fdopen doesn't set errno EBADF and crashes for large fd on debug build */
94 //#define _PyVerify_fd(fd) (_get_osfhandle(fd) >= 0)
95 //#else
96 #define _PyVerify_fd(A) (1) /* dummy */
97 //#endif
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 #endif /* !Py_FILEOBJECT_H */
103