file.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/file.h
  3. // Purpose: wxFile - encapsulates low-level "file descriptor"
  4. // wxTempFile - safely replace the old file
  5. // Author: Vadim Zeitlin
  6. // Modified by:
  7. // Created: 29/01/98
  8. // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_FILEH__
  12. #define _WX_FILEH__
  13. #include "wx/defs.h"
  14. #if wxUSE_FILE
  15. #include "wx/string.h"
  16. #include "wx/filefn.h"
  17. #include "wx/convauto.h"
  18. // ----------------------------------------------------------------------------
  19. // class wxFile: raw file IO
  20. //
  21. // NB: for space efficiency this class has no virtual functions, including
  22. // dtor which is _not_ virtual, so it shouldn't be used as a base class.
  23. // ----------------------------------------------------------------------------
  24. class WXDLLIMPEXP_BASE wxFile
  25. {
  26. public:
  27. // more file constants
  28. // -------------------
  29. // opening mode
  30. enum OpenMode { read, write, read_write, write_append, write_excl };
  31. // standard values for file descriptor
  32. enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr };
  33. // static functions
  34. // ----------------
  35. // check whether a regular file by this name exists
  36. static bool Exists(const wxString& name);
  37. // check whether we can access the given file in given mode
  38. // (only read and write make sense here)
  39. static bool Access(const wxString& name, OpenMode mode);
  40. // ctors
  41. // -----
  42. // def ctor
  43. wxFile() { m_fd = fd_invalid; m_lasterror = 0; }
  44. // open specified file (may fail, use IsOpened())
  45. wxFile(const wxString& fileName, OpenMode mode = read);
  46. // attach to (already opened) file
  47. wxFile(int lfd) { m_fd = lfd; m_lasterror = 0; }
  48. // open/close
  49. // create a new file (with the default value of bOverwrite, it will fail if
  50. // the file already exists, otherwise it will overwrite it and succeed)
  51. bool Create(const wxString& fileName, bool bOverwrite = false,
  52. int access = wxS_DEFAULT);
  53. bool Open(const wxString& fileName, OpenMode mode = read,
  54. int access = wxS_DEFAULT);
  55. bool Close(); // Close is a NOP if not opened
  56. // assign an existing file descriptor and get it back from wxFile object
  57. void Attach(int lfd) { Close(); m_fd = lfd; m_lasterror = 0; }
  58. int Detach() { int fdOld = m_fd; m_fd = fd_invalid; return fdOld; }
  59. int fd() const { return m_fd; }
  60. // read/write (unbuffered)
  61. // read all data from the file into a string (useful for text files)
  62. bool ReadAll(wxString *str, const wxMBConv& conv = wxConvAuto());
  63. // returns number of bytes read or wxInvalidOffset on error
  64. ssize_t Read(void *pBuf, size_t nCount);
  65. // returns the number of bytes written
  66. size_t Write(const void *pBuf, size_t nCount);
  67. // returns true on success
  68. bool Write(const wxString& s, const wxMBConv& conv = wxConvAuto());
  69. // flush data not yet written
  70. bool Flush();
  71. // file pointer operations (return wxInvalidOffset on failure)
  72. // move ptr ofs bytes related to start/current offset/end of file
  73. wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
  74. // move ptr to ofs bytes before the end
  75. wxFileOffset SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); }
  76. // get current offset
  77. wxFileOffset Tell() const;
  78. // get current file length
  79. wxFileOffset Length() const;
  80. // simple accessors
  81. // is file opened?
  82. bool IsOpened() const { return m_fd != fd_invalid; }
  83. // is end of file reached?
  84. bool Eof() const;
  85. // has an error occurred?
  86. bool Error() const { return m_lasterror != 0; }
  87. // get last errno
  88. int GetLastError() const { return m_lasterror; }
  89. // reset error state
  90. void ClearLastError() { m_lasterror = 0; }
  91. // type such as disk or pipe
  92. wxFileKind GetKind() const { return wxGetFileKind(m_fd); }
  93. // dtor closes the file if opened
  94. ~wxFile() { Close(); }
  95. private:
  96. // copy ctor and assignment operator are private because
  97. // it doesn't make sense to copy files this way:
  98. // attempt to do it will provoke a compile-time error.
  99. wxFile(const wxFile&);
  100. wxFile& operator=(const wxFile&);
  101. // Copy the value of errno into m_lasterror if rc == -1 and return true in
  102. // this case (indicating that we've got an error). Otherwise return false.
  103. //
  104. // Notice that we use the possibly 64 bit wxFileOffset instead of int here so
  105. // that it works for checking the result of functions such as tell() too.
  106. bool CheckForError(wxFileOffset rc) const;
  107. int m_fd; // file descriptor or INVALID_FD if not opened
  108. int m_lasterror; // errno value of last error
  109. };
  110. // ----------------------------------------------------------------------------
  111. // class wxTempFile: if you want to replace another file, create an instance
  112. // of wxTempFile passing the name of the file to be replaced to the ctor. Then
  113. // you can write to wxTempFile and call Commit() function to replace the old
  114. // file (and close this one) or call Discard() to cancel the modification. If
  115. // you call neither of them, dtor will call Discard().
  116. // ----------------------------------------------------------------------------
  117. class WXDLLIMPEXP_BASE wxTempFile
  118. {
  119. public:
  120. // ctors
  121. // default
  122. wxTempFile() { }
  123. // associates the temp file with the file to be replaced and opens it
  124. wxTempFile(const wxString& strName);
  125. // open the temp file (strName is the name of file to be replaced)
  126. bool Open(const wxString& strName);
  127. // is the file opened?
  128. bool IsOpened() const { return m_file.IsOpened(); }
  129. // get current file length
  130. wxFileOffset Length() const { return m_file.Length(); }
  131. // move ptr ofs bytes related to start/current offset/end of file
  132. wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart)
  133. { return m_file.Seek(ofs, mode); }
  134. // get current offset
  135. wxFileOffset Tell() const { return m_file.Tell(); }
  136. // I/O (both functions return true on success, false on failure)
  137. bool Write(const void *p, size_t n) { return m_file.Write(p, n) == n; }
  138. bool Write(const wxString& str, const wxMBConv& conv = wxMBConvUTF8())
  139. { return m_file.Write(str, conv); }
  140. // flush data: can be called before closing file to ensure that data was
  141. // correctly written out
  142. bool Flush() { return m_file.Flush(); }
  143. // different ways to close the file
  144. // validate changes and delete the old file of name m_strName
  145. bool Commit();
  146. // discard changes
  147. void Discard();
  148. // dtor calls Discard() if file is still opened
  149. ~wxTempFile();
  150. private:
  151. // no copy ctor/assignment operator
  152. wxTempFile(const wxTempFile&);
  153. wxTempFile& operator=(const wxTempFile&);
  154. wxString m_strName, // name of the file to replace in Commit()
  155. m_strTemp; // temporary file name
  156. wxFile m_file; // the temporary file
  157. };
  158. #endif // wxUSE_FILE
  159. #endif // _WX_FILEH__