views:

63

answers:

1

I know this one has been asked before, however I can't seem to find a suitable solution, so I 'll state the problem:

I have a string of characters that is similar to an XML file. It's not an XML string, but it has opening and closing tags. All the information resides in one single line, for example:

<user>username</username>random data;some more random data<another tag>data</anothertag>randomdata;<mydata>myinfo</mydata>some more random data....

etc...

I am trying to read ONLY what's in between <mydata></mydata>. Any way to just parse this?

thanks, code is appreciated.

+2  A: 

I would just use strstr():

char * get_value(const char *input)
{
  const char *start, *end;

  if((start = strstr(input, "<mydata>")) != NULL)
  {
    start += strlen("<mydata>");
    if((end = strstr(start, "</mydata>")) != NULL)
    {
      char *out = malloc(end - start + 1);
      if(out != NULL)
      {
        memcpy(out, start, (end - start));
        out[end - start] = '\0';
        return out;
      }
    }
  }
  return NULL;
}

Note that the above is untested, written directly into the SO edit box. So, it's almost guaranteed to contain at least one off-by-one error.

unwind
thanks. the code only returns the 1st 12 characters of the string. I can't figure out why.
Jessica
hmmm... sometimes the string passed onto input contains a carriage return even though it is part of the same buffer (string). However I see how this affects strstr() into returning only the 1st 12 characters...
Jessica
it has to do with the memcpy. I just added a for loop and copy each char of start into out until i < end - start and it works.
Jessica
@Jessica: `memcpy()` isn't affected by carriage returns.
caf
@Jessica: You found a bug, the second argument to memcpy() was incorrect. It's fixed now.
unwind