Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to get all files in a directory with A5

Credits go to Matthew Leverton and SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
How to get all files in a directory with A5
Don Freeman
Member #5,110
October 2004
avatar

I know the older version of Allegro had the for_each_file thing, what should I use with A5 to get all the files in a directory? ???

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Matthew Leverton
Supreme Loser
January 1999
avatar

Don Freeman
Member #5,110
October 2004
avatar

I figured as much, but what kind of iteration device do I use?! I see nothing in the manual about such devices.

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

SiegeLord
Member #7,827
October 2006
avatar

This will print the name of everything in the passed directory.

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3 4int main(int argc, char *argv[]) 5{ 6 if(argc > 1) 7 { 8 al_init(); 9 10 ALLEGRO_FS_ENTRY* dir = al_create_fs_entry(argv[1]); 11 if(al_open_directory(dir)) 12 { 13 ALLEGRO_FS_ENTRY* file; 14 while(file = al_read_directory(dir)) 15 { 16 printf("%s\n", al_get_fs_entry_name(file)); 17 al_destroy_fs_entry(file); 18 } 19 } 20 al_destroy_fs_entry(dir); 21 return 0; 22 } 23}

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Don Freeman
Member #5,110
October 2004
avatar

Thanks guys....just got it right before you posted. :-/ I think the function names are a little confusing in this regard... Maybe should post an example of this in the wiki. I start feeling a little better and get some free time, I might do it. Thanks again. :-*

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

SiegeLord
Member #7,827
October 2006
avatar

Hmm. I wonder how you tell if an FS_ENTRY is a file or a directory. Also, it'd be nice to be able to get the ALLEGRO_PATH from an FS_ENTRY without using the potentially unsafe al_get_fs_entry_name.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Elias
Member #358
May 2000

SiegeLord said:

Hmm. I wonder how you tell if an FS_ENTRY is a file or a directory.

al_get_fs_entry_mode(entry) & ALLEGRO_FILEMODE_ISDIR

Does nobody ever look at the examples? There's an ex_dir which re-imlpements the ls command with the A5 API...

Quote:

Also, it'd be nice to be able to get the ALLEGRO_PATH from an FS_ENTRY without using the potentially unsafe al_get_fs_entry_name.

Even better would be if you could get an ALLEGRO_FILE since if filenames are not encoded as utf8 both a string as well as ALLEGRO_PATH would break it.

--
"Either help out or stop whining" - Evert

SiegeLord
Member #7,827
October 2006
avatar

Elias said:

Does nobody ever look at the examples?

I blame the binary distribution 8-).

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Thomas Fjellstrom
Member #476
June 2000
avatar

Elias said:

Even better would be if you could get an ALLEGRO_FILE since if filenames are not encoded as utf8 both a string as well as ALLEGRO_PATH would break it.

That all used to just work with the original API ;D I think the old way was thought to be too confusing. Ah well.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Elias
Member #358
May 2000

SiegeLord said:

I blame the binary distribution 8-).

Maybe we should package binaries of the examples as well? It would increase the size quite a bit though (especially with static linking).

But it would also make it easier to test out A5, just downloading the library with nothing to run is a bit boring :P

--
"Either help out or stop whining" - Evert

Thomas Fjellstrom
Member #476
June 2000
avatar

Maybe provide a separate "example/demo" binary that contains the example sources and binaries.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Arthur Kalliokoski
Second in Command
February 2005
avatar

They'd need the example source files to learn from, and if their binary distribution is OK, a simple makefile with compiler options should suffice.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

AMCerasoli
Member #11,955
May 2010
avatar

that would be great, I always thought why there weren't examples at the binaries if it's what almost everybody uses (at least in windows)...

Evert
Member #794
November 2000
avatar

Elias said:

Maybe we should package binaries of the examples as well? It would increase the size quite a bit though (especially with static linking).

I don't think there's much point in a binary distribution of the examples, to be honest. Now, a test program I could see the use of (I know most of the examples are really test programs in disguise).

However, it would make sense (to me) to include the example sources in the binary package (for Windows anyway, for Linux it should probably be a separate package).

Elias
Member #358
May 2000

Don't know, when I tried for example Ogre3D I really liked how I could try their examples before I had to commit and actually install and compile stuff.

--
"Either help out or stop whining" - Evert

LennyLen
Member #5,313
December 2004
avatar

The examples are provided in binary form here on A.cc.

Matthew Leverton
Supreme Loser
January 1999
avatar

There's also usually working examples linked to each online manual page via forum discussions. Sitting across the Internet, you cannot force someone to click and read.

Go to: