Skip to content

Commit 1a08f1b

Browse files
committed
+ tinydir
1 parent 6a9492f commit 1a08f1b

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ Pascal port of [tinybits](https://github.com/r-lyeh/tinybits).
1414
|:------|:-----|:-----|-------|
1515
|[dtinyarc4.pas](dtinyarc4.pas)||ARC4 stream cypher||
1616
|[dtinyatoi.pas](dtinyatoi.pas)||`atoi()` replacement||
17-
|[dtinyini.pas](dtinyini.pas)||Config parser (ini+)||
1817
|[dtinybsearch.pas](dtinybsearch.pas)||Dichotomic binary search||
1918
|[dtinybuild.inc](dtinybuild.inc)||Build macros||
19+
|[dtinydebug.inc](dtinydebug.inc)||Debug macros||
20+
|[dtinydir.pas](dtinydir.pas)||Recursive directory listing||
21+
|[dtinyini.pas](dtinyini.pas)||Config parser (ini+)||
2022
|[tinydual.sh.bat](tinydual.sh.bat)|=|Dual bash/batch file||
21-
|_tinydebug.h_|-|Debug macros||
22-
|_tinydir.cc_|-|Recursive directory listing||
2323
|_tinydixy.c_|-|Small YAML-subset config file parser||
2424
|_tinyendian.c_|-|Endianness conversion|FPC already has [SwapEndian](https://www.freepascal.org/docs-html/rtl/system/swapendian.html), [NtoLE](https://www.freepascal.org/docs-html/rtl/system/ntole.html), [NtoBE](https://www.freepascal.org/docs-html/rtl/system/ntobe.html)|
2525
|_tinyerror.c_|-|Error handling||
@@ -39,7 +39,7 @@ Pascal port of [tinybits](https://github.com/r-lyeh/tinybits).
3939
|_tinyuniso.cc_|-|.iso/9960 unarchiver||
4040
|_tinyunit.c_|-|Unit-testing||
4141
|_tinyuntar.cc_|-|.tar unarchiver||
42-
|_tinyvariant.cc_|-|Variant class|See [FPC's Variant](https://wiki.freepascal.org/Variant)|
42+
|_tinyvariant.cc_|-|Variant class|See also [FPC's Variant](https://wiki.freepascal.org/Variant)|
4343
|_tinyvbyte.h_|-|VLE encoder/decoder (vbyte)||
4444
|_tinywav.c_|-|Forked WAV writer||
4545
|_tinyzlib.cpp_|-|zlib inflater||

dtinydir.pas

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
unit dtinydir;
2+
3+
// tiny directory listing
4+
// - rlyeh, public domain | wtrmrkrlyeh
5+
// - ported to pascal by Doj
6+
7+
{$MODE FPC}
8+
{$MODESWITCH DEFAULTPARAMETERS}
9+
{$MODESWITCH OUT}
10+
{$MODESWITCH RESULT}
11+
12+
interface
13+
14+
type
15+
TTinyDirCallback = procedure (Name: PAnsiChar; IsDir: Boolean);
16+
function tinydir(directory: PAnsiChar; yield: TTinyDirCallback): Boolean;
17+
18+
implementation
19+
20+
{$IF Defined(WINDOWS)}
21+
uses
22+
windows;
23+
{$ELSE}
24+
uses
25+
BaseUnix;
26+
{$ENDIF}
27+
28+
function tinydir(directory: PAnsiChar; yield: TTinyDirCallback): Boolean;
29+
var
30+
Src: AnsiString;
31+
{$IF Defined(WINDOWS)}
32+
fdata: windows.WIN32_FIND_DATA;
33+
h: windows.HANDLE;
34+
next: Boolean;
35+
{$ELSE}
36+
Dir, Tmp: BaseUnix.PDir
37+
Dirent: BaseUnix.PDirent;
38+
{$ENDIF}
39+
begin
40+
src := directory;
41+
while (Src <> '') and ((Src[Length(Src)] = '/') or (Src[Length(Src)] = '\')) do
42+
SetLength(Src, Length(Src) - 1);
43+
{$IF Defined(WINDOWS)}
44+
h := FindFirstFileA(PAnsiChar(src + '/*'), @fdata);
45+
while h <> INVALID_HANDLE_VALUE do begin
46+
next := True;
47+
while next do begin
48+
if fdata.cFileName[0] <> '.' then begin
49+
yield(PAnsiChar(src + '/' + fdata.cFileName), (fdata.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) > 0);
50+
end;
51+
next := FindNextFileA(h, @fdata);
52+
end;
53+
FindClose(h);
54+
Exit(True);
55+
end;
56+
{$ELSE}
57+
Dir := FpOpenDir(PAnsiChar(Src + '/'));
58+
if Dir <> nil then begin
59+
Dirent := FpReadDir(Dir^);
60+
while Dirent <> nil do begin
61+
if Dirent^.d_name[0] <> '.' then begin
62+
Tmp := FpOpenDir(ep^.d_name);
63+
if Tmp then begin
64+
FpCloseDir(Tmp^);
65+
yield(PAnsiChar(Src + '/' + ep->d_name), True);
66+
end else
67+
yield(PAnsiChar(Src + '/' + ep->d_name), False);
68+
end;
69+
end;
70+
FpCloseDir(Dir^);
71+
return closedir( dir ), true;
72+
end;
73+
{$ENDIF}
74+
Exit(False);
75+
end;
76+
77+
//
78+
// procedure Callback(Name: PAnsiChar; IsDir: Boolean);
79+
// begin
80+
// if IsDir then
81+
// Write('<dir> ');
82+
// Writeln(Name);
83+
// // uncomment for recursive listing:
84+
// // if IsDir then
85+
// // tinydir(Name, @CallbackRecursive);
86+
// end;
87+
//
88+
// begin
89+
// tinydir('./', @Callback);
90+
// end.
91+
//
92+
93+
end.

test/test_tinydir.pas

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{$MODE FPC}
2+
{$MODESWITCH DEFAULTPARAMETERS}
3+
{$MODESWITCH OUT}
4+
{$MODESWITCH RESULT}
5+
6+
uses
7+
dtinydir in '../dtinydir.pas';
8+
9+
procedure Callback(Name: PAnsiChar; IsDir: Boolean);
10+
begin
11+
if IsDir then
12+
Write('<dir> ');
13+
Writeln(Name);
14+
end;
15+
16+
procedure CallbackRecursive(Name: PAnsiChar; IsDir: Boolean);
17+
begin
18+
Callback(Name, IsDir);
19+
if IsDir then
20+
tinydir(Name, @CallbackRecursive);
21+
end;
22+
23+
begin
24+
tinydir('./', @CallbackRecursive);
25+
end.

0 commit comments

Comments
 (0)