[Plugin] dirReader ─ Easiest and fastest way to read a directory!
#1

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
  • 03/07/2013 - 14:02:
    • Added a new flag to ignore directories:
      pawn Код:
      #define DR_NO_DIR       (1 << 6)    // Skips directories
  • 03/07/2013 - 01:35:
    • Initial Release.
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:
  • Directories are also listed unless you use DR_NO_DIR.
  • .zx in these example files are extensions.
  • You can use wildcards to search for specific strings, for example: "*a*b*.txt".
  • You can read up files from your root directory, just navigate to "scriptfiles/" (as I did) to start there.
  • It is also possible to read in other paths like "C:/", but I doubt anyone needing that.
Download

Windows and Linux (CentOS) (source + plugin + include)
Reply
#2

I didnt understand anything for sure
Reply
#3

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
Reply
#4

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

great i like it
Reply
#6

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

Questions:

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

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.
Reply
#9

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

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
Reply
#11

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.
Reply
#12

"|" 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.
Reply
#13

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

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

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.
Reply
#16

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

Seems like plugin with this meaning is already created.
Reply
#17

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.
Reply
#18

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
Reply
#19

Great work ! It's very useful.
Reply
#20

Good work. An useful plugin
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)