You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.2KB

  1. /*
  2. * Buffered file io for ffmpeg system
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include <fcntl.h>
  23. #ifndef __MINGW32__
  24. #include <unistd.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/time.h>
  27. #else
  28. #include <io.h>
  29. #define open(fname,oflag,pmode) _open(fname,oflag,pmode)
  30. #endif /* __MINGW32__ */
  31. /* standard file protocol */
  32. static int file_open(URLContext *h, const char *filename, int flags)
  33. {
  34. int access;
  35. int fd;
  36. strstart(filename, "file:", &filename);
  37. if (flags & URL_RDWR) {
  38. access = O_CREAT | O_TRUNC | O_RDWR;
  39. } else if (flags & URL_WRONLY) {
  40. access = O_CREAT | O_TRUNC | O_WRONLY;
  41. } else {
  42. access = O_RDONLY;
  43. }
  44. #if defined(__MINGW32__) || defined(CONFIG_OS2) || defined(__CYGWIN__)
  45. access |= O_BINARY;
  46. #endif
  47. fd = open(filename, access, 0666);
  48. if (fd < 0)
  49. return -ENOENT;
  50. h->priv_data = (void *)(size_t)fd;
  51. return 0;
  52. }
  53. static int file_read(URLContext *h, unsigned char *buf, int size)
  54. {
  55. int fd = (size_t)h->priv_data;
  56. return read(fd, buf, size);
  57. }
  58. static int file_write(URLContext *h, unsigned char *buf, int size)
  59. {
  60. int fd = (size_t)h->priv_data;
  61. return write(fd, buf, size);
  62. }
  63. /* XXX: use llseek */
  64. static offset_t file_seek(URLContext *h, offset_t pos, int whence)
  65. {
  66. int fd = (size_t)h->priv_data;
  67. #if defined(__MINGW32__)
  68. return _lseeki64(fd, pos, whence);
  69. #else
  70. return lseek(fd, pos, whence);
  71. #endif
  72. }
  73. static int file_close(URLContext *h)
  74. {
  75. int fd = (size_t)h->priv_data;
  76. return close(fd);
  77. }
  78. URLProtocol file_protocol = {
  79. "file",
  80. file_open,
  81. file_read,
  82. file_write,
  83. file_seek,
  84. file_close,
  85. };
  86. /* pipe protocol */
  87. static int pipe_open(URLContext *h, const char *filename, int flags)
  88. {
  89. int fd;
  90. if (flags & URL_WRONLY) {
  91. fd = 1;
  92. } else {
  93. fd = 0;
  94. }
  95. #if defined(__MINGW32__) || defined(CONFIG_OS2) || defined(__CYGWIN__)
  96. setmode(fd, O_BINARY);
  97. #endif
  98. h->priv_data = (void *)(size_t)fd;
  99. h->is_streamed = 1;
  100. return 0;
  101. }
  102. static int pipe_read(URLContext *h, unsigned char *buf, int size)
  103. {
  104. int fd = (size_t)h->priv_data;
  105. return read(fd, buf, size);
  106. }
  107. static int pipe_write(URLContext *h, unsigned char *buf, int size)
  108. {
  109. int fd = (size_t)h->priv_data;
  110. return write(fd, buf, size);
  111. }
  112. static int pipe_close(URLContext *h)
  113. {
  114. return 0;
  115. }
  116. URLProtocol pipe_protocol = {
  117. "pipe",
  118. pipe_open,
  119. pipe_read,
  120. pipe_write,
  121. NULL,
  122. pipe_close,
  123. };