Re: [AD] standard path updates

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Wed, Jan 5, 2011 at 4:18 AM, Sebastian Steinhauer
<s.steinhauer@xxxxxxxxxx> wrote:
> Ahhh okay, I just found out, that OpenSuse (and probably many other distros)
> use the XDG Base Directory Specification.
>
Can you try the attached Allegro 5 program? Just compile it and run it like:

./foo DOCUMENTS

It should output the correct format. If it works, I'll add it to the
Allegro source.

--
Matthew Leverton
#include <allegro5/allegro.h>
#include <stdlib.h>
#include <stdio.h>

#define XDG_MAX_PATH_LEN 1000

/* _al_get_xdg_path - locate an XDG user dir
 */
static ALLEGRO_PATH *_al_get_xdg_path(const char *location)
{
	ALLEGRO_PATH *location_path = NULL;
	ALLEGRO_PATH *xdg_config_path;
	ALLEGRO_FILE *xdg_config_file;	
	const char *xdg_config_home = getenv("XDG_CONFIG_HOME");

	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");	
	xdg_config_file = al_fopen(al_path_cstr(xdg_config_path, '/'), "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 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=\"", 6)) continue;
		p += 6;
		
		/* 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;
}

int main(int argc, const char **argv)
{
	ALLEGRO_PATH *p;
	
	al_init();
	
	p = _al_get_xdg_path(argc > 1 ? argv[1] : "DOCUMENTS");
	if (p) printf("%s\n", al_path_cstr(p, '/'));

	return 0;
}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/