Skip to menu

Robotics with Object Pascal

All New K2

I asked Google's Gemini for parsing code.

2025.04.17 05:03

me Views:82

My Request : 

I need parsing code in Object Pascal if message starts with '|' character followed by 2 characters commands, then ':' to seperate command from following message which ends with '>' character

 

Gemini's answer : 

function ParseMessage(const Message: string; out Command: string; out Data: string): Boolean;

var

  PipePos: Integer;

  ColonPos: Integer;

  GreaterThanPos: Integer;

begin

  Result := False; // Initialize result to failure

  Command := '';

  Data := '';

 

  // Find the position of the '|' character

  PipePos := Pos('|', Message);

 

  // Check if '|' exists and is at the beginning of the message

  if PipePos = 1 then

  begin

    // Check if the message has at least 5 characters (|CC:D>)

    if Length(Message) >= 5 then

    begin

      // Extract the 2-character command

      Command := Copy(Message, 2, 2);

 

      // Find the position of the ':' character after the command

      ColonPos := Pos(':', Message);

 

      // Check if ':' exists and is after the command

      if ColonPos = 4 then

      begin

        // Find the position of the '>' character after the ':'

        GreaterThanPos := Pos('>', Message);

 

        // Check if '>' exists and is after the ':'

        if GreaterThanPos > ColonPos then

        begin

          // Extract the data between ':' and '>'

          Data := Copy(Message, ColonPos + 1, GreaterThanPos - ColonPos - 1);

          Result := True; // Parsing successful

        end;

      end;

    end;

  end;

end;

 

// Example Usage:

var

  Message1: string;

  Command1: string;

  Data1: string;

  Success1: Boolean;

 

  Message2: string;

  Command2: string;

  Data2: string;

  Success2: Boolean;

 

begin

  Message1 := '|AB:This is some data>';

  Success1 := ParseMessage(Message1, Command1, Data1);

 

  if Success1 then

  begin

    Writeln('Message: ', Message1);

    Writeln('Command: ', Command1);

    Writeln('Data: ', Data1);

  end else

  begin

    Writeln('Failed to parse message: ', Message1);

  end;

  Writeln;

 

  Message2 := 'Invalid Message';

  Success2 := ParseMessage(Message2, Command2, Data2);

 

  if Success2 then

  begin

    Writeln('Message: ', Message2);

    Writeln('Command: ', Command2);

    Writeln('Data: ', Data2);

  end else

  begin

    Writeln('Failed to parse message: ', Message2);

  end;

  Writeln;

 

  Message2 := '|CD:Another>Example'; // Extra characters after '>'

  Success2 := ParseMessage(Message2, Command2, Data2);

 

  if Success2 then

  begin

    Writeln('Message: ', Message2);

    Writeln('Command: ', Command2);

    Writeln('Data: ', Data2);

  end else

  begin

    Writeln('Failed to parse message: ', Message2);

  end;

  Writeln;

 

  Message2 := '|E:Short>'; // Command too short

  Success2 := ParseMessage(Message2, Command2, Data2);

 

  if Success2 then

  begin

    Writeln('Message: ', Message2);

    Writeln('Command: ', Command2);

    Writeln('Data: ', Data2);

  end else

  begin

    Writeln('Failed to parse message: ', Message2);

  end;

  Writeln;

 

  Message2 := '|FGData>'; // Missing ':'

  Success2 := ParseMessage(Message2, Command2, Data2);

 

  if Success2 then

  begin

    Writeln('Message: ', Message2);

    Writeln('Command: ', Command2);

    Writeln('Data: ', Data2);

  end else

  begin

    Writeln('Failed to parse message: ', Message2);

  end;

 

  Readln;

end.

=======================================================

Explanation:

  1. function ParseMessage(const Message: string; out Command: string; out Data: string): Boolean;:

    • This defines a function named ParseMessage that takes a constant string Message as input.
    • It has two out parameters: Command (to store the 2-character command) and Data (to store the message data).
    • It returns a Boolean value indicating whether the parsing was successful (True) or not (False).
  2. var PipePos: Integer; ColonPos: Integer; GreaterThanPos: Integer;:

    • Declares integer variables to store the positions of the '|', ':', and '>' characters within the message.
  3. Result := False; Command := ''; Data := '';:

    • Initializes the Result to False (assuming failure initially) and clears the Command and Data strings.
  4. PipePos := Pos('|', Message);:

    • Uses the Pos function to find the first occurrence of the '|' character in the Message.
  5. if PipePos = 1 then:

    • Checks if the '|' character was found and if it's at the beginning of the message (position 1).
  6. if Length(Message) >= 5 then:

    • Ensures the message has at least 5 characters to potentially contain the required structure (|CC:D>).
  7. Command := Copy(Message, 2, 2);:

    • If the '|' is at the beginning, this line extracts the 2 characters immediately following it (from position 2 with a length of 2) and stores them in the Command variable.
  8. ColonPos := Pos(':', Message);:

    • Finds the first occurrence of the ':' character in the Message.
  9. if ColonPos = 4 then:

    • Checks if the ':' character was found and if it's at the expected position (after the '|' and the 2-character command).
  10. GreaterThanPos := Pos('>', Message);:

    • Finds the first occurrence of the '>' character in the Message.
  11. if GreaterThanPos > ColonPos then:

    • Checks if the '>' character was found and if it appears after the ':' character.
  12. Data := Copy(Message, ColonPos + 1, GreaterThanPos - ColonPos - 1);:

    • If all the conditions are met, this line extracts the substring between the ':' (at ColonPos + 1) and the '>' (at GreaterThanPos - 1) and stores it in the Data variable.
  13. Result := True;:

    • Sets the Result to True to indicate successful parsing.
  14. end;:

    • Closes the conditional blocks.

How to Use:

  1. Copy and paste the ParseMessage function into your Object Pascal code (e.g., in the implementation section of a unit).
  2. Call the ParseMessage function with your message string and two variables to receive the parsed Command and Data.
  3. Check the boolean return value of the function to see if the parsing was successful.

The example usage in the begin...end. block demonstrates how to call the function and interpret its results.