1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*****************************************************************
|
| AP4 - Stdc File Byte Stream implementation
|
| Copyright 2002-2008 Axiomatic Systems, LLC
|
|
| This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
| Unless you have obtained Bento4 under a difference license,
| this version of Bento4 is Bento4|GPL.
| Bento4|GPL is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2, or (at your option)
| any later version.
|
| Bento4|GPL is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with Bento4|GPL; see the file COPYING. If not, write to the
| Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
| 02111-1307, USA.
|
****************************************************************/
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#define _LARGEFILE_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <string.h>
#if !defined(_WIN32_WCE)
#include <errno.h>
#include <sys/stat.h>
#endif
#include "Ap4FileByteStream.h"
/*----------------------------------------------------------------------
| compatibility wrappers
+---------------------------------------------------------------------*/
#if !defined(ENOENT)
#define ENOENT 2
#endif
#if !defined(EACCES)
#define EACCES 13
#endif
#if !defined(AP4_CONFIG_HAVE_FOPEN_S)
static int fopen_s(FILE** file,
const char* filename,
const char* mode)
{
*file = fopen(filename, mode);
#if defined(UNDER_CE)
if (*file == NULL) return ENOENT;
#else
if (*file == NULL) return errno;
#endif
return 0;
}
#endif // defined(AP4_CONFIG_HAVE_FOPEN_S
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream
+---------------------------------------------------------------------*/
class AP4_StdcFileByteStream: public AP4_ByteStream
{
public:
// class methods
static AP4_Result Create(AP4_FileByteStream* delegator,
const char* name,
AP4_FileByteStream::Mode mode,
AP4_ByteStream*& stream);
// methods
AP4_StdcFileByteStream(AP4_FileByteStream* delegator,
FILE* file,
AP4_LargeSize size);
~AP4_StdcFileByteStream();
// AP4_ByteStream methods
AP4_Result ReadPartial(void* buffer,
AP4_Size bytesToRead,
AP4_Size& bytesRead);
AP4_Result WritePartial(const void* buffer,
AP4_Size bytesToWrite,
AP4_Size& bytesWritten);
AP4_Result Seek(AP4_Position position);
AP4_Result Tell(AP4_Position& position);
AP4_Result GetSize(AP4_LargeSize& size);
AP4_Result Flush();
// AP4_Referenceable methods
void AddReference();
void Release();
private:
// members
AP4_ByteStream* m_Delegator;
AP4_Cardinal m_ReferenceCount;
FILE* m_File;
AP4_Position m_Position;
AP4_LargeSize m_Size;
};
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::Create
+---------------------------------------------------------------------*/
AP4_Result
AP4_StdcFileByteStream::Create(AP4_FileByteStream* delegator,
const char* name,
AP4_FileByteStream::Mode mode,
AP4_ByteStream*& stream)
{
// default value
stream = NULL;
// check arguments
if (name == NULL) return AP4_ERROR_INVALID_PARAMETERS;
// open the file
FILE* file = NULL;
AP4_Position size = 0;
if (!strcmp(name, "-stdin")) {
file = stdin;
} else if (!strcmp(name, "-stdout")) {
file = stdout;
} else if (!strcmp(name, "-stderr")) {
file = stderr;
} else {
int open_result;
switch (mode) {
case AP4_FileByteStream::STREAM_MODE_READ:
open_result = ::fopen_s(&file, name, "rb");
break;
case AP4_FileByteStream::STREAM_MODE_WRITE:
open_result = ::fopen_s(&file, name, "wb+");
break;
case AP4_FileByteStream::STREAM_MODE_READ_WRITE:
open_result = ::fopen_s(&file, name, "r+b");
break;
default:
return AP4_ERROR_INVALID_PARAMETERS;
}
if (open_result != 0) {
if (open_result == ENOENT) {
return AP4_ERROR_NO_SUCH_FILE;
} else if (open_result == EACCES) {
return AP4_ERROR_PERMISSION_DENIED;
} else {
return AP4_ERROR_CANNOT_OPEN_FILE;
}
}
// get the size
if (AP4_fseek(file, 0, SEEK_END) >= 0) {
size = AP4_ftell(file);
AP4_fseek(file, 0, SEEK_SET);
}
}
stream = new AP4_StdcFileByteStream(delegator, file, size);
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::AP4_StdcFileByteStream
+---------------------------------------------------------------------*/
AP4_StdcFileByteStream::AP4_StdcFileByteStream(AP4_FileByteStream* delegator,
FILE* file,
AP4_LargeSize size) :
m_Delegator(delegator),
m_ReferenceCount(1),
m_File(file),
m_Position(0),
m_Size(size)
{
}
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::~AP4_StdcFileByteStream
+---------------------------------------------------------------------*/
AP4_StdcFileByteStream::~AP4_StdcFileByteStream()
{
if (m_File && m_File != stdin && m_File != stdout && m_File != stderr) {
fclose(m_File);
}
}
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::AddReference
+---------------------------------------------------------------------*/
void
AP4_StdcFileByteStream::AddReference()
{
m_ReferenceCount++;
}
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::Release
+---------------------------------------------------------------------*/
void
AP4_StdcFileByteStream::Release()
{
if (--m_ReferenceCount == 0) {
if (m_Delegator) {
delete m_Delegator;
} else {
delete this;
}
}
}
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::ReadPartial
+---------------------------------------------------------------------*/
AP4_Result
AP4_StdcFileByteStream::ReadPartial(void* buffer,
AP4_Size bytesToRead,
AP4_Size& bytesRead)
{
size_t nbRead;
nbRead = fread(buffer, 1, bytesToRead, m_File);
if (nbRead > 0) {
bytesRead = (AP4_Size)nbRead;
m_Position += nbRead;
return AP4_SUCCESS;
} else if (feof(m_File)) {
bytesRead = 0;
return AP4_ERROR_EOS;
} else {
bytesRead = 0;
return AP4_ERROR_READ_FAILED;
}
}
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::WritePartial
+---------------------------------------------------------------------*/
AP4_Result
AP4_StdcFileByteStream::WritePartial(const void* buffer,
AP4_Size bytesToWrite,
AP4_Size& bytesWritten)
{
size_t nbWritten;
if (bytesToWrite == 0) return AP4_SUCCESS;
nbWritten = fwrite(buffer, 1, bytesToWrite, m_File);
if (nbWritten > 0) {
bytesWritten = (AP4_Size)nbWritten;
m_Position += nbWritten;
return AP4_SUCCESS;
} else {
bytesWritten = 0;
return AP4_ERROR_WRITE_FAILED;
}
}
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::Seek
+---------------------------------------------------------------------*/
AP4_Result
AP4_StdcFileByteStream::Seek(AP4_Position position)
{
// shortcut
if (position == m_Position) return AP4_SUCCESS;
size_t result;
result = AP4_fseek(m_File, position, SEEK_SET);
if (result == 0) {
m_Position = position;
return AP4_SUCCESS;
} else {
return AP4_FAILURE;
}
}
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::Tell
+---------------------------------------------------------------------*/
AP4_Result
AP4_StdcFileByteStream::Tell(AP4_Position& position)
{
position = m_Position;
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::GetSize
+---------------------------------------------------------------------*/
AP4_Result
AP4_StdcFileByteStream::GetSize(AP4_LargeSize& size)
{
size = m_Size;
return AP4_SUCCESS;
}
/*----------------------------------------------------------------------
| AP4_StdcFileByteStream::Flush
+---------------------------------------------------------------------*/
AP4_Result
AP4_StdcFileByteStream::Flush()
{
int ret_val = fflush(m_File);
AP4_Result result((ret_val > 0) ? AP4_FAILURE : AP4_SUCCESS);
if (AP4_SUCCEEDED(result) && GetObserver())
return GetObserver()->OnFlush(this);
return result;
}
/*----------------------------------------------------------------------
| AP4_FileByteStream::Create
+---------------------------------------------------------------------*/
AP4_Result
AP4_FileByteStream::Create(const char* name,
AP4_FileByteStream::Mode mode,
AP4_ByteStream*& stream)
{
return AP4_StdcFileByteStream::Create(NULL, name, mode, stream);
}
#if !defined(AP4_CONFIG_NO_EXCEPTIONS)
/*----------------------------------------------------------------------
| AP4_FileByteStream::AP4_FileByteStream
+---------------------------------------------------------------------*/
AP4_FileByteStream::AP4_FileByteStream(const char* name,
AP4_FileByteStream::Mode mode)
{
AP4_ByteStream* stream = NULL;
AP4_Result result = AP4_StdcFileByteStream::Create(this, name, mode, stream);
if (AP4_FAILED(result)) throw AP4_Exception(result);
m_Delegate = stream;
}
#endif
|