OSDN Git Service

First imported to cvs
[winbottle/winbottle.git] / bottleclient / HeadValue.pas
diff --git a/bottleclient/HeadValue.pas b/bottleclient/HeadValue.pas
new file mode 100755 (executable)
index 0000000..98f6489
--- /dev/null
@@ -0,0 +1,130 @@
+unit HeadValue;
+
+(*
+Content-type: text/html
+Content-length: 1024
+
+\8fã\82Ì\82æ\82¤\82È\95\8fÍ\82ð\93K\90Ø\82É\95ª\89ð\82µ\82Ä\88µ\82¢\82â\82·\82­\82·\82é\81B
+ *)
+
+interface
+uses Classes, SysUtils;
+
+type
+  EHeadValueError = class(Exception);
+
+  THeadValue = class(TObject)
+  private
+    FStrList: TStringList;
+    function GetCount: integer;
+    function GetData(Key: String): String;
+    function GetValueAt(Index: integer): String;
+    function GetKeyAt(Index: integer): String;
+    function GetIntData(Key: String): integer;
+  public
+    constructor Create(const Str: String); overload;
+    constructor Create; overload;
+    destructor Destroy; override;
+    procedure Initialize(const Str: String);
+    function ToStr: String;
+    property Count: integer read GetCount;
+    property KeyAt[Index: integer]: String read GetKeyAt;
+    property ValueAt[Index: integer]: String read GetValueAt;
+    property Data[Key: String]: String read GetData; default;
+    property IntData[Key: String]: integer read GetIntData;
+  end;
+
+implementation
+
+{ THeadValue }
+
+constructor THeadValue.Create(const Str: String);
+begin
+  Create;
+  Initialize(Str);
+end;
+
+constructor THeadValue.Create;
+begin
+  FStrList := TStringList.Create;
+end;
+
+destructor THeadValue.Destroy;
+begin
+  inherited;
+  FStrList.Free;
+end;
+
+function THeadValue.GetCount: integer;
+begin
+  Result := FStrList.Count div 2;
+end;
+
+function THeadValue.GetData(Key: String): String;
+var i: integer;
+begin
+  for i := 0 to Count-1 do
+    if KeyAt[i] = Key then begin
+      Result := ValueAt[i];
+      Exit;
+    end;
+end;
+
+function THeadValue.GetIntData(Key: String): integer;
+begin
+  Result := StrToIntDef(GetData(Key), -1);
+end;
+
+function THeadValue.GetKeyAt(Index: integer): String;
+begin
+  Result := FStrList[Index * 2];
+end;
+
+function THeadValue.GetValueAt(Index: integer): String;
+begin
+  Result := FStrList[Index * 2 +1];
+end;
+
+procedure THeadValue.Initialize(const Str: String);
+var Tmp: TStringList;
+    i, j: integer;
+    Key, Value, S: String;
+begin
+  Tmp := nil;
+  FStrList.Clear;
+  try
+    Tmp := TStringList.Create;
+    Tmp.Text := Str;
+    for i := 0 to Tmp.Count-1 do begin
+      S := Tmp[i];
+      Key := ''; Value := '';
+      j := Pos(':', S);
+      if j > 0 then begin
+        Key := Copy(S, 1, j-1);
+        if Length(S) > j then Inc(j);
+        while (S[j] in [' ']) and (j <= Length(S)) do begin
+          Inc(j);
+        end;
+        Value := Copy(S, j, High(integer));
+      end;
+      if (Key <> '') and (Value <> '') then begin
+        FStrList.Add(Key);
+        FStrList.Add(Value);
+      end;
+    end;
+  finally
+    Tmp.Free;
+  end;
+end;
+
+function THeadValue.ToStr: String;
+var i, max: integer;
+begin
+  max := FStrList.Count div 2 - 1;
+  for i := 0 to max do begin
+    Result := Result + Format('%s: %s', [FStrList[i*2], FStrList[i*2+1]]);
+    if i <> max then Result := Result + #13#10;
+  end;
+end;
+
+end.