const char* getResource(const char* fmt, ...) { va_list ap; static char res[512]; static ALLEGRO_PATH *dir; static ALLEGRO_PATH *path; va_start(ap, fmt); memset(res, 0, 512); snprintf(res, 511, fmt, ap); if (!dir) { dir = al_get_standard_path(ALLEGRO_RESOURCES_PATH); #ifdef ALLEGRO_MSVC { /* Hack to cope automatically with MSVC workspaces. */ const char *last = al_get_path_component(dir, -1); if (0 == strcmp(last, "Debug") || 0 == strcmp(last, "RelWithDebInfo") || 0 == strcmp(last, "Release") || 0 == strcmp(last, "Profile")) { al_remove_path_component(dir, -1); } } #endif al_append_path_component(dir, "data"); } if (path) al_destroy_path(path); path = al_create_path(res); al_rebase_path(dir, path); return al_path_cstr(path, '/'); }
static ALLEGRO_PATH *follow_symlinks(ALLEGRO_PATH *path) { for (;;) { const char *path_str = al_path_cstr(path, '/'); char buf[PATH_MAX]; int len; len = readlink(path_str, buf, sizeof(buf) - 1); if (len <= 0) break; buf[len] = '\0'; al_destroy_path(path); path = al_create_path(buf); } /* Make absolute path. */ { const char *cwd = al_get_current_directory(); ALLEGRO_PATH *cwd_path = al_create_path_for_directory(cwd); if (al_rebase_path(cwd_path, path)) al_make_path_canonical(path); al_destroy_path(cwd_path); al_free((void *) cwd); } return path; }
s_sprite c_tile::get_from_ini(ALLEGRO_CONFIG *config, const char * section, ALLEGRO_PATH * base_path) { s_sprite temp; const char * buffer_file = al_get_config_value(config, section, "image_file"); if(!buffer_file) { temp.index = -1; return temp; } ALLEGRO_PATH * imagepath = al_create_path(buffer_file); al_rebase_path(base_path, imagepath); temp.index = imagelist.load_image(al_path_cstr(imagepath, ALLEGRO_NATIVE_PATH_SEP)); temp.x = get_config_int(config, section, "x"); temp.y = get_config_int(config, section, "y"); temp.width = get_config_int(config, section, "width"); temp.height = get_config_int(config, section, "height"); temp.origin_x = get_config_int(config, section, "origin_x"); temp.origin_y = get_config_int(config, section, "origin_y"); temp.origin_x = 0 - temp.origin_x; temp.origin_y = 0 - temp.origin_y; temp.palette_number = get_config_int(config, section, "palette_index"); temp.column_height = get_config_int(config, section, "column_height"); const char * color_selection = al_get_config_value(config, section, "color_source"); if(color_selection) temp.color_by = get_color_selector(color_selection); const char * pal_source = al_get_config_value(config, section, "palette_source"); if(pal_source) temp.color_source = get_color_source(pal_source); const char * color = al_get_config_value(config, section, "color_html"); if(color) temp.color = color_html(color); const char * off = al_get_config_value(config, section, "offset_type"); if(off) temp.offset_type = get_offset_type(off); const char * neigh = al_get_config_value(config, section, "border_terrain"); if(neigh) temp.border_terrain = get_terrain_type(neigh); const char * neighbobj = al_get_config_value(config, section, "border_structure"); if(neighbobj) temp.border_structure = get_structure_type(neighbobj); temp.offset_amount = get_config_int(config, section, "offset_amount"); al_destroy_path(imagepath); return temp; }
ALLEGRO_PATH *_al_unix_get_path(int id) { switch (id) { case ALLEGRO_TEMP_PATH: { /* Check: TMP, TMPDIR, TEMP or TEMPDIR */ char *envs[] = { "TMP", "TMPDIR", "TEMP", "TEMPDIR", NULL}; uint32_t i = 0; for (; envs[i] != NULL; ++i) { char *tmp = getenv(envs[i]); if (tmp) { return al_create_path_for_directory(tmp); } } /* next try: /tmp /var/tmp /usr/tmp */ char *paths[] = { "/tmp/", "/var/tmp/", "/usr/tmp/", NULL }; for (i=0; paths[i] != NULL; ++i) { ALLEGRO_FS_ENTRY *fse = al_create_fs_entry(paths[i]); bool found = (al_get_fs_entry_mode(fse) & ALLEGRO_FILEMODE_ISDIR) != 0; al_destroy_fs_entry(fse); if (found) { return al_create_path_for_directory(paths[i]); } } /* Give up? */ return NULL; } break; case ALLEGRO_RESOURCES_PATH: { ALLEGRO_PATH *exe = get_executable_name(); exe = follow_symlinks(exe); al_set_path_filename(exe, NULL); return exe; } break; case ALLEGRO_USER_DATA_PATH: case ALLEGRO_USER_SETTINGS_PATH: { ALLEGRO_PATH *local_path = NULL; const char *org_name = al_get_org_name(); const char *app_name = al_get_app_name(); /* to avoid writing directly into the user's directory, require at least an app name */ if (!app_name) return NULL; /* find the appropriate path from the xdg environment variables, if possible */ if (id == ALLEGRO_USER_DATA_PATH) { const char *xdg_data_home = getenv("XDG_DATA_HOME"); local_path = al_create_path_for_directory(xdg_data_home ? xdg_data_home : ".local/share"); } else { const char *xdg_config_home = getenv("XDG_CONFIG_HOME"); local_path = al_create_path_for_directory(xdg_config_home ? xdg_config_home : ".config"); } if (!local_path) return NULL; /* if the path is relative, prepend the user's home directory */ if (al_path_cstr(local_path, '/')[0] != '/') { ALLEGRO_PATH *home_path = _unix_find_home(); if (!home_path) return NULL; al_rebase_path(home_path, local_path); al_destroy_path(home_path); } /* only add org name if not blank */ if (org_name && org_name[0]) { al_append_path_component(local_path, al_get_org_name()); } al_append_path_component(local_path, al_get_app_name()); return local_path; } break; case ALLEGRO_USER_HOME_PATH: return _unix_find_home(); case ALLEGRO_USER_DOCUMENTS_PATH: { ALLEGRO_PATH *local_path = _get_xdg_path("DOCUMENTS"); return local_path ? local_path : _unix_find_home(); } break; case ALLEGRO_EXENAME_PATH: return get_executable_name(); break; default: return NULL; } return NULL; }