- #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
- void dodaj_komiks(MojXML *komiksy, string gdzie_dodac, string rodzaj, string marka, string rocznik)
- {
- xml_node<> * bazy_danych = komiksy->XML->first_node();
- xml_node<> * nowy_komiks = komiksy->utworz_wierzcholek(rodzaj);
- komiksy->dodaj_atrybut(nowy_komiks, (string)("rocznik"), (string)(rocznik));
- komiksy->dodaj_atrybut(nowy_komiks, (string)("marka"), (string)marka);
- komiksy->dodaj_atrybut(nowy_komiks, (string)("rodzaj"), (string)(rodzaj));
- if( gdzie_dodac(bazy_danych,gdzie_dodac) != 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_komiks()
- {
- ///znajdowanie komiksow
- xml_node<>*K = znajdz_komiks_name(XML->first_node(),"komiks");
- xml_node<> *P =XML->first_node("baza_danych")->first_node("komiksy")->next_sibling();
- if (K)
- {
- cout <<(*K);
- xml_node<> * Komiks = utworz_wierzcholek("komiks");
- ///ustawianie wlasciwego id
- {
- xml_attribute<>* ID=NULL, *T;
- if (XML->first_node())
- {
- ID=XML->first_node()->first_attribute("max_id");
- }
- if (ID==NULL) cout << "niepowodzenie";
- cout << atoi(ID->value())<<"!!!!!!"<<endl;
- int nr = atoi(ID->value());
- nr++;
- string temp =IntToString(nr);
- char * NR = XML->allocate_string(temp.c_str());
- ID->value(NR);
- dodaj_atrybut(Komiks,(string)"id",IntToString(nr));
- }
- dodaj_atrybut(Komiks,(string)"name",(string)"Myszojelen\"<>");
- K->append_node(Komiks);
- }
- }
- void dodaj_uzytkownika(){
- cout<<"elo"<<endl;
- }
- void drukuj_komiksy(xml_node<>*K)
- {
- if (K)
- {
- xml_attribute<>* n=K->first_attribute("name");
- if (n)
- {
- cout << "komiks "<< n->value();
- }
- }
- }
- void drukuj_czlowiek(xml_node<>*K)
- {
- if (K)
- {
- xml_attribute<>* n=K->first_attribute("name");
- if (n)
- {
- cout << "czlowiek "<< n->value();
- }
- }
- }
- 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)
- {
- 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);
- }
- }
- }
- }
- };
- 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.Szukaj komiksu po id"<<endl;
- cout<<"4.Wyświetl wszystko"<<endl;
- cout<<"5.Wyswietl liste komiksow"<<endl;
- cout<<"6.Wyswietl liste uzytkownikow"<<endl;
- cout<<"7.Zapisz do pliku"<<endl;
- cout<<"8.Wczytaj baze z pliku"<<endl;
- cout<<"9.Koniec"<<endl;
- cout<<"============================"<<endl;
- cout<<"Wybor:";
- cin>>wybor;
- system("cls");
- }
- void wykonaj(MojXML *obj)
- {
- string nazwa;
- switch(wybor)
- {
- case 1:
- {
- obj->dodaj_komiks();
- }break;
- case 2:
- {
- obj->dodaj_uzytkownika();
- }break;
- // case 3:
- // {
- // int ID;
- // cout<<"Jaki id komiksu:";
- // cin>>ID;
- // obj->wyszukaj_po_id();
- // system("pause");
- // }break;
- case 4:
- {
- }break;
- case 5:
- {
- // obj->drukuj_komiksy();
- system("pause");
- }break;
- case 6:
- {
- // obj->drukuj_czlowiek();
- system("pause");
- }break;
- case 7:
- {
- // obj->zapiszDoPliku();
- }break;
- case 8:
- {
- // obj->odczyt_z_pliku();
- }break;
- case 9:
- {
- pnt = false;
- }break;
- default:
- {
- cout<<"Error, polecenia nie znaleziono"<<endl;
- system("pause");
- }break;
- }
- }
- };
- int main()
- {
- //mozna to wykorzystac do case z wyswietlaniem calej biblioteki
- MojXML Biblioteka((char*)"biblioteka",(char*)".xml");
- cout << "**************caly xml ***********"<<endl;
- Biblioteka.drukuj();
- cout << "**************caly xml ***********"<<endl;
- Menu *menu = new Menu;
- while(menu->pnt){
- menu->rysuj();
- menu->wykonaj(&Biblioteka);
- }
- delete &Biblioteka;
- delete menu;
- return 0;
- }