Thread: Convert gsoap PHP program to C

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27

    Convert gsoap PHP program to C

    I am attempting to convert a PHP, which incorporates/includes gsoap, to C.
    I am having a issue with understanding the following:-
    Code:
    struct ns1__ArrayOfString
    {
    /// Size of array of char** is 0..unbounded
       $int                                  __sizestring                   0;
    /// Array char** of length 0..unbounded
        char**                               string                         0;    ///< Nillable pointer.
    };
    
    struct _ns1__GetMediaListResponse
    {
    /// Element GetMediaListResult of type "http://tempuri.org/":ArrayOfString.
        struct ns1__ArrayOfString*           GetMediaListResult             0;    ///< Optional element.
    };
    In my program I have defined
    Code:
    struct  _ns1__GetMediaListResponse* GetMediaListResponse;
    and I want to access the contents of the variable string

    I have tried various combinations of & * -> and . but not one works for me.

    In PHP this is achieved with
    Code:
    $result2 = $client->GetMediaList($params);
        $d = count($result2->GetMediaListResult->string);
        $MediaOne = $result2->GetMediaListResult->string[$d];
    Please tell me how I should write the following
    Code:
        n=sizeof(GetMediaListResponse.GetMediaListResult)/sizeof(GetMediaListResponse->GetMediaListResult[0].string);

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What does the C version of the struct look like?

    gg

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What you're doing with sizeof wont work, read this: Question 7.28.

    You don't really explain what kind of data string is supposed to be. I assume, since it's a pointer to pointer to char (i.e. double pointer) that it actually contains several different strings. So string[0] should point to the first string, string[1] to the second, etc. Usually, when using something like this, a NULL pointer is used to mark the end of the array of strings. So if string[17] is NULL, then you have 17 strings, string[0]..string[16]. There is no built-in code to count that for you, you must write it yourself (but it's a very simple loop). If you want to access the contents of one particular string, you must do something like string[0][4] to access the 5th character of the first string. To find the length of a single string, you can use the built-in function strlen (make sure you #include <string.h>).

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Why do you need to translate the PHP to C?
    What exactly are you trying to accomplish?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In C, if you have a pointer to a struct, it's -> and if you have a struct itself it's . -- GetMediaListResponse is a pointer, GetMediaListResult is a pointer, so you're looking at GetMediaListReponse->GetMediaListResult->string[0] through [whatever]. You (or somebody) are going to have to do all the memory allocation that PHP does behind the scenes, so you should have a call to malloc to get some space for string to point to (and then realloc when the number of strings change), and then each string itself is a call to malloc to get space for it.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> You (or somebody) are going to have to do all the memory ...
    In this case, gSoap will take care of the memory management of the response structure elements. The memory is released when the soap context is free'd.

    gg

  7. #7
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    When I use the -> as suggested by tabstop I get:-
    Code:
    dmmrp.c: In function ‘doUpdate’:
    dmmrp.c:82:36: error: invalid type argument of ‘->’ (have ‘struct _ns1__GetMediaListResponse’)
         n = sizeof(GetMediaListResponse->GetMediaListResult)/sizeof(GetMediaListResponse->GetMediaListResult->string);
                                        ^
    to answer Codeplug question
    Code:
    struct _ns1__GetMediaListResponse
    {
        struct ns1__ArrayOfString *GetMediaListResult;    /* SOAP 1.2 RPC return element (when namespace qualified) */    /* optional element of type ns1:ArrayOfString */
    };

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> dmmrp.c:82:36: error: invalid type argument of ‘->’ (have ‘struct _ns1__GetMediaListResponse’)
    The variable "GetMediaListResponse" is not a pointer, so you should use "." to access its members.

    What does the C version of ns1__ArrayOfString look like?

    gg

  9. #9
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    Thanks for that and the definition you asked for is:-
    Code:
    /// "http://tempuri.org/":ArrayOfString is a complexType.
    struct ns1__ArrayOfString
    {
    /// Size of array of char** is 0..unbounded
       $int                                  __sizestring                   0;
    /// Array char** of length 0..unbounded
        char**                               string                         0;    ///< Nillable pointer.
    };

  10. #10
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    The line:-
    Code:
        n = sizeof(GetMediaListResponse.GetMediaListResult)/sizeof(GetMediaListResponse.GetMediaListResult->string);
    result = sprintf(Message, "%s Line %d n = %d and First is  %s\n ",__FILE__,__LINE__, n, (char *)GetMediaListResponse.GetMediaListResult);
    LogMessage(Message);
    
    returns
    dmmrp.c Line 84 n = 1 and First is  (null)
    whereas there should be at least 4 items in that array.
    Last edited by straygrey; 10-28-2013 at 05:16 AM. Reason: Make it more clear

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm pretty sure the way you are calculating it, n will always equal one, since those sizeof's are constant (a pointer is always the same size, regardless of what it points to). You should probably be using the internal __sizestring member, as I imagine it's job is to keep track of how many strings you've got.

    As to (null), presumably whatever you're doing to initialize that struct isn't working.

  12. #12
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    am I correct in thinking that getting at __sizestring would be with -> or should I use . ?
    The way I see it currently is that __sizestring is the size of its associated string rather than the number of entries in the array. I want/need to know how many strings there are in the array.
    Once I know how many entries there are in the array I need to extract the value of each string to operate on that. This is where the NULL is confusing me. It should have a value as there should be at least 4 entries in that array.
    Last edited by straygrey; 10-28-2013 at 07:58 AM. Reason: Clarify

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    GetMediaListResult is a pointer to struct, so you use -> to access the members. I don't know why there is a dollar sign before the declaration of __sizestring, or why it starts with a double __, so just in case, here is how to access the members of string with and without using __sizestring:
    Code:
    int i;
    for (i = 0; i < GetMediaListResult->__sizestring; i++) {
        printf("%s\n", GetMediaResult->string[i]);
    }
    // alternatively
    for (i = 0; GetMediaListResult->string[i] != NULL; i++) {
        printf("%s\n", GetMediaResult->string[i]);
    }

  14. #14
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    To answer OogaBooga I am converting PHP to C because that is what the user wants.
    To answer Codeplug the _ns1__GetMediaListResponse is defined as
    Code:
    struct _ns1__GetMediaListResponse
    {
    /// Element GetMediaListResult of type "http://tempuri.org/":ArrayOfString.
        struct ns1__ArrayOfString*           GetMediaListResult             0;    ///< Optional element.
    }
    and to answer anduril462 surely _ns1__GetMediaListResponse needs to be included in your suggestion ?
    I have therefore tried
    Code:
        for (i = 0; GetMediaListResponse.GetMediaListResult->string[i] != NULL; i++) 
            {
            result = sprintf(Message, "%s\n", GetMediaListResponse.GetMediaResult->string[i]);
            LogMessage(Message);
            }
    but this does not compile
    Code:
    dmmrp.c: In function ‘doUpdate’:
    dmmrp.c:90:63: error: ‘struct _ns1__GetMediaListResponse’ has no member named ‘GetMediaResult’
             result = sprintf(Message, "%s\n", GetMediaListResponse.GetMediaResult->string[i]);
                                                                   ^
    and if I use -> it also does not compile
    Code:
    dmmrp.c: In function ‘doUpdate’:
    dmmrp.c:90:63: error: invalid type argument of ‘->’ (have ‘struct _ns1__GetMediaListResponse’)
             result = sprintf(Message, "%s\n", GetMediaListResponse->GetMediaResult->string[i]);
                                                                  ^
    Last edited by straygrey; 10-28-2013 at 09:08 AM.

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The "$" is a flag to the gSoap compiler (which generates the actual C code).
    gSOAP 2.8.16 User Guide

    I don't think gSoap will NULL terminate the array - so just use __sizestring to determine the array size.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gsoap
    By banbino in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2010, 09:55 AM
  2. Problem using GSOAP with OpenSSL on Linux
    By kuhn in forum C Programming
    Replies: 2
    Last Post: 05-19-2010, 05:14 PM
  3. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  4. Convert c++ program to c
    By tnt666 in forum C Programming
    Replies: 7
    Last Post: 12-01-2008, 09:23 AM
  5. Convert a C++ program?
    By unregistered in forum C Programming
    Replies: 4
    Last Post: 05-22-2002, 02:44 AM

Tags for this Thread