Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Titan » External functions using universal charstrings(How to correctly cast universal charstrings into char *)
External functions using universal charstrings [message #1831036] Mon, 10 August 2020 12:42 Go to next message
Olaf Bergengruen is currently offline Olaf BergengruenFriend
Messages: 122
Registered: November 2018
Senior Member
Hi Titan experts,

I am using external functions to process XML documents via the excelent libxml2.

I have now the problems that the MCC TF160 ATSs at ETSI will use universal charstrings instead of charstrings.

Previously I used following cast operators, which worked fine:

/*
   * Returns given node in XML document referred via an xpath expression using libxml2
   */
CHARSTRING fx__XML__GetElement(const CHARSTRING& p_XMLDoc,
				 const CHARSTRING& p_Xpath) {
    
    const char *v_XMLDoc = (const char *) static_cast<const unsigned  char*>(p_XMLDoc);
    xmlChar *xPath =  (xmlChar *) static_cast<const  char*>(p_Xpath);

  ....
      }


This does not work if I have now UNIVERSAL CHARSTRINGS instead of CHARSTRINGS.
Besides, CHARSTRINGS are NULL terminated, whereas UNIVERSAL CHARSTRINGS seem to be not NULL terminated, so I can not just cast the pointers.

Any idea how I can adapt the code above using UNIVERSAL CHARSTRINGs?


The complete (faulty !) code is in the attached file.

Thanks for any hint.
Olaf Bergengruen





Re: External functions using universal charstrings [message #1831044 is a reply to message #1831036] Mon, 10 August 2020 14:35 Go to previous messageGo to next message
Gábor Szalai is currently offline Gábor SzalaiFriend
Messages: 133
Registered: December 2015
Senior Member
The unichar2oct used in the attached code is fine. It encodes the XML document using UTF8.

But in order to avoid memory leak and dangling pointer I suggest the following modification:

OCTETSRTING  v_XMLDOC_oct = unichar2oct(p_XMLDoc);
    const char *v_XMLDoc = (const char *) (const unsigned  char*)v_XMLDOC_oct;

[Updated on: Mon, 10 August 2020 14:36]

Report message to a moderator

Re: External functions using universal charstrings [message #1831046 is a reply to message #1831044] Mon, 10 August 2020 15:34 Go to previous messageGo to next message
Olaf Bergengruen is currently offline Olaf BergengruenFriend
Messages: 122
Registered: November 2018
Senior Member
It works, Gabor!!

Two questions:
- Now v_XMLDoc seems to be 0 terminated, i.e. a normal C string. Is this by luck ( I am compiling with the -g option) or is this the result of unichar2oct?
- Do I need to free v_XMLDOC_oct, or it is automatically freed when exiting the function?

Regards and thanks for the quick reply,
Olaf
Re: External functions using universal charstrings [message #1831068 is a reply to message #1831046] Tue, 11 August 2020 06:13 Go to previous messageGo to next message
Gábor Szalai is currently offline Gábor SzalaiFriend
Messages: 133
Registered: December 2015
Senior Member
It is just luck.
But you can specify the correct length of the UTF-8 string to the xmlReadMemory by calling v_XMLDOC_oct.lengthof()

As the result of unichar2oct is stored in a local variable, no free is needed.
Re: External functions using universal charstrings [message #1831069 is a reply to message #1831068] Tue, 11 August 2020 06:45 Go to previous message
Olaf Bergengruen is currently offline Olaf BergengruenFriend
Messages: 122
Registered: November 2018
Senior Member
Thanks , Gabor again.

Since v_XMLDOC_oct is not NULL terminated, I had to do additional work, which resulted in following code, which may not be the best, but it works.

   OCTETSRTING  v_XMLDOC_oct = unichar2oct(p_XMLDoc);
    size_t v_XMLDoc_len = v_XMLDOC_oct;.lengthof();
    char *v_XMLDoc;
   
   // Prepare v_XMLDoc
    v_XMLDoc = (char *) calloc( v_XMLDoc_len + 1, 1); // Allocate memory and init to 0
    memcpy((char *) v_XMLDoc,  (const char *) (const unsigned  char*) tmp_oct1,  v_XMLDoc_len);
   
   // Do the work

  // Don't forget
  free(v_XMLDoc);



So, now it is done, at least for the moment.

Regards,
Olaf
Previous Topic:TTCN-3 compiler error on activate() function
Next Topic:J1939 Synchronizing Parallel Test Components of different type
Goto Forum:
  


Current Time: Thu Apr 25 13:01:24 GMT 2024

Powered by FUDForum. Page generated in 0.04800 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top