-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.cpp
102 lines (81 loc) · 1.81 KB
/
split.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
#include <windows.h>
#include <stdio.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
HANDLE newFile( size_t cpt )
{
HANDLE hOut ;
cpt++ ;
TCHAR szOutputFilename[MAX_PATH];
sprintf( szOutputFilename, "%05d", cpt );
hOut = ::CreateFile( szOutputFilename,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
if ( hOut == INVALID_HANDLE_VALUE )
{
fprintf( stderr, "Err create output file\n" );
return INVALID_HANDLE_VALUE ;
}
printf("File %s...\n", szOutputFilename);
return hOut ;
}
BOOL byFile( HANDLE hIn, int size )
{
const int chunk_size = 1024 * size ;
const int BUFF_SIZE = 65535;
size_t cpt = 0 ;
HANDLE hOut = newFile(cpt) ;
size_t to_process = chunk_size ;
char* buff = new char[BUFF_SIZE] ;
DWORD toRead = BUFF_SIZE ;
DWORD read ;
while( ::ReadFile( hIn, buff, toRead, &read, NULL ) ) {
DWORD written ;
if ( ::WriteFile( hOut, buff, read, &written, NULL ) == FALSE )
{
fprintf( stderr, "Err write output file\n" );
return FALSE ;
}
if ( read != toRead )
break ; //eof
to_process -= read ;
if ( to_process == 0 )
{
::CloseHandle(hOut);
hOut = newFile(++cpt);
to_process = chunk_size ;
toRead = BUFF_SIZE ;
}
//take min value to nxt read
toRead = MIN( to_process, toRead );
}
delete []buff ;
::CloseHandle(hOut);
return TRUE ;
}
int main(int argc, char** argv)
{
if ( argc != 3 )
{
printf("Program usage:\n\t split.exe input_filename size\nWhere size are in kBytes\n");
return 1 ;
}
HANDLE hIn = ::CreateFile( argv[1],
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
size_t chunk_size = atoi( argv[2] );
if( hIn == INVALID_HANDLE_VALUE ) {
fprintf( stderr, "Error input file\n");
return 1 ;
}
byFile( hIn, chunk_size );
::CloseHandle( hIn );
return 0;
}