/* DELRPTLN.C */ /* Copyright (C) 1996-2002 Norbert H. Doerry Version 1.0a: June 2002, Added GNU License Version 1.0: November 1996 SYNTAX: DELRPTLN Infile -oOutfile -lXX where Infile is the name of the input file Outfile is the name of the output file -lXX where XX is the maximum length of a line (default 1024) This program deletes lines that are identical to their predecessor in Infile and writes the result to Outfile, or stdout if not specified. 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #define VERSION "1.0a" #define MAXCHAR 1024 typedef struct Parameter { char *Infile; FILE *in; char *Outfile; FILE *out; int max_line_length; int debug; long nbr_lines; long nbr_deleted; } PARAMETER; /******************************************************************/ void initialize_parameter(PARAMETER *); void process_command_line(PARAMETER *,int,char **); char *copystring(char *); void print_help(void); void print_parameter(PARAMETER *); void process_file(PARAMETER *); /******************************************************************/ int main(int argc,char **argv) { PARAMETER p; initialize_parameter(&p); process_command_line(&p,argc,argv); if (p.debug) print_parameter(&p); process_file(&p); if (p.debug) print_parameter(&p); if (p.out != stdout) fclose(p.out); if (p.in != stdin) fclose(p.in); return EXIT_SUCCESS; } void initialize_parameter(PARAMETER *p) { p->Infile = (char *) NULL; p->in = (FILE *) NULL; p->Outfile = (char *) NULL; p->out = (FILE *) NULL; p->max_line_length = MAXCHAR; p->debug = 0; p->nbr_lines = 0l; p->nbr_deleted = 0l; } void process_command_line(PARAMETER *p,int argc,char **argv) { int i; for (i=1 ; i < argc ; i++) { if (argv[i][0] == '-' || argv[i][0] == '/') { if (argv[i][1] == 'o' || argv[i][1] == 'O') { if (p->Outfile != (char *) NULL) { fprintf(stderr," ***ERROR: Output File Multiply Defined\n"); exit(EXIT_FAILURE); } p->Outfile = copystring(argv[i]+2); p->out = fopen(p->Outfile,"w"); if (p->out == (FILE *) NULL) { fprintf(stderr," ***ERROR: Unable to open Output File %s\n", p->Outfile); exit(EXIT_FAILURE); } } else if (argv[i][1] == 'l' || argv[i][1] == 'L') { p->max_line_length = atoi(argv[i]+2); if (p->max_line_length < 10) p->max_line_length = MAXCHAR; } else if (argv[i][1] == 'd' || argv[i][1] == 'D') { p->debug = 1; } else if (argv[i][1] == 'h' || argv[i][1] == 'H' || argv[i][1] == '?') { print_help(); exit(EXIT_SUCCESS); } else { fprintf(stderr," ***ERROR: Unknown option: %s\n",argv[i]); exit(EXIT_FAILURE); } } else { if (p->Infile != (char *) NULL) { fprintf(stderr," ***ERROR: Input File Multiply Defined\n"); exit(EXIT_FAILURE); } p->Infile = copystring(argv[i]); p->in = fopen(p->Infile,"r"); if (p->in == (FILE *) NULL) { fprintf(stderr," ***ERROR: Unable to open Input File %s\n", p->Infile); exit(EXIT_FAILURE); } } } if (p->Outfile == (char *) NULL) { p->Outfile = copystring(""); p->out = stdout; } if (p->Infile == (char *) NULL) { p->Infile = copystring(""); p->in = stdin; } } char *copystring(char *s) { char *ps; if (s == (char *) NULL) ps = (char *) calloc((size_t) 1,sizeof(char)); else ps = (char *) calloc(strlen(s)+1,sizeof(char)); if (ps == (char *) NULL) { fprintf(stderr," ***ERROR: OUT OF MEMORY IN COPYSTRING() <%d>\n",__LINE__); exit(EXIT_FAILURE); } if (s == (char *) NULL) *ps = (char) NULL; else { strcpy(ps,s); } return ps; } void print_help(void) { printf(" DELRPTLN.EXE version %s (%s)\n",VERSION,__DATE__); printf(" written by Norbert H. Doerry\n"); printf("\n"); printf(" SYNTAX:\n"); printf(" DELRPTLN Infile -oOutfile -lXX\n\n"); printf(" where:\n\n"); printf(" Infile is the name of the input file (default stdin)\n"); printf(" Outfile is the name of the output file (default stdout)\n"); printf(" -lXX where XX is the maximum length of a line (default 1024)\n"); printf("\n"); printf(" This program deletes lines that are identical to their \n"); printf(" predecessor in Infile and writes the result to Outfile. \n"); } void print_parameter(PARAMETER *p) { printf("p->Infile = %s\n",p->Infile); printf("p->Outfile = %s\n",p->Outfile); printf("p->max_line_length = %d\n",p->max_line_length); printf("p->nbr_lines = %ld\n",p->nbr_lines); printf("p->nbr_deleted = %ld\n",p->nbr_deleted); } void process_file(PARAMETER *p) { char *rdline; char *lastline; rdline = (char *) calloc((size_t)p->max_line_length+1,sizeof(char)); lastline = (char *) calloc((size_t)p->max_line_length+1,sizeof(char)); if (rdline == (char *) NULL || lastline == (char *) NULL) { fprintf(stderr," ***ERROR: Out of Memory in process_file() <%d>\n",__LINE__); exit(EXIT_FAILURE); } p->nbr_lines = 0l; p->nbr_deleted = 0l; lastline[0] = (char) NULL; while (fgets(rdline,p->max_line_length,p->in)) { p->nbr_lines++; /* keep track of the total number of lines */ if (strcmp(rdline,lastline) == 0) { p->nbr_deleted++; /* also keep track of the deleted lines */ } else { fputs(rdline,p->out); strcpy(lastline,rdline); } } free((void *)rdline); free((void *)lastline); }