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.

139 lines
3.2KB

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