1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
Uses TypInfo;
Function StyleToString( styles: TFontStyles ): String;
var
style: TFontStyle;
Begin
Result := '[';
For style := Low(style) To High(style) Do Begin
If style IN styles Then Begin
If Length(result) > 1 Then
result := result + ',';
result := result + GetEnumname( typeInfo(TFontStyle), Ord(style));
End; { If }
End; { For }
Result := Result + ']';
End; { StyleToString }
Function StringToStyle( S: String ): TFontStyles;
Var
sl : TStringlist;
style: TfontStyle;
i : Integer;
Begin
Result := [];
If Length(S) < 2 Then Exit;
If S[1] = '[' Then
Delete(S, 1, 1);
If S[Length(S)] = ']' Then
Delete(S, Length(S), 1);
If Length(S) = 0 Then Exit;
sl:= TStringlist.Create;
try
sl.commatext := S;
For i := 0 To sl.Count-1 Do Begin
try
style := TFontStyle( GetEnumValue( Typeinfo(TFontStyle), sl[i]
));
Include( Result, style );
except
end;
End; { For }
finally
sl.free
end;
End; { StringToStyle }
The problem with this is that the string can get quite long, so it is
not an efficient storage format. A more compact text format would be
the mentioned string of 0 and 1 digits:
type
EFontBitstringError = class(Exception);
const
FalseBitDigit = '0';
TrueBitDigit = '1';
BitDigits : array [boolean] of char = (FalseBitDigit,TrueBitDigit);
function StyleToBitstring( styles: TFontStyles ): String;
var
style: TFontStyle;
begin
SetLength(Result, Ord(High(Style)) - Ord(Low(Style)) + 1);
for style := Low(style) to High(style) do
Result[Ord(style) - Ord(Low(style)) + 1] :=
BitDigits[style In Styles];
end; { StyleToBitstring }
function BitstringToStyle(const S: String): TFontStyles;
var
style: TFontStyle;
N: Integer;
begin
Result := [];
N:= 0;
for style := Low(style) to High(style) do begin
Inc(N);
if (N > 0) and (N <= Length(S)) then
case S[N] of
FalseBitDigit: ; // nothing to do
TrueBitDigit : Include(Result, style);
else
raise EFontBitstringError.CreateFmt(
'Invalid character "%s" in bitstring "%s"!', [S[N], N]);
end; {case}
end; {for}
end; {BitstringToStyle} Uses TypInfo;
Function StyleToString( styles: TFontStyles ): String;
var
style: TFontStyle;
Begin
Result := '[';
For style := Low(style) To High(style) Do Begin
If style IN styles Then Begin
If Length(result) > 1 Then
result := result + ',';
result := result + GetEnumname( typeInfo(TFontStyle), Ord(style));
End; { If }
End; { For }
Result := Result + ']';
End; { StyleToString }
Function StringToStyle( S: String ): TFontStyles;
Var
sl : TStringlist;
style: TfontStyle;
i : Integer;
Begin
Result := [];
If Length(S) < 2 Then Exit;
If S[1] = '[' Then
Delete(S, 1, 1);
If S[Length(S)] = ']' Then
Delete(S, Length(S), 1);
If Length(S) = 0 Then Exit;
sl:= TStringlist.Create;
try
sl.commatext := S;
For i := 0 To sl.Count-1 Do Begin
try
style := TFontStyle( GetEnumValue( Typeinfo(TFontStyle), sl[i]
));
Include( Result, style );
except
end;
End; { For }
finally
sl.free
end;
End; { StringToStyle }
The problem with this is that the string can get quite long, so it is
not an efficient storage format. A more compact text format would be
the mentioned string of 0 and 1 digits:
type
EFontBitstringError = class(Exception);
const
FalseBitDigit = '0';
TrueBitDigit = '1';
BitDigits : array [boolean] of char = (FalseBitDigit,TrueBitDigit);
function StyleToBitstring( styles: TFontStyles ): String;
var
style: TFontStyle;
begin
SetLength(Result, Ord(High(Style)) - Ord(Low(Style)) + 1);
for style := Low(style) to High(style) do
Result[Ord(style) - Ord(Low(style)) + 1] :=
BitDigits[style In Styles];
end; { StyleToBitstring }
function BitstringToStyle(const S: String): TFontStyles;
var
style: TFontStyle;
N: Integer;
begin
Result := [];
N:= 0;
for style := Low(style) to High(style) do begin
Inc(N);
if (N > 0) and (N <= Length(S)) then
case S[N] of
FalseBitDigit: ; // nothing to do
TrueBitDigit : Include(Result, style);
else
raise EFontBitstringError.CreateFmt(
'Invalid character "%s" in bitstring "%s"!', [S[N], N]);
end; {case}
end; {for}
end; {BitstringToStyle} |
Partager