Menu

claude gave me the code to send strings back from C

J McNamara
2025-01-03
2025-01-03
  • J McNamara

    J McNamara - 2025-01-03

    Hi-

    Looks like I will be getting a claude pro plan.

    That was crazy. I can take strings and pass them into C and then get them back and display them appended based on what is passed in on the cobol end. That is what i was after. I will post the code when i get back from all the errands i have to run today.

    THANKS everyone for being so patient.

    jim

    Sent with Proton Mail secure email.

     
    👍
    1
    • J McNamara

      J McNamara - 2025-01-03

      Hi all-

      Simon gave me a concise answer on another post - thank you Simon !
      Here is a little longer answer i worked on with Claude.

      This will allow you to take integer dat and append it to a buffer string. I am working on some plans to get the fields converted to char and then take char input like pic X(1024) and then pass them into C lang and get back the buffer that is appended.

      You can see where I am headed with that logic. I can make a table from sql dat and pass it to whatever in Cobol.

      Some dat types like 9(2) or 9(4) take up a certain length of bytes. I won't know if the field is 9(2) or 9(12). So I have to contend with that in my next plan I am devising with Claude AI.

      It is not just integer dat but any dat I want to append to a char string in a buffer and get the results back.

      i want to make a table viewer with cobjapi.

      So here is what i have so far and how Claude helped me do it.
      Working-Storage Section.
       01 BUFFER-AREA.          05 BUFFER-STRING    PIC X(200) VALUE SPACES.
                05 BUFFER-LEN      PIC S9(4) COMP-5 VALUE 200.
             01 VALUE-TO-ADD       PIC S9(4) COMP-5 VALUE 42.
             01 RETURN-PTR         USAGE POINTER
       MOVE "hello" TO BUFFER-STRING

      Procedure Division.
                    DISPLAY "Initial buffer: " BUFFER-STRING

      *> Call C++ function
             CALL "accumulate_buffer"
                 USING BY REFERENCE RETURN-PTR
                       BY REFERENCE BUFFER-STRING
                       BY REFERENCE VALUE-TO-ADD
                       BY REFERENCE BUFFER-LEN
             END-CALL

      DISPLAY "After first call: " BUFFER-STRING

      *> Try with second value
             MOVE 123 TO VALUE-TO-ADD
             CALL "accumulate_buffer"
                 USING BY REFERENCE RETURN-PTR
                       BY REFERENCE BUFFER-STRING
                       BY REFERENCE VALUE-TO-ADD
                       BY REFERENCE BUFFER-LEN
             END-CALL

      DISPLAY "After second call: " BUFFER-STRING

      *> Test pointer dereferencing
             CALL "dereference_pointer"
                 USING BY REFERENCE BUFFER-STRING
             END-CALL

      If anyone wants how to make the DLL or the way to preprocess and compile please let me know.

      Below is the C++ code Claude did almost all of it. It is so fast in C++ it isn't even funny.

      I am very open with code that I am sharing at this point because the group has taught me several things and I am just a beginner.

      thanks, 
      jim

      include <stdio.h>#include <cstring></cstring></stdio.h>

      include <iostream></iostream>

      using namespace std;

      extern "C" {
          void dereference_pointer(char ptr) {
              std::cout << "Entering dereference_pointer" << std::endl;
              if (ptr != nullptr) {
                  std::cout << "Value at the address: " <<
      ptr << std::endl;
              } else {
                  std::cout << "Null pointer received!" << std::endl;
              }
              std::cout << "Exiting dereference_pointer" << std::endl;
          }

      char accumulate_buffer(char return_ptr, char buffer, int value, int size) {
              std::cout << "Entering accumulate_buffer" << std::endl;
              std::cout << "Raw buffer address: " << (void )buffer << std::endl;
              std::cout << "Raw value address: " << (void
      )value << std::endl;
              std::cout << "Raw size address: " << (void *)size << std::endl;

      // Clean the buffer of newlines and unwanted characters
              for (int i = 0; i < *size; i++) {
                  if (buffer[i] == '\n' || buffer[i] < ' ') {
                      buffer[i] = '\0';
                      break;
                  }
              }

      // Trim trailing spaces and nulls
              int actual_length = 0;
              while (actual_length < *size && buffer[actual_length] != '\0' && buffer[actual_length] != ' ') {
                  actual_length++;
              }
              buffer[actual_length] = '\0';

      std::cout << "Cleaned buffer: '" << buffer << "'" << std::endl;
              std::cout << "Actual length: " << actual_length << std::endl;
              std::cout << "value: " << (value ? value : -1) << std::endl;
              std::cout << "size: " << (size ?
      size : -1) << std::endl;

      if (!buffer || !value || !size || size <= 0) {
                  std::cerr << "Invalid input parameters!" << std::endl;
      return_ptr = nullptr;
                  return NULL;
              }

      // Calculate space needed
              char temp[20];
              snprintf(temp, sizeof(temp), " %d", *value);
              int needed_space = actual_length + strlen(temp);  // Space for current content + new value

      std::cout << "Space needed: " << needed_space << std::endl;

      if (needed_space >= size) {
                  std::cerr << "Buffer would overflow! Needed: " << needed_space << ", Have: " <<
      size << std::endl;
                  *return_ptr = nullptr;
                  return NULL;
              }

      // Append value with space prefix
              strcat(buffer, temp);

      std::cout << "Final buffer: '" << buffer << "'" << std::endl;
      return_ptr = buffer;
              return buffer;
          }
      }
      /

      int main() {
          char buffer[100] = "hello";
          int size = 100;
          int value1 = 42;
          int value2 = 123;
          char* return_ptr = nullptr;

      std::cout << "Initial buffer: " << buffer << std::endl;

      // Test first value
          accumulate_buffer(&return_ptr, buffer, &value1, &size);
          std::cout << "After first call: " << buffer << std::endl;

      // Test second value
          accumulate_buffer(&return_ptr, buffer, &value2, &size);
          std::cout << "After second call: " << buffer << std::endl;

      // Test pointer dereferencing
          dereference_pointer(buffer);

      return 0;
      }
      */

      Here is the output...
      Starting cobsub.cblHERE
      Initial buffer: hello

      Entering accumulate_buffer
      Raw buffer address: 0x7ff67aa9e080
      Raw value address: 0x7ff67aa9e160
      Raw size address: 0x7ff67aa9e148
      Cleaned buffer: 'hello'
      Actual length: 5
      value: 42
      size: 200
      Space needed: 8
      Final buffer: 'hello 42'
      After first call: hello 42

      Entering accumulate_buffer
      Raw buffer address: 0x7ff67aa9e080
      Raw value address: 0x7ff67aa9e160
      Raw size address: 0x7ff67aa9e148
      Cleaned buffer: 'hello'
      Actual length: 5
      value: 123
      size: 200
      Space needed: 9
      Final buffer: 'hello 123'
      After second call: hello 123

      Entering dereference_pointer
      Value at the address: h
      Exiting dereference_pointer

       
      • J McNamara

        J McNamara - 2025-01-03

        Sorry the formatting of my elementary items and group level went kaflooey. It is all there, but I wish when I switched from rich to plain text it didn't do that. I am going to try to find the setting to make it default that it is plain text so it will stop hi-jacking the code formats.

        thanks,
        jim

         
        • Simon Sobisch

          Simon Sobisch - 2025-01-03

          Best is to use the website (not mails) and surround all code by three backticks.

          ```c
          C code highlighted 
          ```
          
          ```cobolfree
          ADD 1 TO CODE
          ```
          

          .. and if you answer by mail, consider Not including the old mail.

           
          👍
          1

Anonymous
Anonymous

Add attachments
Cancel