1
0

virtual_io.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is MPEG4IP.
  13. *
  14. * The Initial Developer of the Original Code is Cisco Systems Inc.
  15. * Portions created by Cisco Systems Inc. are
  16. * Copyright (C) Cisco Systems Inc. 2001 - 2005. All Rights Reserved.
  17. *
  18. * Contributor(s):
  19. * Ben Allison benski at nullsoft.com
  20. *
  21. * Virtual I/O support, for file support other than fopen/fread/fwrite
  22. */
  23. #include "mp4common.h"
  24. #include "virtual_io.h"
  25. /* --------- Virtual IO for FILE * --------- */
  26. u_int64_t FILE_GetFileLength(void *user)
  27. {
  28. FILE *fp = (FILE *)user;
  29. struct stat s;
  30. if (fstat(fileno(fp), &s) < 0) {
  31. throw new MP4Error(errno, "stat failed", "MP4Open");
  32. }
  33. return s.st_size;
  34. }
  35. int FILE_SetPosition(void *user, u_int64_t position)
  36. {
  37. FILE *fp = (FILE *)user;
  38. fpos_t fpos;
  39. VAR_TO_FPOS(fpos, position);
  40. return fsetpos(fp, &fpos);
  41. }
  42. int FILE_GetPosition(void *user, u_int64_t *position)
  43. {
  44. FILE *fp = (FILE *)user;
  45. fpos_t fpos;
  46. if (fgetpos(fp, &fpos) < 0) {
  47. throw new MP4Error(errno, "MP4GetPosition");
  48. }
  49. FPOS_TO_VAR(fpos, u_int64_t, *position);
  50. return 0;
  51. }
  52. size_t FILE_Read(void *user, void *buffer, size_t size)
  53. {
  54. FILE *fp = (FILE *)user;
  55. return fread(buffer, 1, size, fp);
  56. }
  57. size_t FILE_Write(void *user, void *buffer, size_t size)
  58. {
  59. FILE *fp = (FILE *)user;
  60. return fwrite(buffer, 1, size, fp);
  61. }
  62. int FILE_EndOfFile(void *user)
  63. {
  64. FILE *fp = (FILE *)user;
  65. return feof(fp);
  66. }
  67. int FILE_Close(void *user)
  68. {
  69. FILE *fp = (FILE *)user;
  70. return fclose(fp);
  71. }
  72. Virtual_IO FILE_virtual_IO =
  73. {
  74. FILE_GetFileLength,
  75. FILE_SetPosition,
  76. FILE_GetPosition,
  77. FILE_Read,
  78. FILE_Write,
  79. FILE_EndOfFile,
  80. FILE_Close,
  81. };