source: trunk/Packages/VCard/QuotedPrintable.pas

Last change on this file was 148, checked in by chronos, 18 months ago
  • Modified: Update Common and VCard package. Remove U prefix from unit names.
File size: 1.9 KB
Line 
1unit QuotedPrintable;
2
3interface
4
5uses
6 Classes, SysUtils;
7
8function DecodeQuotedPrintable(Text: string; IgnoreErrors: Boolean = False): string;
9function EncodeQuotedPrintable(Text: string; EncodeSpaces: Boolean = False): string;
10
11const
12 QuotedPrintableEscapeCharacter = '=';
13
14implementation
15
16uses
17 Common;
18
19resourcestring
20 SDecodeError = 'Decode error';
21
22function DecodeQuotedPrintable(Text: string; IgnoreErrors: Boolean = False): string;
23var
24 I: Integer;
25 J: Integer;
26 C: Char;
27 IntValue: Integer;
28begin
29 Result := '';
30 SetLength(Result, Length(Text));
31 I := 1;
32 J := 1;
33 while I <= Length(Text) do begin
34 C := Text[I];
35 if C = QuotedPrintableEscapeCharacter then begin
36 if ((I + 2) <= Length(Text)) and TryHexToInt(Text[I + 1] + Text[I + 2], IntValue) then begin
37 Result[J] := Chr(IntValue);
38 Inc(I, 2);
39 Inc(J);
40 SetLength(Result, Length(Result) - 2);
41 end else begin
42 if not IgnoreErrors then raise Exception.Create(SDecodeError)
43 else begin
44 Result[J] := '?';
45 Inc(J);
46 end;
47 end;
48 end else begin
49 Result[J] := C;
50 Inc(J);
51 end;
52 Inc(I);
53 end;
54end;
55
56function EncodeQuotedPrintable(Text: string; EncodeSpaces: Boolean): string;
57var
58 I: Integer;
59 J: Integer;
60 C: Char;
61 LowerLimit: Char;
62const
63 HexDigits : array[0..$F] of AnsiChar = '0123456789ABCDEF';
64begin
65 if EncodeSpaces then LowerLimit := #33
66 else LowerLimit := #32;
67 Result := '';
68 SetLength(Result, Length(Text));
69 I := 1;
70 J := 1;
71 while I <= Length(Text) do begin
72 C := Text[I];
73 if (C = QuotedPrintableEscapeCharacter) or (C < LowerLimit) or (C > #126) then begin
74 SetLength(Result, Length(Result) + 2);
75 Result[J] := QuotedPrintableEscapeCharacter;
76 Result[J + 1] := HexDigits[Ord(C) shr 4];
77 Result[J + 2] := HexDigits[Ord(C) and $f];
78 Inc(J, 3);
79 end else begin
80 Result[J] := C;
81 Inc(J);
82 end;
83 Inc(I);
84 end;
85end;
86
87end.
88
Note: See TracBrowser for help on using the repository browser.