UTFile.h

Go to the documentation of this file.
00001 //==================================================================================================
00002 // Copyright (C) 2010  Brian Tietz    sdbtietz at yahoo dot com
00003 //
00004 // This program is free software; you can redistribute it and/or modify it under the terms of the
00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the
00006 // License.
00007 //
00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00010 // General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License along with this program; if
00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00014 // 02110-1301, USA.
00015 //
00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com)
00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that
00018 // license as applied to the UT library in exchange for either substantial contributions to the
00019 // development of the UT library or other forms of compensation.  Any such waiver must be
00020 // established in writing between the copyright holder and the commercial entity obtaining such a
00021 // waiver.
00022 //==================================================================================================
00023 
00024 
00025 #ifndef _UT_FILE_H_
00026 #define _UT_FILE_H_
00027 
00028 
00029 //==================================================================================================
00030 //=== System headers
00031 //==================================================================================================
00032 #include <fcntl.h>
00033 
00034 
00035 //==================================================================================================
00036 //=== Project headers
00037 //==================================================================================================
00038 #include "UTIOStreams.h"
00039 #include "UTPath.h"
00040 #include "UTMessageLoop.h"
00041 #include "UTEvent.h"
00042 
00043 
00044 //==================================================================================================
00045 //=== Constants
00046 //==================================================================================================
00047 
00048 // \cond DOXYGEN_DOCUMENT_NEVER
00049 
00050 enum async_status_t
00051 {
00052     eAS_closed,
00053     eAS_opening,
00054     eAS_opened,
00055     eAS_idle,
00056     eAS_read,
00057     eAS_write
00058 };
00059 
00060 enum thread_io_helper_command_t
00061 {
00062     eTIOHC_open,
00063     eTIOHC_idle,
00064     eTIOHC_read,
00065     eTIOHC_write,
00066     eTIOHC_quit
00067 };
00068 
00069 // \endcond
00070 
00071 
00072 //==================================================================================================
00073 //=== Forward name declarations
00074 //==================================================================================================
00075 class FileOpenCompleteMessage_t;
00076 class FileReadCompleteMessage_t;
00077 class FileWriteCompleteMessage_t;
00078 class FilePlatformInfo_t;
00079 class ThreadedIOHelper_t;
00080 
00081 
00082 //==================================================================================================
00083 class UT_EXPORT File_t
00084 //==================================================================================================
00085 :   public IOStream_t
00087 {
00088     //----------------------------------------------------------------------------------------------
00089     public:
00090     //----------------------------------------------------------------------------------------------
00091                             File_t();
00094 
00095     virtual                 ~File_t();
00098 
00099     virtual Status_t        Open(
00100                                 const Path_t& path,
00102                                 int flags,
00152                                 mode_t permissions = 0,
00175                                 MessageDestination_t<FileReadCompleteMessage_t>* read_dest = NULL,
00179                                 MessageDestination_t<FileWriteCompleteMessage_t>* write_dest = NULL,
00183                                 MessageDestination_t<FileOpenCompleteMessage_t>* open_dest = NULL
00188                                 );
00197 
00198     inline Status_t         Open(
00199                                 const utf8* path,
00200                                 int flags,
00201                                 mode_t permissions = 0,
00202                                 MessageDestination_t<FileReadCompleteMessage_t>* read_dest = NULL,
00203                                 MessageDestination_t<FileWriteCompleteMessage_t>* write_dest = NULL,
00204                                 MessageDestination_t<FileOpenCompleteMessage_t>* open_dest = NULL );
00207 
00208     inline Status_t         Open(
00209                                 stringliteral* path,
00210                                 int flags,
00211                                 mode_t permissions = 0,
00212                                 MessageDestination_t<FileReadCompleteMessage_t>* read_dest = NULL,
00213                                 MessageDestination_t<FileWriteCompleteMessage_t>* write_dest = NULL,
00214                                 MessageDestination_t<FileOpenCompleteMessage_t>* open_dest = NULL );
00217 
00218     inline int              OpenFlags() const;
00220 
00221     inline int              OpenRWFlags() const;
00224 
00225     inline const Path_t&    Path() const;
00227 
00228     virtual Status_t        Read( byte* buffer, int bytes_to_read, bool force_read_all = true );
00253 
00254     virtual Status_t        Write(  const byte* buffer,
00255                                     int bytes_to_write,
00256                                     bool force_write_all = true );
00289 
00290     virtual Status_t        Size(out fileoff_t* size);
00293 
00294     virtual Status_t        Position(out fileoff_t* position);
00298 
00299     virtual Status_t        Seek(fileoff_t position);
00303 
00304     virtual Status_t        Flush();
00309 
00310     virtual Status_t        Close();
00320 
00321     //----------------------------------------------------------------------------------------------
00322     protected:
00323     //----------------------------------------------------------------------------------------------
00324     virtual Status_t        AsyncOpenComplete();
00335 
00336     //----------------------------------------------------------------------------------------------
00337     protected:
00338     //----------------------------------------------------------------------------------------------
00339     #if LOG_FILE_OPS
00340     virtual void            AppendStateInfo(String_t* state) const;
00343     #endif
00344 
00345     //----------------------------------------------------------------------------------------------
00346     private:
00347     //----------------------------------------------------------------------------------------------
00348     Path_t                                              m_path;
00349     int                                                 m_flags;
00350     int                                                 m_rw_flags;
00351     FilePlatformInfo_t*                                 m_platform_info;
00352     Status_t                                            m_status;
00353     MessageDestination_t<FileReadCompleteMessage_t>     m_read_dest;
00354     MessageDestination_t<FileWriteCompleteMessage_t>    m_write_dest;
00355     MessageDestination_t<FileOpenCompleteMessage_t>     m_open_dest;
00356     async_status_t                                      m_async_status;
00357     byte*                                               m_async_buffer;
00358     int                                                 m_async_bytes;
00359     fileoff_t                                           m_pos;
00360     fileoff_t                                           m_size;
00361 
00362     friend class FilePlatformInfo_t;
00363     friend class ThreadedIOHelper_t;
00364 };
00365 
00366 
00367 //==================================================================================================
00368 class UT_EXPORT FileOpenCompleteMessage_t
00369 //==================================================================================================
00371 : public Message_t
00372 {
00373     //----------------------------------------------------------------------------------------------
00374     public:
00375     //----------------------------------------------------------------------------------------------
00376     inline                  FileOpenCompleteMessage_t(Status_t status);
00378 
00379     inline const Status_t&  Status() const;
00381 
00382     //----------------------------------------------------------------------------------------------
00383     private:
00384     //----------------------------------------------------------------------------------------------
00385     Status_t                m_status;
00386 };
00387 
00388 
00389 //==================================================================================================
00390 class UT_EXPORT FileReadCompleteMessage_t
00391 //==================================================================================================
00396 : public Message_t
00397 { };
00398 
00399 
00400 //==================================================================================================
00401 class UT_EXPORT FileWriteCompleteMessage_t
00402 //==================================================================================================
00408 : public Message_t
00409 { };
00410 
00411 
00412 // \cond DOXYGEN_DOCUMENT_NEVER
00413 
00414 //==================================================================================================
00415 class ThreadedIOHelper_t
00416 //==================================================================================================
00417 : public Thread_t
00418 {
00419     //----------------------------------------------------------------------------------------------
00420     public:
00421     //----------------------------------------------------------------------------------------------
00422                                 ThreadedIOHelper_t( FilePlatformInfo_t* file_info,
00423                                                     File_t* file,
00424                                                     int flags,
00425                                                     int rw_flags,
00426                                                     int permissions );
00427                                 ThreadedIOHelper_t( FilePlatformInfo_t* file_info, File_t* file );
00428                                 ~ThreadedIOHelper_t();
00429 
00430     virtual void                Main();
00431 
00432     int                         Read(   byte* buffer,
00433                                         int bytes_to_read,
00434                                         bool force_read_all );
00435     int                         Write(  const byte* buffer,
00436                                         int bytes_to_write,
00437                                         bool force_write_all );
00438 
00439     //----------------------------------------------------------------------------------------------
00440     private:
00441     //----------------------------------------------------------------------------------------------
00442     FilePlatformInfo_t*         m_file_info;
00443     File_t*                     m_file;
00444     int                         m_flags;
00445     int                         m_rw_flags;
00446     int                         m_permissions;
00447     thread_io_helper_command_t  m_command;
00448     byte*                       m_command_buffer;
00449     int                         m_command_io_length;
00450     bool                        m_command_force_all;
00451     Event_t                     m_command_event;
00452     Event_t                     m_command_complete_event;
00453     error_code_t                m_io_status;
00454 
00455     friend class FilePlatformInfo_t;
00456 };
00457 
00458 // \endcond
00459 
00460 
00461 
00462 
00463 //==================================================================================================
00464 //==================================================================================================
00465 //===
00466 //=== Inline function implementations
00467 //===
00468 //==================================================================================================
00469 //==================================================================================================
00470 inline Status_t
00471 File_t::Open(   const utf8* path,
00472                 int flags,
00473                 mode_t permissions,
00474                 MessageDestination_t<FileReadCompleteMessage_t>* read_dest,
00475                 MessageDestination_t<FileWriteCompleteMessage_t>* write_dest,
00476                 MessageDestination_t<FileOpenCompleteMessage_t>* open_dest )
00477 {
00478     // Using the stringliteral form allows the temporary Path_t to borrow the caller's utf8* rather
00479     // than copying it
00480     return Open( Path_t(path), flags, permissions, read_dest, write_dest, open_dest );
00481 }
00482 
00483 
00484 inline Status_t
00485 File_t::Open(   stringliteral* path,
00486                 int flags,
00487                 mode_t permissions,
00488                 MessageDestination_t<FileReadCompleteMessage_t>* read_dest,
00489                 MessageDestination_t<FileWriteCompleteMessage_t>* write_dest,
00490                 MessageDestination_t<FileOpenCompleteMessage_t>* open_dest )
00491 {
00492     return Open( Path_t(path), flags, permissions, read_dest, write_dest, open_dest );
00493 }
00494 
00495 
00496 inline int
00497 File_t::OpenFlags() const
00498 {
00499     return m_flags;
00500 }
00501 
00502 
00503 inline int
00504 File_t::OpenRWFlags() const
00505 {
00506     return m_rw_flags;
00507 }
00508 
00509 
00510 inline const Path_t&
00511 File_t::Path() const
00512 {
00513     return m_path;
00514 }
00515 
00516 
00517 inline
00518 FileOpenCompleteMessage_t::FileOpenCompleteMessage_t(Status_t status)
00519 {
00520     m_status = status;
00521 }
00522 
00523 
00524 inline const Status_t&
00525 FileOpenCompleteMessage_t::Status() const
00526 {
00527     return m_status;
00528 }
00529 
00530 
00531 #endif // _UT_FILE_H_

Generated on Tue Dec 14 22:35:05 2010 for UT library by  doxygen 1.6.1