File: cleaner.c

package info (click to toggle)
libticalcs3 3.3.5-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,824 kB
  • ctags: 1,880
  • sloc: ansic: 18,809; sh: 9,396; makefile: 349; sed: 93
file content (126 lines) | stat: -rw-r--r-- 3,003 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*  manpage_cleaner - remove the repetition of characters
 *  Copyright (C) 2000  Romain Lievin
 *
 *  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 <stdio.h>
#include <strings.h>
#include <unistd.h>

#define MAXCHARS 256

int main(int argc, char **argv)
{
  char filename[MAXCHARS];
  char filename2[MAXCHARS];
  char filename3[MAXCHARS];
  FILE *in;
  FILE *tmp;
  FILE *out;
  char buffer[3];
  
  /* Retrieve the command line argument */
  if(argc < 2)
    {
      fprintf(stderr, "You must give a filename on the command line.\n");
      exit(1);
    }
  strcpy(filename, argv[1]);
  strcpy(filename2, filename);
  strcat(filename2, ".tmp");
  strcpy(filename3, filename);
  strcat(filename3, ".txt");
  
  fprintf(stdout, "Processing file <%s>:\n", filename);
  fprintf(stdout, "Pass 1... ");
  
  /* Open the file for reading */
  in = fopen(filename, "rb");
  if(in == NULL)
    {
      fprintf(stderr, "Unable to open this file: <%s>\n", filename);
      exit(1);
    }
  
  /* Open a temporary file fpr writing */
  out = fopen(filename2, "wb");
  if(out == NULL)
    {
      fprintf(stderr, "Unable to open this file: <%s>\n", filename2);
      exit(1);
    }
  
  /* Process the file for removing backspace sequences */
  while(!feof(in))
    {
      buffer[0] = fgetc(in);
      if(feof(in))
	{
	  fputc(buffer[0], out);
	  break;
	}
      buffer[1]= fgetc(in);
  
      if(buffer[0] == '\b')
	{
	  continue; // Skip the char and BS
	}
      if(buffer[1] == '\b')
	{
	  fputc(fgetc(in), out); // Skip the 2 previous chars
	  continue;
	}
      fputc(buffer[0], out);
      fputc(buffer[1], out);
    }
  fprintf(stdout, "Done.\n");
  
  /* Close the files */
  fclose(in);
  fclose(out);
  
  fprintf(stdout, "Pass 2... ");
  
  /* Open the temporary file and another file */
  in = fopen(filename2, "rb");
  if(in == NULL)
    {
      fprintf(stderr, "Unable to open this file: <%s>\n", filename);
      exit(1);
    }
  
  out = fopen(filename3, "wb");
  if(out == NULL)
    {
      fprintf(stderr, "Unable to open this file: <%s>\n", filename2);
      exit(1);
    }
  
  /* Copy the file */
  while(!feof(in))
    {
      if(feof(in)) break;
      fputc(fgetc(in), out);
    }
  
  /* Close files */
  fclose(in);
  fclose(out);
  unlink(filename2);
  fprintf(stdout, "Done.\n");
  
  return 0;
}