| 1 |
{ |
| 2 |
Double commander |
| 3 |
------------------------------------------------------------------------- |
| 4 |
Wdx plugin is intended to show one line of a text file |
| 5 |
|
| 6 |
Copyright (C) 2016-2017 Alexander Koblov ([email protected]) |
| 7 |
|
| 8 |
This library is free software; you can redistribute it and/or |
| 9 |
modify it under the terms of the GNU Lesser General Public |
| 10 |
License as published by the Free Software Foundation; either |
| 11 |
version 2.1 of the License, or (at your option) any later version. |
| 12 |
|
| 13 |
This library is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
Lesser General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU Lesser General Public |
| 19 |
License along with this library; if not, write to the Free Software |
| 20 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
} |
| 22 |
|
| 23 |
library TextLine; |
| 24 |
|
| 25 |
{$mode objfpc}{$H+} |
| 26 |
{$include calling.inc} |
| 27 |
|
| 28 |
uses |
| 29 |
SysUtils, Classes, StreamEx, LazUTF8, |
| 30 |
WdxPlugin, DCClassesUtf8, DCConvertEncoding, DCOSUtils; |
| 31 |
|
| 32 |
const |
| 33 |
DETECT_STRING = '(EXT="TXT") | (EXT="LOG") | (EXT="INI") | (EXT="XML")'; |
| 34 |
|
| 35 |
var |
| 36 |
FReplace: Boolean = False; |
| 37 |
FSkipEmpty: Boolean = False; |
| 38 |
FReplaces: array[1..10, 1..2] of String; |
| 39 |
|
| 40 |
function ContentGetSupportedField(FieldIndex: Integer; |
| 41 |
FieldName, Units: PAnsiChar; MaxLen: Integer): Integer; dcpcall; |
| 42 |
begin |
| 43 |
if (FieldIndex < 0) or (FieldIndex > 9) then |
| 44 |
begin |
| 45 |
Result := FT_NOMOREFIELDS; |
| 46 |
Exit; |
| 47 |
end; |
| 48 |
|
| 49 |
StrLCopy(FieldName, PAnsiChar(IntToStr(FieldIndex + 1)), MaxLen - 1); |
| 50 |
StrLCopy(Units, 'ANSI|OEM|UTF-8', MaxLen - 1); |
| 51 |
Result := FT_STRINGW; |
| 52 |
end; |
| 53 |
|
| 54 |
function ContentGetValueW(FileName: PWideChar; FieldIndex, UnitIndex: Integer; |
| 55 |
FieldValue: PWideChar; MaxLen, Flags: Integer): Integer; dcpcall; |
| 56 |
var |
| 57 |
Value: String; |
| 58 |
Index: Integer; |
| 59 |
FileNameU: String; |
| 60 |
Stream: TFileStreamEx; |
| 61 |
Reader: TStreamReader; |
| 62 |
begin |
| 63 |
if (FieldIndex < 0) or (FieldIndex > 9) then |
| 64 |
begin |
| 65 |
Result:= ft_nosuchfield; |
| 66 |
Exit; |
| 67 |
end; |
| 68 |
FileNameU:= UTF16ToUTF8(UnicodeString(FileName)); |
| 69 |
if not mbFileExists(FileNameU) then |
| 70 |
begin |
| 71 |
Result:= ft_fileerror; |
| 72 |
Exit; |
| 73 |
end; |
| 74 |
Result:= ft_fieldempty; |
| 75 |
try |
| 76 |
Stream:= TFileStreamEx.Create(FileNameU, fmOpenRead or fmShareDenyNone); |
| 77 |
try |
| 78 |
Index:= -1; |
| 79 |
Reader:= TStreamReader.Create(Stream, BUFFER_SIZE, True); |
| 80 |
repeat |
| 81 |
Value:= EmptyStr; |
| 82 |
if Reader.Eof then Break; |
| 83 |
Value:= Trim(Reader.ReadLine); |
| 84 |
if (Length(Value) = 0) and FSkipEmpty then |
| 85 |
Continue; |
| 86 |
Inc(Index); |
| 87 |
until Index = FieldIndex; |
| 88 |
finally |
| 89 |
Reader.Free; |
| 90 |
end; |
| 91 |
except |
| 92 |
Exit(ft_fileerror); |
| 93 |
end; |
| 94 |
|
| 95 |
if Value = EmptyStr then Exit; |
| 96 |
|
| 97 |
case UnitIndex of |
| 98 |
0: Value:= CeAnsiToUtf8(Value); |
| 99 |
1: Value:= CeOemToUtf8(Value); |
| 100 |
end; |
| 101 |
|
| 102 |
if FReplace and (Length(Value) > 0) then |
| 103 |
begin |
| 104 |
for Flags:= Low(FReplaces) to High(FReplaces) do |
| 105 |
begin |
| 106 |
if Length(FReplaces[Flags, 1]) > 0 then |
| 107 |
Value:= StringReplace(Value, FReplaces[Flags, 1], FReplaces[Flags, 2], [rfReplaceAll]); |
| 108 |
end; |
| 109 |
end; |
| 110 |
|
| 111 |
if Length(Value) > 0 then |
| 112 |
begin |
| 113 |
MaxLen:= MaxLen div SizeOf(WideChar) - 1; |
| 114 |
StrPLCopy(FieldValue, UTF8ToUTF16(Value), MaxLen); |
| 115 |
Result:= ft_stringw; |
| 116 |
end; |
| 117 |
end; |
| 118 |
|
| 119 |
procedure ContentSetDefaultParams(dps: PContentDefaultParamStruct); dcpcall; |
| 120 |
var |
| 121 |
S: String; |
| 122 |
Index: Integer; |
| 123 |
Ini: TIniFileEx; |
| 124 |
FileName: String; |
| 125 |
begin |
| 126 |
FileName:= CeSysToUtf8(dps^.DefaultIniName); |
| 127 |
FileName:= ExtractFilePath(FileName) + 'textline.ini'; |
| 128 |
try |
| 129 |
Ini:= TIniFileEx.Create(FileName, fmOpenRead); |
| 130 |
try |
| 131 |
FSkipEmpty:= Ini.ReadBool('Options', 'SkipEmpty', FSkipEmpty); |
| 132 |
for Index:= Low(FReplaces) to High(FReplaces) do |
| 133 |
begin |
| 134 |
S:= Ini.ReadString('Replaces', 'S' + IntToStr(Index), '='); |
| 135 |
FReplaces[Index, 1]:= Copy(S, 1, Pos('=', S) - 1); |
| 136 |
FReplaces[Index, 2]:= Copy(S, Pos('=', S) + 1, MaxInt); |
| 137 |
if (FReplace = False) then FReplace:= (S <> '='); |
| 138 |
end; |
| 139 |
finally |
| 140 |
Ini.Free; |
| 141 |
end; |
| 142 |
except |
| 143 |
// Ignore |
| 144 |
end; |
| 145 |
end; |
| 146 |
|
| 147 |
procedure ContentGetDetectString(DetectString: PAnsiChar; MaxLen: Integer); dcpcall; |
| 148 |
begin |
| 149 |
StrPLCopy(DetectString, DETECT_STRING, MaxLen - 1); |
| 150 |
end; |
| 151 |
|
| 152 |
exports |
| 153 |
ContentGetSupportedField, |
| 154 |
ContentGetValueW, |
| 155 |
ContentGetDetectString, |
| 156 |
ContentSetDefaultParams; |
| 157 |
|
| 158 |
begin |
| 159 |
|
| 160 |
end. |
| 161 |
|