码迷,mamicode.com
首页 > 其他好文 > 详细

字符串通信协议解析函数

时间:2015-10-18 11:15:24      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

unit HUtil32;
//提供大量的辅助函数
{$WARNINGS OFF}
interface

uses
   Classes, SysUtils, WinTypes, WinProcs, Graphics, Messages, Dialogs;

type
   Str4096 = array [0..4096] of AnsiChar;
   Str256 = array [0..256] of AnsiChar;
   //名称、值对照表
   TyNameTable = record
     Name: string;
     varl: Longint;
   end;

   TLRect = record
      Left, Top, Right, Bottom: Longint;
   end;
// 取 SearchAfter, ArrestBefore之间的字符串
function  ArrestStringEx (Source, SearchAfter, ArrestBefore: AnsiString; var ArrestStr: AnsiString): AnsiString;
// 获取指定字符串中以指定分割符结尾的子串
function  GetValidStr3 (Str: AnsiString; var Dest: AnsiString; const Divider: array of AnsiChar): AnsiString;

implementation

function ArrestStringEx (Source, SearchAfter, ArrestBefore: AnsiString; var ArrestStr: AnsiString): AnsiString;
var
   SrcLen: integer;
   GoodData: Boolean;
   i, n: integer;
begin
   ArrestStr := ‘‘; {result string}
   if Source = ‘‘ then begin
      Result := ‘‘;
      exit;
   end;

   try
      SrcLen := Length (Source);
      GoodData := FALSE;
      if SrcLen >= 2 then
         if Source[1] = SearchAfter then begin
            Source := Copy (Source, 2, SrcLen-1);
            SrcLen := Length (Source);
            GoodData := TRUE;
         end else begin
            n := Pos (SearchAfter, Source);
            if n > 0 then begin
               Source := Copy (Source, n+1, SrcLen-(n));
               SrcLen := Length(Source);
               GoodData := TRUE;
            end;
         end;
      if GoodData then begin
         n := Pos (ArrestBefore, Source);
         if n > 0 then begin
            ArrestStr := Copy (Source, 1, n-1);
            Result := Copy (Source, n+1, SrcLen-n);
         end else begin
            Result := SearchAfter + Source;
         end;
      end else begin
         for i:=1 to SrcLen do begin
            if Source[i] = SearchAfter then begin
               Result := Copy (Source, i, SrcLen-i+1);
               break;
            end;
         end;
      end;
   except
      ArrestStr := ‘‘;
      Result := ‘‘;
   end;
end;

//获取指定字符串中以指定分割符结尾的子串
function GetValidStr3 (Str: AnsiString; var Dest: AnsiString; const Divider: array of AnsiChar): AnsiString;
const
   BUF_SIZE = 20480; //$7FFF;
var
   Buf: array[0..BUF_SIZE] of AnsiChar;
   BufCount, Count, SrcLen, I, ArrCount: Longint;
   Ch: AnsiChar;
label
 CATCH_DIV;
begin
   try
      SrcLen := Length(Str);
      BufCount := 0;
      Count := 1;

      if SrcLen >= BUF_SIZE-1 then begin
         Result := ‘‘;
         Dest := ‘‘;
         exit;
      end;

      if Str = ‘‘ then begin
         Dest := ‘‘;
         Result := Str;
         exit;
      end;
      ArrCount := sizeof(Divider) div sizeof(AnsiChar);

      while TRUE do begin
         if Count <= SrcLen then begin
            Ch := Str[Count];
            for I:=0 to ArrCount- 1 do
               if Ch = Divider[I] then
                  goto CATCH_DIV;
         end;
         if (Count > SrcLen) then begin
            CATCH_DIV:
            if (BufCount > 0) then begin
               if BufCount < BUF_SIZE-1 then begin
                  Buf[BufCount] := #0;
                  Dest := string (Buf);
                  Result := Copy (Str, Count+1, SrcLen-Count);
               end;
               break;
            end else begin
               if (Count > SrcLen) then begin
                  Dest := ‘‘;
                  Result := Copy (Str, Count+2, SrcLen-1);
                  break;
               end;
            end;
         end else begin
            if BufCount < BUF_SIZE-1 then begin
               Buf[BufCount] := Ch;
               Inc (BufCount);
            end;
         end;
         Inc (Count);
      end;
   except
      Dest := ‘‘;
      Result := ‘‘;
   end;
end;

end.

字符串通信协议解析函数

标签:

原文地址:http://www.cnblogs.com/hnxxcxg/p/4889016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!