-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleanse.c
162 lines (135 loc) · 3.59 KB
/
cleanse.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// cleanse.c -- Data cleansing tool.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <time.h>
#include "symtypes.h"
char inputFilename[ NAME_MAX ] = {0};
char outputFilename[ NAME_MAX ] = {0};
char testDataFilename[ NAME_MAX ] = {0};
char errorFilename[ NAME_MAX] = {0};
char schema[ NAME_MAX ] = {0};
double splitOption = 1.0;
#define getSRand() ( ( float ) rand( ) / ( float ) RAND_MAX )
#define prob( x ) ( getSRand( ) > x )
void usage( char* progname )
{
fprintf( stderr, "Usage: %s -i inputfile -o outputfile -c schema"
" [-s splitprob] [-p]\n", progname );
return;
}
int main( int argc, char *argv[] )
{
int opt;
working_t working;
FILE *fin, *fout, *ferr, *ftst;
int emitSummary = 0;
srand( time( NULL ) );
if ( argc == 1 )
{
usage( argv[0] );
return -1;
}
while ( ( opt = getopt( argc, argv, "i:o:s:c:p" ) ) != -1 )
{
switch( opt )
{
case 'i': // Input filename
strcpy( inputFilename, optarg );
break;
case 'o': // Output filename
strcpy( outputFilename, optarg );
strcat( outputFilename, ".dat" );
strcpy( errorFilename, optarg );
strcat( errorFilename, ".err" );
strcpy( testDataFilename, optarg );
strcat( testDataFilename, ".tst" );
break;
case 's': // Split probability
if ( sscanf( optarg, "%lg", &splitOption ) != 1 )
{
fprintf( stderr, "Unable to parse split probability (-s)\n" );
}
break;
case 'c': // Schema definition
strcpy( schema, optarg );
break;
case 'p': // Profile option
emitSummary = 1;
break;
default:
usage( argv[0] );
return -1;
break;
}
}
// Open file pointers
fin = fopen( inputFilename, "r" );
if ( fin == ( FILE * )0 )
{
fprintf( stderr, "Could not open input file %s.\n", inputFilename );
return -1;
}
fout = fopen( outputFilename, "w" );
if ( fout == ( FILE * )0 )
{
fprintf( stderr, "Could not open output file %s.\n", outputFilename );
fclose( fin );
return -1;
}
ferr = fopen( errorFilename, "w" );
if ( ferr == ( FILE * )0 )
{
fprintf( stderr, "Could not open error file %s.\n", errorFilename );
fclose( fin );
fclose( fout );
return -1;
}
if ( splitOption != 1.0 )
{
ftst = fopen( testDataFilename, "w" );
if ( ftst == ( FILE * )0 )
{
fprintf( stderr, "Could not open test data file %s.\n", testDataFilename );
fclose( fin );
fclose( fout );
fclose( ferr );
return -1;
}
}
InitializeSummary( );
while ( !feof( fin ) )
{
memset( &working, 0, sizeof( working ) );
fgets( working.line, NAME_MAX, fin );
if ( ( working.line[0] == 0 ) || ( working.line[0] == 13 ) ) continue;
parseLine( &working, schema );
if ( working.error )
{
fprintf( ferr, "# Error in field %d\n", working.errorField );
fprintf( ferr, "%s", working.line );
}
else
{
if ( prob( splitOption ) )
{
fprintf( ftst, "%s", working.line );
}
else
{
fprintf( fout, "%s", working.line );
}
}
}
if ( emitSummary )
{
Summarize( );
}
// Close file pointers
fclose( fin );
fclose( fout );
fclose( ferr );
return 0;
}