SA-MP Forums Archive
[Plugin] dirReader ─ Easiest and fastest way to read a directory! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Plugin Development (https://sampforum.blast.hk/forumdisplay.php?fid=18)
+--- Thread: [Plugin] dirReader ─ Easiest and fastest way to read a directory! (/showthread.php?tid=448075)

Pages: 1 2


dirReader ─ Easiest and fastest way to read a directory! - RyDeR` - 02.07.2013

dirReader Plugin

I used to store names of files in a .txt file to know what was there, but it started to annoy me, so I thought, why not make a plugin? dirReader is a basic, yet quite efficient and useful plugin that has the ability to read files in a directory using a pattern (using the fnmatch library) with a couple of useful flags. You can find a detailed explanation here.

Changelog Flags
pawn Код:
#define FNM_PATHNAME    (1 << 0)    // No wildcard can ever match `/'
#define FNM_NOESCAPE    (1 << 1)    // Backslashes don't quote special chars
#define FNM_PERIOD      (1 << 2)    // Leading `.' is matched only explicitly
#define FNM_LEADING_DIR (1 << 3)    // Ignore `/...' after a match
#define FNM_CASEFOLD    (1 << 4)    // Compare without regard to case
#define FNM_EXTMATCH    (1 << 5)    // Use ksh-like extended matching
#define DR_NO_DIR       (1 << 6)    // Skips directories
Natives
pawn Код:
native DR: dirReader_open(szName[], szPattern[], const iFlags);
native dirReader_close(DR: drHandle);
native dirReader_getCount(DR: drHandle);
native dirReader_getFileAtIndex(DR: drHandle, const iIdx, szBuf[], const iSize = sizeof(szBuf));
Example(s)

If I run the following code:
pawn Код:
new
    DR: drHandle,
    szBuf[32],
    iCount
;
drHandle = dirReader_open("scriptfiles/", "*", 0); // Notice no flag
iCount = dirReader_getCount(drHandle); // Efficiently gets total amount of files with matched pattern

for(new i = 0; i < iCount; ++i) {
    dirReader_getFileAtIndex(drHandle, i, szBuf); // Gets file at index
    printf("%s", szBuf); // Prints file at index
}
dirReader_close(drHandle);
This will print all files (including directories) in scriptfiles. Now let's change the pattern:
pawn Код:
drHandle = dirReader_open("scriptfiles/", "*.ZX", DR_NO_DIR); // Directories will be ignored
This will print all files (except directories) ending on .ZX, not .zx. Because it doesn't ignore the letter case. We have to use a flag to make it ignore:
pawn Код:
drHandle = dirReader_open("scriptfiles/", "*.ZX", DR_NO_DIR | FNM_CASEFOLD); // Directories and the letter case will be ignored
This will print all files ending with both .zx and .ZX. You can toggle more flags using the bitwise OR ('|') operator. Consider the next example:
pawn Код:
drHandle = dirReader_open("scriptfiles/", "R*", DR_NO_DIR | FNM_CASEFOLD);
This will print all files (except directories) starting with an "R" or "r". Anyway you get the main point.

A few notes here: Download

Windows and Linux (CentOS) (source + plugin + include)


Re: dirReader ─ Easiest and fastest way to read a directory! - omarnor89 - 03.07.2013

I didnt understand anything for sure


Re: dirReader ─ Easiest and fastest way to read a directory! - CaHbKo - 03.07.2013

This is very cool, thanks! Item descriptions are stored in separate files and there's an index file like the one you described so I'm switching right away


Re: dirReader ─ Easiest and fastest way to read a directory! - TheArcher - 03.07.2013

Can it read files from the directory? :O And does it support any text file extension?


Re: dirReader ─ Easiest and fastest way to read a directory! - Kirollos - 03.07.2013

great i like it


Re: dirReader ─ Easiest and fastest way to read a directory! - Red_Dragon. - 03.07.2013

Great idea. Switching to it right now. Does it support any text file extension (as TheArcher said) ?


Re: dirReader ─ Easiest and fastest way to read a directory! - Pottus - 03.07.2013

Questions:

1.) Can you use wildcards to search for specific strings? Ex. *r*
2.) Does it not list directories as well?


Re: dirReader ─ Easiest and fastest way to read a directory! - Kar - 03.07.2013

Quote:
Originally Posted by Red_Dragon.
Посмотреть сообщение
Great idea. Switching to it right now. Does it support any text file extension (as TheArcher said) ?
I think he means extensions when he says "This will print all files ending on .ZX" or maybe not

Great job though.


Re: dirReader ─ Easiest and fastest way to read a directory! - BJIADOKC - 03.07.2013

Compiled on Debian 32 bit
http://dl.bjiadokc.ru/dirreader/dirReader.so
Makefile:
http://dl.bjiadokc.ru/dirreader/makefile


Re: dirReader ─ Easiest and fastest way to read a directory! - Niko_boy - 03.07.2013

seem nice!
Quote:
Originally Posted by Kar
Посмотреть сообщение
I think he means extensions when he says "This will print all files ending on .ZX" or maybe not

Great job though.
wondering same


Re: dirReader ─ Easiest and fastest way to read a directory! - RyDeR` - 03.07.2013

Quote:
Originally Posted by TheArcher
Посмотреть сообщение
Can it read files from the directory? :O And does it support any text file extension?
Yes!

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
Questions:

1.) Can you use wildcards to search for specific strings? Ex. *r*
2.) Does it not list directories as well?
1) Definitely, using "*r*" would print all files containing a letter "r". Using "*r*.txt" would print all files with an "r" inside and with the .txt extension.
2) Yes, I should probably add a flag for that too, because the main point is to read files, not directories.

Quote:
Originally Posted by Kar
Посмотреть сообщение
I think he means extensions when he says "This will print all files ending on .ZX" or maybe not

Great job though.
Yes, that's what I meant. I should probably add a few more examples.

Quote:
Originally Posted by BJIADOKC
Посмотреть сообщение
Thanks! Adding to first post.


Re: dirReader ─ Easiest and fastest way to read a directory! - BigETI - 03.07.2013

"|" means BITWISE OR not BITWISE AND.
Can you show an example to the users in your main post, how to concat flags with "|", please?
Great job, by the way.


Re: dirReader ─ Easiest and fastest way to read a directory! - .FuneraL. - 03.07.2013

Doubt: This plugin is functional with all directories or only SA:MP directories - example: scriptfiles...?


Re: dirReader ─ Easiest and fastest way to read a directory! - kurta999 - 03.07.2013

Very nice, I think it will be very useful for me.


Re: dirReader ─ Easiest and fastest way to read a directory! - RyDeR` - 03.07.2013

Update
Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Changelog
  • 03/07/2013 - 14:02:
    • Added a new flag to ignore directories:
      pawn Код:
      #define DR_NO_DIR       (1 << 6)    // Skips directories
See examples in the first post. Linux compilation would be appreciated.


Quote:
Originally Posted by BigETI
Посмотреть сообщение
"|" means BITWISE OR not BITWISE AND.
Can you show an example to the users in your main post, how to concat flags with "|", please?
Great job, by the way.
Little confusion right there, fixed, thanks. I also added an example with the new flag.

Quote:
Originally Posted by .FuneraL.
Посмотреть сообщение
Doubt: This plugin is functional with all directories or only SA:MP directories - example: scriptfiles...?
It is actually functional in all directories. You could for example do:
pawn Код:
drHandle = dirReader_open("C:/", "*p*", FNM_CASEFOLD);
As long as no paths like this are specified, it will stay within your SA:MP servers root, though.


Re: dirReader ─ Easiest and fastest way to read a directory! - Swimor - 03.07.2013

https://sampforum.blast.hk/showthread.php?tid=411268

Seems like plugin with this meaning is already created.


Re: dirReader ─ Easiest and fastest way to read a directory! - RyDeR` - 03.07.2013

Added Linux plugin.

Quote:
Originally Posted by Swimor
Посмотреть сообщение
https://sampforum.blast.hk/showthread.php?tid=411268

Seems like plugin with this meaning is already created.
I didn't know about that and besides, there's a difference. Here you have the option to select files with a pattern and it's more straight to the point.


Re: dirReader ─ Easiest and fastest way to read a directory! - .FuneraL. - 03.07.2013

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Update

See examples in the first post. Linux compilation would be appreciated.



Little confusion right there, fixed, thanks. I also added an example with the new flag.


It is actually functional in all directories. You could for example do:
pawn Код:
drHandle = dirReader_open("C:/", "*p*", FNM_CASEFOLD);
As long as no paths like this are specified, it will stay within your SA:MP servers root, though.
Oh... Thanks, i use it in my script, nice

Ryder, one suggestion for this plugin, get the file path with one function, example:
pawn Код:
dirReader_getPath("samp.exe");
it's possible? it's useful for other things


Re: dirReader ─ Easiest and fastest way to read a directory! - IstuntmanI - 03.07.2013

Great work ! It's very useful.


Re: dirReader ─ Easiest and fastest way to read a directory! - Ztan - 04.07.2013

Good work. An useful plugin