Ap4StdCFileByteStream.cpp
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:7k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |      File Byte Stream
  4. |
  5. |      (c) 2001-2002 Gilles Boccon-Gibod
  6. |
  7. |
  8.  ****************************************************************/
  9. /*----------------------------------------------------------------------
  10. |       includes
  11. +---------------------------------------------------------------------*/
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <sys/stat.h>
  15. #include "Ap4FileByteStream.h"
  16. /*----------------------------------------------------------------------
  17. |       AP4_StdcFileByteStream
  18. +---------------------------------------------------------------------*/
  19. class AP4_StdcFileByteStream: public AP4_ByteStream
  20. {
  21. public:
  22.     // methods
  23.     AP4_StdcFileByteStream(AP4_ByteStream*          delegator,
  24.                            const char*              name, 
  25.                            AP4_FileByteStream::Mode mode);
  26.     ~AP4_StdcFileByteStream();
  27.     // AP4_ByteStream methods
  28.     AP4_Result Read(void*    buffer, 
  29.                    AP4_Size  bytesToRead, 
  30.                    AP4_Size* bytesRead);
  31.     AP4_Result Write(const void* buffer, 
  32.                     AP4_Size     bytesToWrite, 
  33.                     AP4_Size*    bytesWritten);
  34.     AP4_Result Seek(AP4_Offset offset);
  35.     AP4_Result Tell(AP4_Offset& offset);
  36.     AP4_Result GetSize(AP4_Size& size);
  37.     // AP4_Referenceable methods
  38.     void AddReference();
  39.     void Release();
  40. private:
  41.     // members
  42.     AP4_ByteStream* m_Delegator;
  43.     AP4_Cardinal    m_ReferenceCount;
  44.     FILE*           m_File;
  45. };
  46. /*----------------------------------------------------------------------
  47. |       AP4_StdcFileByteStream::AP4_StdcFileByteStream
  48. +---------------------------------------------------------------------*/
  49. AP4_StdcFileByteStream::AP4_StdcFileByteStream(
  50.     AP4_ByteStream  *        delegator,
  51.     const char*              name, 
  52.     AP4_FileByteStream::Mode mode) :
  53.     m_Delegator(delegator),
  54.     m_ReferenceCount(1),
  55.     m_File(NULL)
  56. {
  57.     if (!strcmp(name, "-stdin")) {
  58.         m_File = stdin;
  59.     } else if (!strcmp(name, "-stdout")) {
  60.         m_File = stdout;
  61.     } else if (!strcmp(name, "-stderr")) {
  62.         m_File = stderr;
  63.     } else {
  64.         switch (mode) {
  65.           case AP4_FileByteStream::STREAM_MODE_READ:
  66.             m_File = fopen(name, "rb");
  67.             break;
  68.           case AP4_FileByteStream::STREAM_MODE_WRITE:
  69.             m_File = fopen(name, "wb+");
  70.             break;
  71.           default:
  72.             throw AP4_Exception(AP4_ERROR_INVALID_PARAMETERS);
  73.         }
  74.     
  75.         if (m_File == NULL) {
  76.             if (errno == ENOENT) {
  77.                 throw AP4_Exception(AP4_ERROR_NO_SUCH_FILE);
  78.             } else if (errno == EACCES) {
  79.                 throw AP4_Exception(AP4_ERROR_PERMISSION_DENIED);
  80.             } else {
  81.                 throw AP4_Exception(AP4_ERROR_CANNOT_OPEN_FILE);
  82.             }
  83.         }
  84.     }
  85. }
  86. /*----------------------------------------------------------------------
  87. |       AP4_StdcFileByteStream::~AP4_StdcFileByteStream
  88. +---------------------------------------------------------------------*/
  89. AP4_StdcFileByteStream::~AP4_StdcFileByteStream()
  90. {
  91.     if (m_File && m_File != stdin && m_File != stdout && m_File != stderr) {
  92.         fclose(m_File);
  93.     }
  94. }
  95. /*----------------------------------------------------------------------
  96. |       AP4_StdcFileByteStream::AddReference
  97. +---------------------------------------------------------------------*/
  98. void
  99. AP4_StdcFileByteStream::AddReference()
  100. {
  101.     m_ReferenceCount++;
  102. }
  103. /*----------------------------------------------------------------------
  104. |       AP4_StdcFileByteStream::Release
  105. +---------------------------------------------------------------------*/
  106. void
  107. AP4_StdcFileByteStream::Release()
  108. {
  109.     if (--m_ReferenceCount == 0) {
  110.         delete m_Delegator;
  111.     }
  112. }
  113. /*----------------------------------------------------------------------
  114. |       AP4_StdcFileByteStream::Read
  115. +---------------------------------------------------------------------*/
  116. AP4_Result
  117. AP4_StdcFileByteStream::Read(void*    buffer, 
  118.                             AP4_Size  bytesToRead, 
  119.                             AP4_Size* bytesRead)
  120. {
  121.     size_t nbRead;
  122.     nbRead = fread(buffer, 1, bytesToRead, m_File);
  123.     if (nbRead > 0) {
  124.         if (bytesRead) *bytesRead = nbRead;
  125.         return AP4_SUCCESS;
  126.     } else if (feof(m_File)) {
  127.         if (bytesRead) *bytesRead = 0;
  128.         return AP4_ERROR_EOS;
  129.     } else {
  130.         if (bytesRead) *bytesRead = 0;
  131.         return AP4_ERROR_READ_FAILED;
  132.     }
  133. }
  134. /*----------------------------------------------------------------------
  135. |       AP4_StdcFileByteStream::Write
  136. +---------------------------------------------------------------------*/
  137. AP4_Result
  138. AP4_StdcFileByteStream::Write(const void* buffer, 
  139.                              AP4_Size     bytesToWrite, 
  140.                              AP4_Size*    bytesWritten)
  141. {
  142.     size_t nbWritten;
  143.     nbWritten = fwrite(buffer, 1, bytesToWrite, m_File);
  144.     
  145.     if (nbWritten > 0) {
  146.         if (bytesWritten) *bytesWritten = nbWritten;
  147.         return AP4_SUCCESS;
  148.     } else {
  149.         if (bytesWritten) *bytesWritten = 0;
  150.         return AP4_ERROR_WRITE_FAILED;
  151.     }
  152. }
  153. /*----------------------------------------------------------------------
  154. |       AP4_StdcFileByteStream::Seek
  155. +---------------------------------------------------------------------*/
  156. AP4_Result
  157. AP4_StdcFileByteStream::Seek(AP4_Offset offset)
  158. {
  159.     size_t result;
  160.     result = fseek(m_File, offset, SEEK_SET);
  161.     if (result == 0) {
  162.         return AP4_SUCCESS;
  163.     } else {
  164.         return AP4_FAILURE;
  165.     }
  166. }
  167. /*----------------------------------------------------------------------
  168. |       AP4_StdcFileByteStream::Tell
  169. +---------------------------------------------------------------------*/
  170. AP4_Result
  171. AP4_StdcFileByteStream::Tell(AP4_Offset& offset)
  172. {
  173.     offset = ftell(m_File);
  174.     return AP4_SUCCESS;
  175. }
  176. /*----------------------------------------------------------------------
  177. |       AP4_StdcFileByteStream::GetSize
  178. +---------------------------------------------------------------------*/
  179. AP4_Result
  180. AP4_StdcFileByteStream::GetSize(AP4_Size& size)
  181. {
  182.     struct stat info;
  183.     int          result;
  184.     result = fstat(fileno(m_File), &info);
  185.     if (result == 0) {
  186.         size = info.st_size;
  187.         return AP4_SUCCESS;
  188.     } else {
  189.         return AP4_FAILURE;
  190.     }
  191. }
  192. /*----------------------------------------------------------------------
  193. |       AP4_FileByteStream::AP4_FileByteStream
  194. +---------------------------------------------------------------------*/
  195. AP4_FileByteStream::AP4_FileByteStream(const char*             name, 
  196.                                      AP4_FileByteStream::Mode mode)
  197. {
  198.     m_Delegate = new AP4_StdcFileByteStream(this, name, mode);
  199. }