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.

131 lines
2.8KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. if (flags & URL_WRONLY) {
  35. access = O_CREAT | O_TRUNC | O_WRONLY;
  36. } else {
  37. access = O_RDONLY;
  38. }
  39. #ifdef CONFIG_WIN32
  40. access |= O_BINARY;
  41. #endif
  42. fd = open(filename, access, 0666);
  43. if (fd < 0)
  44. return -ENOENT;
  45. h->priv_data = (void *)fd;
  46. return 0;
  47. }
  48. static int file_read(URLContext *h, unsigned char *buf, int size)
  49. {
  50. int fd = (int)h->priv_data;
  51. return read(fd, buf, size);
  52. }
  53. static int file_write(URLContext *h, unsigned char *buf, int size)
  54. {
  55. int fd = (int)h->priv_data;
  56. return write(fd, buf, size);
  57. }
  58. /* XXX: use llseek */
  59. static offset_t file_seek(URLContext *h, offset_t pos, int whence)
  60. {
  61. int fd = (int)h->priv_data;
  62. #ifdef CONFIG_WIN32
  63. return _lseeki64(fd, pos, whence);
  64. #else
  65. return lseek(fd, pos, whence);
  66. #endif
  67. }
  68. static int file_close(URLContext *h)
  69. {
  70. int fd = (int)h->priv_data;
  71. return close(fd);
  72. }
  73. URLProtocol file_protocol = {
  74. "file",
  75. file_open,
  76. file_read,
  77. file_write,
  78. file_seek,
  79. file_close,
  80. };
  81. /* pipe protocol */
  82. static int pipe_open(URLContext *h, const char *filename, int flags)
  83. {
  84. int fd;
  85. if (flags & URL_WRONLY) {
  86. fd = 1;
  87. } else {
  88. fd = 0;
  89. }
  90. h->priv_data = (void *)fd;
  91. return 0;
  92. }
  93. static int pipe_read(URLContext *h, unsigned char *buf, int size)
  94. {
  95. int fd = (int)h->priv_data;
  96. return read(fd, buf, size);
  97. }
  98. static int pipe_write(URLContext *h, unsigned char *buf, int size)
  99. {
  100. int fd = (int)h->priv_data;
  101. return write(fd, buf, size);
  102. }
  103. static int pipe_close(URLContext *h)
  104. {
  105. return 0;
  106. }
  107. URLProtocol pipe_protocol = {
  108. "pipe",
  109. pipe_open,
  110. pipe_read,
  111. pipe_write,
  112. NULL,
  113. pipe_close,
  114. };