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 }

   1 /*
   2 To compile in gcc container
   3 g++ test.cpp -o test
   4 
   5 g++ (GCC) 8.2.0
   6 */
   7 #include<vector>
   8 #include<iostream>
   9 #include<sstream>
  10 #include<random>
  11 #include<ctime>
  12 
  13 class Teste{
  14   public:
  15     Teste(std::string xyz){
  16       std::cout << xyz << "\r\n";
  17     }
  18 };
  19 
  20 int main(){
  21   std::vector<int> listInst = std::vector<int>();
  22   std::cout <<"Hello world \r\n";
  23   std::ostringstream oss(std::ostringstream::out);
  24   int a = 1234;
  25   char xpto[16]="AAA BBB";
  26   oss << a << xpto << "\r\n";
  27 
  28   std::cout << oss.str();
  29   //std::ostringstream ost = std::ostringstream(ostringstream::out);
  30   Teste x1("Ola aaaaaaa");
  31   srand( (unsigned) time(0)  );
  32   std::cout << rand() <<"\n";
  33   std::cout << rand() <<"\n";
  34   std::cout << std::to_string(1234) << " " << std::to_string(34.56) << "\n";
  35   return 0;
  36 }

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 -lxml2
   3 g++ challenge.cpp -o challenge -lstdc++ -lxml2 -I/usr/include/libxml2
   4 g++ challenge.cpp -o challenge -lstdc++ -lxerces-c 
   5 // added random ...
   6 g++ challenge.cpp -o challenge -lstdc++ -lxerces-c -std=c++11
   7 indent -kr -nut challenge.cpp 
   8 */
   9 #include <iostream>
  10 #include <string>
  11 #include <typeinfo>
  12 #include <exception>
  13 #include <fstream>
  14 #include <random>
  15 #include <ctime>
  16 #include <cstdlib>
  17 #include <cstdio>
  18 #include <xercesc/util/PlatformUtils.hpp>
  19 #include <xercesc/parsers/XercesDOMParser.hpp>
  20 #include <xercesc/dom/DOM.hpp>
  21 #include <xercesc/sax/SAXParseException.hpp>
  22 #include <xercesc/sax/HandlerBase.hpp>
  23 #include <xercesc/dom/DOM.hpp>
  24 #include <xercesc/dom/DOMDocument.hpp>
  25 #include <xercesc/dom/DOMNodeList.hpp>
  26 #include <xercesc/dom/DOMNode.hpp>
  27 using namespace std;
  28 using namespace xercesc;
  29 
  30 const int NO_OPTION=-1;
  31 const int GEN_XML=1;
  32 const int GEN_JSON=2;
  33 const int EXIT=3;
  34 const char* OUT_XML="out.xml";
  35 
  36 class ErrorLoadXSDException:public exception {
  37   public:
  38     virtual const char *what() const throw() {
  39         return "Error loading XSD";
  40     }
  41 };
  42 
  43 class OutOfRangeException:public exception {
  44   public:
  45       
  46     OutOfRangeException(int value){
  47         this->value = value;  
  48         this->message = new char[128];
  49     }
  50     
  51     virtual const char *what() const throw() {
  52         char ret[128];
  53         sprintf(this->message,"%d out of range!",this->value);
  54         return this->message;
  55     }
  56     
  57   private:
  58     int value;
  59     char* message;
  60 };
  61 
  62 class Data {
  63   public:
  64       
  65       Data(){
  66           this->b = "";    
  67       }  
  68     
  69       virtual void set_string(string value) {
  70           this->b = value;
  71       } 
  72     
  73       virtual string get_string() {
  74           return this->b;
  75       }
  76 
  77       virtual string to_xml(const char* xsd_file) {
  78           string ret = "";
  79           ret += "<bit:a xmlns:bit=\"http://bitarus.allowed.org/types\" ";
  80           ret += "xsi:schemaLocation=\"http://bitarus.allowed.org/types " + string(xsd_file) + "\" ";
  81           ret += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
  82           ret += "<bit:b>";
  83           ret += this->b;
  84           ret += "</bit:b>\n";
  85           ret += "</bit:a>\n";
  86           return ret;
  87       }
  88 
  89       virtual string to_json() {
  90           string ret = "";
  91           ret += "{\"a\": \n";
  92           ret += "  {\"b\":\"";
  93           ret += this->b;
  94           ret += "\"} \n"; 
  95           ret += "} ";  
  96           return ret;
  97       }      
  98     
  99       static Data* from_xml(const char* xml_file){
 100           Data* ret = new Data();
 101           try{
 102               XercesDOMParser *parser = new XercesDOMParser;
 103               parser->setValidationScheme(XercesDOMParser::Val_Never);
 104               parser->parse(xml_file);
 105               DOMDocument *doc = parser->getDocument();        
 106               DOMElement* root = doc->getDocumentElement();        
 107               DOMNodeList* nl = root->getElementsByTagName( XMLString::transcode("bit:b") );
 108             
 109               if(nl->getLength() > 0){ 
 110                   DOMNode* dn = nl->item(0);
 111                   string v = string(XMLString::transcode(dn->getLastChild()->getNodeValue()) );
 112                   ret->set_string(v);
 113               }            
 114           }
 115           catch(const DOMException& toCatch) {
 116               char *message = XMLString::transcode(toCatch.msg);
 117               cout << "DOMException message is:" << message << endl;
 118               XMLString::release(&message);
 119           }
 120           catch(...){
 121               cout << "Ups" << endl;
 122           }
 123         
 124           return ret;
 125     }
 126     
 127   private:
 128     string b;
 129 };
 130 
 131 class MyErrorHandler:public HandlerBase {
 132   public:
 133     void warning(const SAXParseException& exc) {
 134         throw exc;
 135     } 
 136     
 137     void error(const SAXParseException& exc) {
 138         throw exc;
 139     }
 140     
 141     void fatalError(const SAXParseException & exc) {
 142         throw exc;
 143     }
 144 
 145     void resetErrors() {
 146     }
 147 };
 148 
 149 void check_xml(const char* xsd_file, const char* out_file){
 150     XercesDOMParser *parser = new XercesDOMParser();
 151     MyErrorHandler *errorHandler = new MyErrorHandler();
 152 
 153     try {
 154         Grammar *g = parser->loadGrammar(xsd_file, Grammar::SchemaGrammarType);
 155         if (g == NULL) {
 156             throw new ErrorLoadXSDException();
 157         }
 158 
 159         parser->setErrorHandler(errorHandler);
 160         parser->setDoSchema(true);
 161         parser->setDoNamespaces(true);
 162         parser->setValidationSchemaFullChecking(true);
 163         parser->setValidationScheme(XercesDOMParser::Val_Always);
 164         parser->parse(out_file);
 165         cout << "XML is okay !" << endl;
 166     }
 167     catch(const XMLException& toCatch) {
 168         char *message = XMLString::transcode(toCatch.getMessage());
 169         cout << "XMLException message is: " << message << endl;
 170         XMLString::release(&message);
 171     }
 172     catch(const DOMException& toCatch) {
 173         char *message = XMLString::transcode(toCatch.msg);
 174         cout << "DOMException message is: " << message << endl;
 175         XMLString::release(&message);
 176     }
 177     catch(const SAXParseException& toCatch) {
 178         char *message = XMLString::transcode(toCatch.getMessage());
 179         cout << "SAXParseException message is: " << message << endl;
 180         XMLString::release(&message);
 181     }
 182     catch(const ErrorLoadXSDException* toCatch) {
 183         cout << "ErrorLoadXSDException message is: " << toCatch->what() << endl;
 184     }
 185     catch(...) {
 186         cout << "Unexpected Exception \n";
 187     }
 188 
 189     delete errorHandler;
 190     delete parser;    
 191 }
 192 
 193 void gen_xml(const char*xsd_file, const char* out_file, Data* d){
 194     int ret;
 195     cout << "XSD file " << xsd_file << endl;
 196     ofstream xml_out;
 197     xml_out.open(out_file);
 198     xml_out << d->to_xml(xsd_file) << endl;
 199     xml_out.close();
 200     cout << "Generated XML file " << out_file << endl; 
 201     check_xml(xsd_file,out_file);
 202 }
 203 
 204 void gen_json(const char* out_file, Data* d) {
 205     ofstream json_out;
 206     json_out.open(out_file);
 207     json_out << d->to_json() << endl;
 208     json_out.close();
 209     cout << "Generated JSON file " << out_file << endl; 
 210 }
 211 
 212 int menu(char* file_xsd) {
 213     cout << endl << "Using " << file_xsd << endl;
 214     std::cout << "1) Generate XML" << endl;
 215     std::cout << "2) Generate JSON" << endl;
 216     std::cout << "3) Quit" << std::endl;
 217     std::cout << ">";
 218     string option;
 219     std::cin >> option;
 220     int ret=-1;
 221     try{
 222         ret = std::stoi(option.c_str());
 223         if(ret<GEN_XML || ret >EXIT){            
 224             throw OutOfRangeException(ret);
 225         }
 226     }
 227     catch(OutOfRangeException ex){
 228         cout << ex.what() <<"\n"; 
 229     }
 230     catch(...){
 231         std::cout << "Invalid option selected!" << endl;
 232     }
 233     return ret;
 234 }
 235 
 236 int main(int argc, char** argv) {
 237     try {
 238         XMLPlatformUtils::Initialize();
 239     }
 240     catch(const XMLException& toCatch) {
 241         return 1;
 242     }
 243 
 244     if (argc == 2) {
 245         int res = NO_OPTION;
 246         while (res != EXIT) {
 247             
 248             res = menu(argv[1]);
 249             
 250             if (res == GEN_XML){
 251                 Data d = Data();
 252                 srand(time(0));
 253                 int val = rand();                
 254                 d.set_string("xml" + std::to_string(val) );
 255                 gen_xml(argv[1],OUT_XML,&d);                
 256             }
 257             
 258             if (res == GEN_JSON){
 259                 Data* d=Data::from_xml(OUT_XML);
 260                 gen_json("out.json",d);                
 261             }           
 262         }
 263     }
 264 
 265     XMLPlatformUtils::Terminate();
 266     return 0;
 267 }

CPlusPlus (last edited 2018-10-13 18:50:49 by localhost)