-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfmerge.cpp
136 lines (117 loc) · 3.15 KB
/
fmerge.cpp
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
/*
* merge files for compression
* options:(only regular or standard mode ans random access)
* 0--regular
//* 1--lpaq (not used in this code)
* 2--random access
*/
//
int fmerge(int opt, char * argv[], int nproc) {
char FileName[200];//name of processed Wig file
clock_t start = clock();
FILE *fpResult=fopen("result","a");
if (fpResult == NULL) {
fprintf(stderr,"Can't open output result file!\n");
exit(1);
}
strcpy(FileName, argv[1]);
int N=20;
//list of files to delete given option index
char dlist[3][21][40]={
{"DiffLoc1Ext","DiffLocSeq","DiffLocSeq1", "DiffLocSeq2","DiffValSeq","DiffLocSeqMatlab1", "DiffLocSeqMatlab2","DiffValSeqMatlab","DiffLocSeqMatlab1Code", "DiffLocSeqMatlab2Code","DiffValSeqMatlabCode","ExceedLoc","ExceedVal","DiffLoc1", "Extra","DiffLoc2","ChrmName","SpanName","DiffLocRunLength","DiffVal", "Jump"},
{"DiffSeq","LenDiffSeq","DiffSeqMatlab","LenDiffSeqMatlab"},
{"DiffSeq","LenDiffSeq","DiffSeqMatlab","LenDiffSeqMatlab"},
};
char Type[21][40];//files to merge
char T[21][40];//files to delete
char c[21][10000];
char d[21][10000];
FILE *fp[21];
FILE *fpout;
long int sz[21];
int i,j;
char *buffer;
char str[2],tmpchar[1000];
for (i=0; i<21; i++){
for (j=0; j<40; j++){
Type[i][j] = list[opt][i][j];
T[i][j] = dlist[opt][i][j];
}
}
i=1;
while (i<21 && Type[i][0]){
strcpy(c[i],FileName);
strcat(c[i],Type[i]);
i++;
}
N = i;
//output file
strcpy(c[0],argv[2]);
fpout = fopen(c[0], "w");
if (fpout == NULL) {
fprintf(stderr, "Can't open file %s!\n",c[0]);
exit(1);
}
//compression option
//if (opt == 3)
// fprintf(fpout,"2 "); //prll is same as random access
//else
fprintf(fpout,"%d ",opt);
/*switch (opt){
case 2://random access
fprintf(fpout,"%d ",BlockSize);
//fprintf(fpout,"%d ",int(log10(SetFloatSize)));
break;
case 3://random access and parallel
fmergeadd(nproc,argv); //merge block address
for (i=1;i<N;i++){//merge sub files
if (i<2 || i>8) //except BlockAdd and Diff (Count) files
fmergesub(nproc,c[i]);
}
fprintf(fpout,"%d ",BlockSize);
//fprintf(fpout,"%d ",int(log10(SetFloatSize)));
break;
}*/
//file sizes in first line of output file
for (i=1; i<N; i++){
fp[i] = fopen(c[i], "r");
if (fp[i] == NULL) {
fprintf(stderr, "Can't open file %s!\n",c[i]);
exit(1);
}
//file size
sz[i]=fsize(fp[i]);
fprintf(fpout,"%ld ",sz[i]);
//printf("size i %ld\n",sz[i]);
}
//copy every file
for (i=1; i<N; i++){
buffer = (char*)malloc(sz[i]*sizeof(char));
if(buffer == NULL){
printf("Error! Allocation was not successful.\n");
exit(1);
}
fread(buffer,sizeof(char),sz[i],fp[i]);
fwrite(buffer,sizeof(char),sz[i],fpout);
fclose(fp[i]);
free(buffer);
//remove(c[i]);
}
//get compressed file size
rewind(fpout);
sz[0]=fsize(fpout);
fprintf(fpResult,"output %s\nsize %ld\n",c[0],sz[0]);
fclose(fpout);
//remove useless file
i=0;
while (i<21 && T[i][0]){
strcpy(d[i],FileName);
strcat(d[i],T[i]);
remove(d[i]);
//printf("file %s removed\n",d[i]);
i++;
}
clock_t end = clock();
fprintf(fpResult,"fmerge takes %2.3f sec.\n", (double)(end - start)/CLOCKS_PER_SEC);
return 0;
}