Announcement

Collapse
No announcement yet.

Convert C to PowerBasic

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Convert C to PowerBasic

    Could anyone convert this C code to PowerBasic?

    Thanks!

    // this is just a template
    typedef struct {
    short len; // this is a 16-bit integer
    char data[1]; // Could be any number. This structure is
    // referenced by address, and C doesn't
    // do subscript checking, so ...
    // any number could be used here.
    } STRING;

    char test[] = "hello";

    // *str declares a pointer of type STRING
    // maxlen is the maximum length that is allocated for str->data
    __declspec(dllexport) int __stdcall foo1(STRING *str, int maxlen) {
    int len;
    char *s;

    // This code is only here to show how to access a string
    // It has no effect in this function
    len = str->len;
    s = str->data;

    // Change the passed in string

    len = min(strlen(test), maxlen); // check the maximum len
    memcpy(str->data, test, len); // copy in new data
    str->len = len; // set the length
    return 1; // return success
    }

  • #2
    Doesn't look like much more than:

    Code:
    FUNCTION foo1(inbuf AS STRING, maxlen AS LONG) AS LONG
    
        LOCAL test AS STRING
    
        test="hello"
    
        inbuf=LSET$(test, MIN(LEN(test), maxlen) USING " ")
        FUNCTION=%TRUE
    
    END FUNCTION
    ------------------
    Adam Drake
    Drake Software

    Comment


    • #3
      I greatly appreciate your help. However, I do have a question or two.
      I'm trying to write a DLL in PowerBasic. I have an API that I need to
      interface with. In this API, strings are defined as so: (in C terms):

      typedef struct {
      short len; // a 16-bit integer
      char string_data[maxlen]; // maxlen can be in 1..255
      } PString;

      When this API passes a string, it passes a pointer to the above
      structure.

      So, my questions are:

      1) Is the STRING type in PowerBasic (the one you listed in your
      function) of the identical format of the PString type listed above?

      2) Does the PowerBasic fragment, "inbuf AS STRING" cause inbuf to be
      passed by reference (as opposed to by value)?

      Thanks!

      Comment


      • #4
        1. No. In PB, it is a ASCIIZ string...

        Code:
        TYPE PString
             iLen As Word
             string_data As Asciiz * maxlen
        END TYPE
        2. It is by reference, you have to explicitly declare BYVAL parameters.

        ------------------
        contact me
        kgpsoftware.com - Free and Commercial Software
        kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

        Comment


        • #5
          Ok, I think I'm getting close :-)

          So, I'd want something like:

          TYPE PString
          iLen As Word
          string_data As Asciiz * 255
          END TYPE

          FUNCTION foo1(st AS PString, BYVAL maxlen AS LONG) AS LONG
          st.ILen = 5
          st.string_data = "hello"
          FUNCTION=%TRUE
          END FUNCTION

          Thanks for the help!

          Comment

          Working...
          X