Recent

Author Topic: Unable to Use this function  (Read 3178 times)

Deepaak

  • Sr. Member
  • ****
  • Posts: 445
Unable to Use this function
« on: February 02, 2013, 05:44:36 pm »
This is a delphi code but don't work in freepascal. What changes are required to make it run in freepascal.  :o

Code: [Select]
function flipAndCodeBytes (str: PAnsiChar; pos: Integer; flip: Integer; buf: PAnsiChar): String;
var i, j, k: Integer;
    p: Integer;
    c: AnsiChar;
    t: AnsiChar;
begin
   j := 0;
   k := 0;

   buf [0] := Chr(0);
   if (pos <= 0) then begin
      Result := buf;
      exit;
   end;

   if (j = 0) then begin
      p := 0;

      // First try to gather all characters representing hex digits only.
      j := 1;
      k := 0;
      buf[k] := Chr(0);
      i := pos;
      while (j <> 0) and (str[i] <> Chr(0)) do begin
//        c := tolower(str[i]);
  c:= lowercase(str[i]);
      if (isspace(c)) then c := Chr(0);

      inc(p);
      buf[k] :=  AnsiChar(Chr(Ord(buf[k]) shl 4));

        if ((c >= '0') and (c <= '9'))
          then buf[k] := AnsiChar(Chr(Ord(buf[k]) or Byte(Ord(c) - Ord('0'))))
        else if ((c >= 'a') and (c <= 'f'))
            then buf[k] := AnsiChar(Chr(Ord(buf[k]) or Byte(Ord(c) - Ord('a') + 10)))
          else begin
              j := 0;
            break;
          end;

      if (p = 2) then begin
        if ((buf[k] <> Chr(0)) and (not isprint(buf[k]))) then begin
           j := 0;
           break;
        end;
        inc(k);
        p := 0;
        buf[k] := Chr(0);
      end;
        inc(i);
      end;
   end;

   if (j = 0) then begin
      // There are non-digit characters, gather them as is.
      j := 1;
      k := 0;
      i := pos;
      while ( (j <> 0) and (str[i] <> Chr(0)) ) do begin
        c := str[i];

      if ( not isprint(c)) then begin
        j := 0;
        break;
      end;

      buf[(k)] := c;
        inc(k);
        inc(i);
      end;
   end;

   if (j = 0) then begin
      // The characters are not there or are not printable.
      k := 0;
   end;

   buf[k] := Chr(0);

   if (flip <> 0) then begin
      // Flip adjacent characters
      j := 0;
      while (j < k) do begin
        t := buf[j];
      buf[j] := buf[j + 1];
      buf[j + 1] := t;
        j := j + 2;
      end
   end;

   // Trim any beginning and end space
   i := -1;
   j := -1;
   k := 0;
   while (buf[k] <> Chr(0)) do begin
      if (not isspace(buf[k])) then begin
        if (i < 0) then i := k;
      j := k;
      end;
      inc(k);
   end;

   if ((i >= 0) and (j >= 0)) then begin
      k := i;
      while ( ( k <= j) and (buf[k] <> Chr(0)) ) do begin
         buf[k - i] := buf[k];
         inc(k);
      end;
      buf[k - i] := Chr(0);
   end;

   Result := buf;
end;
Holiday season is online now. :-)

Bart

  • Hero Member
  • *****
  • Posts: 3789
    • Bart en Mariska's Webstek
Re: Unable to Use this function
« Reply #1 on: February 02, 2013, 06:12:45 pm »
Well, look at the errors the compiler emits:
Error: Identifier not found "isspace"
Error: Identifier not found "isprint"

These functions are not defined, and I don't think they are standard delphi functions.
If you define these functions (you do know what they are supposed to do?) then the code compiles just fine.

Bart

Deepaak

  • Sr. Member
  • ****
  • Posts: 445
Re: Unable to Use this function
« Reply #2 on: February 03, 2013, 03:23:05 am »
Well, look at the errors the compiler emits:
Error: Identifier not found "isspace"
Error: Identifier not found "isprint"

These functions are not defined, and I don't think they are standard delphi functions.
If you define these functions (you do know what they are supposed to do?) then the code compiles just fine.

Bart

Yes i know, these are standard c/c++ functions from ctypes.h header. Is there any alternate function in lazarus. I had only found one function till time. It is
Code: [Select]
lowercase()
« Last Edit: February 03, 2013, 04:16:37 am by deepaak99 »
Holiday season is online now. :-)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8203
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Unable to Use this function
« Reply #3 on: February 03, 2013, 05:42:12 am »
Quote
Yes i know, these are standard c/c++ functions from ctypes.h header
Then how the original Delphi code calls them? Port their code or call from C runtime library? Anyway, those are easy functions, even just reading their description should be easy to make one:
http://www.cplusplus.com/reference/cctype/isspace/
http://www.cplusplus.com/reference/cctype/isprint/

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8366
  • FPC developer.
Re: Unable to Use this function
« Reply #4 on: February 03, 2013, 02:39:29 pm »
They might be added in some later (unicode) Delphi. All the ansichar typecast point in the direction that it was an unicode delphi.

What C/C++ does is irrelevant. Neither Delphi or FPC build on C runtimes afaik.

 

TinyPortal © 2005-2018