/*
* parser_xml.c
*
* Copyright 2011 Antonio Cocurullo <darkk94@hotmail.it>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define double_newline printf("\n\n")
static void die(const char *_msg)
{
fprintf(stderr,"\nDie error: [%s:%s:%d:%s]\n\n",__FILE__,__FUNCTION__,__LINE__,_msg);
exit(EXIT_FAILURE);
}
static inline bool parser_is_xml(const char *_filename)
{
return strstr(_filename,".xml") == NULL ? false : true;
}
static void parser_xml(const char *_xmlfile)
{
FILE *fp;
char *buffer,*ptr;
if(!(fp = fopen(_xmlfile,"r")))
die("File could not be opened");
if(!(buffer = (char *) malloc(255 * sizeof(char))))
die("NULL");
while(fgets(buffer,255,fp) != NULL)
{
if((ptr = strchr(buffer,'>')) != NULL)
{
ptr++;
if(*ptr == '\n')
continue;
for(; *ptr != '<'; ptr++)
putchar(*ptr);
putchar('\n');
}
}
double_newline;
fclose(fp);
free(buffer);
}
static void parser_xmlfp(char *_xmlfile)
{
FILE *fp,*fop;
char *buffer,*ptr;
if(!(fp = fopen(_xmlfile,"r")))
die("File could not be opened");
if(!(fop = fopen(strcat(_xmlfile,".tmp"),"w")))
die("Can not create file");
if(!(buffer = (char *) malloc(255 * sizeof(char))))
die("NULL");
while(fgets(buffer,255,fp) != NULL)
{
if((ptr = strchr(buffer,'>')) != NULL)
{
ptr++;
if(*ptr == '\n')
continue;
for(; *ptr != '<'; ptr++)
fputc(*ptr,fop);
fputc('\n',fop);
}
}
fclose(fp);
fclose(fop);
free(buffer);
}
int main(int argc,char **argv)
{
char ch;
if(argc != 2)
{
fprintf(stderr,"Usage: \"%s\" <filename>\n\n",argv[0]);
exit(EXIT_FAILURE);
}
if(parser_is_xml(argv[1]) == false)
die("This file is not valid");
parser_xml(argv[1]);
printf("Save results to a file (y/n)? ");
ch = getchar();
switch(ch)
{
case 'y':
parser_xmlfp(argv[1]);
printf("\nResults saved successfully\n\n");
break;
case 'n':
exit(EXIT_SUCCESS);
break;
default:
die("y or n");
}
return EXIT_SUCCESS;
}