CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Parsing

  1. #1
    Join Date
    Jul 2004
    Posts
    12

    Parsing

    Code:
    CString ID_Parser(CString StrText)
    {
    	int Blah1 = strstr(StrText, "<td><font face=arial size=\"-1\" color=\"#660000\"><b>");
    	int Blah2 = strstr(StrText, "</b></font>");
    
    	StrText = StrText.Mid(0, Blah1 + 50);
    	StrText = StrText.Left(Blah2 - 1);
    
    	return StrText;
    }
    This obviously doesnt work... Im trying to write a function to parse the ID from a yahoo profile, this is what I have so far but isnt working because of strstr(), Im wondering if there is an alternative function (maybe in the CString class) that will return the postition of a certain string, like vb's InStr ? or if you can show me another method it would be much appreciated!

    Thanks

  2. #2
    Join Date
    Aug 2002
    Posts
    757
    Hi, leet3r,
    What do you mean by "it doesn't work because of strstr()"?
    It is an MS extension, but to say, that it doesn't work...

    Thank you.
    Last edited by OneEyeMan; July 31st, 2004 at 10:01 PM.

  3. #3
    Join Date
    Oct 1999
    Location
    Missouri, USA
    Posts
    453
    Im wondering if there is an alternative function (maybe in the CString class) that will return the postition of a certain string...
    int CString::Find( LPCTSTR pstr, int nStart ) will do that.

    Return Value:
    The zero-based index of the first character in this CString object that matches the requested substring or characters; -1 if the substring or character is not found.
    If you need an answer, don't forget to try this link:
    http://www.codeguru.com/forum/search.php?
    __________________

  4. #4
    Join Date
    Jul 2004
    Posts
    12
    Hi,

    OneEyedMan: I meant that the problem with my function is the use of strstr() if that makes any sense :P

    Im going to see what I can do with CString::Find now, thanks! much appreciated.

  5. #5
    Join Date
    Jun 2004
    Location
    England
    Posts
    90
    There is no problem with strstr.

    The trick is to actually get the character position from return
    here from msdn:

    Code:
    printf( "String to be searched:\n   %s\n", string );
    printf( "   %s\n   %s\n\n", fmt1, fmt2 );
    pdest = strstr( string, str );
    result = (int)(pdest - string + 1);
    if ( pdest != NULL )
      printf( "%s found at position %d\n", str, result );
    else
      printf( "%s not found\n", str );

    cheers
    The most knowledgeable people are those who know that they know nothing.

  6. #6
    Join Date
    Jul 2004
    Posts
    12
    Here is what I have so far..

    Code:
    CString ID_Parser(CString StrText)
    {
    	if(strstr(StrText, "Yahoo! ID:"))
    	{
    		char *Blah1 = strstr(StrText, "<td><font face=arial size=\"-1\" color=\"#660000\"><b>");
    		char *Blah2 = strstr(StrText, "</b></font>");
    
    		StrText = StrText.Mid(0, (int)Blah1 + 50);
    		StrText = StrText.Left((int)Blah2 - 1);
    
    		return StrText;
    	
    	}else{
    
    		return "Not Found.";
    	}
    }
    Well it isnt erroring out now, and I added MessageBoxA to see what Blah1 and Blah2 looked like and it looks good, only problem is that it is still returning StrText in full as it was passed to the function Any reason why ?

    P.S. Thanks for the help so far ppl, I'm closer to completing it now

  7. #7
    Join Date
    Aug 2002
    Posts
    757
    Hi, leet3r,
    Are you checking for 'not found' case in your code?

    Thank you.

  8. #8
    Join Date
    Jul 2004
    Posts
    12
    if(strstr(StrText, "Yahoo! ID:"))
    {
    ...

    }else{

    ...
    }
    Do you mean that ?

    It's definately a parsing problem because I saved what winsock returns from profiles.yahoo.com/username and its grabbing the right amount of data.

    Thanks

  9. #9
    Join Date
    Aug 2002
    Posts
    757
    Hi, leet3r,
    Do you know the values of the StrText before and after the first call of strstr()? Maybe you have some garbage in it...

    Thank you.

  10. #10
    Join Date
    Jul 2004
    Posts
    12
    Before. It is the whole profile, example: http://profiles.yahoo.com/birst
    After. It returns the whole data minus everything before <font blah blah.

    Is there another solution to parsing than this ? I haven't found one single article on parsing text, I heard that there is a parsing example in the microsoft code samples, but I'm not sure it's worth the download if there isnt...

    Thanks

  11. #11
    Join Date
    Jul 2004
    Posts
    12
    Guess no-one can answer this, thanks anyway

  12. #12
    Join Date
    Jul 2004
    Posts
    12

    Talking

    For anyone interested, I found a solution to this problem, it is very simple!

    sscanf

    I cant believe the hassle I went through when it was this simple all along..

  13. #13
    Join Date
    Oct 1999
    Location
    Missouri, USA
    Posts
    453
    leet3r, before I knew you found sscanf, I quickly put together this code.
    It assumes that after "Yahoo! ID:" there is one "<b>" just before the name,
    and "</b>" after the name.

    It worked for the Birst example.


    Code:
    CString ID_Parser(CString strText)
    {
    	
    	int nStartIndex, nEndIndex;
    
    	if(nStartIndex = strText.Find("Yahoo! ID:",0) != -1)
    	{
    		      
    	  nStartIndex = strText.Find("<b>", nStartIndex);
    
    	  nEndIndex = strText.Find("</b>",  nStartIndex );
    
    	  strText = strText.Mid(nStartIndex+3, nEndIndex - (nStartIndex+3));
           
    	  return strText;
    	
    	}
    	else
    	{
    
    		return "Not Found.";
    	}
    }
    If you need an answer, don't forget to try this link:
    http://www.codeguru.com/forum/search.php?
    __________________

  14. #14
    Join Date
    Jul 2004
    Posts
    12
    Thanks alot! I will use that for parsing things like profiles. sscanf was just for parsing a cookie, that function is much better.

    Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured