00001
00035 #include "linden_common.h"
00036 #include "llfile.h"
00037 #include "llstring.h"
00038 #include "llerror.h"
00039
00040 using namespace std;
00041
00042
00043 int LLFile::mkdir(const char* dirname, int perms)
00044 {
00045 #if LL_WINDOWS
00046
00047 std::string utf8dirname = dirname;
00048 llutf16string utf16dirname = utf8str_to_utf16str(utf8dirname);
00049 return _wmkdir(utf16dirname.c_str());
00050 #else
00051 return ::mkdir(dirname, (mode_t)perms);
00052 #endif
00053 }
00054
00055
00056 int LLFile::rmdir(const char* dirname)
00057 {
00058 #if LL_WINDOWS
00059
00060 std::string utf8dirname = dirname;
00061 llutf16string utf16dirname = utf8str_to_utf16str(utf8dirname);
00062 return _wrmdir(utf16dirname.c_str());
00063 #else
00064 return ::rmdir(dirname);
00065 #endif
00066 }
00067
00068
00069 LLFILE* LLFile::fopen(const char* filename, const char* mode)
00070 {
00071 #if LL_WINDOWS
00072 std::string utf8filename = filename;
00073 std::string utf8mode = mode;
00074 llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
00075 llutf16string utf16mode = utf8str_to_utf16str(utf8mode);
00076 return _wfopen(utf16filename.c_str(),utf16mode.c_str());
00077 #else
00078 return ::fopen(filename,mode);
00079 #endif
00080 }
00081
00082 LLFILE* LLFile::_fsopen(const char* filename, const char* mode, int sharingFlag)
00083 {
00084 #if LL_WINDOWS
00085 std::string utf8filename = filename;
00086 std::string utf8mode = mode;
00087 llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
00088 llutf16string utf16mode = utf8str_to_utf16str(utf8mode);
00089 return _wfsopen(utf16filename.c_str(),utf16mode.c_str(),sharingFlag);
00090 #else
00091 llassert(0);
00092 return NULL;
00093 #endif
00094 }
00095
00096 int LLFile::remove(const char* filename)
00097 {
00098 #if LL_WINDOWS
00099 std::string utf8filename = filename;
00100 llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
00101 return _wremove(utf16filename.c_str());
00102 #else
00103 return ::remove(filename);
00104 #endif
00105 }
00106
00107 int LLFile::rename(const char* filename, const char* newname)
00108 {
00109 #if LL_WINDOWS
00110 std::string utf8filename = filename;
00111 std::string utf8newname = newname;
00112 llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
00113 llutf16string utf16newname = utf8str_to_utf16str(utf8newname);
00114 return _wrename(utf16filename.c_str(),utf16newname.c_str());
00115 #else
00116 return ::rename(filename,newname);
00117 #endif
00118 }
00119
00120 int LLFile::stat(const char* filename, llstat* filestatus)
00121 {
00122 #if LL_WINDOWS
00123 std::string utf8filename = filename;
00124 llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
00125 return _wstat(utf16filename.c_str(),filestatus);
00126 #else
00127 return ::stat(filename,filestatus);
00128 #endif
00129 }
00130
00131
00132
00133
00134 #if USE_LLFILESTREAMS
00135
00136 LLFILE * LLFile::_Fiopen(const char *filename, std::ios::openmode mode,int)
00137 {
00138 static const char *mods[] =
00139 {
00140 "r", "w", "w", "a", "rb", "wb", "wb", "ab",
00141 "r+", "w+", "a+", "r+b", "w+b", "a+b",
00142 0};
00143 static const int valid[] =
00144 {
00145 ios_base::in,
00146 ios_base::out,
00147 ios_base::out | ios_base::trunc,
00148 ios_base::out | ios_base::app,
00149 ios_base::in | ios_base::binary,
00150 ios_base::out | ios_base::binary,
00151 ios_base::out | ios_base::trunc | ios_base::binary,
00152 ios_base::out | ios_base::app | ios_base::binary,
00153 ios_base::in | ios_base::out,
00154 ios_base::in | ios_base::out | ios_base::trunc,
00155 ios_base::in | ios_base::out | ios_base::app,
00156 ios_base::in | ios_base::out | ios_base::binary,
00157 ios_base::in | ios_base::out | ios_base::trunc
00158 | ios_base::binary,
00159 ios_base::in | ios_base::out | ios_base::app
00160 | ios_base::binary,
00161 0};
00162
00163 FILE *fp = 0;
00164 int n;
00165 ios_base::openmode atendflag = mode & ios_base::ate;
00166 ios_base::openmode norepflag = mode & ios_base::_Noreplace;
00167
00168 if (mode & ios_base::_Nocreate)
00169 mode |= ios_base::in;
00170 mode &= ~(ios_base::ate | ios_base::_Nocreate | ios_base::_Noreplace);
00171 for (n = 0; valid[n] != 0 && valid[n] != mode; ++n)
00172 ;
00173
00174 if (valid[n] == 0)
00175 return (0);
00176 else if (norepflag && mode & (ios_base::out || ios_base::app)
00177 && (fp = LLFile::fopen(filename, "r")) != 0)
00178 {
00179 fclose(fp);
00180 return (0);
00181 }
00182 else if (fp != 0 && fclose(fp) != 0)
00183 return (0);
00184
00185 else if ((fp = LLFile::fopen(filename, mods[n])) == 0)
00186 return (0);
00187
00188 if (!atendflag || fseek(fp, 0, SEEK_END) == 0)
00189 return (fp);
00190
00191 fclose(fp);
00192 return (0);
00193 }
00194
00195
00196
00197 void llifstream::close()
00198 {
00199 if (_Filebuffer && _Filebuffer->close() == 0)
00200 {
00201 _Myios::setstate(ios_base::failbit);
00202 }
00203 }
00204
00205 void llifstream::open(const char* _Filename,
00206 ios_base::openmode _Mode,
00207 int _Prot)
00208 {
00209
00210 FILE* filep = LLFile::_Fiopen(_Filename,_Mode | ios_base::in, _Prot);
00211 if(filep == NULL)
00212 {
00213 _Myios::setstate(ios_base::failbit);
00214 return;
00215 }
00216 llassert(_Filebuffer == NULL);
00217 _Filebuffer = new _Myfb(filep);
00218 _ShouldClose = true;
00219 _Myios::init(_Filebuffer);
00220 }
00221
00222 bool llifstream::is_open() const
00223 {
00224 if(_Filebuffer)
00225 return (_Filebuffer->is_open());
00226 return false;
00227 }
00228 llifstream::~llifstream()
00229 {
00230 if (_ShouldClose)
00231 {
00232 close();
00233 }
00234 delete _Filebuffer;
00235 }
00236
00237 llifstream::llifstream(const char *_Filename,
00238 ios_base::openmode _Mode,
00239 int _Prot)
00240 : std::basic_istream< char , std::char_traits< char > >(NULL,true),_Filebuffer(NULL),_ShouldClose(false)
00241
00242 {
00243 open(_Filename, _Mode | ios_base::in, _Prot);
00244 }
00245
00246
00247
00248
00249 bool llofstream::is_open() const
00250 {
00251 if(_Filebuffer)
00252 return (_Filebuffer->is_open());
00253 return false;
00254 }
00255
00256 void llofstream::open(const char* _Filename,
00257 ios_base::openmode _Mode,
00258 int _Prot)
00259 {
00260
00261 FILE* filep = LLFile::_Fiopen(_Filename,_Mode | ios_base::out, _Prot);
00262 if(filep == NULL)
00263 {
00264 _Myios::setstate(ios_base::failbit);
00265 return;
00266 }
00267 llassert(_Filebuffer==NULL);
00268 _Filebuffer = new _Myfb(filep);
00269 _Myios::init(_Filebuffer);
00270 }
00271
00272 void llofstream::close()
00273 {
00274 llassert(_Filebuffer);
00275 if (_Filebuffer->close() == 0)
00276 _Myios::setstate(ios_base::failbit);
00277 }
00278
00279 llofstream::llofstream(const char *_Filename,
00280 std::ios_base::openmode _Mode,
00281 int _Prot)
00282 : std::basic_ostream<char,std::char_traits < char > >(NULL,true),_Filebuffer(NULL)
00283 {
00284 open(_Filename, _Mode , _Prot);
00285 }
00286
00287 llofstream::~llofstream()
00288 {
00289 delete _Filebuffer;
00290 }
00291
00292 #endif // #if USE_LLFILESTREAMS
00293