[AD] resource path functions [was: [patch] new configuration variable: resource_path]

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


Daniel Schlyder:
> Grzegorz Adam Hankiewicz:
>> It would be better if you added something like
>> set_allegro_resource_path(int num, const char *path) to Allegro. This
>> function would receive the path to look files for and the priority
>> as a number from 0 to 7 inclusive.  Setting the same number to NULL
>> would remove the path.

>> Then find_allegro_resource could loop internally over the non null
>> entries of that array somewhere before giving up in addition to
>> the ones it already does. Instead of using a static char array,
>> use in your implementation a static global 8 position array of
>> pointers to strings.  set_allegro_resource_path would then return
>> 0 if the internal ustrdup call to store the path didn't have enough
>> memory. Return 1 if the string duplication is successful.

>> Ok. Maybe 8 custom paths aren't enough. Make that 16.

> I'll try to implement it and submit a new patch.

Done.

The attached patch adds three new API functions:

    int add_allegro_resource_path(AL_CONST char *path, int priority);
    void remove_allegro_resource_path(AL_CONST char *path);
    void remove_all_allegro_resource_paths();

The paths are stored in a linked list, so there's no arbitrary limit on how
many paths can be added. Paths are sorted from highest to lowest priority on
insertion so the list can simply be iterated through in
find_allegro_resource().

I thought about having just one function with NULL having special meaning,
but I think the interface is cleaner by having separate functions for each
task. remove_all_allegro_resource_paths() was originally an implementation
detail, but I exposed it for completeness sake; not sure how useful it is.


Usage:

// returns 'TRUE' on success; 'FALSE' on failure (ie. if malloc() fails to
// allocate new list node)
add_allegro_resource_path("foo", 100);

// 'bar/baz' will be tried before 'foo' in find_allegro_resource()
add_allegro_resource_path("bar/baz", 200);

// Add an already added path to change its priority.
add_allegro_resource_path("foo", 10000);

remove_allegro_resource_path("bar/baz");
remove_all_allegro_resource_paths();

// The functions can be safely called before initialising Allegro.
allegro_init();

// remove_all_allegro_resource_paths() is in exit_func_list when
// resource_path_list != NULL.
allegro_exit();


The second attachment is a program I used to test the changes.


Comments?

If the patch is accepted, I'll submit a new version that also updates the
docs.

--
Daniel Schlyder
http://bitblaze.com/

Attachment: resource_path_functions.diff
Description: Binary data

Attachment: rc-paths.c
Description: Binary data



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