[xml] Problem with xmlTextReaderRead

2021-03-03 Thread Fabrizio Taretto
Hi guys,
I have a problem to parse the XML file in attach.

Using this code:

...
int options = XML_PARSE_NOENT;
xmlTextReaderPtr reader = xmlReaderForFile(iXMLFileName, gEncoding, 
options);
if (reader)
{
while (1)
{
int16 ret = xmlTextReaderRead(reader);
if (ret != 1)
break;
…

the xmlTextReaderRead function always turn me -1;

If I try to pass XML_PARSE_HUGE to xmlReaderForFile  options parameter, I can 
read the XML file only since to node 

  















  
  
  
  
  
  
  
  
  
  
  
  
  
  

  



___
xml mailing list, project page  http://xmlsoft.org/
xml@gnome.org
https://mail.gnome.org/mailman/listinfo/xml


Re: [xml] about xmlReadMemory()

2021-03-03 Thread nicolas bats via xml
thank you so much Nick,
it's crystal clear now :)

++
nicolas

Le mer. 3 mars 2021 à 11:12, Nick Wellnhofer  a écrit :

> On 03/03/2021 09:30, nicolas bats wrote:
> > Hi Nick,
> > I've experimented with xmlReadIO and it's cool.
> > this message just to check I'm doing right:
> > -I register an xmlInputReadCallback of type: size_t myCallback(void*
> context,
> > char* buffer, int length)
> > -I do my stuff in the callback and if data I use exceed the length of
> the
> > buffer, I realloc it.
> > Is this schema good?
> > Do I need to set size_t as the return type of myCallback?
>
> No, the read callback is supposed to fill the buffer with up to 'length'
> bytes. Try something like:
>
> typedef struct {
>  const char *ptr;
>  size_t remaining;
> } myContext;
>
> static int
> myReadCallback(void *vcontext, char *buffer, int len) {
>  myContext *context = vcontext;
>
>  if (context->remaining < len)
>  len = context->remaining;
>  memcpy(buffer, context->ptr, len);
>  context->ptr += len;
>  context->remaining -= len;
>
>  return len;
> }
>
> xmlDocPtr
> myReadMemory(const char *buffer, size_t size, const char *URL,
>   const char *encoding, int options) {
>  myContext context;
>
>  context.ptr = buffer;
>  context.remaining = size;
>
>  return xmlReadIO(myReadCallback, NULL, &context, URL, encoding,
> options);
> }
>
___
xml mailing list, project page  http://xmlsoft.org/
xml@gnome.org
https://mail.gnome.org/mailman/listinfo/xml


Re: [xml] about xmlReadMemory()

2021-03-03 Thread Nick Wellnhofer via xml

On 03/03/2021 09:30, nicolas bats wrote:

Hi Nick,
I've experimented with xmlReadIO and it's cool.
this message just to check I'm doing right:
-I register an xmlInputReadCallback of type: size_t myCallback(void* context, 
char* buffer, int length)
-I do my stuff in the callback and if data I use exceed the length of the 
buffer, I realloc it.

Is this schema good?
Do I need to set size_t as the return type of myCallback?


No, the read callback is supposed to fill the buffer with up to 'length' 
bytes. Try something like:


typedef struct {
const char *ptr;
size_t remaining;
} myContext;

static int
myReadCallback(void *vcontext, char *buffer, int len) {
myContext *context = vcontext;

if (context->remaining < len)
len = context->remaining;
memcpy(buffer, context->ptr, len);
context->ptr += len;
context->remaining -= len;

return len;
}

xmlDocPtr
myReadMemory(const char *buffer, size_t size, const char *URL,
 const char *encoding, int options) {
myContext context;

context.ptr = buffer;
context.remaining = size;

return xmlReadIO(myReadCallback, NULL, &context, URL, encoding, options);
}
___
xml mailing list, project page  http://xmlsoft.org/
xml@gnome.org
https://mail.gnome.org/mailman/listinfo/xml


Re: [xml] about xmlReadMemory()

2021-03-03 Thread nicolas bats via xml
Hi Nick,
I've experimented with xmlReadIO and it's cool.
this message just to check I'm doing right:
-I register an xmlInputReadCallback of type: size_t myCallback(void*
context, char* buffer, int length)
-I do my stuff in the callback and if data I use exceed the length of the
buffer, I realloc it.
Is this schema good?
Do I need to set size_t as the return type of myCallback?

thank you for your enlightenment .
best regards,
nicolas


Le mar. 2 mars 2021 à 19:32, nicolas bats  a écrit :

> Thank you Nick for your answer, I'll give a try to xmlReadIO.
> thanks again.
> ++
>
> Le mar. 2 mars 2021 à 18:02, Nick Wellnhofer  a
> écrit :
>
>> On 02/03/2021 16:28, nicolas bats via xml wrote:
>> > Hi,
>> > is there's a reason why xmlReadMemory
>> > () accepts
>> int as
>> > the size of the array to transform to xmlDocPtr.
>> > no doubt there's one...
>>
>> That's simply a design mistake. The API was created 20 years ago when
>> 64-bit
>> systems were rare.
>>
>> > and in that case how could I retrieve a xmlDocPtr from
>> > memory where size is type of size_t?
>>
>> If you want to process memory buffers larger than INT_MAX, you can use
>> xmlReadIO with a custom read callback that uses a size_t to store the
>> offset.
>>
>> Nick
>>
>
___
xml mailing list, project page  http://xmlsoft.org/
xml@gnome.org
https://mail.gnome.org/mailman/listinfo/xml