Facebook
From Speedy Lion, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 114
  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 dodanie uzytkownika i komiksu
  143. void dodaj_komiks(MojXML *komiksy, string gdzie_dodac_, string autor, string uniwersum, string name)
  144. {
  145.  
  146.     xml_node<> * bazy_danych = komiksy->XML->first_node();
  147.     xml_node<> *nowy_komiks = komiksy->utworz_wierzcholek(autor);
  148.         komiksy->dodaj_atrybut(nowy_komiks, (string)("nazwa"), (string)(name));
  149.     komiksy->dodaj_atrybut(nowy_komiks, (string)("uniwersum"), (string)(uniwersum));
  150.     komiksy->dodaj_atrybut(nowy_komiks, (string)("autor"), (string)autor);
  151.  
  152.     if(gdzie_dodac_(bazy_danych,gdzie_dodac_P)!=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. void dodaj_uzytkownika(MojXML *ludzie, string gdzie_dodac_, string rocznik, string nazwisko, string imie)
  160. {
  161.  
  162.     xml_node<> * bazy_danych = ludzie->XML->first_node();
  163.     xml_node<> * nowy_czlowiek = ludzie->utworz_wierzcholek(rocznik);
  164.     ludzie->dodaj_atrybut(nowy_czlowiek, (string)("imie"), (string)(name));
  165.     ludzie->dodaj_atrybut(nowy_czlowiek, (string)("nazwisko"), (string)nazwisko);
  166.         ludzie->dodaj_atrybut(nowy_czlowiek, (string)("rocznik"), (string)rocznik);
  167.        
  168.     if(string gdzie_dodac_(bazy_danych,gdzie_dodac_)    !=      NULL)
  169.     {
  170.     xml_node<>* tutaj_dodaj = gdzie_dodac_(ludzie,gdzie_dodac_);
  171.     tutaj_dodaj->append_node(nowy_czlowiek);
  172.     ludzie->Zapisz_do_pliku((char*)"biblioteka",(char*)".xml");
  173.     }
  174. }
  175. //////////////////////////////////
  176.  
  177.  
  178. string pobierz_wartosc_atrybutu(xml_node <> * N, string nazwa)
  179. {
  180.     for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() )
  181.     {
  182.         string name (atrybut->name(),atrybut->name_size());
  183.         string value (atrybut->value(),atrybut->value_size());
  184.         if( name == nazwa )
  185.         {
  186.             return value;
  187.         }
  188.     }
  189. }
  190.  
  191. void wyswietl_jeden_komiks(xml_node<> *N)
  192. {
  193.      for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() )
  194.      {
  195.         string name (atrybut->name(),atrybut->name_size());
  196.         string value (atrybut->value(),atrybut->value_size());
  197.         if (name == "uniwersum")
  198.         {
  199.             cout<<" jego universum to:"<<value;
  200.         }
  201.         else if (name == "autor")
  202.         {
  203.             cout<<" i jego autorem jest: " << value <<" ";
  204.         }
  205.  
  206.      }
  207. }
  208.  
  209. void drukuj_komiksy(xml_node <> *N, string rodzaj)
  210. {
  211.     int czy_istnieje = 0;
  212.     string nazwa;      
  213.                 rodzaj=nazwa;
  214.     for(xml_node <> *node = N->first_node() ; node ; node = node->next_sibling())
  215.     {
  216.         if(pobierz_wartosc_atrybutu(node , "name") == rodzaj)
  217.         {
  218.             czy_istnieje = 1;
  219.             for(xml_node<> *node_komiks = node->first_node() ; node_komiks ; node_komiks = node_komiks->next_sibling())
  220.             {
  221.                 cout<< " ";
  222.                 wyswietl_jeden_komiks(node_komiks);
  223.             }
  224.             cout<<endl;
  225.             break;
  226.         }
  227.     }
  228.     if(czy_istnieje == 0){ cout<<"Nie ma takiego rodzaju"<<endl;}
  229. }
  230.  
  231.  
  232. void drukuj_liste_uzytkownikow()
  233. {
  234.        
  235. }
  236.            
  237. void wypozycz_ksiazke(){
  238.        
  239. }
  240. void oddaj_ksiazke(){
  241.        
  242. }
  243. void wyswietl_komixy_uzytkownika(xml_node<> *N)   //nie skonczone
  244. {
  245.  
  246. }
  247.  
  248.  
  249. };
  250.  
  251.  
  252. class Menu
  253. {
  254.     int wybor;
  255. public:
  256.     bool pnt = true;
  257.     void rysuj()
  258.     {
  259.         system("cls");
  260.         cout<<"=================================="<<endl;
  261.         cout<<"1.Dodaj komiks do biblioteki"<<endl;
  262.         cout<<"2.Dodaj uzytkownika do biblioteki"<<endl;
  263.         cout<<"3.Autor i uniwersum komiksu"<<endl;
  264.         cout<<"4.Podglad bazy XML"<<endl;
  265.         cout<<"5.Wyswietl liste komiksow"<<endl;
  266.         cout<<"6.Wyswietl liste uzytkownikow"<<endl;
  267.         cout<<"7.Wypozycz ksiazke"<<endl;
  268.         cout<<"8.Oddaj ksiazke"<<endl;
  269.         cout<<"9.Koniec"<<endl;
  270.         cout<<"=================================="<<endl;
  271.         cout<<"Wybor:";
  272.         cin>>wybor;
  273.         system("cls");
  274.        
  275.     }
  276.     void wykonaj(MojXML *obj)
  277.     {
  278.         MojXML Biblioteka((char*)"biblioteka",(char*)".xml");
  279.        
  280.         string nazwa;
  281.         switch(wybor)
  282.         {
  283.         case 1:
  284.             {
  285. //              cout<<"Podaj nazwe komiksu:";
  286. //              cin>>name;
  287. //              cout<<"Podaj autora:";
  288. //              cin>>autor;
  289. //              cout<<"Podaj uniwersum:";
  290. //              cin>>uniwersum;
  291.  //               obj->dodaj_komiks(); 
  292.             }break;
  293.         case 2:
  294.             {
  295. //              cout<<"Podaj imie uzytkownika:";
  296. //              cin>>name;
  297. //              cout<<"Podaj nazwisko:";
  298. //              cin>>nazwisko;
  299. //              cout<<"Podaj rocznik:";
  300. //              cin>>rocznik;
  301. //               obj->dodaj_uzytkownika();
  302.             }break;
  303.        case 3:
  304.             {
  305.                 cout<<"Jaki nazwa komiksu ?:";
  306.                 cin>>nazwa;
  307.                 drukuj_komiksy(komiks);
  308.                 system("pause");
  309.             }break;
  310.         case 4:
  311.             {
  312.      
  313.                 cout << "**************caly xml ***********"<<endl;
  314.                 Biblioteka.drukuj();
  315.                 cout << "**************caly xml ***********"<<endl;    
  316.                         system("pause");       
  317.                
  318.             }break;
  319.         case 5:
  320.             {
  321.               obj->drukuj_komiksy(baza_danych,"przyklad");
  322.                 system("pause");
  323.             }break;
  324.         case 6:
  325.             {
  326.   //             obj->drukuj_czlowiek();
  327.                 system("pause");
  328.             }break;
  329.         case 7:
  330.             {
  331.                 //wywolanie funkcji drukuj_komiksy();
  332.                 cout<<"Jaki komiks chcesz wypozyczyc?"<<endl;
  333.     //          cin>>komix;
  334.                
  335. //              wypozycz_ksiazke();                            
  336.                                 system("pause");
  337.             }break;
  338.         case 8:
  339.             {
  340.                 cout<<"Jakie nazwisko ma uczen?"<<endl;
  341.          //     cin>>uzytkownik;
  342.          //jezeli uczen ma wypozyczana ksiazke to
  343.          //             cout<<"Ktora ksiazke oddaje?"<<endl;
  344. //              oddaj_ksiazke();
  345.                 //      jezeli nie ma to
  346.                 //  cout<<"uczen nie ma wypozyczonej ksiazki";
  347.                                 system("pause");
  348.  
  349.             }break;
  350.         case 9:
  351.             {
  352.                 pnt = false;
  353.             }break;
  354.         default:
  355.             {
  356.                 cout<<"Error, polecenia nie znaleziono"<<endl;
  357.                 system("pause");
  358.             }break;
  359.         }
  360.     }
  361.  
  362. };
  363. int main()
  364. {
  365.         MojXML Biblioteka((char*)"biblioteka",(char*)".xml");
  366.        
  367.     Menu *menu = new Menu;
  368.     while(menu->pnt){
  369.         menu->rysuj();
  370.         menu->wykonaj(&Biblioteka);
  371.     }
  372.     delete &Biblioteka;
  373.     delete menu;
  374.    
  375. //    Biblioteka.Zapisz_do_pliku((char*)"biblioteka",(char*)".xml");
  376.   return 0;
  377. }
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394. //void wyszukajKomiks(xml_node<>*K,int i){
  395. //    xml_attribute<>*ID;
  396. //    ID  =K->first_attribute("id");
  397. //    if (ID)
  398. //    {
  399. //         if (atoi(ID->value())==i)
  400. //            drukuj_komiksy(K);
  401. //    }
  402. //    for(xml_node<>* temp = K->first_node();temp;temp=temp->next_sibling())
  403. //      {      
  404. //       wyszukajKomiks(temp,i);
  405. //                              }
  406. //        }      
  407.  
  408. //void wyszukaj_po_id(xml_node<>*N,string id)        //blad
  409. //{
  410. //    for(xml_node<>*komix=N->first_node()->first_node();komix;komix=komix->next_sibling())
  411. //    {
  412. //          for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() )
  413. //        {
  414. //            string name (atrybut->name(),atrybut->name_size());
  415. //            string value (atrybut->value(),atrybut->value_size());
  416. //            string ajdi = value;
  417. //
  418. //            if(id==ajdi)
  419. //                      {
  420. //                              drukuj_komiksy(komix);
  421. //                      }
  422. //        }
  423. //
  424. //    }
  425. //}