fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. // ----- Definitions from the general purpose library -----
  6. // This is the C-based XML parser object
  7. struct XmlParser
  8. {
  9. int dummy;
  10. };
  11.  
  12. // The read function our parser uses to get more data
  13. typedef int (*xmlReadFn)(void* user_data, char* s, int len);
  14.  
  15. #define BUF_SIZE 5
  16.  
  17. // The parse function for our parser
  18. void xmlParse(XmlParser* parser, xmlReadFn read_fn, void* user_data)
  19. {
  20. char buf[BUF_SIZE];
  21. int read_len;
  22. do
  23. {
  24. read_len = read_fn(user_data, buf, BUF_SIZE);
  25. // Here I'm just printing out what we read, one chunk per line
  26. std::string str(buf, read_len);
  27. std::cout << str << std::endl;
  28. }
  29. while (read_len == BUF_SIZE);
  30. }
  31.  
  32. // ----- Our implementation -----
  33.  
  34. // This is our implementation of the xmlReadFn function
  35. int readFromStream(void* user_data, char* s, int len)
  36. {
  37. std::istream* str = (std::istream*)user_data;
  38. return str->readsome(s, len);
  39. }
  40.  
  41. int main()
  42. {
  43. // Allocate the parser
  44. XmlParser parser;
  45.  
  46. // Construct the stream we want to read from. Normally, this would come from a file
  47. std::stringstream str("The quick brown fox ran over the lazy dog.");
  48.  
  49. xmlParse(&parser, readFromStream, &str);
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0.02s 2860KB
stdin
Standard input is empty
stdout
The q
uick 
brown
 fox 
ran o
ver t
he la
zy do
g.