Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Parser_xml - parser_xml.c

parser_xml.c

Caricato da:
Scarica il programma completo

  1. /*
  2.  *      parser_xml.c
  3.  *
  4.  *      Copyright 2011 Antonio Cocurullo <darkk94@hotmail.it>
  5.  *
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19.  *      MA 02110-1301, USA.
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdbool.h>
  26. #define double_newline printf("\n\n")
  27.  
  28. static void die(const char *_msg)
  29. {
  30.     fprintf(stderr,"\nDie error: [%s:%s:%d:%s]\n\n",__FILE__,__FUNCTION__,__LINE__,_msg);
  31.     exit(EXIT_FAILURE);
  32. }
  33.  
  34. static inline bool parser_is_xml(const char *_filename)
  35. {
  36.     return strstr(_filename,".xml") == NULL ? false : true;
  37. }
  38.  
  39. static void parser_xml(const char *_xmlfile)
  40. {
  41.     FILE *fp;
  42.     char *buffer,*ptr;
  43.  
  44.     if(!(fp = fopen(_xmlfile,"r")))
  45.         die("File could not be opened");
  46.  
  47.     if(!(buffer = (char *) malloc(255 * sizeof(char))))
  48.         die("NULL");
  49.  
  50.     printf("\n\n");
  51.  
  52.     while(fgets(buffer,255,fp) != NULL)
  53.     {
  54.         if((ptr = strchr(buffer,'>')) != NULL)
  55.         {
  56.             ptr++;
  57.  
  58.             if(*ptr == '\n')
  59.                 continue;
  60.  
  61.             for(; *ptr != '<'; ptr++)
  62.                 putchar(*ptr);
  63.  
  64.             putchar('\n');
  65.         }
  66.     }
  67.  
  68.     double_newline;
  69.  
  70.     fclose(fp);
  71.     free(buffer);
  72. }
  73.  
  74. static void parser_xmlfp(char *_xmlfile)
  75. {
  76.     FILE *fp,*fop;
  77.     char *buffer,*ptr;
  78.  
  79.     if(!(fp = fopen(_xmlfile,"r")))
  80.         die("File could not be opened");
  81.  
  82.     if(!(fop = fopen(strcat(_xmlfile,".tmp"),"w")))
  83.         die("Can not create file");
  84.  
  85.     if(!(buffer = (char *) malloc(255 * sizeof(char))))
  86.         die("NULL");
  87.  
  88.     while(fgets(buffer,255,fp) != NULL)
  89.     {
  90.         if((ptr = strchr(buffer,'>')) != NULL)
  91.         {
  92.             ptr++;
  93.  
  94.             if(*ptr == '\n')
  95.                 continue;
  96.  
  97.             for(; *ptr != '<'; ptr++)
  98.                 fputc(*ptr,fop);
  99.  
  100.             fputc('\n',fop);
  101.         }
  102.     }
  103.  
  104.     fclose(fp);
  105.     fclose(fop);
  106.  
  107.     free(buffer);
  108. }
  109.  
  110. int main(int argc,char **argv)
  111. {
  112.     char ch;
  113.  
  114.     if(argc != 2)
  115.     {
  116.         fprintf(stderr,"Usage: \"%s\" <filename>\n\n",argv[0]);
  117.         exit(EXIT_FAILURE);
  118.     }
  119.  
  120.     if(parser_is_xml(argv[1]) == false)
  121.         die("This file is not valid");
  122.  
  123.     parser_xml(argv[1]);
  124.  
  125.     printf("Save results to a file (y/n)? ");
  126.     ch = getchar();
  127.  
  128.     switch(ch)
  129.     {
  130.         case 'y':
  131.                  parser_xmlfp(argv[1]);
  132.                  printf("\nResults saved successfully\n\n");
  133.  
  134.                  break;
  135.  
  136.         case 'n':
  137.                  exit(EXIT_SUCCESS);
  138.  
  139.                  break;
  140.  
  141.         default:
  142.                 die("y or n");
  143.     }
  144.  
  145.     return EXIT_SUCCESS;
  146. }