summaryrefslogtreecommitdiff
path: root/indra/llcommon/llfile.h
blob: d59e68367e8cc9429a15397c506455999314fc1a (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/** 
 * @file llfile.h
 * @author Michael Schlachter
 * @date 2006-03-23
 * @brief Declaration of cross-platform POSIX file buffer and c++
 * stream classes.
 *
 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#ifndef LL_LLFILE_H
#define LL_LLFILE_H

/**
 * This class provides a cross platform interface to the filesystem.
 * Attempts to mostly mirror the POSIX style IO functions.
 */

typedef FILE	LLFILE;

#include <fstream>
#include <sys/stat.h>

#if LL_WINDOWS
// windows version of stat function and stat data structure are called _stat
typedef struct _stat	llstat;
#else
typedef struct stat		llstat;
#include <ext/stdio_filebuf.h>
#include <bits/postypes.h>
#endif

#ifndef S_ISREG
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
#endif

#ifndef S_ISDIR
# define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
#endif

#include "llstring.h" // safe char* -> std::string conversion

class LL_COMMON_API LLFile
{
public:
	// All these functions take UTF8 path/filenames.
	static	LLFILE*	fopen(const std::string& filename,const char* accessmode);	/* Flawfinder: ignore */
	static	LLFILE*	_fsopen(const std::string& filename,const char* accessmode,int	sharingFlag);

	static	int		close(LLFILE * file);

	// perms is a permissions mask like 0777 or 0700.  In most cases it will
	// be overridden by the user's umask.  It is ignored on Windows.
	static	int		mkdir(const std::string& filename, int perms = 0700);

	static	int		rmdir(const std::string& filename);
	static	int		remove(const std::string& filename);
	static	int		rename(const std::string& filename,const std::string&	newname);
	static	int		stat(const std::string&	filename,llstat*	file_status);
	static	bool	isdir(const std::string&	filename);
	static	bool	isfile(const std::string&	filename);
	static	LLFILE *	_Fiopen(const std::string& filename, 
			std::ios::openmode mode);

	static  const char * tmpdir();
};

/**
 *  @brief Provides a layer of compatibility for C/POSIX.
 *
 *  This is taken from both the GNU __gnu_cxx::stdio_filebuf extension and 
 *  VC's basic_filebuf implementation.
 *  This file buffer provides extensions for working with standard C FILE*'s 
 *  and POSIX file descriptors for platforms that support this.
*/
namespace
{
#if LL_WINDOWS
typedef std::filebuf						_Myfb;
#else
typedef  __gnu_cxx::stdio_filebuf< char >	_Myfb;
typedef std::__c_file						_Filet;
#endif /* LL_WINDOWS */
}

class LL_COMMON_API llstdio_filebuf : public _Myfb
{
public:
	/**
	 * deferred initialization / destruction
	*/
	llstdio_filebuf() : _Myfb() {}
	virtual ~llstdio_filebuf() {} 

	/**
	 *  @param  f  An open @c FILE*.
	 *  @param  mode  Same meaning as in a standard filebuf.
	 *  @param  size  Optimal or preferred size of internal buffer, in chars.
	 *                Defaults to system's @c BUFSIZ.
	 *
	 *  This constructor associates a file stream buffer with an open
	 *  C @c FILE*.  The @c FILE* will not be automatically closed when the
	 *  stdio_filebuf is closed/destroyed.
	*/
	llstdio_filebuf(_Filet* __f, std::ios_base::openmode __mode,
		    //size_t __size = static_cast<size_t>(BUFSIZ)) :
		    size_t __size = static_cast<size_t>(1)) :
#if LL_WINDOWS
		_Myfb(__f) {}
#else
		_Myfb(__f, __mode, __size) {}
#endif

	/**
	 *  @brief  Opens an external file.
	 *  @param  s  The name of the file.
	 *  @param  mode  The open mode flags.
	 *  @return  @c this on success, NULL on failure
	 *
	 *  If a file is already open, this function immediately fails.
	 *  Otherwise it tries to open the file named @a s using the flags
	 *  given in @a mode.
	*/
	//llstdio_filebuf* open(const char *_Filename,
	//		std::ios_base::openmode _Mode);

	/**
	 *  @param  fd  An open file descriptor.
	 *  @param  mode  Same meaning as in a standard filebuf.
	 *  @param  size  Optimal or preferred size of internal buffer, in chars.
	 *
	 *  This constructor associates a file stream buffer with an open
	 *  POSIX file descriptor. The file descriptor will be automatically
	 *  closed when the stdio_filebuf is closed/destroyed.
	*/
#if !LL_WINDOWS
	llstdio_filebuf(int __fd, std::ios_base::openmode __mode,
		//size_t __size = static_cast<size_t>(BUFSIZ)) :
		size_t __size = static_cast<size_t>(1)) :
		_Myfb(__fd, __mode, __size) {}
#endif

// *TODO: Seek the underlying c stream for better cross-platform compatibility?
#if !LL_WINDOWS
protected:
	/** underflow() and uflow() functions are called to get the next
	 *  character from the real input source when the buffer is empty.
	 *  Buffered input uses underflow()
	*/
	/*virtual*/ int_type underflow();

	/*  Convert internal byte sequence to external, char-based
	 * sequence via codecvt.
	*/
	bool _convert_to_external(char_type*, std::streamsize);

	/** The overflow() function is called to transfer characters to the
	 *  real output destination when the buffer is full. A call to
	 *  overflow(c) outputs the contents of the buffer plus the
	 *  character c.
	 *  Consume some sequence of the characters in the pending sequence.
	*/
	/*virtual*/ int_type overflow(int_type __c = traits_type::eof());

	/** sync() flushes the underlying @c FILE* stream.
	*/
	/*virtual*/ int sync();

	std::streamsize xsgetn(char_type*, std::streamsize);
	std::streamsize xsputn(char_type*, std::streamsize);
#endif
};


/**
 *  @brief  Controlling input for files.
 *
 *  This class supports reading from named files, using the inherited
 *  functions from std::basic_istream.  To control the associated
 *  sequence, an instance of std::basic_filebuf (or a platform-specific derivative)
 *  which allows construction using a pre-exisintg file stream buffer. 
 *  We refer to this std::basic_filebuf (or derivative) as @c sb.
*/
class LL_COMMON_API llifstream	:	public	std::istream
{
	// input stream associated with a C stream
public:
	// Constructors:
	/**
	 *  @brief  Default constructor.
	 *
	 *  Initializes @c sb using its default constructor, and passes
	 *  @c &sb to the base class initializer.  Does not open any files
	 *  (you haven't given it a filename to open).
	*/
	llifstream();

	/**
	 *  @brief  Create an input file stream.
	 *  @param  Filename  String specifying the filename.
	 *  @param  Mode  Open file in specified mode (see std::ios_base).
	 *
     *  @c ios_base::in is automatically included in @a mode.
	*/
	explicit llifstream(const std::string& _Filename,
			ios_base::openmode _Mode = ios_base::in);
	explicit llifstream(const char* _Filename,
			ios_base::openmode _Mode = ios_base::in);

	/**
	 *  @brief  Create a stream using an open c file stream.
	 *  @param  File  An open @c FILE*.
        @param  Mode  Same meaning as in a standard filebuf.
        @param  Size  Optimal or preferred size of internal buffer, in chars.
                      Defaults to system's @c BUFSIZ.
	*/
	explicit llifstream(_Filet *_File,
			ios_base::openmode _Mode = ios_base::in,
			//size_t _Size = static_cast<size_t>(BUFSIZ));
			size_t _Size = static_cast<size_t>(1));
	
	/**
	 *  @brief  Create a stream using an open file descriptor.
	 *  @param  fd    An open file descriptor.
        @param  Mode  Same meaning as in a standard filebuf.
        @param  Size  Optimal or preferred size of internal buffer, in chars.
                      Defaults to system's @c BUFSIZ.
	*/
#if !LL_WINDOWS
	explicit llifstream(int __fd,
			ios_base::openmode _Mode = ios_base::in,
			//size_t _Size = static_cast<size_t>(BUFSIZ));
			size_t _Size = static_cast<size_t>(1));
#endif

	/**
	 *  @brief  The destructor does nothing.
	 *
	 *  The file is closed by the filebuf object, not the formatting
	 *  stream.
	*/
	virtual ~llifstream() {}

	// Members:
	/**
	 *  @brief  Accessing the underlying buffer.
	 *  @return  The current basic_filebuf buffer.
	 *
	 *  This hides both signatures of std::basic_ios::rdbuf().
	*/
	llstdio_filebuf* rdbuf() const
	{ return const_cast<llstdio_filebuf*>(&_M_filebuf); }

	/**
	 *  @brief  Wrapper to test for an open file.
	 *  @return  @c rdbuf()->is_open()
	*/
	bool is_open() const;

	/**
	 *  @brief  Opens an external file.
	 *  @param  Filename  The name of the file.
	 *  @param  Node  The open mode flags.
	 *
	 *  Calls @c llstdio_filebuf::open(s,mode|in).  If that function
	 *  fails, @c failbit is set in the stream's error state.
	*/
	void open(const std::string& _Filename,
			ios_base::openmode _Mode = ios_base::in)
	{ open(_Filename.c_str(), _Mode); }
	void open(const char* _Filename,
			ios_base::openmode _Mode = ios_base::in);

	/**
	 *  @brief  Close the file.
	 *
	 *  Calls @c llstdio_filebuf::close().  If that function
	 *  fails, @c failbit is set in the stream's error state.
	*/
	void close();

private:
	llstdio_filebuf _M_filebuf;
};


/**
 *  @brief  Controlling output for files.
 *
 *  This class supports writing to named files, using the inherited
 *  functions from std::basic_ostream.  To control the associated
 *  sequence, an instance of std::basic_filebuf (or a platform-specific derivative)
 *  which allows construction using a pre-exisintg file stream buffer. 
 *  We refer to this std::basic_filebuf (or derivative) as @c sb.
*/
class LL_COMMON_API llofstream	:	public	std::ostream
{
public:
	// Constructors:
	/**
	 *  @brief  Default constructor.
	 *
	 *  Initializes @c sb using its default constructor, and passes
	 *  @c &sb to the base class initializer.  Does not open any files
	 *  (you haven't given it a filename to open).
	*/
	llofstream();

	/**
	 *  @brief  Create an output file stream.
	 *  @param  Filename  String specifying the filename.
	 *  @param  Mode  Open file in specified mode (see std::ios_base).
	 *
	 *  @c ios_base::out|ios_base::trunc is automatically included in
	 *  @a mode.
	*/
	explicit llofstream(const std::string& _Filename,
			ios_base::openmode _Mode = ios_base::out|ios_base::trunc);
	explicit llofstream(const char* _Filename,
			ios_base::openmode _Mode = ios_base::out|ios_base::trunc);

	/**
	 *  @brief  Create a stream using an open c file stream.
	 *  @param  File  An open @c FILE*.
        @param  Mode  Same meaning as in a standard filebuf.
        @param  Size  Optimal or preferred size of internal buffer, in chars.
                      Defaults to system's @c BUFSIZ.
	*/
	explicit llofstream(_Filet *_File,
			ios_base::openmode _Mode = ios_base::out,
			//size_t _Size = static_cast<size_t>(BUFSIZ));
			size_t _Size = static_cast<size_t>(1));

	/**
	 *  @brief  Create a stream using an open file descriptor.
	 *  @param  fd    An open file descriptor.
        @param  Mode  Same meaning as in a standard filebuf.
        @param  Size  Optimal or preferred size of internal buffer, in chars.
                      Defaults to system's @c BUFSIZ.
	*/
#if !LL_WINDOWS
	explicit llofstream(int __fd,
			ios_base::openmode _Mode = ios_base::out,
			//size_t _Size = static_cast<size_t>(BUFSIZ));
			size_t _Size = static_cast<size_t>(1));
#endif

	/**
	 *  @brief  The destructor does nothing.
	 *
	 *  The file is closed by the filebuf object, not the formatting
	 *  stream.
	*/
	virtual ~llofstream() {}

	// Members:
	/**
	 *  @brief  Accessing the underlying buffer.
	 *  @return  The current basic_filebuf buffer.
	 *
	 *  This hides both signatures of std::basic_ios::rdbuf().
	*/
	llstdio_filebuf* rdbuf() const
	{ return const_cast<llstdio_filebuf*>(&_M_filebuf); }

	/**
	 *  @brief  Wrapper to test for an open file.
	 *  @return  @c rdbuf()->is_open()
	*/
	bool is_open() const;

	/**
	 *  @brief  Opens an external file.
	 *  @param  Filename  The name of the file.
	 *  @param  Node  The open mode flags.
	 *
	 *  Calls @c llstdio_filebuf::open(s,mode|out).  If that function
	 *  fails, @c failbit is set in the stream's error state.
	*/
	void open(const std::string& _Filename,
			ios_base::openmode _Mode = ios_base::out|ios_base::trunc)
	{ open(_Filename.c_str(), _Mode); }
	void open(const char* _Filename,
			ios_base::openmode _Mode = ios_base::out|ios_base::trunc);

	/**
	 *  @brief  Close the file.
	 *
	 *  Calls @c llstdio_filebuf::close().  If that function
	 *  fails, @c failbit is set in the stream's error state.
	*/
	void close();

private:
	llstdio_filebuf _M_filebuf;
};


/**
 * @breif filesize helpers.
 *
 * The file size helpers are not considered particularly efficient,
 * and should only be used for config files and the like -- not in a
 * loop.
 */
std::streamsize LL_COMMON_API llifstream_size(llifstream& fstr);
std::streamsize LL_COMMON_API llofstream_size(llofstream& fstr);

#endif // not LL_LLFILE_H