aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llcommon/llfile.cpp
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llcommon/llfile.cpp
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llcommon/llfile.cpp')
-rw-r--r--linden/indra/llcommon/llfile.cpp270
1 files changed, 270 insertions, 0 deletions
diff --git a/linden/indra/llcommon/llfile.cpp b/linden/indra/llcommon/llfile.cpp
new file mode 100644
index 0000000..d9fd360
--- /dev/null
+++ b/linden/indra/llcommon/llfile.cpp
@@ -0,0 +1,270 @@
1/**
2 * @file llfile.cpp
3 * @author Michael Schlachter
4 * @date 2006-03-23
5 * @brief Implementation of cross-platform POSIX file buffer and c++
6 * stream classes.
7 *
8 * Copyright (c) 2006-2007, Linden Research, Inc.
9 *
10 * The source code in this file ("Source Code") is provided by Linden Lab
11 * to you under the terms of the GNU General Public License, version 2.0
12 * ("GPL"), unless you have obtained a separate licensing agreement
13 * ("Other License"), formally executed by you and Linden Lab. Terms of
14 * the GPL can be found in doc/GPL-license.txt in this distribution, or
15 * online at http://secondlife.com/developers/opensource/gplv2
16 *
17 * There are special exceptions to the terms and conditions of the GPL as
18 * it is applied to this Source Code. View the full text of the exception
19 * in the file doc/FLOSS-exception.txt in this software distribution, or
20 * online at http://secondlife.com/developers/opensource/flossexception
21 *
22 * By copying, modifying or distributing this software, you acknowledge
23 * that you have read and understood your obligations described above,
24 * and agree to abide by those obligations.
25 *
26 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
27 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
28 * COMPLETENESS OR PERFORMANCE.
29 */
30
31#include "llfile.h"
32#include "llstring.h"
33#include "llerror.h"
34
35using namespace std;
36
37// static
38int LLFile::mkdir(const char* dirname, int perms)
39{
40#if LL_WINDOWS
41 // permissions are ignored on Windows
42 std::string utf8dirname = dirname;
43 llutf16string utf16dirname = utf8str_to_utf16str(utf8dirname);
44 return _wmkdir(utf16dirname.c_str());
45#else
46 return ::mkdir(dirname, (mode_t)perms);
47#endif
48}
49
50// static
51LLFILE* LLFile::fopen(const char* filename, const char* mode) /* Flawfinder: ignore */
52{
53#if LL_WINDOWS
54 std::string utf8filename = filename;
55 std::string utf8mode = mode;
56 llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
57 llutf16string utf16mode = utf8str_to_utf16str(utf8mode);
58 return _wfopen(utf16filename.c_str(),utf16mode.c_str());
59#else
60 return ::fopen(filename,mode); /* Flawfinder: ignore */
61#endif
62}
63
64LLFILE* LLFile::_fsopen(const char* filename, const char* mode, int sharingFlag)
65{
66#if LL_WINDOWS
67 std::string utf8filename = filename;
68 std::string utf8mode = mode;
69 llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
70 llutf16string utf16mode = utf8str_to_utf16str(utf8mode);
71 return _wfsopen(utf16filename.c_str(),utf16mode.c_str(),sharingFlag);
72#else
73 llassert(0);//No corresponding function on non-windows
74 return NULL;
75#endif
76}
77
78int LLFile::remove(const char* filename)
79{
80#if LL_WINDOWS
81 std::string utf8filename = filename;
82 llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
83 return _wremove(utf16filename.c_str());
84#else
85 return ::remove(filename);
86#endif
87}
88
89int LLFile::rename(const char* filename, const char* newname)
90{
91#if LL_WINDOWS
92 std::string utf8filename = filename;
93 std::string utf8newname = newname;
94 llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
95 llutf16string utf16newname = utf8str_to_utf16str(utf8newname);
96 return _wrename(utf16filename.c_str(),utf16newname.c_str());
97#else
98 return ::rename(filename,newname);
99#endif
100}
101
102int LLFile::stat(const char* filename, llstat* filestatus)
103{
104#if LL_WINDOWS
105 std::string utf8filename = filename;
106 llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
107 return _wstat(utf16filename.c_str(),filestatus);
108#else
109 return ::stat(filename,filestatus);
110#endif
111}
112
113
114/***************** Modified file stream created to overcome the incorrect behaviour of posix fopen in windows *******************/
115
116#if USE_LLFILESTREAMS
117
118LLFILE * LLFile::_Fiopen(const char *filename, std::ios::openmode mode,int) // protection currently unused
119{ // open a file
120 static const char *mods[] =
121 { // fopen mode strings corresponding to valid[i]
122 "r", "w", "w", "a", "rb", "wb", "wb", "ab",
123 "r+", "w+", "a+", "r+b", "w+b", "a+b",
124 0};
125 static const int valid[] =
126 { // valid combinations of open flags
127 ios_base::in,
128 ios_base::out,
129 ios_base::out | ios_base::trunc,
130 ios_base::out | ios_base::app,
131 ios_base::in | ios_base::binary,
132 ios_base::out | ios_base::binary,
133 ios_base::out | ios_base::trunc | ios_base::binary,
134 ios_base::out | ios_base::app | ios_base::binary,
135 ios_base::in | ios_base::out,
136 ios_base::in | ios_base::out | ios_base::trunc,
137 ios_base::in | ios_base::out | ios_base::app,
138 ios_base::in | ios_base::out | ios_base::binary,
139 ios_base::in | ios_base::out | ios_base::trunc
140 | ios_base::binary,
141 ios_base::in | ios_base::out | ios_base::app
142 | ios_base::binary,
143 0};
144
145 FILE *fp = 0;
146 int n;
147 ios_base::openmode atendflag = mode & ios_base::ate;
148 ios_base::openmode norepflag = mode & ios_base::_Noreplace;
149
150 if (mode & ios_base::_Nocreate)
151 mode |= ios_base::in; // file must exist
152 mode &= ~(ios_base::ate | ios_base::_Nocreate | ios_base::_Noreplace);
153 for (n = 0; valid[n] != 0 && valid[n] != mode; ++n)
154 ; // look for a valid mode
155
156 if (valid[n] == 0)
157 return (0); // no valid mode
158 else if (norepflag && mode & (ios_base::out || ios_base::app)
159 && (fp = LLFile::fopen(filename, "r")) != 0) /* Flawfinder: ignore */
160 { // file must not exist, close and fail
161 fclose(fp);
162 return (0);
163 }
164 else if (fp != 0 && fclose(fp) != 0)
165 return (0); // can't close after test open
166// should open with protection here, if other than default
167 else if ((fp = LLFile::fopen(filename, mods[n])) == 0) /* Flawfinder: ignore */
168 return (0); // open failed
169
170 if (!atendflag || fseek(fp, 0, SEEK_END) == 0)
171 return (fp); // no need to seek to end, or seek succeeded
172
173 fclose(fp); // can't position at end
174 return (0);
175}
176
177/************** input file stream ********************************/
178
179void llifstream::close()
180{ // close the C stream
181 if (_Filebuffer && _Filebuffer->close() == 0)
182 {
183 _Myios::setstate(ios_base::failbit); /*Flawfinder: ignore*/
184 }
185}
186
187void llifstream::open(const char *_Filename,
188 ios_base::openmode _Mode,
189 int _Prot) /* Flawfinder: ignore */
190{ // open a C stream with specified mode
191
192 FILE* filep = LLFile::_Fiopen(_Filename,_Mode | ios_base::in, _Prot);
193 if(filep == NULL)
194 {
195 _Myios::setstate(ios_base::failbit); /*Flawfinder: ignore*/
196 return;
197 }
198 llassert(_Filebuffer == NULL);
199 _Filebuffer = new _Myfb(filep);
200 _Myios::init(_Filebuffer);
201}
202
203bool llifstream::is_open() const
204{ // test if C stream has been opened
205 if(_Filebuffer)
206 return (_Filebuffer->is_open());
207 return false;
208}
209llifstream::~llifstream()
210{
211 delete _Filebuffer;
212}
213
214llifstream::llifstream(const char *_Filename,
215 ios_base::openmode _Mode,
216 int _Prot)
217 : std::basic_istream< char , std::char_traits< char > >(NULL,true),_Filebuffer(NULL)
218
219{ // construct with named file and specified mode
220 open(_Filename, _Mode | ios_base::in, _Prot); /* Flawfinder: ignore */
221}
222
223
224/************** output file stream ********************************/
225
226bool llofstream::is_open() const
227{ // test if C stream has been opened
228 if(_Filebuffer)
229 return (_Filebuffer->is_open());
230 return false;
231}
232
233void llofstream::open(const char *_Filename,
234 ios_base::openmode _Mode,
235 int _Prot) /* Flawfinder: ignore */
236{ // open a C stream with specified mode
237
238 FILE* filep = LLFile::_Fiopen(_Filename,_Mode | ios_base::out, _Prot);
239 if(filep == NULL)
240 {
241 _Myios::setstate(ios_base::failbit); /*Flawfinder: ignore*/
242 return;
243 }
244 llassert(_Filebuffer==NULL);
245 _Filebuffer = new _Myfb(filep);
246 _Myios::init(_Filebuffer);
247}
248
249void llofstream::close()
250{ // close the C stream
251 llassert(_Filebuffer);
252 if (_Filebuffer->close() == 0)
253 _Myios::setstate(ios_base::failbit); /*Flawfinder: ignore*/
254}
255
256llofstream::llofstream(const char *_Filename,
257 std::ios_base::openmode _Mode,
258 int _Prot)
259 : std::basic_ostream<char,std::char_traits < char > >(NULL,true),_Filebuffer(NULL)
260{ // construct with named file and specified mode
261 open(_Filename, _Mode , _Prot); /* Flawfinder: ignore */
262}
263
264llofstream::~llofstream()
265{ // destroy the object
266 delete _Filebuffer;
267}
268
269#endif // #if USE_LLFILESTREAMS
270