ffile.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/ffile.h
  3. // Purpose: wxFFile - encapsulates "FILE *" stream
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 14.07.99
  7. // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_FFILE_H_
  11. #define _WX_FFILE_H_
  12. #include "wx/defs.h" // for wxUSE_FFILE
  13. #if wxUSE_FFILE
  14. #include "wx/string.h"
  15. #include "wx/filefn.h"
  16. #include "wx/convauto.h"
  17. #include <stdio.h>
  18. // ----------------------------------------------------------------------------
  19. // class wxFFile: standard C stream library 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 wxFFile
  25. {
  26. public:
  27. // ctors
  28. // -----
  29. // def ctor
  30. wxFFile() { m_fp = NULL; }
  31. // open specified file (may fail, use IsOpened())
  32. wxFFile(const wxString& filename, const wxString& mode = wxT("r"));
  33. // attach to (already opened) file
  34. wxFFile(FILE *lfp) { m_fp = lfp; }
  35. // open/close
  36. // open a file (existing or not - the mode controls what happens)
  37. bool Open(const wxString& filename, const wxString& mode = wxT("r"));
  38. // closes the opened file (this is a NOP if not opened)
  39. bool Close();
  40. // assign an existing file descriptor and get it back from wxFFile object
  41. void Attach(FILE *lfp, const wxString& name = wxEmptyString)
  42. { Close(); m_fp = lfp; m_name = name; }
  43. FILE* Detach() { FILE* fpOld = m_fp; m_fp = NULL; return fpOld; }
  44. FILE *fp() const { return m_fp; }
  45. // read/write (unbuffered)
  46. // read all data from the file into a string (useful for text files)
  47. bool ReadAll(wxString *str, const wxMBConv& conv = wxConvAuto());
  48. // returns number of bytes read - use Eof() and Error() to see if an error
  49. // occurred or not
  50. size_t Read(void *pBuf, size_t nCount);
  51. // returns the number of bytes written
  52. size_t Write(const void *pBuf, size_t nCount);
  53. // returns true on success
  54. bool Write(const wxString& s, const wxMBConv& conv = wxConvAuto());
  55. // flush data not yet written
  56. bool Flush();
  57. // file pointer operations (return ofsInvalid on failure)
  58. // move ptr ofs bytes related to start/current pos/end of file
  59. bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
  60. // move ptr to ofs bytes before the end
  61. bool SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); }
  62. // get current position in the file
  63. wxFileOffset Tell() const;
  64. // get current file length
  65. wxFileOffset Length() const;
  66. // simple accessors: note that Eof() and Error() may only be called if
  67. // IsOpened()!
  68. // is file opened?
  69. bool IsOpened() const { return m_fp != NULL; }
  70. // is end of file reached?
  71. bool Eof() const { return feof(m_fp) != 0; }
  72. // has an error occurred?
  73. bool Error() const { return ferror(m_fp) != 0; }
  74. // get the file name
  75. const wxString& GetName() const { return m_name; }
  76. // type such as disk or pipe
  77. wxFileKind GetKind() const { return wxGetFileKind(m_fp); }
  78. // dtor closes the file if opened
  79. ~wxFFile() { Close(); }
  80. private:
  81. // copy ctor and assignment operator are private because it doesn't make
  82. // sense to copy files this way: attempt to do it will provoke a compile-time
  83. // error.
  84. wxFFile(const wxFFile&);
  85. wxFFile& operator=(const wxFFile&);
  86. FILE *m_fp; // IO stream or NULL if not opened
  87. wxString m_name; // the name of the file (for diagnostic messages)
  88. };
  89. #endif // wxUSE_FFILE
  90. #endif // _WX_FFILE_H_