OSDN Git Service

パネルの配置についてコメント追加
[winbottle/winbottle.git] / bottleclient / HeadValue.pas
1 unit HeadValue;
2
3 (*
4 Content-type: text/html
5 Content-length: 1024
6
7 \8fã\82Ì\82æ\82¤\82È\95\8fÍ\82ð\93K\90Ø\82É\95ª\89ð\82µ\82Ä\88µ\82¢\82â\82·\82­\82·\82é\82½\82ß\82Ì\83N\83\89\83X\81B
8  *)
9
10 interface
11 uses Classes, SysUtils;
12
13 type
14   EHeadValueError = class(Exception);
15
16   THeadValue = class(TPersistent)
17   private
18     FStrList: TStringList;
19     function GetCount: integer;
20     function GetData(Key: String): String;
21     function GetValueAt(Index: integer): String;
22     function GetKeyAt(Index: integer): String;
23     function GetIntData(Key: String): integer;
24     procedure SetData(Key: String; const Value: String);
25     procedure SetValueAt(Index: integer; const Value: String);
26   public
27     constructor Create(const Str: String); overload;
28     constructor Create; overload;
29     destructor Destroy; override;
30     procedure Initialize(const Str: String);
31     function ToStr: String;
32     property Count: integer read GetCount;
33     property KeyAt[Index: integer]: String read GetKeyAt;
34     property ValueAt[Index: integer]: String read GetValueAt write SetValueAt;
35     property Data[Key: String]: String read GetData write SetData; default;
36     property IntData[Key: String]: integer read GetIntData;
37     procedure Assign(Source: TPersistent); override;
38   end;
39
40 implementation
41
42 { THeadValue }
43
44 constructor THeadValue.Create(const Str: String);
45 begin
46   Create;
47   Initialize(Str);
48 end;
49
50 procedure THeadValue.Assign(Source: TPersistent);
51 begin
52   if Source is THeadValue then
53     FStrList.Assign((Source as THeadValue).FStrList)
54   else
55     inherited;
56 end;
57
58 constructor THeadValue.Create;
59 begin
60   FStrList := TStringList.Create;
61 end;
62
63 destructor THeadValue.Destroy;
64 begin
65   inherited;
66   FStrList.Free;
67 end;
68
69 function THeadValue.GetCount: integer;
70 begin
71   Result := FStrList.Count div 2;
72 end;
73
74 function THeadValue.GetData(Key: String): String;
75 var i: integer;
76 begin
77   Result := '';
78   for i := 0 to Count-1 do
79     if KeyAt[i] = Key then begin
80       Result := ValueAt[i];
81       Exit;
82     end;
83 end;
84
85 function THeadValue.GetIntData(Key: String): integer;
86 begin
87   Result := StrToIntDef(GetData(Key), -1);
88 end;
89
90 function THeadValue.GetKeyAt(Index: integer): String;
91 begin
92   Result := FStrList[Index * 2];
93 end;
94
95 function THeadValue.GetValueAt(Index: integer): String;
96 begin
97   Result := FStrList[Index * 2 +1];
98 end;
99
100 procedure THeadValue.Initialize(const Str: String);
101 var Tmp: TStringList;
102     i, j: integer;
103     Key, Value, S: String;
104 begin
105   Tmp := nil;
106   FStrList.Clear;
107   try
108     Tmp := TStringList.Create;
109     Tmp.Text := Str;
110     for i := 0 to Tmp.Count-1 do begin
111       S := Tmp[i];
112       Key := ''; Value := '';
113       j := Pos(':', S);
114       if j > 0 then begin
115         Key := Copy(S, 1, j-1);
116         if Length(S) > j then Inc(j);
117         while j <= Length(S) do
118         begin
119           if (S[j] in [' ']) then
120             Inc(j)
121           else
122             Break;
123         end;
124         Value := Copy(S, j, High(integer));
125       end;
126       if Key <> '' then begin // Value\82Í\8bó\82Å\82à\82æ\82¢
127         FStrList.Add(Key);
128         FStrList.Add(Value);
129       end;
130     end;
131   finally
132     Tmp.Free;
133   end;
134 end;
135
136 function THeadValue.ToStr: String;
137 var i, max: integer;
138 begin
139   max := FStrList.Count div 2 - 1;
140   Result := '';
141   for i := 0 to max do begin
142     Result := Result + Format('%s: %s', [FStrList[i*2], FStrList[i*2+1]]);
143     if i <> max then Result := Result + #13#10;
144   end;
145 end;
146
147 procedure THeadValue.SetData(Key: String; const Value: String);
148 var i: integer;
149 begin
150   for i := 0 to Count-1 do
151     if KeyAt[i] = Key then begin
152       ValueAt[i] := Value;
153       Exit;
154     end;
155   // \92Ç\89Á
156   FStrList.Add(Key);
157   FStrList.Add(Value);
158 end;
159
160 procedure THeadValue.SetValueAt(Index: integer; const Value: String);
161 begin
162   FStrList[Index * 2 +1] := Value;
163 end;
164
165 end.