- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyFast.cpp
More file actions
Latest commit
130 lines (120 loc) · 2.62 KB
/
Copy pathCopyFast.cpp
File metadata and controls
130 lines (120 loc) · 2.62 KB
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
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cstdio>
#include"CopyFast.h"
usingnamespacestd;
FILE *tmp_file = fopen("tmpfile.tmp", "w");
int size_origin_file = 0;
int size_target_file = 0;
intstrsize(char *str)
{
int i = 0;
while(str[i++] != '\0'){}
return (i);
}
intfilesize(char *f_name)
{
char c[100];
FILE* f = fopen(f_name, "r");
int k = 0;
while(fgets(c, sizeof c, f))
{
k++;
}
return (k);
}
// x^y
intpow(int x, int y)
{
int i = 1, k = x;
for(i; i < y; i++)
{
x*= k;
}
return x;
}
intStrToInt(char *str)
{
if(str[0] == '\0')
{
return (-1);
}
int k = 0;
int i = 0;
int p = pow(10, strsize(str) - 1);
while(str[i] != '\0'){
if(str[i] > 47 && str[i] < 58)
{
k += (str[i++] - 48) * p;
p /= 10;
}
else
{
cout << "'" << str << "' isn't a number" << endl;
return -1;
}
}
return (k/10);
}
//write the content of filename from x to y to tmpfile
voidwrite_file(FILE *f, int x, int y)
{
char line[1000];
int k = 0;
for(k; k < x && fgets(line, sizeof line, f); k++);
for(k; k < y && fgets(line, sizeof line, f); k++)
{
printf("%s\n", line);
fputs(line, tmp_file);
}
}
interror(FILE *origin_f, FILE *target_f, int l1, int l2, int l3)
{
if(l1 == -1 || l2 == -1 || l3 == -1)
return -1;
if(l1 > size_origin_file || l2 > size_origin_file || l3 > size_target_file)
{
printf("lines must not be out of the files");
return -1;
}
if(l1 > l2)
{
cout << "'" << l2 << "' doesn't be sup than '" << l1 << "'" << endl;
return -1;
}
if(origin_f == NULL)
{
std::cout << "origin file doesnt exist" << endl;
return -1;
}
if(target_f == NULL)
{
std::cout << "target file doesnt exist" << endl;
return -1;
}
return0;
}
//Algorithm
//make tmp file
//write in tmpfile: read target_f [0; l3[
//write in tmpfile: read origin_f [l1; l2]
//write in tmpfile: read target_f [l3; EoF[
//rename tmp file: target_f
voidfile_copy(char *origin_f, char *l_1, char *l_2, char *target_f, char *l_3)
{
int l1 = StrToInt(l_1), l2 = StrToInt(l_2), l3 = StrToInt(l_3);
FILE *origin_file = fopen(origin_f, "r");
FILE *target_file = fopen(target_f, "r");
size_origin_file = filesize(origin_f);
size_target_file = filesize(target_f);
if(error(origin_file, target_file, l1, l2, l3) == -1)
return;
write_file(target_file, 0, l3);
write_file(origin_file, l1, l2);
write_file(target_file, l3+1, size_target_file);
rename("tmpfile.tmp", target_f);
fclose(origin_file);
fclose(target_file);
fclose(tmp_file);
}