/project/jni/sdl_sound/extra_rwops.c

https://github.com/aichunyu/FFPlayer · C · 135 lines · 73 code · 26 blank · 36 comment · 7 complexity · 887cf997cb8a515479f26856b6491752 MD5 · raw file

  1. /*
  2. * SDL_sound -- An abstract sound format decoding API.
  3. * Copyright (C) 2001 Ryan C. Gordon.
  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.1 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. /*
  20. * Some extra RWops that are needed or are just handy to have.
  21. *
  22. * Please see the file COPYING in the source's root directory.
  23. *
  24. * This file written by Ryan C. Gordon. (icculus@icculus.org)
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "SDL.h"
  29. /*
  30. * The Reference Counter RWops...
  31. */
  32. typedef struct
  33. {
  34. SDL_RWops *rw; /* The actual RWops we're refcounting... */
  35. int refcount; /* The refcount; starts at 1. If goes to 0, delete. */
  36. } RWRefCounterData;
  37. /* Just pass through to the actual SDL_RWops's method... */
  38. static int refcounter_seek(SDL_RWops *rw, int offset, int whence)
  39. {
  40. RWRefCounterData *data = (RWRefCounterData *) rw->hidden.unknown.data1;
  41. return(data->rw->seek(data->rw, offset, whence));
  42. } /* refcounter_seek */
  43. /* Just pass through to the actual SDL_RWops's method... */
  44. static int refcounter_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
  45. {
  46. RWRefCounterData *data = (RWRefCounterData *) rw->hidden.unknown.data1;
  47. return(data->rw->read(data->rw, ptr, size, maxnum));
  48. } /* refcounter_read */
  49. /* Just pass through to the actual SDL_RWops's method... */
  50. static int refcounter_write(SDL_RWops *rw, const void *ptr, int size, int num)
  51. {
  52. RWRefCounterData *data = (RWRefCounterData *) rw->hidden.unknown.data1;
  53. return(data->rw->write(data->rw, ptr, size, num));
  54. } /* refcounter_write */
  55. /*
  56. * Decrement the reference count. If there are no more references, pass
  57. * through to the actual SDL_RWops's method, and then clean ourselves up.
  58. */
  59. static int refcounter_close(SDL_RWops *rw)
  60. {
  61. int retval = 0;
  62. RWRefCounterData *data = (RWRefCounterData *) rw->hidden.unknown.data1;
  63. data->refcount--;
  64. if (data->refcount <= 0)
  65. {
  66. retval = data->rw->close(data->rw);
  67. free(data);
  68. SDL_FreeRW(rw);
  69. } /* if */
  70. return(retval);
  71. } /* refcounter_close */
  72. void RWops_RWRefCounter_addRef(SDL_RWops *rw)
  73. {
  74. RWRefCounterData *data = (RWRefCounterData *) rw->hidden.unknown.data1;
  75. data->refcount++;
  76. } /* RWops_RWRefCounter_addRef */
  77. SDL_RWops *RWops_RWRefCounter_new(SDL_RWops *rw)
  78. {
  79. SDL_RWops *retval = NULL;
  80. if (rw == NULL)
  81. {
  82. SDL_SetError("NULL argument to RWops_RWRefCounter_new().");
  83. return(NULL);
  84. } /* if */
  85. retval = SDL_AllocRW();
  86. if (retval != NULL)
  87. {
  88. RWRefCounterData *data;
  89. data = (RWRefCounterData *) malloc(sizeof (RWRefCounterData));
  90. if (data == NULL)
  91. {
  92. SDL_SetError("Out of memory.");
  93. SDL_FreeRW(retval);
  94. retval = NULL;
  95. } /* if */
  96. else
  97. {
  98. data->rw = rw;
  99. data->refcount = 1;
  100. retval->hidden.unknown.data1 = data;
  101. retval->seek = refcounter_seek;
  102. retval->read = refcounter_read;
  103. retval->write = refcounter_write;
  104. retval->close = refcounter_close;
  105. } /* else */
  106. } /* if */
  107. return(retval);
  108. } /* RWops_RWRefCounter_new */
  109. /* end of extra_rwops.c ... */