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.

122 lines
2.6KB

  1. /*
  2. * Buffered file io for ffmpeg system
  3. * Copyright (c) 2001 Gerard Lantau
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <sys/ioctl.h>
  24. #include <errno.h>
  25. #include <sys/time.h>
  26. #include "avformat.h"
  27. /* standard file protocol */
  28. static int file_open(URLContext *h, const char *filename, int flags)
  29. {
  30. int access;
  31. int fd;
  32. if (flags & URL_WRONLY) {
  33. access = O_CREAT | O_TRUNC | O_WRONLY;
  34. } else {
  35. access = O_RDONLY;
  36. }
  37. fd = open(filename, access, 0666);
  38. if (fd < 0)
  39. return -ENOENT;
  40. h->priv_data = (void *)fd;
  41. return 0;
  42. }
  43. static int file_read(URLContext *h, unsigned char *buf, int size)
  44. {
  45. int fd = (int)h->priv_data;
  46. return read(fd, buf, size);
  47. }
  48. static int file_write(URLContext *h, unsigned char *buf, int size)
  49. {
  50. int fd = (int)h->priv_data;
  51. return write(fd, buf, size);
  52. }
  53. /* XXX: use llseek */
  54. static offset_t file_seek(URLContext *h, offset_t pos, int whence)
  55. {
  56. int fd = (int)h->priv_data;
  57. return lseek(fd, pos, whence);
  58. }
  59. static int file_close(URLContext *h)
  60. {
  61. int fd = (int)h->priv_data;
  62. return close(fd);
  63. }
  64. URLProtocol file_protocol = {
  65. "file",
  66. file_open,
  67. file_read,
  68. file_write,
  69. file_seek,
  70. file_close,
  71. };
  72. /* pipe protocol */
  73. static int pipe_open(URLContext *h, const char *filename, int flags)
  74. {
  75. int fd;
  76. if (flags & URL_WRONLY) {
  77. fd = 1;
  78. } else {
  79. fd = 0;
  80. }
  81. h->priv_data = (void *)fd;
  82. return 0;
  83. }
  84. static int pipe_read(URLContext *h, unsigned char *buf, int size)
  85. {
  86. int fd = (int)h->priv_data;
  87. return read(fd, buf, size);
  88. }
  89. static int pipe_write(URLContext *h, unsigned char *buf, int size)
  90. {
  91. int fd = (int)h->priv_data;
  92. return write(fd, buf, size);
  93. }
  94. static int pipe_close(URLContext *h)
  95. {
  96. return 0;
  97. }
  98. URLProtocol pipe_protocol = {
  99. "pipe",
  100. pipe_open,
  101. pipe_read,
  102. pipe_write,
  103. NULL,
  104. pipe_close,
  105. };