PageRenderTime 140ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): LGPL-2.1
  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. #ifndef LL_LLFILE_H
  30. #define LL_LLFILE_H
  31. /**
  32. * This class provides a cross platform interface to the filesystem.
  33. * Attempts to mostly mirror the POSIX style IO functions.
  34. */
  35. typedef FILE LLFILE;
  36. #include <fstream>
  37. #ifdef LL_WINDOWS
  38. #define USE_LLFILESTREAMS 1
  39. #else
  40. #define USE_LLFILESTREAMS 0
  41. #endif
  42. #include <sys/stat.h>
  43. #if LL_WINDOWS
  44. // windows version of stat function and stat data structure are called _stat
  45. typedef struct _stat llstat;
  46. #else
  47. typedef struct stat llstat;
  48. #endif
  49. #ifndef S_ISREG
  50. # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
  51. #endif
  52. #ifndef S_ISDIR
  53. # define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
  54. #endif
  55. #include "llstring.h" // safe char* -> std::string conversion
  56. class LL_COMMON_API LLFile
  57. {
  58. public:
  59. // All these functions take UTF8 path/filenames.
  60. static LLFILE* fopen(const std::string& filename,const char* accessmode); /* Flawfinder: ignore */
  61. static LLFILE* _fsopen(const std::string& filename,const char* accessmode,int sharingFlag);
  62. static int close(LLFILE * file);
  63. // perms is a permissions mask like 0777 or 0700. In most cases it will
  64. // be overridden by the user's umask. It is ignored on Windows.
  65. static int mkdir(const std::string& filename, int perms = 0700);
  66. static int rmdir(const std::string& filename);
  67. static int remove(const std::string& filename);
  68. static int rename(const std::string& filename,const std::string& newname);
  69. static int stat(const std::string& filename,llstat* file_status);
  70. static bool isdir(const std::string& filename);
  71. static bool isfile(const std::string& filename);
  72. static LLFILE * _Fiopen(const std::string& filename, std::ios::openmode mode,int); // protection currently unused
  73. static const char * tmpdir();
  74. };
  75. #if USE_LLFILESTREAMS
  76. class LL_COMMON_API llifstream : public std::basic_istream < char , std::char_traits < char > >
  77. {
  78. // input stream associated with a C stream
  79. public:
  80. typedef std::basic_ifstream<char,std::char_traits < char > > _Myt;
  81. typedef std::basic_filebuf<char,std::char_traits< char > > _Myfb;
  82. typedef std::basic_ios<char,std::char_traits< char > > _Myios;
  83. llifstream()
  84. : std::basic_istream<char,std::char_traits< char > >(NULL,true),_Filebuffer(NULL),_ShouldClose(false)
  85. { // construct unopened
  86. }
  87. explicit llifstream(const std::string& _Filename,
  88. ios_base::openmode _Mode = ios_base::in,
  89. int _Prot = (int)ios_base::_Openprot);
  90. explicit llifstream(_Filet *_File)
  91. : std::basic_istream<char,std::char_traits< char > >(NULL,true),
  92. _Filebuffer(new _Myfb(_File)),
  93. _ShouldClose(false)
  94. { // construct with specified C stream
  95. }
  96. virtual ~llifstream();
  97. _Myfb *rdbuf() const
  98. { // return pointer to file buffer
  99. return _Filebuffer;
  100. }
  101. bool is_open() const;
  102. void open(const std::string& _Filename, /* Flawfinder: ignore */
  103. ios_base::openmode _Mode = ios_base::in,
  104. int _Prot = (int)ios_base::_Openprot);
  105. void close();
  106. private:
  107. _Myfb* _Filebuffer; // the file buffer
  108. bool _ShouldClose;
  109. };
  110. class LL_COMMON_API llofstream : public std::basic_ostream< char , std::char_traits < char > >
  111. {
  112. public:
  113. typedef std::basic_ostream< char , std::char_traits < char > > _Myt;
  114. typedef std::basic_filebuf< char , std::char_traits < char > > _Myfb;
  115. typedef std::basic_ios<char,std::char_traits < char > > _Myios;
  116. llofstream()
  117. : std::basic_ostream<char,std::char_traits < char > >(NULL,true),_Filebuffer(NULL),_ShouldClose(false)
  118. { // construct unopened
  119. }
  120. explicit llofstream(const std::string& _Filename,
  121. std::ios_base::openmode _Mode = ios_base::out,
  122. int _Prot = (int)std::ios_base::_Openprot);
  123. explicit llofstream(_Filet *_File)
  124. : std::basic_ostream<char,std::char_traits < char > >(NULL,true),
  125. _Filebuffer(new _Myfb(_File)),//_File)
  126. _ShouldClose(false)
  127. { // construct with specified C stream
  128. }
  129. virtual ~llofstream();
  130. _Myfb *rdbuf() const
  131. { // return pointer to file buffer
  132. return _Filebuffer;
  133. }
  134. bool is_open() const;
  135. void open(const std::string& _Filename,ios_base::openmode _Mode = ios_base::out,int _Prot = (int)ios_base::_Openprot); /* Flawfinder: ignore */
  136. void close();
  137. private:
  138. _Myfb *_Filebuffer; // the file buffer
  139. bool _ShouldClose;
  140. };
  141. #else
  142. //Use standard file streams on non windows platforms
  143. //#define llifstream std::ifstream
  144. //#define llofstream std::ofstream
  145. class LL_COMMON_API llifstream : public std::ifstream
  146. {
  147. public:
  148. llifstream() : std::ifstream()
  149. {
  150. }
  151. explicit llifstream(const std::string& _Filename, std::_Ios_Openmode _Mode = in)
  152. : std::ifstream(_Filename.c_str(), _Mode)
  153. {
  154. }
  155. void open(const std::string& _Filename, std::_Ios_Openmode _Mode = in) /* Flawfinder: ignore */
  156. {
  157. std::ifstream::open(_Filename.c_str(), _Mode);
  158. }
  159. };
  160. class LL_COMMON_API llofstream : public std::ofstream
  161. {
  162. public:
  163. llofstream() : std::ofstream()
  164. {
  165. }
  166. explicit llofstream(const std::string& _Filename, std::_Ios_Openmode _Mode = out)
  167. : std::ofstream(_Filename.c_str(), _Mode)
  168. {
  169. }
  170. void open(const std::string& _Filename, std::_Ios_Openmode _Mode = out) /* Flawfinder: ignore */
  171. {
  172. std::ofstream::open(_Filename.c_str(), _Mode);
  173. }
  174. };
  175. #endif
  176. /**
  177. * @breif filesize helpers.
  178. *
  179. * The file size helpers are not considered particularly efficient,
  180. * and should only be used for config files and the like -- not in a
  181. * loop.
  182. */
  183. std::streamsize LL_COMMON_API llifstream_size(llifstream& fstr);
  184. std::streamsize LL_COMMON_API llofstream_size(llofstream& fstr);
  185. #endif // not LL_LLFILE_H