= CPlusPlus = C++ programming language created by Bjarne Stroustrup == Sample app for Linux == {{{#!highlight c++ /* gcc test.cpp -lstdc++ -o test cpp test.cpp -o test g++ test.cpp -o test */ #include #include #include class Teste{ public: Teste(std::string xyz){ std::cout << xyz << "\r\n"; } }; int main(){ std::vector listInst = std::vector(); std::cout <<"Hello world \r\n"; std::ostringstream oss(std::ostringstream::out); int a = 1234; char xpto[16]="AAA BBB"; oss << a << xpto << "\r\n"; std::cout << oss.str(); //std::ostringstream ost = std::ostringstream(ostringstream::out); Teste x1("Ola aaaaaaa"); return 0; } /* To compile in gcc container g++ test.cpp -o test g++ (GCC) 8.2.0 */ #include #include #include #include #include class Teste{ public: Teste(std::string xyz){ std::cout << xyz << "\r\n"; } }; int main(){ std::vector listInst = std::vector(); std::cout <<"Hello world \r\n"; std::ostringstream oss(std::ostringstream::out); int a = 1234; char xpto[16]="AAA BBB"; oss << a << xpto << "\r\n"; std::cout << oss.str(); //std::ostringstream ost = std::ostringstream(ostringstream::out); Teste x1("Ola aaaaaaa"); srand( (unsigned) time(0) ); std::cout << rand() <<"\n"; std::cout << rand() <<"\n"; std::cout << std::to_string(1234) << " " << std::to_string(34.56) << "\n"; return 0; } }}} == dummy1.cpp == {{{#!highlight cpp /** g++ dummy1.cpp -o dummy1 */ #include #include using namespace std; int main(){ std::string input; std::cout <<"Hello world \r\n"; cout << ">"; std::cin >> input; std::cout << input << "!!!\n"; char x[128]; std::cout << ">"; cin >> x; std::cout << x << "???\n"; return 0; } }}} == challenge.cpp == {{{#!highlight cpp /** g++ challenge.cpp -o challenge -lstdc++ -lxerces-c -std=c++11 indent -kr -nut challenge.cpp */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace xercesc; class ErrorLoadXSDException:public exception { public: virtual const char *what() const throw() { return "Error loading XSD"; } }; class Data { public: virtual void set_string(string value) { this->value = value; } virtual string get_string() { return this->value; } virtual string to_xml() { string ret = ""; ret += ""; ret += ""; ret += this->value; ret += ""; ret += ""; return ret; } private: string value; }; class MyErrorHandler:public HandlerBase { public: void warning(const SAXParseException & exc) { throw exc; } void error(const SAXParseException & exc) { throw exc; } void fatalError(const SAXParseException & exc) { throw exc; } void resetErrors() { } }; void gen_xml(char *xsd_file) { int ret; cout << "XSD file " << xsd_file << "\n"; Data d = Data(); srand(time(0)); int val = rand(); d.set_string("asffgg" + std::to_string(val)); ofstream xml_out; xml_out.open("out.xml"); xml_out << d.to_xml() << "\n"; xml_out.close(); XercesDOMParser *parser = new XercesDOMParser(); MyErrorHandler *errorHandler = new MyErrorHandler(); try { Grammar *g = parser->loadGrammar(xsd_file, Grammar::SchemaGrammarType); if (g == NULL) { throw new ErrorLoadXSDException(); }; parser->setErrorHandler(errorHandler); parser->setDoSchema(true); parser->setDoNamespaces(true); parser->setValidationSchemaFullChecking(true); parser->setValidationScheme(XercesDOMParser::Val_Always); parser->parse("out.xml"); cout << "XML is okay !\n"; } catch(const XMLException & toCatch) { char *message = XMLString::transcode(toCatch.getMessage()); cout << "XMLException message is: \n" << message << "\n"; XMLString::release(&message); } catch(const DOMException & toCatch) { char *message = XMLString::transcode(toCatch.msg); cout << "DOMException message is: \n" << message << "\n"; XMLString::release(&message); } catch(const SAXParseException & toCatch) { char *message = XMLString::transcode(toCatch.getMessage()); cout << "SAXParseException message is: \n" << message << "\n"; XMLString::release(&message); } catch(const ErrorLoadXSDException * toCatch) { cout << "ErrorLoadXSDException message is: \n" << toCatch->what() << "\n"; } catch(...) { cout << "Unexpected Exception \n"; } delete errorHandler; delete parser; } void gen_json() { cout << "Print JSON" << "\n"; } int menu(char *file_xsd) { cout << "\nUsing " << file_xsd << "\n"; std::cout << "1) Generate XML\r\n"; std::cout << "2) Generate JSON\r\n"; std::cout << "3) Quit\r\n"; std::cout << ">"; string option; std::cin >> option; return std::stoi(option.c_str()); } int main(int argc, char **argv) { try { XMLPlatformUtils::Initialize(); } catch(const XMLException & toCatch) { return 1; } if (argc == 2) { int res = -1; while (res != 3) { res = menu(argv[1]); if (res == 1) gen_xml(argv[1]); if (res == 2) gen_json(); } } XMLPlatformUtils::Terminate(); return 0; } }}}