DEADSOFTWARE

allow to open any wad type from recent files
[d2df-editor.git] / src / shared / wadreader.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE a_modes.inc}
16 unit wadreader;
18 interface
20 // g_ExtractWadName C:\svr\shit.wad:\MAPS\MAP01 -> C:/svr/shit.wad
21 function g_ExtractWadName (resourceStr: AnsiString): AnsiString;
23 // g_ExtractWadNameNoPath C:\svr\shit.wad:\MAPS\MAP01 -> shit.wad
24 function g_ExtractWadNameNoPath (resourceStr: AnsiString): AnsiString;
26 // g_ExtractFilePath C:\svr\shit.wad:\MAPS\MAP01 -> :/MAPS
27 function g_ExtractFilePath (resourceStr: AnsiString): AnsiString;
29 // g_ExtractFileName C:\svr\shit.wad:\MAPS\MAP01 -> MAP01
30 function g_ExtractFileName (resourceStr: AnsiString): AnsiString; // without path
32 // g_ExtractFilePathName C:\svr\shit.wad:\MAPS\MAP01 -> MAPS/MAP01
33 function g_ExtractFilePathName (resourceStr: AnsiString): AnsiString;
35 implementation
37 function normSlashes (s: AnsiString): AnsiString;
38 var
39 f: Integer;
40 begin
41 for f := 1 to length(s) do if s[f] = '\' then s[f] := '/';
42 result := s;
43 end;
45 function g_ExtractWadNameNoPath (resourceStr: AnsiString): AnsiString;
46 var
47 f, c: Integer;
48 begin
49 for f := length(resourceStr) downto 1 do
50 begin
51 if resourceStr[f] = ':' then
52 begin
53 result := normSlashes(Copy(resourceStr, 1, f-1));
54 c := length(result);
55 while (c > 0) and (result[c] <> '/') do Dec(c);
56 if c > 0 then result := Copy(result, c+1, length(result));
57 exit;
58 end;
59 end;
60 result := '';
61 end;
63 function g_ExtractWadName (resourceStr: AnsiString): AnsiString;
64 var
65 f: Integer;
66 begin
67 for f := length(resourceStr) downto 1 do
68 begin
69 if resourceStr[f] = ':' then
70 begin
71 result := normSlashes(Copy(resourceStr, 1, f-1));
72 exit;
73 end;
74 end;
75 result := '';
76 end;
78 function g_ExtractFilePath (resourceStr: AnsiString): AnsiString;
79 var
80 f, lastSlash: Integer;
81 begin
82 result := '';
83 lastSlash := -1;
84 for f := length(resourceStr) downto 1 do
85 begin
86 if (lastSlash < 0) and (resourceStr[f] = '\') or (resourceStr[f] = '/') then lastSlash := f;
87 if resourceStr[f] = ':' then
88 begin
89 if lastSlash > 0 then
90 begin
91 result := normSlashes(Copy(resourceStr, f, lastSlash-f));
92 while (length(result) > 0) and (result[1] = '/') do Delete(result, 1, 1);
93 end;
94 exit;
95 end;
96 end;
97 if lastSlash > 0 then result := normSlashes(Copy(resourceStr, 1, lastSlash-1));
98 end;
100 function g_ExtractFileName (resourceStr: AnsiString): AnsiString; // without path
101 var
102 f, lastSlash: Integer;
103 begin
104 result := '';
105 lastSlash := -1;
106 for f := length(resourceStr) downto 1 do
107 begin
108 if (lastSlash < 0) and (resourceStr[f] = '\') or (resourceStr[f] = '/') then lastSlash := f;
109 if resourceStr[f] = ':' then
110 begin
111 if lastSlash > 0 then result := Copy(resourceStr, lastSlash+1, length(resourceStr));
112 exit;
113 end;
114 end;
115 if lastSlash > 0 then result := Copy(resourceStr, lastSlash+1, length(resourceStr));
116 end;
118 function g_ExtractFilePathName (resourceStr: AnsiString): AnsiString;
119 var
120 f: Integer;
121 begin
122 result := '';
123 for f := length(resourceStr) downto 1 do
124 begin
125 if resourceStr[f] = ':' then
126 begin
127 result := normSlashes(Copy(resourceStr, f+1, length(resourceStr)));
128 while (length(result) > 0) and (result[1] = '/') do Delete(result, 1, 1);
129 exit;
130 end;
131 end;
132 result := normSlashes(resourceStr);
133 while (length(result) > 0) and (result[1] = '/') do Delete(result, 1, 1);
134 end;
136 end.