- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <algorithm>
- #include "rapidxml-1.13/rapidxml.hpp"
- #include "rapidxml-1.13/rapidxml_print.hpp"
- #include "rapidxml-1.13/rapidxml_utils.hpp"
- #include <vector>
- using namespace rapidxml;
- using namespace std;
- string IntToString (int a)
- {
- ostringstream temp;
- temp<<a;
- return temp.str();
- }
- bool pobierz(xml_document <> *doc,char * Nazwa, char * rozszezenie,vector<char> ** buffer)
- {
- string nazwa_doc = string(Nazwa) + string(rozszezenie);
- char *cstr = new char[nazwa_doc.length() + 1];
- strcpy(cstr, nazwa_doc.c_str());
- ifstream XmlF (cstr);
- if (!XmlF.good()){cout << "file no exists"; exit(1);}
- vector<char> *B =new vector<char>((istreambuf_iterator<char>(XmlF)), istreambuf_iterator<char>());
- B->push_back('\0');
- try{
- doc->parse<0>(&(*B)[0]);
- }
- catch( parse_error p )
- {
- cout <<"|" <<p.what()<<"|";
- cout << p.where<char>();
- exit(1);
- return false;
- }
- (*buffer) = B;
- delete cstr;
- return true;
- }
- class MojXML
- {
- public :
- xml_document<> * XML;
- vector<char> * XMLvect;
- MojXML(char * nazwa,char * rozszezenie)
- {
- XML = new xml_document<>();
- if (!pobierz(XML,nazwa,rozszezenie,&XMLvect))
- {cout << "cos poszlo nie tak" << nazwa<< ".xml";exit(1);}
- }
- ~MojXML()
- {
- XML->clear();
- delete XML;
- delete XMLvect;
- }
- xml_node <>* utworz_wierzcholek(string name)
- {
- char *Name = XML->allocate_string(name.c_str());
- return XML->allocate_node( node_element,Name);
- }
- void dodaj_atrybut(xml_node<>* W,string attr, string value)
- {
- char *Attr = XML->allocate_string(attr.c_str());
- char *Value = XML->allocate_string(value.c_str());
- W->append_attribute(XML->allocate_attribute(Attr,Value));
- }
- void Zapisz_do_pliku(char * nazwa,char * rozszezenie)
- {
- string nazwa_doc = string(nazwa) + string(rozszezenie);
- char *cstr = new char[nazwa_doc.length() + 1];
- strcpy(cstr, nazwa_doc.c_str());
- ofstream File;
- File.open(cstr);
- File << *XML;
- File.flush();
- File.close();
- delete cstr;
- }
- void drukuj()
- {
- xml_node<>* temp = XML->first_node();
- if (!temp) {cout << 1;exit(1);}
- temp = temp->first_node();
- while (temp)
- {
- cout << * temp;
- temp = temp->next_sibling();
- }
- }
- void WyswietlAtrybuty(xml_node <> * N )
- {
- cout << "<" << N->name() <<">";
- for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() ) // (7)
- {
- string name (atrybut->name(),atrybut->name_size());
- string value (atrybut->value(),atrybut->value_size());
- cout << "|" << name << "=" << value << "| ";
- }
- }
- void WyswietlPotomkow(xml_node <> * N)
- {
- for( xml_node <>* node = N->first_node(); node; node = node->next_sibling() ) // (7)
- {
- cout << "<" << node->name() <<" ";
- WyswietlAtrybuty(node);
- cout << ">" << endl;
- }
- }
- ///////////////////////////////////////////////////////////////////////
- xml_node<> * znajdz_komiks_name(xml_node<>*start,string name)
- {
- xml_attribute<>*NAME;
- NAME =start->first_attribute("name");
- if (NAME)
- {
- if (strcmp(NAME->value(),name.c_str())==0)
- return start;
- }
- xml_node<>* RET;
- for(xml_node<>* temp = start->first_node();temp;temp=temp->next_sibling())
- {
- RET=znajdz_komiks_name(temp,name);
- if (RET) return RET;
- }
- return NULL;
- }
- //////////////////////////////// nie dziala dodanie uzytkownika i komiksu
- void dodaj_komiks(MojXML *komiksy, string gdzie_dodac_, string autor, string uniwersum, string name)
- {
- xml_node<> * bazy_danych = komiksy->XML->first_node();
- xml_node<> *nowy_komiks = komiksy->utworz_wierzcholek(autor);
- komiksy->dodaj_atrybut(nowy_komiks, (string)("nazwa"), (string)(name));
- komiksy->dodaj_atrybut(nowy_komiks, (string)("uniwersum"), (string)(uniwersum));
- komiksy->dodaj_atrybut(nowy_komiks, (string)("autor"), (string)autor);
- if(gdzie_dodac_(bazy_danych,gdzie_dodac_P)!=NULL)
- {
- xml_node<> *tutaj_dodaj = gdzie_dodac_(komiksy,gdzie_dodac_);
- tutaj_dodaj->append_node(nowy_komiks);
- komiksy->Zapisz_do_pliku((char*)"biblioteka",(char*)".xml");
- }
- }
- void dodaj_uzytkownika(MojXML *ludzie, string gdzie_dodac_, string rocznik, string nazwisko, string imie)
- {
- xml_node<> * bazy_danych = ludzie->XML->first_node();
- xml_node<> * nowy_czlowiek = ludzie->utworz_wierzcholek(rocznik);
- ludzie->dodaj_atrybut(nowy_czlowiek, (string)("imie"), (string)(name));
- ludzie->dodaj_atrybut(nowy_czlowiek, (string)("nazwisko"), (string)nazwisko);
- ludzie->dodaj_atrybut(nowy_czlowiek, (string)("rocznik"), (string)rocznik);
- if(string gdzie_dodac_(bazy_danych,gdzie_dodac_) != NULL)
- {
- xml_node<>* tutaj_dodaj = gdzie_dodac_(ludzie,gdzie_dodac_);
- tutaj_dodaj->append_node(nowy_czlowiek);
- ludzie->Zapisz_do_pliku((char*)"biblioteka",(char*)".xml");
- }
- }
- //////////////////////////////////
- string pobierz_wartosc_atrybutu(xml_node <> * N, string nazwa)
- {
- for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() )
- {
- string name (atrybut->name(),atrybut->name_size());
- string value (atrybut->value(),atrybut->value_size());
- if( name == nazwa )
- {
- return value;
- }
- }
- }
- void wyswietl_jeden_komiks(xml_node<> *N)
- {
- for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() )
- {
- string name (atrybut->name(),atrybut->name_size());
- string value (atrybut->value(),atrybut->value_size());
- if (name == "uniwersum")
- {
- cout<<" jego universum to:"<<value;
- }
- else if (name == "autor")
- {
- cout<<" i jego autorem jest: " << value <<" ";
- }
- }
- }
- void drukuj_komiksy(xml_node <> *N, string rodzaj)
- {
- int czy_istnieje = 0;
- string nazwa;
- rodzaj=nazwa;
- for(xml_node <> *node = N->first_node() ; node ; node = node->next_sibling())
- {
- if(pobierz_wartosc_atrybutu(node , "name") == rodzaj)
- {
- czy_istnieje = 1;
- for(xml_node<> *node_komiks = node->first_node() ; node_komiks ; node_komiks = node_komiks->next_sibling())
- {
- cout<< " ";
- wyswietl_jeden_komiks(node_komiks);
- }
- cout<<endl;
- break;
- }
- }
- if(czy_istnieje == 0){ cout<<"Nie ma takiego rodzaju"<<endl;}
- }
- void drukuj_liste_uzytkownikow()
- {
- }
- void wypozycz_ksiazke(){
- }
- void oddaj_ksiazke(){
- }
- void wyswietl_komixy_uzytkownika(xml_node<> *N) //nie skonczone
- {
- }
- };
- class Menu
- {
- int wybor;
- public:
- bool pnt = true;
- void rysuj()
- {
- system("cls");
- cout<<"=================================="<<endl;
- cout<<"1.Dodaj komiks do biblioteki"<<endl;
- cout<<"2.Dodaj uzytkownika do biblioteki"<<endl;
- cout<<"3.Autor i uniwersum komiksu"<<endl;
- cout<<"4.Podglad bazy XML"<<endl;
- cout<<"5.Wyswietl liste komiksow"<<endl;
- cout<<"6.Wyswietl liste uzytkownikow"<<endl;
- cout<<"7.Wypozycz ksiazke"<<endl;
- cout<<"8.Oddaj ksiazke"<<endl;
- cout<<"9.Koniec"<<endl;
- cout<<"=================================="<<endl;
- cout<<"Wybor:";
- cin>>wybor;
- system("cls");
- }
- void wykonaj(MojXML *obj)
- {
- MojXML Biblioteka((char*)"biblioteka",(char*)".xml");
- string nazwa;
- switch(wybor)
- {
- case 1:
- {
- // cout<<"Podaj nazwe komiksu:";
- // cin>>name;
- // cout<<"Podaj autora:";
- // cin>>autor;
- // cout<<"Podaj uniwersum:";
- // cin>>uniwersum;
- // obj->dodaj_komiks();
- }break;
- case 2:
- {
- // cout<<"Podaj imie uzytkownika:";
- // cin>>name;
- // cout<<"Podaj nazwisko:";
- // cin>>nazwisko;
- // cout<<"Podaj rocznik:";
- // cin>>rocznik;
- // obj->dodaj_uzytkownika();
- }break;
- case 3:
- {
- cout<<"Jaki nazwa komiksu ?:";
- cin>>nazwa;
- drukuj_komiksy(komiks);
- system("pause");
- }break;
- case 4:
- {
- cout << "**************caly xml ***********"<<endl;
- Biblioteka.drukuj();
- cout << "**************caly xml ***********"<<endl;
- system("pause");
- }break;
- case 5:
- {
- obj->drukuj_komiksy(baza_danych,"przyklad");
- system("pause");
- }break;
- case 6:
- {
- // obj->drukuj_czlowiek();
- system("pause");
- }break;
- case 7:
- {
- //wywolanie funkcji drukuj_komiksy();
- cout<<"Jaki komiks chcesz wypozyczyc?"<<endl;
- // cin>>komix;
- // wypozycz_ksiazke();
- system("pause");
- }break;
- case 8:
- {
- cout<<"Jakie nazwisko ma uczen?"<<endl;
- // cin>>uzytkownik;
- //jezeli uczen ma wypozyczana ksiazke to
- // cout<<"Ktora ksiazke oddaje?"<<endl;
- // oddaj_ksiazke();
- // jezeli nie ma to
- // cout<<"uczen nie ma wypozyczonej ksiazki";
- system("pause");
- }break;
- case 9:
- {
- pnt = false;
- }break;
- default:
- {
- cout<<"Error, polecenia nie znaleziono"<<endl;
- system("pause");
- }break;
- }
- }
- };
- int main()
- {
- MojXML Biblioteka((char*)"biblioteka",(char*)".xml");
- Menu *menu = new Menu;
- while(menu->pnt){
- menu->rysuj();
- menu->wykonaj(&Biblioteka);
- }
- delete &Biblioteka;
- delete menu;
- // Biblioteka.Zapisz_do_pliku((char*)"biblioteka",(char*)".xml");
- return 0;
- }
- //void wyszukajKomiks(xml_node<>*K,int i){
- // xml_attribute<>*ID;
- // ID =K->first_attribute("id");
- // if (ID)
- // {
- // if (atoi(ID->value())==i)
- // drukuj_komiksy(K);
- // }
- // for(xml_node<>* temp = K->first_node();temp;temp=temp->next_sibling())
- // {
- // wyszukajKomiks(temp,i);
- // }
- // }
- //void wyszukaj_po_id(xml_node<>*N,string id) //blad
- //{
- // for(xml_node<>*komix=N->first_node()->first_node();komix;komix=komix->next_sibling())
- // {
- // for( xml_attribute <>* atrybut = N->first_attribute(); atrybut; atrybut = atrybut->next_attribute() )
- // {
- // string name (atrybut->name(),atrybut->name_size());
- // string value (atrybut->value(),atrybut->value_size());
- // string ajdi = value;
- //
- // if(id==ajdi)
- // {
- // drukuj_komiksy(komix);
- // }
- // }
- //
- // }
- //}