char* nk_common_file_load(const char* path, size_t* size) { void *buf; ssize_t *length = (ssize_t*)size; filestream_read_file(path, &buf, length); return (char*)buf; }
/** * content_file_read: * @path : path to file. * @buf : buffer to allocate and read the contents of the * file into. Needs to be freed manually. * @length : Number of items read, -1 on error. * * Read the contents of a file into @buf. Will call file_archive_compressed_read * if path contains a compressed file, otherwise will call filestream_read_file(). * * Returns: 1 if file read, 0 on error. */ static int content_file_read(const char *path, void **buf, ssize_t *length) { #ifdef HAVE_COMPRESSION if (path_contains_compressed_file(path)) { if (file_archive_compressed_read(path, buf, NULL, length)) return 1; } #endif return filestream_read_file(path, buf, length); }
static bool gl_glsl_load_source_path(struct video_shader_pass *pass, const char *path) { ssize_t len; bool ret = filestream_read_file(path, (void**)&pass->source.string.vertex, &len); if (!ret || len <= 0) return false; gl_glsl_strip_parameter_pragmas(pass->source.string.vertex); pass->source.string.fragment = strdup(pass->source.string.vertex); return pass->source.string.fragment && pass->source.string.vertex; }
static bool file_get_crc(database_state_handle_t *db_state, const char *name, uint32_t *crc) { ssize_t ret; int read_from = filestream_read_file( name, (void**)&db_state->buf, &ret); if (read_from != 1 || ret <= 0) return 0; *crc = encoding_crc32(0, db_state->buf, ret); return 1; }
static void *font_renderer_stb_init(const char *font_path, float font_size) { int ascent, descent, line_gap; stbtt_fontinfo info; uint8_t *font_data = NULL; stb_font_renderer_t *self = (stb_font_renderer_t*) calloc(1, sizeof(*self)); /* See https://github.com/nothings/stb/blob/master/stb_truetype.h#L539 */ font_size = STBTT_POINT_SIZE(font_size); if (!self) goto error; if (!filestream_read_file(font_path, (void**)&font_data, NULL)) goto error; if (!font_renderer_stb_create_atlas(self, font_data, font_size, 512, 512)) goto error; if (!stbtt_InitFont(&info, font_data, stbtt_GetFontOffsetForIndex(font_data, 0))) goto error; stbtt_GetFontVMetrics(&info, &ascent, &descent, &line_gap); self->line_height = ascent - descent; if (font_size < 0) self->line_height *= stbtt_ScaleForMappingEmToPixels(&info, -font_size); else self->line_height *= stbtt_ScaleForPixelHeight(&info, font_size); free(font_data); return self; error: if (font_data) free(font_data); if (self) font_renderer_stb_free(self); return NULL; }
/** * load_ram_file: * @path : path of RAM state that will be loaded from. * @type : type of memory * * Load a RAM state from disk to memory. */ static bool load_ram_file(void *data) { ssize_t rc; retro_ctx_memory_info_t mem_info; void *buf = NULL; ram_type_t *ram = (ram_type_t*)data; if (!ram) return false; mem_info.id = ram->type; core_ctl(CORE_CTL_RETRO_GET_MEMORY, &mem_info); if (mem_info.size == 0 || !mem_info.data) return false; if (!filestream_read_file(ram->path, &buf, &rc)) return false; if (rc > 0) { if (rc > (ssize_t)mem_info.size) { RARCH_WARN("SRAM is larger than implementation expects, " "doing partial load (truncating %u %s %s %u).\n", (unsigned)rc, msg_hash_to_str(MSG_BYTES), msg_hash_to_str(MSG_TO), (unsigned)mem_info.size); rc = mem_info.size; } memcpy(mem_info.data, buf, rc); } if (buf) free(buf); return true; }
static bool file_get_crc(database_state_handle_t *db_state, const char *name, uint32_t *crc) { ssize_t ret; int read_from = filestream_read_file( name, (void**)&db_state->buf, &ret); #ifdef HAVE_ZLIB const struct file_archive_file_backend *stream_backend = file_archive_get_default_file_backend(); #endif if (read_from != 1 || ret <= 0) return 0; #ifdef HAVE_ZLIB *crc = stream_backend->stream_crc_calculate( 0, db_state->buf, ret); #endif return 1; }
static void *file_archive_open(const char *path) { ssize_t ret = -1; bool read_from_file = false; file_archive_file_data_t *data = (file_archive_file_data_t*) calloc(1, sizeof(*data)); if (!data) return NULL; read_from_file = filestream_read_file(path, &data->data, &ret); /* Failed to open archive? */ if (!read_from_file || ret < 0) goto error; data->size = ret; return data; error: file_archive_free(data); return NULL; }
/** * content_load_state: * @path : path that state will be loaded from. * * Load a state from disk to memory. * * Returns: true if successful, false otherwise. **/ static bool content_load_state(const char *path) { unsigned i; ssize_t size; retro_ctx_serialize_info_t serial_info; unsigned num_blocks = 0; void *buf = NULL; struct sram_block *blocks = NULL; settings_t *settings = config_get_ptr(); global_t *global = global_get_ptr(); bool ret = filestream_read_file(path, &buf, &size); RARCH_LOG("%s: \"%s\".\n", msg_hash_to_str(MSG_LOADING_STATE), path); if (!ret || size < 0) goto error; RARCH_LOG("%s: %u %s.\n", msg_hash_to_str(MSG_STATE_SIZE), (unsigned)size, msg_hash_to_str(MSG_BYTES)); if (settings->block_sram_overwrite && global->savefiles && global->savefiles->size) { RARCH_LOG("%s.\n", msg_hash_to_str(MSG_BLOCKING_SRAM_OVERWRITE)); blocks = (struct sram_block*) calloc(global->savefiles->size, sizeof(*blocks)); if (blocks) { num_blocks = global->savefiles->size; for (i = 0; i < num_blocks; i++) blocks[i].type = global->savefiles->elems[i].attr.i; } } for (i = 0; i < num_blocks; i++) { retro_ctx_memory_info_t mem_info; mem_info.id = blocks[i].type; core_ctl(CORE_CTL_RETRO_GET_MEMORY, &mem_info); blocks[i].size = mem_info.size; } for (i = 0; i < num_blocks; i++) if (blocks[i].size) blocks[i].data = malloc(blocks[i].size); /* Backup current SRAM which is overwritten by unserialize. */ for (i = 0; i < num_blocks; i++) { if (blocks[i].data) { retro_ctx_memory_info_t mem_info; const void *ptr = NULL; mem_info.id = blocks[i].type; core_ctl(CORE_CTL_RETRO_GET_MEMORY, &mem_info); ptr = mem_info.data; if (ptr) memcpy(blocks[i].data, ptr, blocks[i].size); } } serial_info.data_const = buf; serial_info.size = size; ret = core_ctl(CORE_CTL_RETRO_UNSERIALIZE, &serial_info); /* Flush back. */ for (i = 0; i < num_blocks; i++) { if (blocks[i].data) { retro_ctx_memory_info_t mem_info; void *ptr = NULL; mem_info.id = blocks[i].type; core_ctl(CORE_CTL_RETRO_GET_MEMORY, &mem_info); ptr = mem_info.data; if (ptr) memcpy(ptr, blocks[i].data, blocks[i].size); } } for (i = 0; i < num_blocks; i++) free(blocks[i].data); free(blocks); free(buf); if (!ret) goto error; return true; error: RARCH_ERR("%s \"%s\".\n", msg_hash_to_str(MSG_FAILED_TO_LOAD_STATE), path); return false; }
py_state_t *py_state_new(const char *script, unsigned is_file, const char *pyclass) { py_state_t *handle; PyObject *hook; RARCH_LOG("Initializing Python runtime ...\n"); PyImport_AppendInittab("rarch", &PyInit_Retro); Py_Initialize(); RARCH_LOG("Initialized Python runtime.\n"); handle = (py_state_t*)calloc(1, sizeof(*handle)); hook = NULL; handle->main = PyImport_AddModule("__main__"); if (!handle->main) goto error; Py_INCREF(handle->main); if (is_file) { /* Have to hack around the fact that the FILE struct * isn't standardized across environments. * PyRun_SimpleFile() breaks on Windows because it's * compiled with MSVC. */ ssize_t len; char *script_ = NULL; bool ret = filestream_read_file (script, (void**)&script_, &len); if (!ret || len < 0) { RARCH_ERR("Python: Failed to read script\n"); free(script_); goto error; } PyRun_SimpleString(script_); free(script_); } else { char *script_ = align_program(script); if (script_) { PyRun_SimpleString(script_); free(script_); } } RARCH_LOG("Python: Script loaded.\n"); handle->dict = PyModule_GetDict(handle->main); if (!handle->dict) { RARCH_ERR("Python: PyModule_GetDict() failed.\n"); goto error; } Py_INCREF(handle->dict); hook = PyDict_GetItemString(handle->dict, pyclass); if (!hook) { RARCH_ERR("Python: PyDict_GetItemString() failed.\n"); goto error; } handle->inst = PyObject_CallFunction(hook, NULL); if (!handle->inst) { RARCH_ERR("Python: PyObject_CallFunction() failed.\n"); goto error; } Py_INCREF(handle->inst); return handle; error: PyErr_Print(); PyErr_Clear(); py_state_free(handle); return NULL; }