/* insrecdl.c */ /* Version 1.0a COPYRIGHT (C) 1996-2002 Norbert H. Doerry SYNTAX: INSRECDL Infile -oOutfile -lXXX WHERE Infile is the input file Outfile is the output file (or stdout if omitted) -lxxx xxx is the length of a line without a newline character INSRECDL inserts a newline character after xxx characters of each line if the newline character isn't already there. Version 1.0: December 1996 Version 1.0a: June 22, 2002 Added GNU GPL, recompiled with 32bit compiler --------------------------------------------------------------- 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. --------------------------------------------------------------- If you discover any bugs, or have any questions concerning these programs, please send me an email (doerry@aol.com) */ #include #include #define VERSION "1.0a" typedef struct Parameter { char *Infile; FILE *in; char *Outfile; FILE *out; int len; int debug; } PARAMETER; /********************************************************************/ void initialize_parameter(PARAMETER *); void read_command_line(PARAMETER *,int,char **); void print_help(void); void process_file(PARAMETER *); /********************************************************************/ int main(int argc,char **argv) { PARAMETER p; initialize_parameter(&p); read_command_line(&p,argc,argv); process_file(&p); if (p.in != stdin) fclose(p.in); if (p.out != stdout) fclose(p.out); 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->len = 0; p->debug = 0; } void read_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') { p->Outfile = argv[i]+2; p->out = fopen(p->Outfile,"w"); if (p->out == (FILE *) NULL) { fprintf(stderr," ***ERROR: Unable to open file %s\n",p->Outfile); exit(EXIT_FAILURE); } } else if (argv[i][1] == 'l' || argv[i][1] == 'L') { p->len = atoi(argv[i]+2); if (p->len < 1) { fprintf(stderr," ***ERROR: Line Length must be > 0 (%s)\n",p->len); exit(EXIT_FAILURE); } } 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 parameter: %s\n",argv[i]); exit(EXIT_FAILURE); } } else { p->Infile = 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->in == (FILE *) NULL) { fprintf(stderr," ***ERROR: Input Filename not specified\n"); exit(EXIT_FAILURE); } if (p->out == (FILE *) NULL) p->out = stdout; if (p->len < 1) { fprintf(stderr," ***ERROR: Line length not specified\n"); exit(EXIT_FAILURE); } } void print_help(void) { printf(" INSRECDL version %s (%s)\n",VERSION,__DATE__); printf(" COPYRIGHT (C) 1996-2002 Norbert Doerry\n"); printf("\n"); printf(" SYNTAX: INSRECDL Infile -oOutfile -lXXX\n"); printf("\n"); printf(" Where:\n"); printf(" Infile = input filename\n"); printf(" Outfile = Output filename (default = stdout)\n"); printf(" -lxxx = xxx is the length of a line without a new line\n"); printf("\n"); printf(" INSRECDL inserts a newline character after every xxx characters in\n"); printf(" Infile if the newline character is not already present. The results\n"); printf(" are written to Outfile (or stdout if omitted)\n"); } void process_file(PARAMETER *p) { char *rdline; int cnt; char c; char *s; int eor; /* end of record flag, = 0 input file doesn't have it, = 1 does have it */ /* allocate rdline */ rdline = (char *) calloc((size_t)p->len+3,sizeof(char)); if (rdline == (char *) NULL) { fprintf(stderr," ***ERROR: Out of memory in process_file() <%d>\n",__LINE__); exit(EXIT_FAILURE); } /* NULL terminate it */ rdline[p->len] = (char) NULL; /* see if the first record is terminated with a new line character */ fgets(rdline,p->len+2,p->in); eor = (rdline[p->len] == '\n') ? 1 : 0; /* print debug info */ if (p->debug) printf(" eor = %d\n",eor); /* move the file pointer back to the beginning */ rewind(p->in); /* read and write the file */ while(fgets(rdline,p->len+eor+1,p->in)) { rdline[p->len+1] = (char) NULL; rdline[p->len] = '\n'; fprintf(p->out,"%s",rdline); } /* free rdline */ free((void *) rdline); }