- #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;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////
- //wyswietlanie atrybutu
- void wyswietl_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)
- {
- cout<<value;
- break;
- }
- }
- }
- //zczytywanie atrybutu
- 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;
- }
- }
- }
- //ladne wyswietlanie atrybytów pojazdu
- void wyswietl_ladnie_samochod(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 == "rodzaj")
- {
- cout<<" i nalezy do rodzaju:"<<value;
- }
- else if (name == "marka")
- {
- cout<<" jest marki " << value <<" ";
- }
- else if(name == "rocznik")
- {
- cout<<"Rocznik pojazdu to " << value <<",";
- }
- }
- cout<<endl;
- }
- //wyswietlanie calosci
- void wyswietl_ladnie_wszystko(xml_node<> *N)
- {
- for( xml_node <>* node = N->first_node(); node; node = node->next_sibling() )
- {
- cout<<"Rodzaj: ";
- cout<<node->name()<<" "<<endl;
- for( xml_node <>* node_pojazd = node->first_node(); node_pojazd; node_pojazd = node_pojazd->next_sibling() )
- {
- wyswietl_ladnie_samochod(node_pojazd);
- }
- cout<<endl;
- }
- }
- //wyswietlanie po rodzaju
- void wyswietl_ladnie_po_rodzaju(xml_node <> *N, string rodzaj)
- {
- int czy_istnieje = 0;
- cout<<"Podaj rodzaj pojazdu (ciezarowe/osobowe/...): ";
- cin>>rodzaj;
- 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_pojazd = node->first_node() ; node_pojazd ; node_pojazd = node_pojazd->next_sibling())
- {
- cout<< " ";
- wyswietl_ladnie_samochod(node_pojazd);
- }
- cout<<endl;
- break;
- }
- }
- if(czy_istnieje == 0){ cout<<"Nie ma takiego rodzaju"<<endl;}
- }
- //wyswietlanie po marce
- void wyswietl_ladnie_po_marce(xml_node <> *N)
- {
- cout<<"Podaj marke szukanego pojazdu: ";
- string marka;cin>>marka;
- int czy_istnieje=0;
- for(xml_node<>*node=N->first_node();node;node=node->next_sibling())
- {
- for(xml_node<>*pomocniczy = node->first_node();pomocniczy;pomocniczy=pomocniczy->next_sibling())
- {
- if (pobierz_wartosc_atrybutu(pomocniczy,"marka") == marka)
- {
- wyswietl_ladnie_samochod(pomocniczy);
- czy_istnieje = 1;
- continue;
- }
- }
- //if(czy_istnieje==1)break;
- }
- if(czy_istnieje==0)
- {cout<<"Nie posiadamy pojazdu podanej marki. Upewnij sie co do wielkosci wpisywanych liter.";}
- }
- //funkcja pomocnicza do dodawania
- xml_node<> * gdzie_dodac(xml_node<>* Salon, string name)
- {
- int czy_istnieje=0;
- for(xml_node <> * node = Salon->first_node(); node ; node = node->next_sibling())
- {
- if(pobierz_wartosc_atrybutu(node,"name") == name)
- {
- return node;
- czy_istnieje=1;
- }
- }
- if(czy_istnieje==0)
- {
- cout<<"Nie ma takiego rodzaju pojazdu"<<endl;
- return NULL;
- }
- }
- //dodawanie samochodu
- void dodaj_samochod(MojXML *Salon, string gdzie_dodac_ , string rodzaj, string marka, string rocznik)
- {
- xml_node<> * Samochody = Salon->XML->first_node();
- xml_node<> * nowy_samochod = Salon->utworz_wierzcholek(rodzaj);
- Salon->dodaj_atrybut(nowy_samochod, (string)("rocznik"), (string)(rocznik));
- Salon->dodaj_atrybut(nowy_samochod, (string)("marka"), (string)marka);
- Salon->dodaj_atrybut(nowy_samochod, (string)("rodzaj"), (string)(rodzaj));
- if(gdzie_dodac(Samochody,gdzie_dodac_)!=NULL)
- {
- xml_node<>* tutaj_dodaj = gdzie_dodac(Samochody,gdzie_dodac_);
- tutaj_dodaj->append_node(nowy_samochod);
- Salon->Zapisz_do_pliku((char*)"salon_samochodowy",(char*)".xml");
- }
- }
- //tworzenie nowego rodzaju
- void dodaj_rodzaj(MojXML *Salon, xml_node<> *N, string rodzaj)
- {
- xml_node<> *ostatni_rodzaj = N->last_node();
- xml_node<> *nowy_rodzaj = Salon->utworz_wierzcholek(rodzaj);
- Salon->dodaj_atrybut(nowy_rodzaj,(string)"name",(string)rodzaj);
- N->append_node(nowy_rodzaj);
- Salon->Zapisz_do_pliku((char*)"salon_samochodowy",(char*)".xml");
- }
- ////////////////////////////////////MENU//////////////////////////////////////////
- int menu()
- { int wybor;
- while(true)
- {
- cout<<"1.Wyswietl wszystkie pojazdy"<<endl;
- cout<<"2.Wyswietl wszystkie pojazdy z danego rodzaju"<<endl;
- cout<<"3.Wyszukaj pojazd danej marki"<<endl;
- cout<<"4.Dodaj pojazd do istniejacego rodzaju"<<endl;
- cout<<"5.Stworz nowy rodzaj"<<endl;
- cout<<"6.Wyjdz"<<endl;
- cout<<"Wybierz numer i zatwierdz enterem:";
- cin>>wybor;
- system("cls");
- if(wybor==1||wybor==2||wybor==3||wybor==4||wybor==5||wybor==6){break;}
- if(wybor!=1&&wybor!=2&&wybor!=3&&wybor!=4&&wybor!=5&&wybor!=6)
- {
- //cout<<"Nie ma takiego numeru, sprobuj ponownie"<<endl<<endl;
- cout<<"1.Wyswietl wszystkie pojazdy"<<endl;
- cout<<"2.Wyswietl wszystkie pojazdy z danego rodzaju"<<endl;
- cout<<"3.Wyszukaj pojazd danej marki"<<endl;
- cout<<"4.Dodaj pojazd do istniejacego rodzaju"<<endl;
- cout<<"5.Stworz nowy rodzaj"<<endl;
- cout<<"6.Wyjdz"<<endl;
- cout<<"Wybierz numer i zatwierdz enterem:";
- cin>>wybor;
- system("cls");
- if(wybor==1||wybor==2||wybor==3||wybor==4||wybor==5||wybor==6){break;}
- }
- }
- return wybor;
- }
- int main()
- {
- MojXML Salon_samochodowy((char*)"salon_samochodowy",(char*)".xml");
- xml_node<> * salon_samochodowy = Salon_samochodowy.XML->first_node();
- poczatek:
- int powrot;
- int wybor;
- wybor = menu();
- if(wybor == 1)
- {
- wyswietl_ladnie_wszystko(salon_samochodowy);
- cout<<endl<<endl<<"Wpisz 0 zeby powrocic: ";
- cin>>powrot;
- while(true)
- {
- if (powrot!=0)
- {
- system("cls");
- wyswietl_ladnie_wszystko(salon_samochodowy);
- cout<<endl<<endl<<"Wpisz 0 zeby powrocic: ";
- cin>>powrot;
- }
- else
- {
- system("cls");
- goto poczatek;
- }
- }
- }
- if (wybor == 2)
- {
- wyswietl_ladnie_po_rodzaju(salon_samochodowy,"przyklad");
- cout<<endl<<endl<<"Wpisz 0 zeby powrocic: ";
- cin>>powrot;
- while(true)
- {
- if (powrot!=0)
- {
- cout<<endl<<endl<<"Wpisz 0 zeby powrocic: ";
- cin>>powrot;
- }
- else
- {
- system("cls");
- goto poczatek;
- }
- }
- }
- if (wybor == 3)
- {
- wyswietl_ladnie_po_marce(salon_samochodowy);
- cout<<endl<<endl<<"Wpisz 0 zeby powrocic: ";
- cin>>powrot;
- while(true)
- {
- if (powrot!=0)
- {
- cout<<endl<<endl<<"Wpisz 0 zeby powrocic: ";
- cin>>powrot;
- }
- else
- {
- system("cls");
- goto poczatek;
- }
- }
- }
- if (wybor == 4)
- {
- cout<<"Do jakiego rodzaju (osobowe/ciezarowe/...)pojazd chcesz dodac : ";
- string rodzaj;cin>>rodzaj;
- cout<<endl<<"Podaj marke pojazdu: ";
- string marka;cin>>marka;
- cout<<endl<<"Podaj rocznik pojazdu: ";
- string rocznik;cin>>rocznik;
- string pomocnicze;
- if (rodzaj=="osobowy") rodzaj ="osobowe";
- else if (rodzaj=="ciezarowy") rodzaj ="ciezarowe";
- else{ dodaj_samochod(&Salon_samochodowy,rodzaj,rodzaj,marka,rocznik);}
- cout<<endl<<endl<<"Wpisz 0 zeby powrocic: ";
- cin>>powrot;
- while(true)
- {
- if (powrot!=0)
- {
- cout<<endl<<endl<<"Wpisz 0 zeby powrocic: ";
- cin>>powrot;
- }
- else
- {
- system("cls");
- goto poczatek;
- }
- }
- }
- if (wybor==5)
- {
- cout<<"Podaj rodzaj: ";
- string rodzaj;cin>>rodzaj;
- dodaj_rodzaj(&Salon_samochodowy,salon_samochodowy,rodzaj);
- cout<<endl<<endl<<"Wpisz 0 zeby powrocic: ";
- cin>>powrot;
- while(true)
- {
- if (powrot!=0)
- {
- cout<<endl<<endl<<"Wpisz 0 zeby powrocic: ";
- cin>>powrot;
- }
- else
- {
- system("cls");
- goto poczatek;
- }
- }
- }
- if (wybor == 6) return 0;
- Salon_samochodowy.Zapisz_do_pliku((char*)"salon_samochodowy",(char*)".xml");
- return 0;
- }