CPlusPlus

C++ programming language created by Bjarne Stroustrup

Sample app for Linux

   1 /*
   2 gcc test.cpp -lstdc++ -o test
   3 cpp test.cpp -o test
   4 g++ test.cpp -o test
   5 */
   6 
   7 #include<vector>
   8 #include<iostream>
   9 #include<sstream>
  10 
  11 class Teste{
  12   public:
  13     Teste(std::string xyz){
  14       std::cout << xyz << "\r\n";
  15     }
  16 };
  17 
  18 int main(){
  19   std::vector<int> listInst = std::vector<int>(); 
  20   std::cout <<"Hello world \r\n";
  21   std::ostringstream oss(std::ostringstream::out);
  22   int a = 1234;
  23   char xpto[16]="AAA BBB";
  24   oss << a << xpto << "\r\n";
  25 
  26   std::cout << oss.str();
  27   //std::ostringstream ost = std::ostringstream(ostringstream::out);
  28   Teste x1("Ola aaaaaaa");
  29   return 0;
  30 }
  31 
  32 /*
  33 To compile in gcc container
  34 g++ test.cpp -o test
  35 
  36 g++ (GCC) 8.2.0
  37 */
  38 #include<vector>
  39 #include<iostream>
  40 #include<sstream>
  41 #include<random>
  42 #include<ctime>
  43 
  44 class Teste{
  45   public:
  46     Teste(std::string xyz){
  47       std::cout << xyz << "\r\n";
  48     }
  49 };
  50 
  51 int main(){
  52   std::vector<int> listInst = std::vector<int>();
  53   std::cout <<"Hello world \r\n";
  54   std::ostringstream oss(std::ostringstream::out);
  55   int a = 1234;
  56   char xpto[16]="AAA BBB";
  57   oss << a << xpto << "\r\n";
  58 
  59   std::cout << oss.str();
  60   //std::ostringstream ost = std::ostringstream(ostringstream::out);
  61   Teste x1("Ola aaaaaaa");
  62   srand( (unsigned) time(0)  );
  63   std::cout << rand() <<"\n";
  64   std::cout << rand() <<"\n";
  65   std::cout << std::to_string(1234) << " " << std::to_string(34.56) << "\n";
  66   return 0;
  67 }

dummy1.cpp

   1 /**
   2 g++ dummy1.cpp -o dummy1
   3 */
   4 #include <iostream>
   5 #include <string>
   6 using namespace std;
   7 
   8 int main(){
   9   std::string input;
  10   std::cout <<"Hello world \r\n";
  11   cout << ">";
  12   std::cin >> input;
  13   std::cout << input << "!!!\n";
  14   char x[128];
  15   std::cout << ">";
  16   cin >> x;
  17   std::cout << x << "???\n";
  18   return 0;
  19 }

challenge.cpp

   1 /**
   2 g++ challenge.cpp -o challenge -lstdc++ -lxerces-c -std=c++11
   3 indent -kr -nut challenge.cpp 
   4 */
   5 #include <iostream>
   6 #include <string>
   7 #include <typeinfo>
   8 #include <exception>
   9 #include <fstream>
  10 #include <random>
  11 
  12 #include <ctime>
  13 #include <cstdlib>
  14 
  15 #include <xercesc/util/PlatformUtils.hpp>
  16 #include <xercesc/parsers/XercesDOMParser.hpp>
  17 #include <xercesc/dom/DOM.hpp>
  18 #include <xercesc/sax/SAXParseException.hpp>
  19 #include <xercesc/sax/ErrorHandler.hpp>
  20 #include <xercesc/sax/HandlerBase.hpp>
  21 
  22 using namespace std;
  23 using namespace xercesc;
  24 
  25 class ErrorLoadXSDException:public exception {
  26   public:
  27     virtual const char *what() const throw() {
  28         return "Error loading XSD";
  29     }
  30 };
  31 
  32 class Data {
  33   public:
  34     virtual void set_string(string value) {
  35         this->value = value;
  36     } 
  37     
  38     virtual string get_string() {
  39         return this->value;
  40     }
  41 
  42     virtual string to_xml() {
  43         string ret = "";
  44         ret += "<bit:a xmlns:bit=\"http://bitarus.allowed.org/types\" ";
  45         ret +=
  46             "xsi:schemaLocation=\"http://bitarus.allowed.org/types test.xsd\" ";
  47         ret += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
  48         ret += "<bit:b>";
  49         ret += this->value;
  50         ret += "</bit:b>";
  51         ret += "</bit:a>";
  52         return ret;
  53     }
  54 
  55   private:
  56     string value;
  57 };
  58 
  59 class MyErrorHandler:public HandlerBase {
  60   public:
  61     void warning(const SAXParseException & exc) {
  62         throw exc;
  63     } 
  64     
  65 void error(const SAXParseException & exc) {
  66         throw exc;
  67     }
  68     void fatalError(const SAXParseException & exc) {
  69         throw exc;
  70     }
  71 
  72     void resetErrors() {
  73     }
  74 };
  75 
  76 void gen_xml(char *xsd_file)
  77 {
  78     int ret;
  79     cout << "XSD file " << xsd_file << "\n";
  80     Data d = Data();
  81     srand(time(0));
  82     int val = rand();
  83     d.set_string("asffgg" + std::to_string(val));
  84     ofstream xml_out;
  85     xml_out.open("out.xml");
  86     xml_out << d.to_xml() << "\n";
  87     xml_out.close();
  88 
  89     XercesDOMParser *parser = new XercesDOMParser();
  90     MyErrorHandler *errorHandler = new MyErrorHandler();
  91 
  92     try {
  93         Grammar *g =
  94             parser->loadGrammar(xsd_file, Grammar::SchemaGrammarType);
  95         if (g == NULL) {
  96             throw new ErrorLoadXSDException();
  97         };
  98 
  99         parser->setErrorHandler(errorHandler);
 100         parser->setDoSchema(true);
 101         parser->setDoNamespaces(true);
 102         parser->setValidationSchemaFullChecking(true);
 103         parser->setValidationScheme(XercesDOMParser::Val_Always);
 104         parser->parse("out.xml");
 105         cout << "XML is okay !\n";
 106     }
 107     catch(const XMLException & toCatch) {
 108         char *message = XMLString::transcode(toCatch.getMessage());
 109         cout << "XMLException message is: \n" << message << "\n";
 110         XMLString::release(&message);
 111     }
 112     catch(const DOMException & toCatch) {
 113         char *message = XMLString::transcode(toCatch.msg);
 114         cout << "DOMException message is: \n" << message << "\n";
 115         XMLString::release(&message);
 116     }
 117     catch(const SAXParseException & toCatch) {
 118         char *message = XMLString::transcode(toCatch.getMessage());
 119         cout << "SAXParseException message is: \n" << message << "\n";
 120         XMLString::release(&message);
 121     }
 122     catch(const ErrorLoadXSDException * toCatch) {
 123         cout << "ErrorLoadXSDException message is: \n" <<
 124             toCatch->what() << "\n";
 125     }
 126     catch(...) {
 127         cout << "Unexpected Exception \n";
 128     }
 129 
 130     delete errorHandler;
 131     delete parser;
 132 }
 133 
 134 void gen_json()
 135 {
 136     cout << "Print JSON" << "\n";
 137 }
 138 
 139 int menu(char *file_xsd)
 140 {
 141     cout << "\nUsing " << file_xsd << "\n";
 142     std::cout << "1) Generate XML\r\n";
 143     std::cout << "2) Generate JSON\r\n";
 144     std::cout << "3) Quit\r\n";
 145     std::cout << ">";
 146     string option;
 147     std::cin >> option;
 148     return std::stoi(option.c_str());
 149 }
 150 
 151 int main(int argc, char **argv)
 152 {
 153     try {
 154         XMLPlatformUtils::Initialize();
 155     }
 156     catch(const XMLException & toCatch) {
 157         return 1;
 158     }
 159 
 160     if (argc == 2) {
 161         int res = -1;
 162         while (res != 3) {
 163             res = menu(argv[1]);
 164             if (res == 1)
 165                 gen_xml(argv[1]);
 166             if (res == 2)
 167                 gen_json();
 168         }
 169     }
 170 
 171     XMLPlatformUtils::Terminate();
 172     return 0;
 173 }

CPlusPlus (last edited 2018-09-11 00:17:25 by localhost)