[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


Messages In This Thread
dirReader ─ Easiest and fastest way to read a directory! - by RyDeR` - 02.07.2013, 23:35
Re: dirReader ─ Easiest and fastest way to read a directory! - by omarnor89 - 03.07.2013, 00:13
Re: dirReader ─ Easiest and fastest way to read a directory! - by CaHbKo - 03.07.2013, 00:17
Re: dirReader ─ Easiest and fastest way to read a directory! - by TheArcher - 03.07.2013, 00:35
Re: dirReader ─ Easiest and fastest way to read a directory! - by Kirollos - 03.07.2013, 01:02
Re: dirReader ─ Easiest and fastest way to read a directory! - by Red_Dragon. - 03.07.2013, 01:42
Re: dirReader ─ Easiest and fastest way to read a directory! - by Pottus - 03.07.2013, 02:05
Re: dirReader ─ Easiest and fastest way to read a directory! - by Kar - 03.07.2013, 02:39
Re: dirReader ─ Easiest and fastest way to read a directory! - by BJIADOKC - 03.07.2013, 03:27
Re: dirReader ─ Easiest and fastest way to read a directory! - by Niko_boy - 03.07.2013, 04:59
Re: dirReader ─ Easiest and fastest way to read a directory! - by RyDeR` - 03.07.2013, 07:59
Re: dirReader ─ Easiest and fastest way to read a directory! - by BigETI - 03.07.2013, 09:32
Re: dirReader ─ Easiest and fastest way to read a directory! - by .FuneraL. - 03.07.2013, 09:41
Re: dirReader ─ Easiest and fastest way to read a directory! - by kurta999 - 03.07.2013, 10:18
Re: dirReader ─ Easiest and fastest way to read a directory! - by RyDeR` - 03.07.2013, 12:09
Re: dirReader ─ Easiest and fastest way to read a directory! - by Swimor - 03.07.2013, 12:45
Re: dirReader ─ Easiest and fastest way to read a directory! - by RyDeR` - 03.07.2013, 13:50
Re: dirReader ─ Easiest and fastest way to read a directory! - by .FuneraL. - 03.07.2013, 14:52
Re: dirReader ─ Easiest and fastest way to read a directory! - by IstuntmanI - 03.07.2013, 17:47
Re: dirReader ─ Easiest and fastest way to read a directory! - by Ztan - 04.07.2013, 08:00
Re: dirReader ─ Easiest and fastest way to read a directory! - by CoaPsyFactor - 04.07.2013, 08:57

Forum Jump:


Users browsing this thread: 1 Guest(s)