gen.cxx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <fstream>
  2. #include <sstream>
  3. #include <iostream>
  4. using namespace std;
  5. static const char* enums[] =
  6. {
  7. "romance",
  8. "fiction",
  9. "horror",
  10. "history",
  11. "philosophy"
  12. };
  13. int
  14. main (int argc, char* argv[])
  15. {
  16. if (argc != 3)
  17. {
  18. cerr << "usage: " << argv[0] << " <count> <output-file>" << endl;
  19. return 1;
  20. }
  21. unsigned long n (0);
  22. istringstream is (argv[1]);
  23. is >> n;
  24. if (n == 0)
  25. {
  26. cerr << "record count argument should be a positive number" << endl;
  27. return 1;
  28. }
  29. ofstream ofs (argv[2]);
  30. if (!ofs.is_open ())
  31. {
  32. cerr << "unable to open '" << argv[2] << "' in write mode" << endl;
  33. return 1;
  34. }
  35. ofs << "<t:root xmlns:t='test' " <<
  36. "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " <<
  37. "xsi:schemaLocation='test test.xsd'>";
  38. unsigned short ch (1), en (0);
  39. for (unsigned long i (0); i < n; ++i)
  40. {
  41. ofs << "<record orange=\"" << i << "\"";
  42. if (i % 2 == 0)
  43. ofs << " apple=\"true\"";
  44. ofs << ">"
  45. << "<int>42</int>"
  46. << "<double>42345.4232</double>"
  47. << "<name>name123_45</name>";
  48. if (i % 2 == 1)
  49. ofs << "<string>one two three</string>";
  50. ofs << "<choice" << ch << ">" << ch << " choice</choice" << ch << ">"
  51. << "<enum>" << enums[en] << "</enum>"
  52. << "</record>";
  53. if (++ch > 4)
  54. ch = 1;
  55. if (++en > 4)
  56. en = 0;
  57. }
  58. ofs << "</t:root>";
  59. }