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.

133 lines
2.9KB

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