1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
|
/* Copyright (C) 2010-2018 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (interface_stream.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <streams/interface_stream.h>
#include <streams/file_stream.h>
#include <streams/memory_stream.h>
#ifdef HAVE_CHD
#include <streams/chd_stream.h>
#endif
struct intfstream_internal
{
enum intfstream_type type;
struct
{
RFILE *fp;
} file;
struct
{
struct
{
uint8_t *data;
uint64_t size;
} buf;
memstream_t *fp;
bool writable;
} memory;
#ifdef HAVE_CHD
struct
{
int32_t track;
chdstream_t *fp;
} chd;
#endif
};
int64_t intfstream_get_size(intfstream_internal_t *intf)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_get_size(intf->file.fp);
case INTFSTREAM_MEMORY:
return intf->memory.buf.size;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return chdstream_get_size(intf->chd.fp);
#else
break;
#endif
}
return 0;
}
bool intfstream_resize(intfstream_internal_t *intf, intfstream_info_t *info)
{
if (!intf || !info)
return false;
switch (intf->type)
{
case INTFSTREAM_FILE:
break;
case INTFSTREAM_MEMORY:
intf->memory.buf.data = info->memory.buf.data;
intf->memory.buf.size = info->memory.buf.size;
memstream_set_buffer(intf->memory.buf.data,
intf->memory.buf.size);
break;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
#endif
break;
}
return true;
}
bool intfstream_open(intfstream_internal_t *intf, const char *path,
unsigned mode, unsigned hints)
{
if (!intf)
return false;
switch (intf->type)
{
case INTFSTREAM_FILE:
intf->file.fp = filestream_open(path, mode, hints);
if (!intf->file.fp)
return false;
break;
case INTFSTREAM_MEMORY:
intf->memory.fp = memstream_open(intf->memory.writable);
if (!intf->memory.fp)
return false;
break;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
intf->chd.fp = chdstream_open(path, intf->chd.track);
if (!intf->chd.fp)
return false;
break;
#else
return false;
#endif
}
return true;
}
int intfstream_flush(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_flush(intf->file.fp);
case INTFSTREAM_MEMORY:
case INTFSTREAM_CHD:
/* Should we stub this for these interfaces? */
break;
}
return 0;
}
int intfstream_close(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
if (intf->file.fp)
return filestream_close(intf->file.fp);
return 0;
case INTFSTREAM_MEMORY:
if (intf->memory.fp)
memstream_close(intf->memory.fp);
return 0;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
if (intf->chd.fp)
chdstream_close(intf->chd.fp);
#endif
return 0;
}
return -1;
}
void *intfstream_init(intfstream_info_t *info)
{
intfstream_internal_t *intf = NULL;
if (!info)
goto error;
intf = (intfstream_internal_t*)calloc(1, sizeof(*intf));
if (!intf)
goto error;
intf->type = info->type;
switch (intf->type)
{
case INTFSTREAM_FILE:
break;
case INTFSTREAM_MEMORY:
intf->memory.writable = info->memory.writable;
if (!intfstream_resize(intf, info))
goto error;
break;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
intf->chd.track = info->chd.track;
break;
#else
goto error;
#endif
}
return intf;
error:
if (intf)
free(intf);
return NULL;
}
int64_t intfstream_seek(intfstream_internal_t *intf, int64_t offset, int whence)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
{
int seek_position = 0;
switch (whence)
{
case SEEK_SET:
seek_position = RETRO_VFS_SEEK_POSITION_START;
break;
case SEEK_CUR:
seek_position = RETRO_VFS_SEEK_POSITION_CURRENT;
break;
case SEEK_END:
seek_position = RETRO_VFS_SEEK_POSITION_END;
break;
}
return (int64_t)filestream_seek(intf->file.fp, (int64_t)offset,
seek_position);
}
case INTFSTREAM_MEMORY:
return (int64_t)memstream_seek(intf->memory.fp, offset, whence);
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return (int64_t)chdstream_seek(intf->chd.fp, offset, whence);
#else
break;
#endif
}
return -1;
}
int64_t intfstream_read(intfstream_internal_t *intf, void *s, uint64_t len)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_read(intf->file.fp, s, len);
case INTFSTREAM_MEMORY:
return memstream_read(intf->memory.fp, s, len);
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return chdstream_read(intf->chd.fp, s, len);
#else
break;
#endif
}
return -1;
}
int64_t intfstream_write(intfstream_internal_t *intf,
const void *s, uint64_t len)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_write(intf->file.fp, s, len);
case INTFSTREAM_MEMORY:
return memstream_write(intf->memory.fp, s, len);
case INTFSTREAM_CHD:
return -1;
}
return 0;
}
char *intfstream_gets(intfstream_internal_t *intf,
char *buffer, uint64_t len)
{
if (!intf)
return NULL;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_gets(intf->file.fp,
buffer, (size_t)len);
case INTFSTREAM_MEMORY:
return memstream_gets(intf->memory.fp,
buffer, (size_t)len);
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return chdstream_gets(intf->chd.fp, buffer, len);
#else
break;
#endif
}
return NULL;
}
int intfstream_getc(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_getc(intf->file.fp);
case INTFSTREAM_MEMORY:
return memstream_getc(intf->memory.fp);
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return chdstream_getc(intf->chd.fp);
#else
break;
#endif
}
return -1;
}
int64_t intfstream_tell(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return (int64_t)filestream_tell(intf->file.fp);
case INTFSTREAM_MEMORY:
return (int64_t)memstream_pos(intf->memory.fp);
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return (int64_t)chdstream_tell(intf->chd.fp);
#else
break;
#endif
}
return -1;
}
void intfstream_rewind(intfstream_internal_t *intf)
{
switch (intf->type)
{
case INTFSTREAM_FILE:
filestream_rewind(intf->file.fp);
break;
case INTFSTREAM_MEMORY:
memstream_rewind(intf->memory.fp);
break;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
chdstream_rewind(intf->chd.fp);
#endif
break;
}
}
void intfstream_putc(intfstream_internal_t *intf, int c)
{
if (!intf)
return;
switch (intf->type)
{
case INTFSTREAM_FILE:
filestream_putc(intf->file.fp, c);
break;
case INTFSTREAM_MEMORY:
memstream_putc(intf->memory.fp, c);
break;
case INTFSTREAM_CHD:
break;
}
}
intfstream_t* intfstream_open_file(const char *path,
unsigned mode, unsigned hints)
{
intfstream_info_t info;
intfstream_t *fd = NULL;
info.type = INTFSTREAM_FILE;
fd = (intfstream_t*)intfstream_init(&info);
if (!fd)
return NULL;
if (!intfstream_open(fd, path, mode, hints))
goto error;
return fd;
error:
if (fd)
{
intfstream_close(fd);
free(fd);
}
return NULL;
}
intfstream_t *intfstream_open_memory(void *data,
unsigned mode, unsigned hints, uint64_t size)
{
intfstream_info_t info;
intfstream_t *fd = NULL;
info.type = INTFSTREAM_MEMORY;
info.memory.buf.data = (uint8_t*)data;
info.memory.buf.size = size;
info.memory.writable = false;
fd = (intfstream_t*)intfstream_init(&info);
if (!fd)
return NULL;
if (!intfstream_open(fd, NULL, mode, hints))
goto error;
return fd;
error:
if (fd)
{
intfstream_close(fd);
free(fd);
}
return NULL;
}
intfstream_t *intfstream_open_chd_track(const char *path,
unsigned mode, unsigned hints, int32_t track)
{
intfstream_info_t info;
intfstream_t *fd = NULL;
info.type = INTFSTREAM_CHD;
info.chd.track = track;
fd = (intfstream_t*)intfstream_init(&info);
if (!fd)
return NULL;
if (!intfstream_open(fd, path, mode, hints))
goto error;
return fd;
error:
if (fd)
{
intfstream_close(fd);
free(fd);
}
return NULL;
}
|