Facebook
From Trivial Agouti, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 128
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <algorithm>
  7.  
  8. #include "rapidxml-1.13/rapidxml.hpp"
  9. #include "rapidxml-1.13/rapidxml_print.hpp"
  10. #include "rapidxml-1.13/rapidxml_utils.hpp"
  11. #include <vector>
  12.  
  13. using namespace rapidxml;
  14. using namespace std;
  15.  
  16. string IntToString (int a)
  17. {
  18.     ostringstream temp;
  19.     temp<<a;
  20.     return temp.str();
  21. }
  22.  
  23. bool pobierz(xml_document <> *doc,char * Nazwa, char * rozszezenie,vector<char> ** buffer)
  24. {
  25.     string nazwa_doc = string(Nazwa) + string(rozszezenie);
  26.     char *cstr = new char[nazwa_doc.length() + 1];
  27.     strcpy(cstr, nazwa_doc.c_str());
  28.  
  29.     ifstream XmlF (cstr);
  30.     if (!XmlF.good()){cout << "file no exists"; exit(1);}
  31.         vector<char> *B =new vector<char>((istreambuf_iterator<char>(XmlF)), istreambuf_iterator<char>());
  32.         B->push_back('\0');
  33.         try{
  34.           doc->parse<0>(&(*B)[0]);
  35.         }
  36.      catch( parse_error p )
  37.     {
  38.         cout <<"|" <<p.what()<<"|";
  39.         cout << p.where<char>();
  40.         exit(1);
  41.         return false;
  42.     }
  43.     (*buffer) = B;
  44.     delete cstr;
  45.     return true;
  46. }
  47.  
  48. class MojXML
  49. {
  50. public :
  51.     xml_document<> * XML;
  52.     vector<char> * XMLvect;
  53.     MojXML(char * nazwa,char * rozszezenie)
  54.     {
  55.        XML = new xml_document<>();
  56.        if (!pobierz(XML,nazwa,rozszezenie,&XMLvect))
  57.             {cout << "cos poszlo nie tak" << nazwa<< ".xml";exit(1);}
  58.     }
  59.     ~MojXML()
  60.     {
  61.         XML->clear();
  62.         delete XML;
  63.         delete XMLvect;
  64.     }
  65.     xml_node <>* utworz_wierzcholek(string name)
  66.       {
  67.           char *Name = XML->allocate_string(name.c_str());
  68.           return XML->allocate_node( node_element,Name);
  69.       }
  70.     void dodaj_atrybut(xml_node<>* W,string attr, string value)
  71.       {
  72.           char *Attr = XML->allocate_string(attr.c_str());
  73.           char *Value = XML->allocate_string(value.c_str());
  74.           W->append_attribute(XML->allocate_attribute(Attr,Value));
  75.       }
  76.     void Zapisz_do_pliku(char * nazwa,char * rozszezenie)
  77.     {
  78.         string nazwa_doc = string(nazwa) + string(rozszezenie);
  79.         char *cstr = new char[nazwa_doc.length() + 1];
  80.         strcpy(cstr, nazwa_doc.c_str());
  81.         ofstream File;
  82.         File.open(cstr);
  83.         File << *XML;
  84.         File.flush();
  85.         File.close();
  86.         delete cstr;
  87.     }
  88.     void drukuj()
  89.     {
  90.         xml_node<>* temp = XML->first_node();
  91.        if (!temp) {cout << 1;exit(1);}
  92.        temp = temp->first_node();
  93.        while (temp)
  94.          {
  95.            cout << * temp;
  96.            temp = temp->next_sibling();
  97.          }
  98.     }
  99.    
  100. void WyswietlAtrybuty(xml_node <> * N )
  101. {
  102.     cout << "<" << N->name() <<">";
  103.     for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() ) // (7)
  104.     {
  105.         string name (atrybut->name(),atrybut->name_size());
  106.         string value (atrybut->value(),atrybut->value_size());
  107.         cout << "|" << name << "=" << value << "| ";
  108.     }
  109. }
  110.  
  111. void WyswietlPotomkow(xml_node <> * N)
  112. {
  113.     for( xml_node <>* node = N->first_node(); node; node = node->next_sibling() ) // (7)
  114.     {
  115.         cout << "<" << node->name() <<" ";
  116.         WyswietlAtrybuty(node);
  117.         cout << ">" << endl;
  118.     }
  119. }
  120.    
  121.     xml_node<> * znajdz_komiks_name(xml_node<>*start,string name)
  122.     {
  123.     xml_attribute<>*NAME;
  124.     NAME  =start->first_attribute("name");
  125.     if (NAME)
  126.     {
  127.          if (strcmp(NAME->value(),name.c_str())==0)
  128.             return start;
  129.     }
  130.     xml_node<>* RET;
  131.     for(xml_node<>* temp = start->first_node();temp;temp=temp->next_sibling())
  132.        {
  133.  
  134.            RET=znajdz_komiks_name(temp,name);
  135.            if (RET) return RET;
  136.        }
  137.        return NULL;
  138.     }
  139.  
  140.  
  141.  
  142. //////////////////////////////// nie dziala
  143. void dodaj_komiks(MojXML *komiksy, string gdzie_dodac, string rodzaj, string marka, string rocznik)
  144. {
  145.  
  146.     xml_node<> * bazy_danych = komiksy->XML->first_node();
  147.     xml_node<> * nowy_komiks = komiksy->utworz_wierzcholek(rodzaj);
  148.     komiksy->dodaj_atrybut(nowy_komiks, (string)("rocznik"), (string)(rocznik));
  149.     komiksy->dodaj_atrybut(nowy_komiks, (string)("marka"), (string)marka);
  150.     komiksy->dodaj_atrybut(nowy_komiks, (string)("rodzaj"), (string)(rodzaj));
  151.  
  152.     if( gdzie_dodac(bazy_danych,gdzie_dodac)    !=      NULL)
  153.     {
  154.     xml_node<>* tutaj_dodaj = gdzie_dodac(komiksy,gdzie_dodac);
  155.     tutaj_dodaj->append_node(nowy_komiks);
  156.     komiksy->Zapisz_do_pliku((char*)"biblioteka",(char*)".xml");
  157.     }
  158. }
  159. ////////////////////////////////
  160. void dodaj_komiks()
  161. {
  162.         ///znajdowanie komiksow
  163.         xml_node<>*K = znajdz_komiks_name(XML->first_node(),"komiks");
  164.  
  165.  
  166.        xml_node<> *P =XML->first_node("baza_danych")->first_node("komiksy")->next_sibling();
  167.         if (K)
  168.         {
  169.            cout <<(*K);
  170.               xml_node<> * Komiks = utworz_wierzcholek("komiks");
  171.  
  172.               ///ustawianie wlasciwego id
  173. {
  174.  
  175.    xml_attribute<>* ID=NULL, *T;
  176. if (XML->first_node())
  177. {
  178.    ID=XML->first_node()->first_attribute("max_id");
  179. }
  180.    if (ID==NULL) cout << "niepowodzenie";
  181.    cout << atoi(ID->value())<<"!!!!!!"<<endl;
  182.    int nr = atoi(ID->value());
  183.    nr++;
  184.    string temp =IntToString(nr);
  185.    char * NR = XML->allocate_string(temp.c_str());
  186.    ID->value(NR);
  187.       dodaj_atrybut(Komiks,(string)"id",IntToString(nr));
  188.         }
  189.  
  190.          dodaj_atrybut(Komiks,(string)"name",(string)"Myszojelen\"<>");
  191.          K->append_node(Komiks);
  192.     }
  193. }
  194. void dodaj_uzytkownika(){
  195.                 cout<<"elo"<<endl;
  196.         }
  197. void drukuj_komiksy(xml_node<>*K)
  198. {
  199.     if (K)
  200.     {
  201.         xml_attribute<>* n=K->first_attribute("name");
  202.  
  203.     if (n)
  204.     {
  205.         cout << "komiks "<< n->value();
  206.     }
  207.     }
  208. }
  209. void drukuj_czlowiek(xml_node<>*K)
  210. {
  211.     if (K)
  212.     {
  213.         xml_attribute<>* n=K->first_attribute("name");
  214.  
  215.     if (n)
  216.     {
  217.         cout << "czlowiek "<< n->value();
  218.     }
  219.     }
  220. }
  221. void wyszukajKomiks(xml_node<>*K,int i){
  222.     xml_attribute<>*ID;
  223.     ID  =K->first_attribute("id");
  224.     if (ID)
  225.     {
  226.          if (atoi(ID->value())==i)
  227.             drukuj_komiksy(K);
  228.     }
  229.     for(xml_node<>* temp = K->first_node();temp;temp=temp->next_sibling())
  230.         {      
  231.        wyszukajKomiks(temp,i);
  232.                                 }
  233.         }      
  234.  
  235. void wyszukaj_po_id(xml_node<>*N,string id)
  236. {
  237.     for(xml_node<>*komix=N->first_node()->first_node();komix;komix=komix->next_sibling())
  238.     {
  239.           for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() )
  240.         {
  241.             string name (atrybut->name(),atrybut->name_size());
  242.             string value (atrybut->value(),atrybut->value_size());
  243.             string ajdi = value;
  244.  
  245.             if(id==ajdi)
  246.                         {
  247.                                 drukuj_komiksy(komix);
  248.                         }
  249.         }
  250.  
  251.     }
  252. }    
  253.    
  254.            
  255. };
  256.  
  257.  
  258. class Menu
  259. {
  260.     int wybor;
  261. public:
  262.     bool pnt = true;
  263.     void rysuj()
  264.     {
  265.         system("cls");
  266.         cout<<"============================"<<endl;
  267.         cout<<"1.Dodaj komiks do biblioteki"<<endl;
  268.         cout<<"2.Dodaj uzytkownika do biblioteki"<<endl;
  269.         cout<<"3.Szukaj komiksu po id"<<endl;
  270.         cout<<"4.Wyświetl wszystko"<<endl;
  271.         cout<<"5.Wyswietl liste komiksow"<<endl;
  272.         cout<<"6.Wyswietl liste uzytkownikow"<<endl;
  273.         cout<<"7.Zapisz do pliku"<<endl;
  274.         cout<<"8.Wczytaj baze z pliku"<<endl;
  275.         cout<<"9.Koniec"<<endl;
  276.         cout<<"============================"<<endl;
  277.         cout<<"Wybor:";
  278.         cin>>wybor;
  279.         system("cls");
  280.     }
  281.     void wykonaj(MojXML *obj)
  282.     {
  283.         string nazwa;
  284.         switch(wybor)
  285.         {
  286.         case 1:
  287.             {
  288.                 obj->dodaj_komiks();   
  289.             }break;
  290.         case 2:
  291.             {
  292.                 obj->dodaj_uzytkownika();
  293.             }break;
  294. //       case 3:
  295. //            {
  296. //              int ID;
  297. //                cout<<"Jaki id komiksu:";
  298. //                cin>>ID;
  299. //                obj->wyszukaj_po_id();
  300. //                system("pause");
  301. //            }break;
  302.         case 4:
  303.             {
  304.                
  305.                
  306.             }break;
  307.         case 5:
  308.             {
  309.    //             obj->drukuj_komiksy();
  310.                 system("pause");
  311.             }break;
  312.         case 6:
  313.             {
  314.   //             obj->drukuj_czlowiek();
  315.                 system("pause");
  316.             }break;
  317.         case 7:
  318.             {
  319.     //            obj->zapiszDoPliku();
  320.             }break;
  321.         case 8:
  322.             {
  323.    //             obj->odczyt_z_pliku();
  324.             }break;
  325.         case 9:
  326.             {
  327.                 pnt = false;
  328.             }break;
  329.         default:
  330.             {
  331.                 cout<<"Error, polecenia nie znaleziono"<<endl;
  332.                 system("pause");
  333.             }break;
  334.         }
  335.     }
  336. };
  337. int main()
  338. {
  339. //mozna to wykorzystac do case z wyswietlaniem calej biblioteki
  340.   MojXML Biblioteka((char*)"biblioteka",(char*)".xml");
  341. cout << "**************caly xml ***********"<<endl;
  342.     Biblioteka.drukuj();
  343. cout << "**************caly xml ***********"<<endl;    
  344.     Menu *menu = new Menu;
  345.     while(menu->pnt){
  346.         menu->rysuj();
  347.         menu->wykonaj(&Biblioteka);
  348.     }
  349.     delete &Biblioteka;
  350.     delete menu;
  351.    
  352.   return 0;
  353. }
  354.