xref: /openbsd-src/gnu/llvm/lldb/bindings/interface/SBFile.i (revision be691f3bb6417f04a68938fadbcaee2d5795e764)
1061da546Spatrick //===-- SWIG Interface for SBFile -----------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick namespace lldb {
10061da546Spatrick 
11061da546Spatrick %feature("docstring",
12061da546Spatrick "Represents a file."
13061da546Spatrick ) SBFile;
14061da546Spatrick 
15061da546Spatrick class SBFile
16061da546Spatrick {
17061da546Spatrick public:
18061da546Spatrick 
19061da546Spatrick     SBFile();
20061da546Spatrick 
21061da546Spatrick     %feature("docstring", "
22061da546Spatrick     Initialize a SBFile from a file descriptor.  mode is
23061da546Spatrick     'r', 'r+', or 'w', like fdopen.");
24061da546Spatrick     SBFile(int fd, const char *mode, bool transfer_ownership);
25061da546Spatrick 
26061da546Spatrick     %feature("docstring", "initialize a SBFile from a python file object");
27061da546Spatrick     SBFile(FileSP file);
28061da546Spatrick 
29061da546Spatrick     %extend {
MakeBorrowed(lldb::FileSP BORROWED)30061da546Spatrick         static lldb::SBFile MakeBorrowed(lldb::FileSP BORROWED) {
31061da546Spatrick             return lldb::SBFile(BORROWED);
32061da546Spatrick         }
MakeForcingIOMethods(lldb::FileSP FORCE_IO_METHODS)33061da546Spatrick         static lldb::SBFile MakeForcingIOMethods(lldb::FileSP FORCE_IO_METHODS) {
34061da546Spatrick             return lldb::SBFile(FORCE_IO_METHODS);
35061da546Spatrick         }
MakeBorrowedForcingIOMethods(lldb::FileSP BORROWED_FORCE_IO_METHODS)36061da546Spatrick         static lldb::SBFile MakeBorrowedForcingIOMethods(lldb::FileSP BORROWED_FORCE_IO_METHODS) {
37061da546Spatrick             return lldb::SBFile(BORROWED_FORCE_IO_METHODS);
38061da546Spatrick         }
39061da546Spatrick     }
40061da546Spatrick 
41061da546Spatrick #ifdef SWIGPYTHON
42061da546Spatrick     %pythoncode {
43061da546Spatrick         @classmethod
44061da546Spatrick         def Create(cls, file, borrow=False, force_io_methods=False):
45061da546Spatrick             """
46061da546Spatrick             Create a SBFile from a python file object, with options.
47061da546Spatrick 
48061da546Spatrick             If borrow is set then the underlying file will
49061da546Spatrick             not be closed when the SBFile is closed or destroyed.
50061da546Spatrick 
51061da546Spatrick             If force_scripting_io is set then the python read/write
52061da546Spatrick             methods will be called even if a file descriptor is available.
53061da546Spatrick             """
54061da546Spatrick             if borrow:
55061da546Spatrick                 if force_io_methods:
56061da546Spatrick                     return cls.MakeBorrowedForcingIOMethods(file)
57061da546Spatrick                 else:
58061da546Spatrick                     return cls.MakeBorrowed(file)
59061da546Spatrick             else:
60061da546Spatrick                 if force_io_methods:
61061da546Spatrick                     return cls.MakeForcingIOMethods(file)
62061da546Spatrick                 else:
63061da546Spatrick                     return cls(file)
64061da546Spatrick     }
65061da546Spatrick #endif
66061da546Spatrick 
67061da546Spatrick     ~SBFile ();
68061da546Spatrick 
69061da546Spatrick     %feature("autodoc", "Read(buffer) -> SBError, bytes_read") Read;
70061da546Spatrick     SBError Read(uint8_t *buf, size_t num_bytes, size_t *OUTPUT);
71061da546Spatrick 
72061da546Spatrick     %feature("autodoc", "Write(buffer) -> SBError, written_read") Write;
73061da546Spatrick     SBError Write(const uint8_t *buf, size_t num_bytes, size_t *OUTPUT);
74061da546Spatrick 
75061da546Spatrick     void Flush();
76061da546Spatrick 
77061da546Spatrick     bool IsValid() const;
78061da546Spatrick 
79061da546Spatrick     operator bool() const;
80061da546Spatrick 
81061da546Spatrick     SBError Close();
82061da546Spatrick 
83061da546Spatrick     %feature("docstring", "
84061da546Spatrick     Convert this SBFile into a python io.IOBase file object.
85061da546Spatrick 
86061da546Spatrick     If the SBFile is itself a wrapper around a python file object,
87061da546Spatrick     this will return that original object.
88061da546Spatrick 
89061da546Spatrick     The file returned from here should be considered borrowed,
90061da546Spatrick     in the sense that you may read and write to it, and flush it,
91061da546Spatrick     etc, but you should not close it.   If you want to close the
92061da546Spatrick     SBFile, call SBFile.Close().
93061da546Spatrick 
94061da546Spatrick     If there is no underlying python file to unwrap, GetFile will
95dda28197Spatrick     use the file descriptor, if available to create a new python
96*be691f3bSpatrick     file object using ``open(fd, mode=..., closefd=False)``
97061da546Spatrick     ");
98061da546Spatrick     FileSP GetFile();
99061da546Spatrick };
100061da546Spatrick 
101061da546Spatrick } // namespace lldb
102