summaryrefslogtreecommitdiffstats
path: root/src/include/usr/util/utilfile.H
blob: ab8c4014d0b637cb8b027ea9d6f4d36e343dc4c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/include/usr/util/utilfile.H $                             */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2003,2014              */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

#ifndef UTILFILE_H
#define UTILFILE_H

/**
 * @file utilfile.H
 *
 * @brief File Stream manipulation
 *
 * Used for creating and manipulating file streams
 *
 */

/*****************************************************************************/
// I n c l u d e s
/*****************************************************************************/

// Utility Includes
#include <util/utilstream.H>
#include <util/utilmem.H>
#include <util/util_reasoncodes.H>

/*****************************************************************************/
// File Stream class
/*****************************************************************************/
/**
 *  @brief     Utility file stream class
 *
 *  Used for file streams
 *
 */
class UtilFile : public UtilStream
{
  public:

    /**
     *  @brief Default Constructor
     *
     *  Initializes internals to NULL and will thus cause an exception if
     *  an invalid operation is performed ( such as writing to the file
     *  without opening it )
     *
     */
    UtilFile();

    /**
     *  @brief Constructor
     *
     *  Initializes internals of the file stream object with the file path name.
     *
     */
    UtilFile(const char * i_filePathName);

    /**
     *  @brief Destructor
     *
     *  1. Closes the file ( if it's open )
     *  2. Deletes allocated resources
     *  3. Deletes any pending error logs ( effectively aborting )
     *
     */
    virtual ~UtilFile();

    /**
     *  @brief Determines if the file is available in the filesystem
     *
     *  Attempts to open the file for readonly to determine if it exists
     *  in the file system.  Secondary failures are not reported by
     *  this interface.
     *
     *  @return
     *      Bool: true, the file exists.  false, it doesn't exist or a
     *      secondary failure occurred while making the determination.
     *
     */
    bool exists( void ) const;

    /**
     *  @brief Determines if the file is available in the filesystem
     *
     *  This is a static function that to determine if it exists
     *  in the file system and is a REGULAR file.
     *  Secondary failures are not reported by this interface.
     *
     *  @return
     *      Bool: true, the file exists.  false, it doesn't exist or a
     *      secondary failure occurred while making the determination.
     *
     */
    static bool exists(const char * );

    /**
     *  @brief Open the file associated with the object
     *
     *  Opens the file based off of the file name associated with the object
     *  with the specified file mode by calling Open with the flag set to false
     *  to always open the flash file by default.
     *
     *  @return
     *      Nothing. Errors are deferred until the user calls getLastError
     *
     */
    void Open(
        const char * i_mode ///<Mode file is to be opened in
    );

    /**
     *  @brief Open the file associated with the object
     *
     *  Opens the file based off of the file name associated with the object
     *  with the specified file mode calling the open with the flag set to false
     *  to always open the flash file by default.
     *
     *  @return
     *      A error handle specifying the failure ( if any ).
     *
     */
    errlHndl_t open(
        const char * i_mode ///<Mode file is to be opened in
    )
    {
        Open(i_mode);
        return getLastError();
    }

    /**
     *  @brief Open a file for the specified access
     *
     *  If a file is currently open, it will be closed.  Then the
     *  specified file will be opened for the given access mode.
     *  Any failures are reported through the return code
     *
     *  @return
     *      Nothing. Errors are deferred until the user calls getLastError
     *
     */
    void Open(
        const char * i_file,    ///< Path filename
        const char * i_mode     ///< Mode file is to be opened in
    );

    /**
     *  @brief Open a file for the specified access
     *
     *  If a file is currently open, it will be closed.  Then the
     *  specified file will be opened for the given access mode.
     *  Any failures are reported through the return code
     *
     *  @return
     *      A error handle specifying the failure ( if any ).
     *
     */
    errlHndl_t open(
        const char * i_file,    ///< Path filename
        const char * i_mode     ///< Mode file is to be opened in
    )
    {
        Open(i_file,i_mode);
        return getLastError();
    }

    /**
     *  @brief Close the file associated with the object
     *
     *  Closes the file based off file pointer associated with the file.
     *
     *  @return
     *      Nothing. Errors are deferred until the user calls getLastError
     *
     */
    void Close();

    /**
     *  @brief Close the file associated with the object
     *
     *  Closes the file based off file pointer associated with the file.
     *
     *  @return
     *      A error handle specifying the failure ( if any ).
     *
     */
    errlHndl_t close()
    {
        Close();
        return getLastError();
    }

    /**
     *  @brief Reads data from current position of the stream
     *
     *  Reads data from the current postion of the stream for the
     *  specified number of bytes.
     *
     */
    uint32_t read(
        void * o_buffer,    ///< Buffer data is read into
        uint32_t i_size     ///< Size in bytes of data to be read
    );

    /**
     *  @brief Writes data to current position of the stream
     *
     *  Writes data to the current postion of the stream for the
     *  specified number of bytes.
     *
     *  @note This will assert in Hostboot! Writing of files is not supported.
     *
     */
    uint32_t write(
        const void *i_buffer,     ///< Source buffer data is written from
        uint32_t    i_size        ///< Size in bytes of data to be written
    );

    /**
     *  @brief Seek to the specified position in the stream
     *
     *  Performs a seek based on the specified position and offset.
     *
     */
    uint32_t seek(
        int i_pos,          ///< Seek offset in bytes
        whence i_whence     ///< Position to seek from
    );

    /**
     *  @brief Return the current size of the stream
     *
     *  Returns the current size of the stream.
     *
     */
    uint32_t size() const;

    /**
     * @brief Query the file's open/closed state
     *
     * Determines if the file is currently open
     *
     * @return bool value indicating the open status of the file
     *
    */
    bool isOpen( void ) const
    {
        return ( iv_contents.size() != 0 );
    }

    /**
     *  @brief Expose the filename
     *
     *  Return a const char * to the filename
     *
     *  @return Filename pointer
     *
     */
    const char * Name() const;

private:

    /**
     * @brief Change the object's filename
     *
     * Set the object's filename
     *
     * @param i_name
     *      New name, or null to simply get
     *
     * @return void
     *
    */
    void FileName( const char * i_name );

    // Instance variables
    char*       iv_filePathName;    ///< Name of file being operated on
    UtilMem     iv_contents;
};


/*****************************************************************************/
// Get Filename
/*****************************************************************************/
inline const char * UtilFile::Name() const
{
    return iv_filePathName;
}

#endif //UTILFILE_H
OpenPOWER on IntegriCloud