/** opens a file. @param fd file descriptor. @param mode mode. @return true on success. */ bool open(int fd, const char *mode) { reset(new AllegroFile(al_fopen_fd(fd, mode))); return (bool)(*this); }
/* get_xdg_path - locate an XDG user dir */ static ALLEGRO_PATH *_get_xdg_path(const char *location) { ALLEGRO_PATH *location_path = NULL; ALLEGRO_PATH *xdg_config_path = NULL; ALLEGRO_FILE *xdg_config_file = NULL; const char *xdg_config_home = getenv("XDG_CONFIG_HOME"); int fd; if (xdg_config_home) { /* use $XDG_CONFIG_HOME since it exists */ xdg_config_path = al_create_path_for_directory(xdg_config_home); } else { /* the default XDG location is ~/.config */ xdg_config_path = al_get_standard_path(ALLEGRO_USER_HOME_PATH); if (!xdg_config_path) return NULL; al_append_path_component(xdg_config_path, ".config"); } al_set_path_filename(xdg_config_path, "user-dirs.dirs"); fd = open(al_path_cstr(xdg_config_path, '/'), O_RDONLY); if (fd != -1) { xdg_config_file = al_fopen_fd(fd, "r"); } al_destroy_path(xdg_config_path); if (!xdg_config_file) return NULL; while (!al_feof(xdg_config_file)) { char line[XDG_MAX_PATH_LEN]; /* one line of the config file */ const char *p = line; /* where we're at in the line */ char component[XDG_MAX_PATH_LEN]; /* the path component being parsed */ int i = 0; /* how long the current component is */ al_fgets(xdg_config_file, line, XDG_MAX_PATH_LEN); /* skip leading white space */ while (*p == ' ' || *p == '\t') p++; /* skip the line if it does not begin with XDG_location_DIR */ if (strncmp(p, "XDG_", 4)) continue; p += 4; if (strncmp(p, location, strlen(location))) continue; p += strlen(location); if (strncmp(p, "_DIR", 4)) continue; p += 4; /* skip past the =", allowing for white space */ while (*p == ' ' || *p == '\t') p++; if (*p++ != '=') continue; while (*p == ' ' || *p == '\t') p++; if (*p++ != '"') continue; /* We've found the right line. Now parse it, basically assuming that it is in a sane format. */ if (!strncmp(p, "$HOME", 5)) { /* $HOME is the only environment variable that the path is allowed to use, and it must be first, by specification. */ location_path = al_get_standard_path(ALLEGRO_USER_HOME_PATH); p += 5; } else { location_path = al_create_path("/"); } while (*p) { if (*p == '"' || *p == '/') { /* add the component (if non-empty) to the path */ if (i > 0) { component[i] = 0; al_append_path_component(location_path, component); i = 0; } if (*p == '"') break; } else { if (*p == '\\') { /* treat any escaped character as a literal */ p++; if (!*p) break; } component[i++] = *p; } p++; } /* Finished parsing the path. */ break; } al_fclose(xdg_config_file); return location_path; }