Url Parsing in COBOL

Here’s the COBOL translation of the URL parsing example:

Our program will demonstrate how to parse URLs in COBOL. While COBOL doesn’t have built-in URL parsing capabilities like more modern languages, we can implement basic parsing functionality.

IDENTIFICATION DIVISION.
PROGRAM-ID. URL-PARSING.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

DATA DIVISION.
FILE SECTION.

WORKING-STORAGE SECTION.
01 WS-URL PIC X(100) VALUE 
   "postgres://user:pass@host.com:5432/path?k=v#f".
01 WS-URL-PARTS.
   05 WS-SCHEME PIC X(20).
   05 WS-USER PIC X(20).
   05 WS-PASS PIC X(20).
   05 WS-HOST PIC X(20).
   05 WS-PORT PIC 9(5).
   05 WS-PATH PIC X(20).
   05 WS-QUERY PIC X(20).
   05 WS-FRAGMENT PIC X(20).
01 WS-TEMP PIC X(100).
01 WS-IDX PIC 99.

PROCEDURE DIVISION.
MAIN-PROCEDURE.
    DISPLAY "Original URL: " WS-URL

    PERFORM PARSE-SCHEME
    PERFORM PARSE-USER-PASS
    PERFORM PARSE-HOST-PORT
    PERFORM PARSE-PATH
    PERFORM PARSE-QUERY
    PERFORM PARSE-FRAGMENT

    DISPLAY "Scheme: " WS-SCHEME
    DISPLAY "User: " WS-USER
    DISPLAY "Password: " WS-PASS
    DISPLAY "Host: " WS-HOST
    DISPLAY "Port: " WS-PORT
    DISPLAY "Path: " WS-PATH
    DISPLAY "Query: " WS-QUERY
    DISPLAY "Fragment: " WS-FRAGMENT

    STOP RUN.

PARSE-SCHEME.
    MOVE FUNCTION SUBSTITUTE(WS-URL, "://", SPACE) TO WS-TEMP
    UNSTRING WS-TEMP DELIMITED BY SPACE
        INTO WS-SCHEME
             WS-TEMP.

PARSE-USER-PASS.
    MOVE FUNCTION SUBSTITUTE(WS-TEMP, "@", SPACE) TO WS-TEMP
    UNSTRING WS-TEMP DELIMITED BY SPACE
        INTO WS-TEMP
             WS-TEMP
    UNSTRING WS-TEMP DELIMITED BY ":"
        INTO WS-USER
             WS-PASS.

PARSE-HOST-PORT.
    UNSTRING WS-TEMP DELIMITED BY ":"
        INTO WS-HOST
             WS-PORT.

PARSE-PATH.
    MOVE FUNCTION SUBSTITUTE(WS-TEMP, "/", SPACE) TO WS-TEMP
    UNSTRING WS-TEMP DELIMITED BY SPACE
        INTO WS-TEMP
             WS-PATH.

PARSE-QUERY.
    MOVE FUNCTION SUBSTITUTE(WS-TEMP, "?", SPACE) TO WS-TEMP
    UNSTRING WS-TEMP DELIMITED BY SPACE
        INTO WS-TEMP
             WS-QUERY.

PARSE-FRAGMENT.
    MOVE FUNCTION SUBSTITUTE(WS-TEMP, "#", SPACE) TO WS-TEMP
    UNSTRING WS-TEMP DELIMITED BY SPACE
        INTO WS-TEMP
             WS-FRAGMENT.

This COBOL program demonstrates basic URL parsing. It breaks down a URL into its components: scheme, user, password, host, port, path, query, and fragment.

To run the program:

  1. Save the code in a file with a .cob extension (e.g., url-parsing.cob).
  2. Compile the program using a COBOL compiler.
  3. Run the compiled program.

The output will show the parsed components of the URL:

Original URL: postgres://user:pass@host.com:5432/path?k=v#f
Scheme: postgres
User: user
Password: pass
Host: host.com
Port: 05432
Path: path
Query: k=v
Fragment: f

Note that this is a basic implementation and may not handle all possible URL formats or edge cases. In a real-world scenario, you might want to use more robust parsing techniques or external libraries if available for your COBOL environment.