Read string resources with XtVaGetApplicationResources? - Xwindows

This is a discussion on Read string resources with XtVaGetApplicationResources? - Xwindows ; Hello all, at the moment i try to app-defaults support to my application. My problem is that i don't know how to read string values. Here is my sample code: #include #include #include #include #include #include typedef struct { int ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Read string resources with XtVaGetApplicationResources?

  1. Read string resources with XtVaGetApplicationResources?

    Hello all,

    at the moment i try to app-defaults support to my application. My
    problem is that i don't know how to read string values. Here is my
    sample code:


    #include
    #include
    #include
    #include
    #include
    #include

    typedef struct {
    int vncport;
    char macaddress[64];
    } device_config;


    static XtResource application_resources[] = {
    { "vncport","VNCPort",XtRInt, sizeof(int),
    XtOffsetOf(device_config,vncport),XtRImmediate, (XtPointer) 5900},
    { "macaddress","MACAddress",XtRString, 64,
    XtOffsetOf(device_config,macaddress), XtRStringArray,
    (XtPointer)"000000000000"}
    };

    device_config myres;

    int main (int argc, char **argv) {
    Widget top_wid;
    XtAppContext app;

    top_wid = XtVaAppInitialize(&app, "xres", NULL, 0 ,
    &argc, argv, NULL, NULL);

    memset(&myres,0,sizeof(myres));

    XtVaGetApplicationResources( top_wid, (XtPointer) &myres,
    application_resources,XtNumber(application_resourc es),NULL,
    0);

    printf("vncport: %d\nMAC Address: %s
    \n",myres.vncport,myres.macaddress);

    sprintf(myres.macaddress,"111111111111");
    printf("vncport: %d\nMAC Address: %s
    \n",myres.vncport,myres.macaddress);



    return EXIT_SUCCESS;
    }


    If i use this code the value in my app-default file
    (*MACAddress: ....) is ignored. But i can set value with sprintf. If i
    change the code like this:
    from char macaddress[64]; to char * macaddress;
    from { "macaddress","MACAddress",XtRString, 64,
    XtOffsetOf(device_config,macaddress), XtRStringArray,
    (XtPointer)"000000000000"} to
    { "macaddress","MACAddress",XtRString,sizeof(char *),
    XtOffsetOf(device_config,macaddress), XtRString,
    (XtPointer)"000000000000"}
    It looks all fine. The mac address which is in my app-defaults is
    used. If i now comment out the macaddress in the app-defaults the
    sprintf(myres.macaddress,"111111111111"); results in a Segmentation
    fault. What goes wrong here?

    Best regards,
    flo


  2. Re: Read string resources with XtVaGetApplicationResources?


    "mich1985" wrote in message
    news:1180072520.695495.294870@u30g2000hsc.googlegr oups.com...
    > Hello all,
    >
    > at the moment i try to app-defaults support to my application. My
    > problem is that i don't know how to read string values. Here is my
    > sample code:
    >
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > typedef struct {
    > int vncport;
    > char macaddress[64];
    > } device_config;
    >
    >
    > static XtResource application_resources[] = {
    > { "vncport","VNCPort",XtRInt, sizeof(int),
    > XtOffsetOf(device_config,vncport),XtRImmediate, (XtPointer) 5900},
    > { "macaddress","MACAddress",XtRString, 64,
    > XtOffsetOf(device_config,macaddress), XtRStringArray,
    > (XtPointer)"000000000000"}
    > };
    >
    > device_config myres;
    >
    > int main (int argc, char **argv) {
    > Widget top_wid;
    > XtAppContext app;
    >
    > top_wid = XtVaAppInitialize(&app, "xres", NULL, 0 ,
    > &argc, argv, NULL, NULL);
    >
    > memset(&myres,0,sizeof(myres));
    >
    > XtVaGetApplicationResources( top_wid, (XtPointer) &myres,
    > application_resources,XtNumber(application_resourc es),NULL,
    > 0);
    >
    > printf("vncport: %d\nMAC Address: %s
    > \n",myres.vncport,myres.macaddress);
    >
    > sprintf(myres.macaddress,"111111111111");
    > printf("vncport: %d\nMAC Address: %s
    > \n",myres.vncport,myres.macaddress);
    >
    >
    >
    > return EXIT_SUCCESS;
    > }
    >

    >
    > If i use this code the value in my app-default file
    > (*MACAddress: ....) is ignored. But i can set value with sprintf. If i
    > change the code like this:
    > from char macaddress[64]; to char * macaddress;
    > from { "macaddress","MACAddress",XtRString, 64,
    > XtOffsetOf(device_config,macaddress), XtRStringArray,
    > (XtPointer)"000000000000"} to
    > { "macaddress","MACAddress",XtRString,sizeof(char *),
    > XtOffsetOf(device_config,macaddress), XtRString,
    > (XtPointer)"000000000000"}
    > It looks all fine. The mac address which is in my app-defaults is
    > used. If i now comment out the macaddress in the app-defaults the
    > sprintf(myres.macaddress,"111111111111"); results in a Segmentation
    > fault. What goes wrong here?
    >


    The correct way is to decalar the variable type as "char *":
    typedef struct {
    int vncport;
    char *macaddress;
    } device_config;

    Then in your resources array, use sixeof(char *).

    Then, here is the important part:
    after your call to XtVaGetApplicationResources you
    must make a copy of the resource:

    myres.macaddress = XtNewString( myres.macaddress);
    (You need to make a copy of all pointer type resources)

    Be sure to free it when you no longer need it.
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Aero Stability and Controls Computing



  3. Re: Read string resources with XtVaGetApplicationResources?

    On 25 Mai, 16:19, "Fred Kleinschmidt"
    wrote:
    > "mich1985" wrote in message
    >
    > news:1180072520.695495.294870@u30g2000hsc.googlegr oups.com...
    >
    >
    >
    > > Hello all,

    >
    > > at the moment i try to app-defaults support to my application. My
    > > problem is that i don't know how to read string values. Here is my
    > > sample code:

    >
    > >
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include

    >
    > > typedef struct {
    > > int vncport;
    > > char macaddress[64];
    > > } device_config;

    >
    > > static XtResource application_resources[] = {
    > > { "vncport","VNCPort",XtRInt, sizeof(int),
    > > XtOffsetOf(device_config,vncport),XtRImmediate, (XtPointer) 5900},
    > > { "macaddress","MACAddress",XtRString, 64,
    > > XtOffsetOf(device_config,macaddress), XtRStringArray,
    > > (XtPointer)"000000000000"}
    > > };

    >
    > > device_config myres;

    >
    > > int main (int argc, char **argv) {
    > > Widget top_wid;
    > > XtAppContext app;

    >
    > > top_wid = XtVaAppInitialize(&app, "xres", NULL, 0 ,
    > > &argc, argv, NULL, NULL);

    >
    > > memset(&myres,0,sizeof(myres));

    >
    > > XtVaGetApplicationResources( top_wid, (XtPointer) &myres,
    > > application_resources,XtNumber(application_resourc es),NULL,
    > > 0);

    >
    > > printf("vncport: %d\nMAC Address: %s
    > > \n",myres.vncport,myres.macaddress);

    >
    > > sprintf(myres.macaddress,"111111111111");
    > > printf("vncport: %d\nMAC Address: %s
    > > \n",myres.vncport,myres.macaddress);

    >
    > > return EXIT_SUCCESS;
    > > }
    > >

    >
    > > If i use this code the value in my app-default file
    > > (*MACAddress: ....) is ignored. But i can set value with sprintf. If i
    > > change the code like this:
    > > from char macaddress[64]; to char * macaddress;
    > > from { "macaddress","MACAddress",XtRString, 64,
    > > XtOffsetOf(device_config,macaddress), XtRStringArray,
    > > (XtPointer)"000000000000"} to
    > > { "macaddress","MACAddress",XtRString,sizeof(char *),
    > > XtOffsetOf(device_config,macaddress), XtRString,
    > > (XtPointer)"000000000000"}
    > > It looks all fine. The mac address which is in my app-defaults is
    > > used. If i now comment out the macaddress in the app-defaults the
    > > sprintf(myres.macaddress,"111111111111"); results in a Segmentation
    > > fault. What goes wrong here?

    >
    > The correct way is to decalar the variable type as "char *":
    > typedef struct {
    > int vncport;
    > char *macaddress;
    > } device_config;
    >
    > Then in your resources array, use sixeof(char *).
    >
    > Then, here is the important part:
    > after your call to XtVaGetApplicationResources you
    > must make a copy of the resource:
    >
    > myres.macaddress = XtNewString( myres.macaddress);
    > (You need to make a copy of all pointer type resources)
    >
    > Be sure to free it when you no longer need it.
    > --
    > Fred L. Kleinschmidt
    > Boeing Associate Technical Fellow
    > Aero Stability and Controls Computing


    thx. It works.

    Regards,
    flo


+ Reply to Thread