File.h revision 60a63aea48af3c5fb34a808e97cb5227eebbaf00
1//===-- File.h --------------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_File_h_
11#define liblldb_File_h_
12#if defined(__cplusplus)
13
14#include "lldb/lldb-private.h"
15#include "lldb/Host/FileSpec.h"
16
17namespace lldb_private {
18
19//----------------------------------------------------------------------
20/// @class File File.h "lldb/Host/File.h"
21/// @brief A file class.
22///
23/// A file class that divides abstracts the LLDB core from host file
24/// functionality.
25//----------------------------------------------------------------------
26class File
27{
28public:
29
30    enum OpenOptions
31    {
32        eOpenOptionRead                 = (1u << 0),    // Open file for reading
33        eOpenOptionWrite                = (1u << 1),    // Open file for writing
34        eOpenOptionAppend               = (1u << 2),    // Don't truncate file when opening, append to end of file
35        eOpenOptionNonBlocking          = (1u << 3),    // File reads
36        eOpenOptionCanCreate            = (1u << 4),    // Create file if doesn't already exist
37        eOpenOptionCanCreateNewOnly     = (1u << 5),    // Can create file only if it doesn't already exist
38        eOpenOptionTruncate             = (1u << 6),    // Truncate file when opening existing
39        eOpenOptionSharedLock           = (1u << 7),    // Open file and get shared lock
40        eOpenOptionExclusiveLock        = (1u << 8)     // Open file and get exclusive lock
41    };
42
43    enum Permissions
44    {
45        ePermissionsUserRead        = (1u << 0),
46        ePermissionsUserWrite       = (1u << 1),
47        ePermissionsUserExecute     = (1u << 2),
48        ePermissionsGroupRead       = (1u << 3),
49        ePermissionsGroupWrite      = (1u << 4),
50        ePermissionsGroupExecute    = (1u << 5),
51        ePermissionsWorldRead       = (1u << 6),
52        ePermissionsWorldWrite      = (1u << 7),
53        ePermissionsWorldExecute    = (1u << 8)
54    };
55
56    File() :
57        m_file_spec (),
58        m_file_desc (-1)
59    {
60    }
61
62    //------------------------------------------------------------------
63    /// Constructor with path.
64    ///
65    /// Takes a path to a file which can be just a filename, or a full
66    /// path. If \a path is not NULL or empty, this function will call
67    /// FileSpec::SetFile (const char *path, bool resolve).
68    ///
69    /// @param[in] path
70    ///     The full or partial path to a file.
71    ///
72    /// @param[in] options
73    ///     Options to use when opening (see OpenOptions)
74    ///
75    /// @param[in] permissions
76    ///     Options to use when opening (see OpenOptions)
77    ///
78    /// @see FileSpec::SetFile (const char *path, bool resolve)
79    //------------------------------------------------------------------
80    File (const char *path,
81          uint32_t options,
82          uint32_t permissions);
83
84    //------------------------------------------------------------------
85    /// Destructor.
86    ///
87    /// The destructor is virtual in case this class is subclassed.
88    //------------------------------------------------------------------
89    virtual
90    ~File ();
91
92    bool
93    IsValid () const
94    {
95        return m_file_desc >= 0;
96    }
97
98    //------------------------------------------------------------------
99    /// Convert to pointer operator.
100    ///
101    /// This allows code to check a File object to see if it
102    /// contains anything valid using code such as:
103    ///
104    /// @code
105    /// File file(...);
106    /// if (file)
107    /// { ...
108    /// @endcode
109    ///
110    /// @return
111    ///     A pointer to this object if either the directory or filename
112    ///     is valid, NULL otherwise.
113    //------------------------------------------------------------------
114    operator
115    bool () const
116    {
117        return m_file_desc >= 0;
118    }
119
120    //------------------------------------------------------------------
121    /// Logical NOT operator.
122    ///
123    /// This allows code to check a File object to see if it is
124    /// invalid using code such as:
125    ///
126    /// @code
127    /// File file(...);
128    /// if (!file)
129    /// { ...
130    /// @endcode
131    ///
132    /// @return
133    ///     Returns \b true if the object has an empty directory and
134    ///     filename, \b false otherwise.
135    //------------------------------------------------------------------
136    bool
137    operator! () const
138    {
139        return m_file_desc < 0;
140    }
141
142    //------------------------------------------------------------------
143    /// Get the file spec for this file.
144    ///
145    /// @return
146    ///     A reference to the file specification object.
147    //------------------------------------------------------------------
148    const FileSpec &
149    GetFileSpec () const
150    {
151        return m_file_spec;
152    }
153
154    Error
155    Open (const char *path,
156          uint32_t options,
157          uint32_t permissions);
158
159    Error
160    Close ();
161
162    Error
163    Read (void *dst, size_t &num_bytes);
164
165    Error
166    Write (const void *src, size_t &num_bytes);
167
168protected:
169    //------------------------------------------------------------------
170    // Member variables
171    //------------------------------------------------------------------
172    FileSpec m_file_spec;   ///< The file specific for the current file (if any)
173    int m_file_desc;    ///< The open file handle or NULL if the file isn't opened
174};
175
176} // namespace lldb_private
177
178#endif  // #if defined(__cplusplus)
179#endif  // liblldb_FileSpec_h_
180