Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: falconez en 8 Junio 2015, 04:31 am



Título: Lectura de archivo log, error al eliminar IPs duplicadas!
Publicado por: falconez en 8 Junio 2015, 04:31 am
Tengo un problema al eliminar las direcciones IP que se repiten!

Código
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <ctime>
  5. #include <string>
  6. #include<cstdio>
  7.  
  8. using namespace std;
  9.  
  10. /*
  11.  *Leer el log y extraer la primera dirección IP  (nnn.nnn.nnn.nnn).
  12.  *Guardar la dirección ip en una lista -evitar que se repite la dirección ip.
  13.  *Resumir cuantas veces se repite cada ip y visualizar en un SVG el resumen.
  14.  */
  15.  
  16. struct miDataStruct {
  17.    string direccionIP;
  18.    int id;
  19. };
  20.  
  21. struct nodo {
  22.    miDataStruct datoDelNodo;
  23.    nodo *ant;
  24.    nodo *next;
  25. };
  26.  
  27.  
  28. //PROTOTIPOS
  29.  
  30. void archivos_lectura(nodo * list);
  31. int getPosition(string linea,int opcion,int ini);
  32. //FUNCIONES DE MI LISTA DOBLE
  33. nodo * new_list();
  34. nodo * insert_right(nodo *list, nodo data);
  35. void printListAsc(nodo *head);
  36. void printListDesc(nodo *head);
  37. void printList(  nodo* list);
  38.  
  39. void borrarNodo(nodo **head, nodo *node);
  40. void removerDuplicado(nodo **head);
  41.  
  42.  
  43.  
  44. int main () {
  45.  
  46. nodo *head = new_list();
  47.     nodo *current = head;
  48.      archivos_lectura(current);
  49.  
  50.      printList( current);
  51.     removerDuplicado(&head);
  52.  
  53.  
  54.  
  55.  return 0;
  56. }
  57.  
  58. nodo * new_list(){
  59.  
  60.    nodo *newelement= new nodo;
  61.    newelement->datoDelNodo.direccionIP = - 1 ;
  62.    newelement->next = newelement;
  63.    newelement->ant = newelement;
  64.    return newelement;
  65.  
  66. }
  67.  
  68. void addList(  nodo *list , string number){    
  69.        nodo *newelement = new nodo;
  70.        newelement->datoDelNodo.direccionIP = number;      
  71.        newelement->next = list;
  72.        newelement->ant = list->ant;
  73.        list->ant = newelement;
  74.        newelement->ant->next = newelement;
  75. }
  76.  
  77.  
  78. void printListAsc(nodo *head) {
  79.    nodo *current = head;
  80.    while (current->next != head) {
  81.        current = current->next;        
  82.        printList(current);      
  83.    }
  84. }
  85.  
  86.  
  87. void printListDesc(nodo *head) {
  88.    nodo *current = head;
  89.    //printNode(current);
  90.    while (current->ant != head) {
  91.        current = current->ant;
  92.        printList(current);
  93.  
  94.    }
  95. }
  96.  
  97. void printList(  nodo* list){
  98. nodo *head = list;
  99. nodo *current = list;
  100. int i=1;
  101. char caracter;
  102. while (current != head->ant){
  103.            current=current->next;
  104.            cout<<"LINEA --------> "<<i++<<endl<<endl;
  105.       cout<<"Numero de IP: "<<current->datoDelNodo.direccionIP<<endl;
  106.       cout<<"__________________________________"<<endl<<endl;
  107.      // caracter = getchar();
  108.       //getch();
  109.        }
  110. cout<< endl ;
  111.  
  112. }
  113.  
  114. // Proceso de los ficheros...
  115.  
  116. //Obteniendo la posicion de los datos del .log        
  117. int getPosition(string linea,int opcion,int ini){
  118.  
  119. int r;
  120.  
  121. switch(opcion) {
  122.    case 1:
  123.        r = linea.find("UDP") + 4;break;
  124.    case 2:
  125.        r=linea.find(" ",ini);break;
  126. }
  127. // -----> En caso de mas parametros a obtener, mas case; <-
  128. return r;
  129. }
  130.  
  131.  
  132. void archivos_lectura(nodo * list) {
  133.  nodo *current=list;  
  134.  
  135.  string line;
  136.  ifstream myfile ("firewall_1.log");
  137.  int p1,p2;
  138.  string numero;
  139.  
  140.  nodo myDataIP;
  141.  
  142.  if (myfile.is_open())
  143.  {
  144.    while ( getline (myfile,line) )
  145.    {
  146.        p1 =getPosition(line,1,0); // p1=29
  147.        p2=getPosition(line,2,p1);
  148.        numero=line.substr(p1,p2-p1);    
  149.        addList(current,numero);
  150.  
  151.        //cout << line << '\n';
  152.    }
  153.  
  154.    myfile.close();
  155.  }
  156.  
  157.  else cout << "Unable to open file";
  158. }
  159.  
  160.  
  161. // FUNCIONES PARA ELIMINAR LOS DUPLICADOS
  162.  
  163. void removerDuplicado(nodo **head)
  164. {
  165. if((*head)->next == NULL) return;
  166. nodo *current = *head;
  167. nodo *aux;
  168. while(current) {
  169. aux = current->next;
  170. while(aux) {
  171. if(current->datoDelNodo.direccionIP == aux->datoDelNodo.direccionIP) {
  172. borrarNodo(head, aux);
  173. }
  174. aux = aux->next;
  175. }
  176. current = current->next;
  177. }
  178. return;
  179. }
  180.  
  181.  
  182.  
  183. void borrarNodo(nodo **head, nodo *node)
  184. {
  185. nodo *current = *head;
  186. nodo *ant = *head;
  187. if(node == *head) {
  188. if((*head)->next != NULL) {
  189. *head = (*head)->next;
  190. }
  191. return;
  192. }
  193. while(current) {
  194. if(current == node) {
  195. ant->next = current->next;
  196. return;
  197. }
  198. ant = current;
  199. current = current->next;
  200. }
  201. }
  202.  
  203.  

//aqui les adjunto el codigo completo con el archivo log para que lo abran como proyecto!
https://mega.co.nz/#F!L8NSyIxZ!RZX98C_HXUVnnwaW30UYFw (https://mega.co.nz/#F!L8NSyIxZ!RZX98C_HXUVnnwaW30UYFw)

Mod: No escribir en mayúsculas