rxml_document_t *rxml_load_document(const char *path) { #ifndef RXML_TEST RARCH_WARN("Using RXML as drop in for libxml2. Behavior might be very buggy.\n"); #endif char *memory_buffer = NULL; char *new_memory_buffer = NULL; const char *mem_ptr = NULL; long len = 0; RFILE *file = filestream_open(path, RFILE_MODE_READ, -1); if (!file) return NULL; rxml_document_t *doc = (rxml_document_t*)calloc(1, sizeof(*doc)); if (!doc) goto error; filestream_seek(file, 0, SEEK_END); len = filestream_tell(file); filestream_rewind(file); memory_buffer = (char*)malloc(len + 1); if (!memory_buffer) goto error; memory_buffer[len] = '\0'; if (filestream_read(file, memory_buffer, len) != (size_t)len) goto error; filestream_close(file); file = NULL; mem_ptr = memory_buffer; if (!validate_header(&mem_ptr)) goto error; new_memory_buffer = purge_xml_comments(mem_ptr); if (!new_memory_buffer) goto error; free(memory_buffer); mem_ptr = memory_buffer = new_memory_buffer; doc->root_node = rxml_parse_node(&mem_ptr); if (!doc->root_node) goto error; free(memory_buffer); return doc; error: free(memory_buffer); filestream_close(file); rxml_free_document(doc); return NULL; }
/** * filestream_read_file: * @path : path to file. * @buf : buffer to allocate and read the contents of the * file into. Needs to be freed manually. * * Read the contents of a file into @buf. * * Returns: number of items read, -1 on error. */ int64_t filestream_read_file(const char *path, void **buf, int64_t *len) { int64_t ret = 0; int64_t content_buf_size = 0; void *content_buf = NULL; RFILE *file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!file) { fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno)); goto error; } content_buf_size = filestream_get_size(file); if (content_buf_size < 0) goto error; content_buf = malloc((size_t)(content_buf_size + 1)); if (!content_buf) goto error; if ((int64_t)(uint64_t)(content_buf_size + 1) != (content_buf_size + 1)) goto error; ret = filestream_read(file, content_buf, (int64_t)content_buf_size); if (ret < 0) { fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno)); goto error; } filestream_close(file); *buf = content_buf; /* Allow for easy reading of strings to be safe. * Will only work with sane character formatting (Unix). */ ((char*)content_buf)[ret] = '\0'; if (len) *len = ret; return 1; error: if (file) filestream_close(file); if (content_buf) free(content_buf); if (len) *len = -1; *buf = NULL; return 0; }
/** * filestream_read_file: * @path : path to file. * @buf : buffer to allocate and read the contents of the * file into. Needs to be freed manually. * * Read the contents of a file into @buf. * * Returns: number of items read, -1 on error. */ int filestream_read_file(const char *path, void **buf, ssize_t *len) { ssize_t ret = 0; ssize_t content_buf_size = 0; void *content_buf = NULL; RFILE *file = filestream_open(path, RFILE_MODE_READ, -1); if (!file) { fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno)); goto error; } if (filestream_seek(file, 0, SEEK_END) != 0) goto error; content_buf_size = filestream_tell(file); if (content_buf_size < 0) goto error; filestream_rewind(file); content_buf = malloc(content_buf_size + 1); if (!content_buf) goto error; ret = filestream_read(file, content_buf, content_buf_size); if (ret < 0) { fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno)); goto error; } filestream_close(file); *buf = content_buf; /* Allow for easy reading of strings to be safe. * Will only work with sane character formatting (Unix). */ ((char*)content_buf)[content_buf_size] = '\0'; if (len) *len = ret; return 1; error: if (file) filestream_close(file); if (content_buf) free(content_buf); if (len) *len = -1; *buf = NULL; return 0; }
rxml_document_t *rxml_load_document(const char *path) { rxml_document_t *doc; char *memory_buffer = NULL; char *new_memory_buffer = NULL; const char *mem_ptr = NULL; long len = 0; RFILE *file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!file) return NULL; doc = (rxml_document_t*)calloc(1, sizeof(*doc)); if (!doc) goto error; len = filestream_get_size(file); memory_buffer = (char*)malloc(len + 1); if (!memory_buffer) goto error; memory_buffer[len] = '\0'; if (filestream_read(file, memory_buffer, len) != (size_t)len) goto error; filestream_close(file); file = NULL; mem_ptr = memory_buffer; if (!validate_header(&mem_ptr)) goto error; new_memory_buffer = purge_xml_comments(mem_ptr); if (!new_memory_buffer) goto error; free(memory_buffer); mem_ptr = memory_buffer = new_memory_buffer; doc->root_node = rxml_parse_node(&mem_ptr); if (!doc->root_node) goto error; free(memory_buffer); return doc; error: free(memory_buffer); filestream_close(file); rxml_free_document(doc); return NULL; }
static bool gfx_ctx_mali_fbdev_set_video_mode(void *data, video_frame_info_t *video_info, unsigned width, unsigned height, bool fullscreen) { struct fb_var_screeninfo vinfo; static const EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, /* Use version 2, even for GLES3. */ EGL_NONE }; mali_ctx_data_t *mali = (mali_ctx_data_t*)data; RFILE *fd = filestream_open("/dev/fb0", RFILE_MODE_READ_WRITE, -1); int fb = filestream_get_fd(fd); if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) < 0) { RARCH_ERR("Error obtaining framebuffer info.\n"); goto error; } filestream_close(fd); width = vinfo.xres; height = vinfo.yres; mali->width = width; mali->height = height; mali->native_window.width = vinfo.xres; mali->native_window.height = vinfo.yres; #ifdef HAVE_EGL if (!egl_create_context(&mali->egl, attribs)) { egl_report_error(); goto error; } #endif #ifdef HAVE_EGL if (!egl_create_surface(&mali->egl, &mali->native_window)) goto error; #endif return true; error: if (fd) filestream_close(fd); RARCH_ERR("[Mali fbdev]: EGL error: %d.\n", eglGetError()); gfx_ctx_mali_fbdev_destroy(data); return false; }
static void gfx_ctx_mali_fbdev_destroy(void *data) { int fb; RFILE *fd = NULL; mali_ctx_data_t *mali = (mali_ctx_data_t*)data; if (mali) { #ifdef HAVE_EGL egl_destroy(&mali->egl); #endif mali->resize = false; free(mali); } /* Clear framebuffer and set cursor on again */ fd = filestream_open("/dev/tty", RFILE_MODE_READ_WRITE, -1); fb = filestream_get_fd(fd); ioctl(fb, VT_ACTIVATE,5); ioctl(fb, VT_ACTIVATE,1); filestream_close(fd); system("setterm -cursor on"); }
static bool tryload(Settings *setfile) { char setfilename_tmp[1024]; RFILE *fp = NULL; retro_create_path_string(setfilename_tmp, sizeof(setfilename_tmp), g_dir, setfilename); fp = filestream_open(setfilename_tmp, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!fp) { NX_ERR("Couldn't open file %s.\n", setfilename_tmp); return 1; } NX_LOG("Loading settings...\n"); setfile->version = 0; filestream_read(fp, setfile, sizeof(Settings)); if (setfile->version != SETTINGS_VERSION) { NX_ERR("Wrong settings version %04x.\n", setfile->version); return 1; } filestream_close(fp); return 0; }
void playlist_write_file(playlist_t *playlist) { size_t i; RFILE *file = NULL; if (!playlist || !playlist->modified) return; file = filestream_open(playlist->conf_path, RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!file) { RARCH_ERR("Failed to write to playlist file: %s\n", playlist->conf_path); return; } for (i = 0; i < playlist->size; i++) filestream_printf(file, "%s\n%s\n%s\n%s\n%s\n%s\n", playlist->entries[i].path ? playlist->entries[i].path : "", playlist->entries[i].label ? playlist->entries[i].label : "", playlist->entries[i].core_path, playlist->entries[i].core_name, playlist->entries[i].crc32 ? playlist->entries[i].crc32 : "", playlist->entries[i].db_name ? playlist->entries[i].db_name : "" ); playlist->modified = false; RARCH_LOG("Written to playlist file: %s\n", playlist->conf_path); filestream_close(file); }
bool settings_save(Settings *setfile) { char setfilename_tmp[1024]; RFILE *fp = NULL; if (!setfile) setfile = &normal_settings; retro_create_path_string(setfilename_tmp, sizeof(setfilename_tmp), g_dir, setfilename); fp = filestream_open(setfilename_tmp, RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!fp) { NX_ERR("Couldn't open file %s.\n", setfilename_tmp); return 1; } NX_LOG("Writing settings...\n"); for(int i=0;i<INPUT_COUNT;i++) setfile->input_mappings[i] = input_get_mapping(i); setfile->version = SETTINGS_VERSION; filestream_write(fp, setfile, sizeof(Settings)); filestream_close(fp); 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; }
int detect_system(const char *track_path, const char **system_name) { int rv; char magic[MAGIC_LEN]; int i; RFILE *fd = filestream_open(track_path, RFILE_MODE_READ, -1); if (!fd) { RARCH_LOG("Could not open data track of file '%s': %s\n", track_path, strerror(errno)); rv = -errno; goto clean; } RARCH_LOG("%s\n", msg_hash_to_str(MSG_COMPARING_WITH_KNOWN_MAGIC_NUMBERS)); for (i = 0; MAGIC_NUMBERS[i].system_name != NULL; i++) { filestream_seek(fd, MAGIC_NUMBERS[i].offset, SEEK_SET); if (filestream_read(fd, magic, MAGIC_LEN) < MAGIC_LEN) { RARCH_LOG("Could not read data from file '%s' at offset %d: %s\n", track_path, MAGIC_NUMBERS[i].offset, strerror(errno)); rv = -errno; goto clean; } if (!string_is_empty(MAGIC_NUMBERS[i].magic) && !string_is_empty(magic) && string_is_equal_fast(MAGIC_NUMBERS[i].magic, magic, MAGIC_LEN)) { *system_name = MAGIC_NUMBERS[i].system_name; rv = 0; goto clean; } } filestream_seek(fd, 0x8008, SEEK_SET); if (filestream_read(fd, magic, 8) > 0) { magic[8] = '\0'; if (!string_is_empty(magic) && string_is_equal_fast(magic, "PSP GAME", 8)) { *system_name = "psp\0"; rv = 0; goto clean; } } RARCH_LOG("%s\n", msg_hash_to_str(MSG_COULD_NOT_FIND_COMPATIBLE_SYSTEM)); rv = -EINVAL; clean: filestream_close(fd); return rv; }
int detect_system(const char *track_path, int32_t offset, const char **system_name) { int rv; char magic[MAGIC_LEN]; int i; RFILE *fd = filestream_open(track_path, RFILE_MODE_READ, -1); if (!fd) { RARCH_LOG("Could not open data track of file '%s': %s\n", track_path, strerror(errno)); rv = -errno; goto clean; } filestream_seek(fd, offset, SEEK_SET); if (filestream_read(fd, magic, MAGIC_LEN) < MAGIC_LEN) { RARCH_LOG("Could not read data from file '%s' at offset %d: %s\n", track_path, offset, strerror(errno)); rv = -errno; goto clean; } RARCH_LOG("Comparing with known magic numbers...\n"); for (i = 0; MAGIC_NUMBERS[i].system_name != NULL; i++) { if (memcmp(MAGIC_NUMBERS[i].magic, magic, MAGIC_LEN) == 0) { *system_name = MAGIC_NUMBERS[i].system_name; rv = 0; goto clean; } } filestream_seek(fd, 0x8008, SEEK_SET); if (filestream_read(fd, magic, 8) > 0) { magic[8] = '\0'; if (string_is_equal(magic, "PSP GAME")) { *system_name = "psp\0"; rv = 0; goto clean; } } RARCH_LOG("Could not find compatible system\n"); rv = -EINVAL; clean: filestream_close(fd); return rv; }
/** * filestream_write_file: * @path : path to file. * @data : contents to write to the file. * @size : size of the contents. * * Writes data to a file. * * Returns: true (1) on success, false (0) otherwise. */ bool filestream_write_file(const char *path, const void *data, ssize_t size) { ssize_t ret = 0; RFILE *file = filestream_open(path, RFILE_MODE_WRITE, -1); if (!file) return false; ret = filestream_write(file, data, size); filestream_close(file); return (ret == size); }
int sha1_calculate(const char *path, char *result) { SHA1Context sha; unsigned char buff[4096]; int rv = 1; RFILE *fd = filestream_open(path, RFILE_MODE_READ, -1); if (!fd) goto error; buff[0] = '\0'; SHA1Reset(&sha); do { rv = filestream_read(fd, buff, 4096); if (rv < 0) goto error; SHA1Input(&sha, buff, rv); }while(rv); if (!SHA1Result(&sha)) goto error; sprintf(result, "%08X%08X%08X%08X%08X", sha.Message_Digest[0], sha.Message_Digest[1], sha.Message_Digest[2], sha.Message_Digest[3], sha.Message_Digest[4]); filestream_close(fd); return 0; error: if (fd) filestream_close(fd); return -1; }
int intfstream_close(intfstream_internal_t *intf) { if (!intf) return -1; switch (intf->type) { case INTFSTREAM_FILE: return filestream_close(intf->file.fp); case INTFSTREAM_MEMORY: memstream_close(intf->memory.fp); return 0; } return -1; }
/** * filestream_write_file: * @path : path to file. * @data : contents to write to the file. * @size : size of the contents. * * Writes data to a file. * * Returns: true (1) on success, false (0) otherwise. */ bool filestream_write_file(const char *path, const void *data, int64_t size) { int64_t ret = 0; RFILE *file = filestream_open(path, RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!file) return false; ret = filestream_write(file, data, size); filestream_close(file); if (ret != size) return false; return true; }
/* Callback wrappers */ bool filestream_exists(const char *path) { RFILE *dummy = NULL; if (!path || !*path) return false; dummy = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!dummy) return false; filestream_close(dummy); return true; }
/** * libretrodb_cursor_close: * @cursor : Handle to database cursor. * * Closes cursor and frees up allocated memory. **/ void libretrodb_cursor_close(libretrodb_cursor_t *cursor) { if (!cursor) return; if (cursor->fd) filestream_close(cursor->fd); if (cursor->query) libretrodb_query_free(cursor->query); cursor->is_valid = 0; cursor->eof = 1; cursor->fd = NULL; cursor->db = NULL; cursor->query = NULL; }
int libretrodb_open(const char *path, libretrodb_t *db) { libretrodb_header_t header; libretrodb_metadata_t md; int rv; RFILE *fd = filestream_open(path, RFILE_MODE_READ, -1); if (!fd) return -errno; strlcpy(db->path, path, sizeof(db->path)); db->root = filestream_seek(fd, 0, SEEK_CUR); if ((rv = filestream_read(fd, &header, sizeof(header))) == -1) { rv = -errno; goto error; } if (strncmp(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)) != 0) { rv = -EINVAL; goto error; } header.metadata_offset = swap_if_little64(header.metadata_offset); filestream_seek(fd, (ssize_t)header.metadata_offset, SEEK_SET); if (libretrodb_read_metadata(fd, &md) < 0) { rv = -EINVAL; goto error; } db->count = md.count; db->first_index_offset = filestream_seek(fd, 0, SEEK_CUR); db->fd = fd; return 0; error: if (fd) filestream_close(fd); return rv; }
static void free_drm_resources(gfx_ctx_drm_data_t *drm) { if (!drm) return; if (g_gbm_surface) gbm_surface_destroy(g_gbm_surface); if (g_gbm_dev) gbm_device_destroy(g_gbm_dev); drm_free(); if (drm->drm) if (g_drm_fd >= 0) filestream_close(drm->drm); g_gbm_surface = NULL; g_gbm_dev = NULL; g_drm_fd = -1; }
/* Parses log file referenced by runtime_log->path. * Does nothing if log file does not exist. */ static void runtime_log_read_file(runtime_log_t *runtime_log) { unsigned runtime_hours = 0; unsigned runtime_minutes = 0; unsigned runtime_seconds = 0; unsigned last_played_year = 0; unsigned last_played_month = 0; unsigned last_played_day = 0; unsigned last_played_hour = 0; unsigned last_played_minute = 0; unsigned last_played_second = 0; RtlJSONContext context = {0}; RFILE *file = NULL; int ret = 0; /* Check if log file exists */ if (!filestream_exists(runtime_log->path)) return; /* Attempt to open log file */ file = filestream_open(runtime_log->path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!file) { RARCH_ERR("Failed to open runtime log file: %s\n", runtime_log->path); return; } /* Initialise JSON parser */ context.runtime_string = NULL; context.last_played_string = NULL; context.parser = JSON_Parser_Create(NULL); context.file = file; if (!context.parser) { RARCH_ERR("Failed to create JSON parser.\n"); goto end; } /* Configure parser */ JSON_Parser_SetAllowBOM(context.parser, JSON_True); JSON_Parser_SetStringHandler(context.parser, &RtlJSONStringHandler); JSON_Parser_SetObjectMemberHandler(context.parser, &RtlJSONObjectMemberHandler); JSON_Parser_SetUserData(context.parser, &context); /* Read file */ while (!filestream_eof(file)) { /* Runtime log files are tiny - use small chunk size */ char chunk[128] = {0}; int64_t length = filestream_read(file, chunk, sizeof(chunk)); /* Error checking... */ if (!length && !filestream_eof(file)) { RARCH_ERR("Failed to read runtime log file: %s\n", runtime_log->path); JSON_Parser_Free(context.parser); goto end; } /* Parse chunk */ if (!JSON_Parser_Parse(context.parser, chunk, length, JSON_False)) { RARCH_ERR("Error parsing chunk of runtime log file: %s\n---snip---\n%s\n---snip---\n", runtime_log->path, chunk); RtlJSONLogError(&context); JSON_Parser_Free(context.parser); goto end; } } /* Finalise parsing */ if (!JSON_Parser_Parse(context.parser, NULL, 0, JSON_True)) { RARCH_WARN("Error parsing runtime log file: %s\n", runtime_log->path); RtlJSONLogError(&context); JSON_Parser_Free(context.parser); goto end; } /* Free parser */ JSON_Parser_Free(context.parser); /* Process string values read from JSON file */ /* Runtime */ ret = 0; if (!string_is_empty(context.runtime_string)) ret = sscanf(context.runtime_string, LOG_FILE_RUNTIME_FORMAT_STR, &runtime_hours, &runtime_minutes, &runtime_seconds); if (ret != 3) { RARCH_ERR("Runtime log file - invalid 'runtime' entry detected: %s\n", runtime_log->path); goto end; } /* Last played */ ret = 0; if (!string_is_empty(context.last_played_string)) ret = sscanf(context.last_played_string, LOG_FILE_LAST_PLAYED_FORMAT_STR, &last_played_year, &last_played_month, &last_played_day, &last_played_hour, &last_played_minute, &last_played_second); if (ret != 6) { RARCH_ERR("Runtime log file - invalid 'last played' entry detected: %s\n", runtime_log->path); goto end; } /* If we reach this point then all is well * > Assign values to runtime_log object */ runtime_log->runtime.hours = runtime_hours; runtime_log->runtime.minutes = runtime_minutes; runtime_log->runtime.seconds = runtime_seconds; runtime_log->last_played.year = last_played_year; runtime_log->last_played.month = last_played_month; runtime_log->last_played.day = last_played_day; runtime_log->last_played.hour = last_played_hour; runtime_log->last_played.minute = last_played_minute; runtime_log->last_played.second = last_played_second; end: /* Clean up leftover strings */ if (context.runtime_string) free(context.runtime_string); if (context.last_played_string) free(context.last_played_string); /* Close log file */ filestream_close(file); }
int detect_psp_game(const char *track_path, char *game_id) { unsigned pos; bool rv = false; RFILE *fd = filestream_open(track_path, RFILE_MODE_READ, -1); if (!fd) { RARCH_LOG("Could not open data track: %s\n", strerror(errno)); return -errno; } for (pos = 0; pos < 100000; pos++) { filestream_seek(fd, pos, SEEK_SET); if (filestream_read(fd, game_id, 5) > 0) { game_id[5] = '\0'; if (string_is_equal(game_id, "ULES-") || string_is_equal(game_id, "ULUS-") || string_is_equal(game_id, "ULJS-") || string_is_equal(game_id, "ULEM-") || string_is_equal(game_id, "ULUM-") || string_is_equal(game_id, "ULJM-") || string_is_equal(game_id, "UCES-") || string_is_equal(game_id, "UCUS-") || string_is_equal(game_id, "UCJS-") || string_is_equal(game_id, "UCAS-") || string_is_equal(game_id, "NPEH-") || string_is_equal(game_id, "NPUH-") || string_is_equal(game_id, "NPJH-") || string_is_equal(game_id, "NPEG-") || string_is_equal(game_id, "NPUG-") || string_is_equal(game_id, "NPJG-") || string_is_equal(game_id, "NPHG-") || string_is_equal(game_id, "NPEZ-") || string_is_equal(game_id, "NPUZ-") || string_is_equal(game_id, "NPJZ-") ) { filestream_seek(fd, pos, SEEK_SET); if (filestream_read(fd, game_id, 10) > 0) { #if 0 game_id[4] = '-'; game_id[8] = game_id[9]; game_id[9] = game_id[10]; #endif game_id[10] = '\0'; rv = true; } break; } } else break; } filestream_close(fd); return rv; }
static int detect_ps1_game_sub(const char *track_path, char *game_id, int sub_channel_mixed) { uint8_t* tmp; uint8_t buffer[2048 * 2]; int skip, frame_size, is_mode1, cd_sector; RFILE *fp = filestream_open(track_path, RFILE_MODE_READ, -1); if (!fp) return 0; is_mode1 = 0; filestream_seek(fp, 0, SEEK_END); if (!sub_channel_mixed) { if (!(filestream_tell(fp) & 0x7FF)) { unsigned int mode_test = 0; filestream_seek(fp, 0, SEEK_SET); filestream_read(fp, &mode_test, 4); if (mode_test != MODETEST_VAL) is_mode1 = 1; } } skip = is_mode1? 0: 24; frame_size = sub_channel_mixed? 2448: is_mode1? 2048: 2352; filestream_seek(fp, 156 + skip + 16 * frame_size, SEEK_SET); filestream_read(fp, buffer, 6); cd_sector = buffer[2] | (buffer[3] << 8) | (buffer[4] << 16); filestream_seek(fp, skip + cd_sector * frame_size, SEEK_SET); filestream_read(fp, buffer, 2048 * 2); tmp = buffer; while (tmp < (buffer + 2048 * 2)) { if (!*tmp) return 0; if (!strncasecmp((const char*)(tmp + 33), "SYSTEM.CNF;1", 12)) break; tmp += *tmp; } if(tmp >= (buffer + 2048 * 2)) return 0; cd_sector = tmp[2] | (tmp[3] << 8) | (tmp[4] << 16); filestream_seek(fp, 13 + skip + cd_sector * frame_size, SEEK_SET); filestream_read(fp, buffer, 256); tmp = (uint8_t*)strrchr((const char*)buffer, '\\'); if(!tmp) tmp = buffer; else tmp++; *game_id++ = toupper(*tmp++); *game_id++ = toupper(*tmp++); *game_id++ = toupper(*tmp++); *game_id++ = toupper(*tmp++); *game_id++ = '-'; tmp++; *game_id++ = *tmp++; *game_id++ = *tmp++; *game_id++ = *tmp++; tmp++; *game_id++ = *tmp++; *game_id++ = *tmp++; *game_id = 0; filestream_close(fp); return 1; }
RFILE *filestream_open(const char *path, unsigned mode, ssize_t len) { int flags = 0; int mode_int = 0; const char *mode_str = NULL; RFILE *stream = (RFILE*)calloc(1, sizeof(*stream)); if (!stream) return NULL; (void)mode_str; (void)mode_int; (void)flags; stream->hints = mode; #ifdef HAVE_MMAP if (stream->hints & RFILE_HINT_MMAP && (stream->hints & 0xff) == RFILE_MODE_READ) stream->hints |= RFILE_HINT_UNBUFFERED; else #endif stream->hints &= ~RFILE_HINT_MMAP; switch (mode & 0xff) { case RFILE_MODE_READ: #if defined(VITA) || defined(PSP) mode_int = 0777; flags = PSP_O_RDONLY; #elif defined(__CELLOS_LV2__) mode_int = 0777; flags = CELL_FS_O_RDONLY; #else #if defined(HAVE_BUFFERED_IO) if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) mode_str = "rb"; #endif /* No "else" here */ flags = O_RDONLY; #endif break; case RFILE_MODE_WRITE: #if defined(VITA) || defined(PSP) mode_int = 0777; flags = PSP_O_CREAT | PSP_O_WRONLY | PSP_O_TRUNC; #elif defined(__CELLOS_LV2__) mode_int = 0777; flags = CELL_FS_O_CREAT | CELL_FS_O_WRONLY | CELL_FS_O_TRUNC; #else #if defined(HAVE_BUFFERED_IO) if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) mode_str = "wb"; #endif else { flags = O_WRONLY | O_CREAT | O_TRUNC; #ifndef _WIN32 flags |= S_IRUSR | S_IWUSR; #endif } #endif break; case RFILE_MODE_READ_WRITE: #if defined(VITA) || defined(PSP) mode_int = 0777; flags = PSP_O_RDWR; #elif defined(__CELLOS_LV2__) mode_int = 0777; flags = CELL_FS_O_RDWR; #else #if defined(HAVE_BUFFERED_IO) if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) mode_str = "w+"; #endif else { flags = O_RDWR; #ifdef _WIN32 flags |= O_BINARY; #endif } #endif break; } #if defined(VITA) || defined(PSP) stream->fd = sceIoOpen(path, flags, mode_int); #elif defined(__CELLOS_LV2__) cellFsOpen(path, flags, &stream->fd, NULL, 0); #else #if defined(HAVE_BUFFERED_IO) if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) { stream->fp = fopen(path, mode_str); if (!stream->fp) goto error; } else #endif { stream->fd = open(path, flags); if (stream->fd == -1) goto error; #ifdef HAVE_MMAP if (stream->hints & RFILE_HINT_MMAP) { stream->mappos = 0; stream->mapped = NULL; stream->mapsize = filestream_seek(stream, 0, SEEK_END); if (stream->mapsize == (uint64_t)-1) goto error; filestream_rewind(stream); stream->mapped = (uint8_t*)mmap((void*)0, stream->mapsize, PROT_READ, MAP_SHARED, stream->fd, 0); if (stream->mapped == MAP_FAILED) stream->hints &= ~RFILE_HINT_MMAP; } #endif } #endif #if defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__) if (stream->fd == -1) goto error; #endif return stream; error: filestream_close(stream); return NULL; }
static config_file_t *config_file_new_internal( const char *path, unsigned depth, config_file_cb_t *cb) { RFILE *file = NULL; struct config_file *conf = (struct config_file*)malloc(sizeof(*conf)); if (!conf) return NULL; conf->path = NULL; conf->entries = NULL; conf->tail = NULL; conf->last = NULL; conf->includes = NULL; conf->include_depth = 0; conf->guaranteed_no_duplicates = false ; if (!path || !*path) return conf; if (path_is_directory(path)) goto error; conf->path = strdup(path); if (!conf->path) goto error; conf->include_depth = depth; file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!file) { free(conf->path); goto error; } while (!filestream_eof(file)) { char *line = NULL; struct config_entry_list *list = (struct config_entry_list*)malloc(sizeof(*list)); if (!list) { config_file_free(conf); filestream_close(file); return NULL; } list->readonly = false; list->key = NULL; list->value = NULL; list->next = NULL; line = filestream_getline(file); if (!line) { free(list); continue; } if (*line && parse_line(conf, list, line, cb)) { if (conf->entries) conf->tail->next = list; else conf->entries = list; conf->tail = list; if (cb != NULL && list->key != NULL && list->value != NULL) cb->config_file_new_entry_cb(list->key, list->value) ; } free(line); if (list != conf->tail) free(list); } filestream_close(file); return conf; error: free(conf); return NULL; }
void FileStream::close(void) { if (!fp) return; filestream_close(fp); }
/* Saves specified runtime log to disk */ void runtime_log_save(runtime_log_t *runtime_log) { RtlJSONContext context = {0}; RFILE *file = NULL; char value_string[64]; /* 64 characters should be enough for a very long runtime... :) */ int n; if (!runtime_log) return; RARCH_LOG("Saving runtime log file: %s\n", runtime_log->path); /* Attempt to open log file */ file = filestream_open(runtime_log->path, RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!file) { RARCH_ERR("Failed to open runtime log file: %s\n", runtime_log->path); return; } /* Initialise JSON writer */ context.writer = JSON_Writer_Create(NULL); context.file = file; if (!context.writer) { RARCH_ERR("Failed to create JSON writer.\n"); goto end; } /* Configure JSON writer */ JSON_Writer_SetOutputEncoding(context.writer, JSON_UTF8); JSON_Writer_SetOutputHandler(context.writer, &RtlJSONOutputHandler); JSON_Writer_SetUserData(context.writer, &context); /* Write output file */ JSON_Writer_WriteStartObject(context.writer); JSON_Writer_WriteNewLine(context.writer); /* > Version entry */ JSON_Writer_WriteSpace(context.writer, 2); JSON_Writer_WriteString(context.writer, "version", strlen("version"), JSON_UTF8); JSON_Writer_WriteColon(context.writer); JSON_Writer_WriteSpace(context.writer, 1); JSON_Writer_WriteString(context.writer, "1.0", strlen("1.0"), JSON_UTF8); JSON_Writer_WriteComma(context.writer); JSON_Writer_WriteNewLine(context.writer); /* > Runtime entry */ value_string[0] = '\0'; n = snprintf(value_string, sizeof(value_string), LOG_FILE_RUNTIME_FORMAT_STR, runtime_log->runtime.hours, runtime_log->runtime.minutes, runtime_log->runtime.seconds); if ((n < 0) || (n >= 64)) n = 0; /* Silence GCC warnings... */ JSON_Writer_WriteSpace(context.writer, 2); JSON_Writer_WriteString(context.writer, "runtime", strlen("runtime"), JSON_UTF8); JSON_Writer_WriteColon(context.writer); JSON_Writer_WriteSpace(context.writer, 1); JSON_Writer_WriteString(context.writer, value_string, strlen(value_string), JSON_UTF8); JSON_Writer_WriteComma(context.writer); JSON_Writer_WriteNewLine(context.writer); /* > Last played entry */ value_string[0] = '\0'; n = snprintf(value_string, sizeof(value_string), LOG_FILE_LAST_PLAYED_FORMAT_STR, runtime_log->last_played.year, runtime_log->last_played.month, runtime_log->last_played.day, runtime_log->last_played.hour, runtime_log->last_played.minute, runtime_log->last_played.second); if ((n < 0) || (n >= 64)) n = 0; /* Silence GCC warnings... */ JSON_Writer_WriteSpace(context.writer, 2); JSON_Writer_WriteString(context.writer, "last_played", strlen("last_played"), JSON_UTF8); JSON_Writer_WriteColon(context.writer); JSON_Writer_WriteSpace(context.writer, 1); JSON_Writer_WriteString(context.writer, value_string, strlen(value_string), JSON_UTF8); JSON_Writer_WriteNewLine(context.writer); /* > Finalise */ JSON_Writer_WriteEndObject(context.writer); JSON_Writer_WriteNewLine(context.writer); /* Free JSON writer */ JSON_Writer_Free(context.writer); end: /* Close log file */ filestream_close(file); }
int find_first_data_track(const char *cue_path, int32_t *offset, char *track_path, size_t max_len) { int rv, m, s, f; char tmp_token[MAX_TOKEN_LEN]; char cue_dir[PATH_MAX_LENGTH]; RFILE *fd; strlcpy(cue_dir, cue_path, sizeof(cue_dir)); path_basedir(cue_dir); fd = filestream_open(cue_path, RFILE_MODE_READ, -1); if (!fd) { RARCH_LOG("Could not open CUE file '%s': %s\n", cue_path, strerror(errno)); return -errno; } RARCH_LOG("Parsing CUE file '%s'...\n", cue_path); while (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0) { if (string_is_equal(tmp_token, "FILE")) { get_token(fd, tmp_token, MAX_TOKEN_LEN); fill_pathname_join(track_path, cue_dir, tmp_token, max_len); } else if (string_is_equal_noncase(tmp_token, "TRACK")) { get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, MAX_TOKEN_LEN); if (string_is_equal_noncase(tmp_token, "AUDIO")) continue; find_token(fd, "INDEX"); get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, MAX_TOKEN_LEN); if (sscanf(tmp_token, "%02d:%02d:%02d", &m, &s, &f) < 3) { RARCH_LOG("Error parsing time stamp '%s'\n", tmp_token); return -errno; } *offset = ((m * 60) * (s * 75) * f) * 25; RARCH_LOG("Found 1st data track on file '%s+%d'\n", track_path, *offset); rv = 0; goto clean; } } rv = -EINVAL; clean: filestream_close(fd); return rv; }
int rfclose(RFILE* stream) { return filestream_close(stream); }
RFILE *filestream_open(const char *path, unsigned mode, ssize_t len) { int flags = 0; int mode_int = 0; #if defined(HAVE_BUFFERED_IO) const char *mode_str = NULL; #endif RFILE *stream = (RFILE*)calloc(1, sizeof(*stream)); if (!stream) return NULL; (void)mode_int; (void)flags; stream->hints = mode; #ifdef HAVE_MMAP if (stream->hints & RFILE_HINT_MMAP && (stream->hints & 0xff) == RFILE_MODE_READ) stream->hints |= RFILE_HINT_UNBUFFERED; else #endif stream->hints &= ~RFILE_HINT_MMAP; switch (mode & 0xff) { case RFILE_MODE_READ_TEXT: #if defined(PSP) mode_int = 0666; flags = PSP_O_RDONLY; #else #if defined(HAVE_BUFFERED_IO) if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) mode_str = MODE_STR_READ; #endif /* No "else" here */ flags = O_RDONLY; #endif break; case RFILE_MODE_READ: #if defined(PSP) mode_int = 0666; flags = PSP_O_RDONLY; #else #if defined(HAVE_BUFFERED_IO) if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) mode_str = MODE_STR_READ_UNBUF; #endif /* No "else" here */ flags = O_RDONLY; #endif break; case RFILE_MODE_WRITE: #if defined(PSP) mode_int = 0666; flags = PSP_O_CREAT | PSP_O_WRONLY | PSP_O_TRUNC; #else #if defined(HAVE_BUFFERED_IO) if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) mode_str = MODE_STR_WRITE_UNBUF; #endif else { flags = O_WRONLY | O_CREAT | O_TRUNC; #ifndef _WIN32 flags |= S_IRUSR | S_IWUSR; #endif } #endif break; case RFILE_MODE_READ_WRITE: #if defined(PSP) mode_int = 0666; flags = PSP_O_RDWR; #else #if defined(HAVE_BUFFERED_IO) if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) mode_str = MODE_STR_WRITE_PLUS; #endif else { flags = O_RDWR; #ifdef _WIN32 flags |= O_BINARY; #endif } #endif break; } #if defined(PSP) stream->fd = sceIoOpen(path, flags, mode_int); #else #if defined(HAVE_BUFFERED_IO) if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0) { stream->fp = fopen(path, mode_str); if (!stream->fp) goto error; } else #endif { /* FIXME: HAVE_BUFFERED_IO is always 1, but if it is ever changed, open() needs to be changed to _wopen() for WIndows. */ stream->fd = open(path, flags); if (stream->fd == -1) goto error; #ifdef HAVE_MMAP if (stream->hints & RFILE_HINT_MMAP) { stream->mappos = 0; stream->mapped = NULL; stream->mapsize = filestream_seek(stream, 0, SEEK_END); if (stream->mapsize == (uint64_t)-1) goto error; filestream_rewind(stream); stream->mapped = (uint8_t*)mmap((void*)0, stream->mapsize, PROT_READ, MAP_SHARED, stream->fd, 0); if (stream->mapped == MAP_FAILED) stream->hints &= ~RFILE_HINT_MMAP; } #endif } #endif #if defined(PSP) if (stream->fd == -1) goto error; #endif return stream; error: filestream_close(stream); return NULL; }