Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- //#define DEBUGGER // Solo en modo DEBUG
- using namespace std;
- //----------------------------------------------------------------------------
- typedef struct {unsigned short pos[4];}s_ip;
- //----------------------------------------------------------------------------
- //Clase para leer una direccion IP aaa.bbb.ccc.ddd
- class leeIp{
- public:
- bool lectura();
- private:
- bool test(string s);//Analiza si los datos son correctos
- s_ip ip;//structura contenedora de la direccion ip
- };
- //----------------------------------------------------------------------------
- bool leeIp::test(string s){//Analiza si los datos son correctos
- string::iterator it=s.begin();
- const string MODEL ("aaa.bbb.ccc.ddd");
- string buffer("");
- int p=0;
- for (p=0,it=s.begin();it!=(s.end());it++){
- if (*it=='.'){p++ ;}//Cuenta puntos maximo 3 !
- if (p>3){cout <<"Error demasiados puntos "<<endl;return false;}
- //Solo valores numericos o puntos
- if ((!isdigit (*it)) && (*it!='.')){cout<<"Error invalid value "<<*it<<endl;return false;}
- }
- //Evita cadena vacia o demasiado grande
- if (s.empty() ||s.size()> (MODEL.size()) || (s.size()<7)){cout<<"Error size "<<endl;return false;}
- //Escribe datos
- for (p=0, it=s.begin();it!=(s.end());it++)
- {
- if ((*it!='.')||(it== (s.end()))) {buffer+=*it;}
- else {
- ip.pos[p]=stoi (buffer);
- buffer.clear();
- p++;
- }
- }
- ip.pos[p]=stoi (buffer);
- #if defined (DEBUGGER)
- for (size_t i=0;i<4;i++){//Muestra valores
- if(ip.pos[i]>255){cout <<"Error valor ip invalido "<<ip.pos[i]<<endl;return false;}
- cout <<ip.pos[i];
- if (i<3){cout<<".";}
- }
- #endif
- return true;
- }
- //----------------------------------------------------------------------------
- bool leeIp::lectura(){
- string buffer;
- cout<<" IP :";cin>>buffer;
- return test(buffer);//Analiza si los datos son correctos
- };
- //----------------------------------------------------------------------------
- int main (){
- leeIp c_ip;
- while (!(c_ip.lectura())){};//Pide si los datos son incorrectos
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement