Debian Codegraph @

plugins/wdx/deb_wdx/src/deb_wdx_intf.pas

raw
1 //***************************************************************
2 // This file is part of DEBWDX, a content plugin for
3 // Total Commander handling Debian Linux package.
4 //
5 // Copyright (C) 2005 Ralgh Young ([email protected])
6 //***************************************************************
7
8 // This program is free software; you can redistribute it and/or modify it
9 // under the GPL.
10 //
11 // Only version 2.0 package is supported, and I would not to add support for
12 // old format since I haven't old packages. But if you have some package of old format,
13 // and you're interested in, you're welcomed to modify the source by yourself.
14 // FYI: refer to dpkg-deb/extract.c in dpkg-deb's source,
15 // and search '0.93' in function extracthalf, you're find something useful.
16
17 {$A-,I-} //no alignment, no I/O error autochecking
18 unit deb_wdx_intf;
19
20 {$mode delphi}{$H+}
21 {$include calling.inc}
22
23 interface
24
25 uses
26 Classes, WdxPlugin;
27
28 procedure ContentGetDetectString(DetectString:pchar; maxlen:integer); dcpcall; export;
29 function ContentGetSupportedField(FieldIndex:integer;FieldName:pchar;
30 Units:pchar;maxlen:integer):integer; dcpcall; export;
31 function ContentGetValue(FileName:pchar;FieldIndex,UnitIndex:integer;FieldValue:pbyte;
32 maxlen,flags:integer):integer; dcpcall; export;
33
34 implementation
35
36 uses
37 SysUtils, debunpak;
38
39 var
40 IDX_PACKAGE,
41 IDX_VERSION,
42 IDX_SECTION,
43 IDX_PRIORITY,
44 IDX_ARCH,
45 IDX_DEPENDS,
46 IDX_RECOMMENDS,
47 IDX_SUGGESTS,
48 IDX_CONFLICTS,
49 IDX_INSTALLED_SIZE,
50 IDX_MAINTAINER,
51 IDX_SOURCE,
52 IDX_SUMMARY,
53 IDX_DESCRIPTION : integer;
54
55 CurrentPackageFile: String;
56 FieldList : TStrings;
57 FileInfo : TStrings;
58
59 procedure ContentGetDetectString(DetectString:pchar; maxlen:integer);
60 begin
61 StrPCopy(DetectString, 'EXT="DEB"');
62 end;
63
64 function ContentGetSupportedField(FieldIndex:integer; FieldName:pchar;
65 Units:pchar;maxlen:integer):integer;
66 begin
67 StrPCopy(Units, '');
68
69 if FieldIndex =IDX_INSTALLED_SIZE then
70 begin
71 StrPCopy(FieldName, FieldList.Strings[FieldIndex]);
72 StrPCopy(Units, 'bytes|kbytes|Mbytes|Gbytes'+#0);
73 Result := FT_NUMERIC_64;
74 end
75 else if FieldIndex < FieldList.Count then
76 begin
77 StrPCopy(FieldName, FieldList.Strings[FieldIndex]);
78 Result := FT_STRING;
79 end
80 else
81 Result := FT_NOMOREFIELDS;
82 end;
83
84 {$WRITEABLECONST ON}
85 function ContentGetValue(FileName:pchar; FieldIndex,UnitIndex:integer; FieldValue:pbyte;
86 maxlen,flags:integer):integer;
87
88 function EnsureLength(S: string; nMaxlen: integer): string;
89 begin
90 Result := S;
91 if length(Result)>=nMaxlen then
92 begin
93 Result := Copy(Result, 1, nMaxlen-4);
94 Result := Result + '...';
95 end;
96 end;
97
98 const
99 DescTmpFile: String = '';
100 var
101 Field, Value : String;
102 i, where_start_desc : integer;
103 size : int64;
104 begin
105 Result := FT_FILEERROR;
106 if not FileExists(FileName) then exit;
107
108 if CurrentPackageFile<>FileName then
109 begin
110
111 if not Deb_ExtractCtrlInfoFile(FileName, DescTmpFile) then exit;
112
113 FileInfo.Text := DescTmpFile;
114
115 CurrentPackageFile := FileName;
116 end
117 {$IFDEF GDEBUG}
118 else
119 WriteLn('Cached info reused for '+FileName);
120 {$ENDIF};
121
122
123 if (FieldIndex>=FieldList.Count) then
124 begin
125 Result := FT_NOSUCHFIELD;
126 exit;
127 end;
128
129 if FieldIndex<>IDX_DESCRIPTION then
130 begin
131 if FieldIndex=IDX_SUMMARY then //for 'Summary', return the value of Description
132 Field := 'Description'
133 else
134 Field := FieldList.Strings[FieldIndex];
135
136 Value := '';
137 Value := Trim(FileInfo.Values[Field]);
138
139 if Value='' then
140 begin
141 Result := FT_FIELDEMPTY;
142 exit;
143 end;
144
145 if FieldIndex=IDX_INSTALLED_SIZE then
146 begin
147 size := StrToInt64Def(Value, -1);
148 case UnitIndex of
149 0: //bytes
150 size := size * 1024;
151 // 1: //kbytes
152 // pass
153 2: //mbytes
154 size := size div 1024;
155 3: //gbytes
156 size := size div (1024 * 1024);
157 end;
158 Move(size, FieldValue^, sizeof(size));
159 Result := FT_NUMERIC_64;
160 end
161 else //other fields, just string
162 begin
163 StrPCopy(PChar(FieldValue), EnsureLength(Value, maxlen));
164 Result := FT_STRING;
165 end;
166 end
167 else //IDX_DESCRIPTION,
168 begin
169 Value := '';
170 where_start_desc := -1;
171 for i:=0 to FileInfo.Count-1 do
172 begin
173 if FileInfo.Names[i]='Description' then
174 begin
175 where_start_desc := i;
176 break;
177 end;
178 end;
179
180 if where_start_desc>=0 then
181 begin
182 for i:=where_start_desc+1 to FileInfo.Count-1 do
183 begin
184 Value := Value + FileInfo.Strings[i];
185 end;
186 StrPCopy(PChar(FieldValue), EnsureLength(Value, maxlen));
187 //Result := FT_FULLTEXT;
188 Result := FT_STRING;
189 end;
190 end
191 end;
192
193 initialization
194 CurrentPackageFile := '';
195
196 FileInfo := TStringList.Create;
197 FileInfo.NameValueSeparator := ':';
198
199 FieldList := TStringList.Create;
200 IDX_PACKAGE := FieldList.Add('Package');
201 IDX_VERSION := FieldList.Add('Version');
202 IDX_SECTION := FieldList.Add('Section');
203 IDX_PRIORITY := FieldList.Add('Priority');
204 IDX_ARCH := FieldList.Add('Architecture');
205 IDX_DEPENDS := FieldList.Add('Depends');
206 IDX_RECOMMENDS:= FieldList.Add('Recommends');
207 IDX_SUGGESTS := FieldList.Add('Suggests');
208 IDX_CONFLICTS := FieldList.Add('Conflicts');
209 IDX_INSTALLED_SIZE := FieldList.Add('Installed-Size');
210 IDX_MAINTAINER := FieldList.Add('Maintainer');
211 IDX_SOURCE := FieldList.Add('Source');
212 IDX_SUMMARY := FieldList.Add('Summary');
213 IDX_DESCRIPTION := FieldList.Add('Description');
214
215 finalization
216 FileInfo.Free;
217 FieldList.Free;
218
219 end.
220