PageRenderTime 55ms CodeModel.GetById 16ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/llcommon/llfile.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 233 lines | 138 code | 48 blank | 47 comment | 2 complexity | 4aa4d8f2ba27620f148fc822bfa008f6 MD5 | raw file
  1/** 
  2 * @file llfile.h
  3 * @author Michael Schlachter
  4 * @date 2006-03-23
  5 * @brief Declaration of cross-platform POSIX file buffer and c++
  6 * stream classes.
  7 *
  8 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  9 * Second Life Viewer Source Code
 10 * Copyright (C) 2010, Linden Research, Inc.
 11 * 
 12 * This library is free software; you can redistribute it and/or
 13 * modify it under the terms of the GNU Lesser General Public
 14 * License as published by the Free Software Foundation;
 15 * version 2.1 of the License only.
 16 * 
 17 * This library is distributed in the hope that it will be useful,
 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 20 * Lesser General Public License for more details.
 21 * 
 22 * You should have received a copy of the GNU Lesser General Public
 23 * License along with this library; if not, write to the Free Software
 24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 25 * 
 26 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 27 * $/LicenseInfo$
 28 */
 29
 30#ifndef LL_LLFILE_H
 31#define LL_LLFILE_H
 32
 33/**
 34 * This class provides a cross platform interface to the filesystem.
 35 * Attempts to mostly mirror the POSIX style IO functions.
 36 */
 37
 38typedef FILE	LLFILE;
 39
 40#include <fstream>
 41
 42#ifdef LL_WINDOWS
 43#define	USE_LLFILESTREAMS	1
 44#else
 45#define	USE_LLFILESTREAMS	0
 46#endif
 47
 48#include <sys/stat.h>
 49
 50#if LL_WINDOWS
 51// windows version of stat function and stat data structure are called _stat
 52typedef struct _stat	llstat;
 53#else
 54typedef struct stat		llstat;
 55#endif
 56
 57#ifndef S_ISREG
 58# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
 59#endif
 60
 61#ifndef S_ISDIR
 62# define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
 63#endif
 64
 65#include "llstring.h" // safe char* -> std::string conversion
 66
 67class LL_COMMON_API LLFile
 68{
 69public:
 70	// All these functions take UTF8 path/filenames.
 71	static	LLFILE*	fopen(const std::string& filename,const char* accessmode);	/* Flawfinder: ignore */
 72	static	LLFILE*	_fsopen(const std::string& filename,const char* accessmode,int	sharingFlag);
 73
 74	static	int		close(LLFILE * file);
 75
 76	// perms is a permissions mask like 0777 or 0700.  In most cases it will
 77	// be overridden by the user's umask.  It is ignored on Windows.
 78	static	int		mkdir(const std::string& filename, int perms = 0700);
 79
 80	static	int		rmdir(const std::string& filename);
 81	static	int		remove(const std::string& filename);
 82	static	int		rename(const std::string& filename,const std::string&	newname);
 83	static	int		stat(const std::string&	filename,llstat*	file_status);
 84	static	bool	isdir(const std::string&	filename);
 85	static	bool	isfile(const std::string&	filename);
 86	static	LLFILE *	_Fiopen(const std::string& filename, std::ios::openmode mode,int);	// protection currently unused
 87
 88	static  const char * tmpdir();
 89};
 90
 91
 92#if USE_LLFILESTREAMS
 93
 94class LL_COMMON_API llifstream	:	public	std::basic_istream < char , std::char_traits < char > >
 95{
 96	// input stream associated with a C stream
 97public:
 98	typedef std::basic_ifstream<char,std::char_traits < char > > _Myt;
 99	typedef std::basic_filebuf<char,std::char_traits< char > > _Myfb;
100	typedef std::basic_ios<char,std::char_traits< char > > _Myios;
101
102	llifstream()
103		: std::basic_istream<char,std::char_traits< char > >(NULL,true),_Filebuffer(NULL),_ShouldClose(false)
104	{	// construct unopened
105	}
106
107	explicit llifstream(const std::string& _Filename,
108		ios_base::openmode _Mode = ios_base::in,
109		int _Prot = (int)ios_base::_Openprot);
110
111	explicit llifstream(_Filet *_File)
112		: std::basic_istream<char,std::char_traits< char > >(NULL,true),
113			_Filebuffer(new _Myfb(_File)),
114			_ShouldClose(false)
115	{	// construct with specified C stream
116	}
117	virtual ~llifstream();
118
119	_Myfb *rdbuf() const
120	{	// return pointer to file buffer
121		return _Filebuffer;
122	}
123	bool is_open() const;
124	void open(const std::string& _Filename,	/* Flawfinder: ignore */
125		ios_base::openmode _Mode = ios_base::in,
126		int _Prot = (int)ios_base::_Openprot);	
127	void close();
128
129private:
130	_Myfb* _Filebuffer;	// the file buffer
131	bool _ShouldClose;
132};
133
134
135class LL_COMMON_API llofstream	:	public	std::basic_ostream< char , std::char_traits < char > >
136{
137public:
138	typedef std::basic_ostream< char , std::char_traits < char > > _Myt;
139	typedef std::basic_filebuf< char , std::char_traits < char > > _Myfb;
140	typedef std::basic_ios<char,std::char_traits < char > > _Myios;
141
142	llofstream()
143		: std::basic_ostream<char,std::char_traits < char > >(NULL,true),_Filebuffer(NULL),_ShouldClose(false)
144	{	// construct unopened
145	}
146
147	explicit llofstream(const std::string& _Filename,
148		std::ios_base::openmode _Mode = ios_base::out,
149		int _Prot = (int)std::ios_base::_Openprot);
150	
151
152	explicit llofstream(_Filet *_File)
153		: std::basic_ostream<char,std::char_traits < char > >(NULL,true),
154			_Filebuffer(new _Myfb(_File)),//_File)
155			_ShouldClose(false)
156	{	// construct with specified C stream
157	}
158
159	virtual ~llofstream();
160
161	_Myfb *rdbuf() const
162	{	// return pointer to file buffer
163		return _Filebuffer;
164	}
165
166	bool is_open() const;
167
168	void open(const std::string& _Filename,ios_base::openmode _Mode = ios_base::out,int _Prot = (int)ios_base::_Openprot);	/* Flawfinder: ignore */
169
170	void close();
171
172private:
173	_Myfb *_Filebuffer;	// the file buffer
174	bool _ShouldClose;
175};
176
177
178
179#else
180//Use standard file streams on non windows platforms
181//#define	llifstream	std::ifstream
182//#define	llofstream	std::ofstream
183
184class LL_COMMON_API llifstream	:	public	std::ifstream
185{
186public:
187	llifstream() : std::ifstream()
188	{
189	}
190
191	explicit llifstream(const std::string& _Filename, std::_Ios_Openmode _Mode = in)
192		: std::ifstream(_Filename.c_str(), _Mode)
193	{
194	}
195	void open(const std::string& _Filename, std::_Ios_Openmode _Mode = in)	/* Flawfinder: ignore */
196	{
197		std::ifstream::open(_Filename.c_str(), _Mode);
198	}
199};
200
201
202class LL_COMMON_API llofstream	:	public	std::ofstream
203{
204public:
205	llofstream() : std::ofstream()
206	{
207	}
208
209	explicit llofstream(const std::string& _Filename, std::_Ios_Openmode _Mode = out)
210		: std::ofstream(_Filename.c_str(), _Mode)
211	{
212	}
213
214	void open(const std::string& _Filename, std::_Ios_Openmode _Mode = out)	/* Flawfinder: ignore */
215	{
216		std::ofstream::open(_Filename.c_str(), _Mode);
217	}
218
219};
220
221#endif
222
223/**
224 * @breif filesize helpers.
225 *
226 * The file size helpers are not considered particularly efficient,
227 * and should only be used for config files and the like -- not in a
228 * loop.
229 */
230std::streamsize LL_COMMON_API llifstream_size(llifstream& fstr);
231std::streamsize LL_COMMON_API llofstream_size(llofstream& fstr);
232
233#endif // not LL_LLFILE_H