02.07.2013, 23:35
(
Последний раз редактировалось RyDeR`; 03.07.2013 в 13:40.
)
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
Natives
Example(s)
If I run the following code:
This will print all files (including directories) in scriptfiles. Now let's change the pattern:
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:
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:
This will print all files (except directories) starting with an "R" or "r". Anyway you get the main point.
A few notes here:
Windows and Linux (CentOS) (source + plugin + include)
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
- Added a new flag to ignore directories:
- 03/07/2013 - 01:35:
- Initial Release.
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
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));
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);
pawn Код:
drHandle = dirReader_open("scriptfiles/", "*.ZX", DR_NO_DIR); // Directories will be ignored
pawn Код:
drHandle = dirReader_open("scriptfiles/", "*.ZX", DR_NO_DIR | FNM_CASEFOLD); // Directories and the letter case will be ignored
pawn Код:
drHandle = dirReader_open("scriptfiles/", "R*", DR_NO_DIR | FNM_CASEFOLD);
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.
Windows and Linux (CentOS) (source + plugin + include)