#include #include #include #include #include #include #include "rapidxml-1.13/rapidxml.hpp" #include "rapidxml-1.13/rapidxml_print.hpp" #include "rapidxml-1.13/rapidxml_utils.hpp" #include using namespace rapidxml; using namespace std; string IntToString (int a) { ostringstream temp; temp< *doc,char * Nazwa, char * rozszezenie,vector ** 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 *B =new vector((istreambuf_iterator(XmlF)), istreambuf_iterator()); B->push_back('\0'); try{ doc->parse<0>(&(*B)[0]); } catch( parse_error p ) { cout <<"|" <(); exit(1); return false; } (*buffer) = B; delete cstr; return true; } class MojXML { public : xml_document<> * XML; vector * 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< * 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:"< *N) { for( xml_node <>* node = N->first_node(); node; node = node->next_sibling() ) { cout<<"Rodzaj: "; cout<name()<<" "<* node_pojazd = node->first_node(); node_pojazd; node_pojazd = node_pojazd->next_sibling() ) { wyswietl_ladnie_samochod(node_pojazd); } cout< *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< *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"< * 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"<>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"<>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<>powrot; while(true) { if (powrot!=0) { system("cls"); wyswietl_ladnie_wszystko(salon_samochodowy); cout<>powrot; } else { system("cls"); goto poczatek; } } } if (wybor == 2) { wyswietl_ladnie_po_rodzaju(salon_samochodowy,"przyklad"); cout<>powrot; while(true) { if (powrot!=0) { cout<>powrot; } else { system("cls"); goto poczatek; } } } if (wybor == 3) { wyswietl_ladnie_po_marce(salon_samochodowy); cout<>powrot; while(true) { if (powrot!=0) { cout<>powrot; } else { system("cls"); goto poczatek; } } } if (wybor == 4) { cout<<"Do jakiego rodzaju (osobowe/ciezarowe/...)pojazd chcesz dodac : "; string rodzaj;cin>>rodzaj; cout<>marka; cout<>rocznik; string pomocnicze; if (rodzaj=="osobowy") rodzaj ="osobowe"; else if (rodzaj=="ciezarowy") rodzaj ="ciezarowe"; else{ dodaj_samochod(&Salon_samochodowy,rodzaj,rodzaj,marka,rocznik);} cout<>powrot; while(true) { if (powrot!=0) { cout<>powrot; } else { system("cls"); goto poczatek; } } } if (wybor==5) { cout<<"Podaj rodzaj: "; string rodzaj;cin>>rodzaj; dodaj_rodzaj(&Salon_samochodowy,salon_samochodowy,rodzaj); cout<>powrot; while(true) { if (powrot!=0) { cout<>powrot; } else { system("cls"); goto poczatek; } } } if (wybor == 6) return 0; Salon_samochodowy.Zapisz_do_pliku((char*)"salon_samochodowy",(char*)".xml"); return 0; }