1//===-- SWIG Interface for SBFile -----------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9namespace lldb {
10
11%feature("docstring",
12"Represents a file."
13) SBFile;
14
15class SBFile
16{
17public:
18
19    SBFile();
20
21    %feature("docstring", "
22    Initialize a SBFile from a file descriptor.  mode is
23    'r', 'r+', or 'w', like fdopen.");
24    SBFile(int fd, const char *mode, bool transfer_ownership);
25
26    %feature("docstring", "initialize a SBFile from a python file object");
27    SBFile(FileSP file);
28
29    %extend {
30        static lldb::SBFile MakeBorrowed(lldb::FileSP BORROWED) {
31            return lldb::SBFile(BORROWED);
32        }
33        static lldb::SBFile MakeForcingIOMethods(lldb::FileSP FORCE_IO_METHODS) {
34            return lldb::SBFile(FORCE_IO_METHODS);
35        }
36        static lldb::SBFile MakeBorrowedForcingIOMethods(lldb::FileSP BORROWED_FORCE_IO_METHODS) {
37            return lldb::SBFile(BORROWED_FORCE_IO_METHODS);
38        }
39    }
40
41#ifdef SWIGPYTHON
42    %pythoncode {
43        @classmethod
44        def Create(cls, file, borrow=False, force_io_methods=False):
45            """
46            Create a SBFile from a python file object, with options.
47
48            If borrow is set then the underlying file will
49            not be closed when the SBFile is closed or destroyed.
50
51            If force_scripting_io is set then the python read/write
52            methods will be called even if a file descriptor is available.
53            """
54            if borrow:
55                if force_io_methods:
56                    return cls.MakeBorrowedForcingIOMethods(file)
57                else:
58                    return cls.MakeBorrowed(file)
59            else:
60                if force_io_methods:
61                    return cls.MakeForcingIOMethods(file)
62                else:
63                    return cls(file)
64    }
65#endif
66
67    ~SBFile ();
68
69    %feature("autodoc", "Read(buffer) -> SBError, bytes_read") Read;
70    SBError Read(uint8_t *buf, size_t num_bytes, size_t *OUTPUT);
71
72    %feature("autodoc", "Write(buffer) -> SBError, written_read") Write;
73    SBError Write(const uint8_t *buf, size_t num_bytes, size_t *OUTPUT);
74
75    void Flush();
76
77    bool IsValid() const;
78
79    operator bool() const;
80
81    SBError Close();
82
83    %feature("docstring", "
84    Convert this SBFile into a python io.IOBase file object.
85
86    If the SBFile is itself a wrapper around a python file object,
87    this will return that original object.
88
89    The file returned from here should be considered borrowed,
90    in the sense that you may read and write to it, and flush it,
91    etc, but you should not close it.   If you want to close the
92    SBFile, call SBFile.Close().
93
94    If there is no underlying python file to unwrap, GetFile will
95    use the file descriptor, if availble to create a new python
96    file object using `open(fd, mode=..., closefd=False)`
97    ");
98    FileSP GetFile();
99};
100
101} // namespace lldb
102