|
Last change
on this file was 148, checked in by chronos, 3 years ago |
- Modified: Update Common and VCard package. Remove U prefix from unit names.
|
|
File size:
1.9 KB
|
| Line | |
|---|
| 1 | unit QuotedPrintable;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils;
|
|---|
| 7 |
|
|---|
| 8 | function DecodeQuotedPrintable(Text: string; IgnoreErrors: Boolean = False): string;
|
|---|
| 9 | function EncodeQuotedPrintable(Text: string; EncodeSpaces: Boolean = False): string;
|
|---|
| 10 |
|
|---|
| 11 | const
|
|---|
| 12 | QuotedPrintableEscapeCharacter = '=';
|
|---|
| 13 |
|
|---|
| 14 | implementation
|
|---|
| 15 |
|
|---|
| 16 | uses
|
|---|
| 17 | Common;
|
|---|
| 18 |
|
|---|
| 19 | resourcestring
|
|---|
| 20 | SDecodeError = 'Decode error';
|
|---|
| 21 |
|
|---|
| 22 | function DecodeQuotedPrintable(Text: string; IgnoreErrors: Boolean = False): string;
|
|---|
| 23 | var
|
|---|
| 24 | I: Integer;
|
|---|
| 25 | J: Integer;
|
|---|
| 26 | C: Char;
|
|---|
| 27 | IntValue: Integer;
|
|---|
| 28 | begin
|
|---|
| 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;
|
|---|
| 54 | end;
|
|---|
| 55 |
|
|---|
| 56 | function EncodeQuotedPrintable(Text: string; EncodeSpaces: Boolean): string;
|
|---|
| 57 | var
|
|---|
| 58 | I: Integer;
|
|---|
| 59 | J: Integer;
|
|---|
| 60 | C: Char;
|
|---|
| 61 | LowerLimit: Char;
|
|---|
| 62 | const
|
|---|
| 63 | HexDigits : array[0..$F] of AnsiChar = '0123456789ABCDEF';
|
|---|
| 64 | begin
|
|---|
| 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;
|
|---|
| 85 | end;
|
|---|
| 86 |
|
|---|
| 87 | end.
|
|---|
| 88 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.