⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpas.pas

📁 雷神之锤2(Quake2)Delphi源码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
begin
  Result := dst;
  while dst[0] <> #0 do
    Inc(Dst);
  memcpy(dst, src, strlen(src) + 1);
end;

function strncat(dst: PChar; const src: PChar; len: size_t): PChar;
begin
  Result := dst;
  while dst[0] <> #0 do
    Inc(Dst);
  memcpy(dst, src, min(strlen(src) + 1, len));
end;

function memcmp(const buf1, buf2: Pointer; len: size_t): Integer;
var
  i: Integer;
begin
  Result := 0;
  i := 0;
  while (i < len) and (Result = 0) do
  begin
    if PChar(buf1)[i] < PChar(buf2)[i] then
      Result := -1
    else if PChar(buf1)[i] > PChar(buf2)[i] then
      Result := 1;
    Inc(i);
  end;
end;

function strcmp(const str1, str2: PChar): Integer;
var
  l1, l2: Integer;
begin
  l1 := strlen(str1);
  l2 := strlen(str2);
  Result := memcmp(str1, str2, min(l1, l2));
  if Result = 0 then
    if l1 < l2 then
      Result := -1
    else if l1 > l2 then
      Result := 1;
end;

function strcoll(const str1, str2: PChar): Integer;
begin
  Result := strcmp(str1, str2);
end;

function strncmp(const str1, str2: PChar; len: size_t): Integer;
var
  l1, l2: Integer;
begin
  l1 := min(strlen(str1), len);
  l2 := min(strlen(str2), len);
  Result := memcmp(str1, str2, min(l1, l2));
  if Result = 0 then
    if l1 < l2 then
      Result := -1
    else if l2 > l1 then
      Result := 1;
end;

function strxfrm(dst: PChar; const src: PChar; len: size_t): size_t;
begin
  Result := strlen(src);
  if Result <= len then
    strcpy(dst, src);
end;

function memchr(const buf: Pointer; c: Integer; len: size_t): Pointer;
var
  l: Char;
begin
  Result := buf;
  l := chr(c);
  while len <> 0 do
  begin
    if PChar(Result)[0] = l then
      Exit;
    Inc(Integer(Result));
    Dec(len);
  end;
  Result := NULL;
end;

function strchr(const str: PChar; c: Integer): PChar;
begin
  Result := memchr(str, c, strlen(str) + 1);
end;

function strcspn(const str1, str2: PChar): size_t;
var
  t: PChar;
begin
  Result := 0;
  t := str1;
  while t[0] <> #0 do
  begin
    if strchr(str2, Ord(t[0])) <> NULL then
      Exit;
    Inc(Result);
    Inc(t);
  end;
end;

function strpbrk(const str1, str2: PChar): PChar;
begin
  Result := str1;
  while Result[0] <> #0 do
  begin
    if strchr(str2, Ord(Result[0])) <> NULL then
      Exit;
    Inc(Result);
  end;
  Result := NULL;
end;

function strrchr(const str: PChar; c: Integer): PChar;
var
  len: Integer;
  l: Char;
begin
  len := strlen(str);
  Result := str + len;
  l := chr(c);
  while len <> 0 do
  begin
    if Result[0] = l then
      Exit;
    Dec(Result);
    Dec(len);
  end;
  Result := NULL;
end;

function strspn(const str1, str2: PChar): size_t;
var
  t: PChar;
begin
  Result := 0;
  t := str1;
  while t[0] <> #0 do
  begin
    if strchr(str2, Ord(t[0])) = NULL then
      Exit;
    Inc(Result);
    Inc(t);
  end;
end;

function strstr(const str1, str2: PChar): PChar;
var
  l: Integer;
begin
  l := strlen(str2);
  Result := str1;
  while Result[0] <> #0 do
  begin
    if strncmp(Result, str2, l) = 0 then
      Exit;
    Inc(Result);
  end;
  Result := NULL;
end;

var
  strtok_str: PChar;

function strtok(str: PChar; const tok: PChar): PChar;
begin
  if str <> NULL then
    strtok_str := str;
  Result := strtok_str;
  while strtok_str[0] <> #0 do
  begin
    if strchr(tok, Ord(strtok_str[0])) <> NULL then
    begin
      strtok_str[0] := #0;
      Inc(strtok_str);
      Exit;
    end;
    Inc(strtok_str);
  end;
  Result := NULL;
end;

function memset(buf: Pointer; c: Integer; len: size_t): Pointer;
begin
  FillChar(buf^, len, c);
  Result := buf;
end;

function strerror(nb_error: Integer): PChar;
begin
  Result := NULL;
end;

function strlen(const str1: PChar): size_t;
begin
  Result := 0;
  while str1[Result] <> #0 do
    Inc(Result);
end;

function atoi(s: PChar): Integer;
begin
  Result := StrToIntDef(s, 0);
end;

function atof(s: PChar): Single;
var
  s2: String;
  i: Integer;
begin
  s2 := s;
  for i := 1 to Length(s2) do
  begin
    if s2[i] in ['.', ','] then
      s2[i] := SysUtils.DecimalSeparator;
  end;
  Result := StrToFloatDef(s2, 0.0);
end;

function sscanf(const s: PChar; const fmt: PChar;
  const pointers: array of Pointer) : Integer;
type
  TScanfFmtType = (sftInvalid, sftInteger, sftFloat, sftString);
var
  i, n, m: integer;
  s1: array [0..1023] of Char;

  procedure AddChar(c: Char);
  begin
    s1[strlen(s1) + 1] := #0;
    s1[strlen(s1)] := c;
  end;

  function GetInt: Integer;
  begin
    s1[0] := #0;
    while (s[n] = ' ')  and (strlen(s) > n) do
      Inc(n);
    while (s[n] in ['0'..'9', '+', '-']) and (strlen(s) >= n) do
    begin
      AddChar(s[n]);
      Inc(n);
    end;
    Result := strlen(s1);
  end;

  function GetFloat: Integer;
  begin
    s1[0] := #0;
    while (s[n] = ' ') and (strlen(s) > n) do
      Inc(n);
    while (s[n] in ['0'..'9', '+', '-', '.', 'e', 'E'])
      and (strlen(s) >= n) do
    begin
      AddChar(s[n]);
      Inc(n);
    end;
    Result := strlen(s1);
  end;

  function GetString: Integer;
  begin
    s1[0] := #0;
    while (s[n] = ' ') and (strlen(s) > n) do
      Inc(n);
    while (s[n] <> ' ') and (strlen(s) >= n) do
    begin
      AddChar(s[n]);
      Inc(n);
    end;
    Result := strlen(s1);
  end;

  function ScanStr(c: Char): Boolean;
  begin
    Result := False;

    while (s[n] <> c) and (strlen(s) > n) do
      Inc(n);
    Inc(n);

    if (n <= strlen(s)) then
      Result := True
  end;

  function GetFmt: TScanfFmtType;
  begin
    Result := sftInvalid;

    while (True) do
    begin
      while (fmt[m] = ' ') and (strlen(fmt) > m) do
        Inc(m);
      if (m >= strlen(fmt)) then
        Break;

      if (fmt[m] = '%') then
      begin
        Inc(m);
        case fmt[m] of
          'd': Result := sftInteger;
          'f': Result := sftFloat;
          's': Result := sftString;
        end;
        Inc(m);
        Break;
      end;

      if (not ScanStr(fmt[m])) then
        Break;
      Inc(m);
    end;
  end;

begin
  n := 0;
  m := 0;
  Result := 0;

  for i := 0 to High(pointers) do
  begin
    case GetFmt of
    sftInteger:
    begin
      if GetInt > 0 then
      begin
//        l := atoi(s1);
//        Move(l, pointers[i]^, SizeOf(LongInt));
        PInteger(pointers[i])^ := atoi(s1);
        Inc(Result);
      end
      else
        Break;
    end;

    sftFloat:
    begin
      if GetFloat > 0 then
      begin
//        x := atof(s1);
//        Move(x, pointers[i]^, SizeOf(Extended));
        PSingle(pointers[i])^ := atof(s1);
        Inc(Result);
      end
      else
        Break;
    end;

    sftString:
    begin
      if GetString > 0 then
      begin
//        Move(s1, pointers[i]^, strlen(s1) + 1);
        strcpy(pointers[i], s1);
        Inc(Result);
      end
      else
        Break;
    end;

    else
      Break;
    end;
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -