Files
Work/parts/main.cpp
2024-08-07 09:16:27 -04:00

116 lines
2.4 KiB
C++

#include <common/windows.hpp>
#include <common/array.hpp>
#include <common/string.hpp>
#include <common/winsvc.hpp>
#include <common/file.hpp>
#define MAX_BYTES 64000000
#define BUF_SIZE 1024000
void combine();
void divide(const String &pathFileName);
void main(int argc,char ** argv)
{
String command;
if(argc<2)
{
::printf("USAGE parts {divide[filename]|combine} {filename} ** if combine then filename \n");
return;
}
command=argv[1];
if(command=="divide")
{
if(argc!=3)
{
::printf("USAGE parts {divide[filename]|combine} {filename} ** if combine then filename \n");
return;
}
divide(argv[2]);
}
else if(command=="combine")combine();
else ::printf("unknown command\n");
}
void combine()
{
Array<BYTE> bytes;
File inFile;
File outFile;
DWORD part=0;
DWORD readCount=0;
String strPathFileName;
if(!outFile.open("result.dat","wb"))
{
::printf("unable to open output 'result.dat'\n");
return;
}
bytes.size(BUF_SIZE);
while(true)
{
::sprintf(strPathFileName,"part_%d.dat",part);
if(!inFile.open(strPathFileName,"rb"))
{
::printf("unable to open %s\n",strPathFileName.str());
return;
}
::printf("Reading %s\n",strPathFileName.str());
while(0!=(readCount=inFile.read((char*)&bytes[0],bytes.size())))
outFile.write((char*)&bytes[0],readCount);
inFile.close();
part++;
}
}
void divide(const String &strPathInput)
{
Array<BYTE> bytes;
DWORD part=0;
DWORD count=0;
DWORD byteCount=0;
DWORD totalCount=0;
String strPathFileName;
File inFile;
File outFile;
bytes.size(BUF_SIZE);
if(!inFile.open(strPathInput,"rb"))
{
::printf("Cannot open input\n");
return;
}
::sprintf(strPathFileName,"part_%d.dat",part);
if(!outFile.open(strPathFileName,"wb"))
{
::printf("cannot open output '%s'\n",strPathFileName.str());
return;
}
while(true)
{
if(count>MAX_BYTES)
{
outFile.close();
part++;
::sprintf(strPathFileName,"writing 'part_%d.dat'",part);
if(!outFile.open(strPathFileName,"wb"))
{
::printf("cannot open output '%s'\n",strPathFileName.str());
return;
}
count=0;
}
byteCount=inFile.read((char*)&bytes[0],bytes.size());
if(!byteCount)break;
if(!outFile.write(&bytes[0],byteCount))
{
::printf("Unable to write to '%s'\n",strPathFileName.str());
return;
}
count+=byteCount;
totalCount+=byteCount;
// if(!(totalCount%BUF_SIZE))::printf("Wrote %d bytes to %s\n",totalCount,strPathFileName.str());
}
}