MainForm.h
#pragma once
#include"FileSplitter.h"
class MainForm :public Form, public IProgress
{
TextBox *txtFilePath;
TextBox *txtFileNumber;
ProgressBar *progressBar;
TextBox *txtMethodID;
public:
void Button1_Click()
{
std::string filePath = txtFilePath->getText();
int number = atoi(txtFileNumber->getText().c_str());
int id = txtMethodID->getText();
FileSplitter splitter(filePath, number, id);
splitter.addIProgress(this);
splitter.Split();
splitter.eraseIprogress(this);
}
virtual void DoProgress(float value)
{
progressBar->steVaule(value);
}
};
FileSplitter.h
#pragma once
#include<string>
#include<list>
class IProgress
{
public:
virtual void DoProgress(float value) = 0;
virtual ~IProgress() {};
};
class Splitter
{
public:
static SplitterStrategy *createSplitter(int MethodId)
{
if (MethodId == 1) return new class PrefectSplitter ();
if (MethodId == 2) return new QuickSplitter();
if (MethodId == 3) return new RoughSplitter();
return nullptr;
}
};
class SplitterStrategy
{
public:
virtual void DoSplit(const std::string &filePath, int fileNumber) = 0;
virtual ~SplitterStrategy() = 0;
};
class QuickSplitter:public SplitterStrategy
{
public:
virtual void DoSplit(const std::string &filePath, int fileNumber)
{
//。。。
}
};
class RoughSplitter :public SplitterStrategy
{
public:
virtual void DoSplit(const std::string &filePath, int fileNumber)
{
//。。。
}
};
class PrefectSplitter :public SplitterStrategy
{
public:
virtual void DoSplit(const std::string &filePath, int fileNumber)
{
//。。。
}
};
class FileSplitter
{
std::string filePath;
int fileNumber;
int MethodID;
SplitterStrategy *strategy;
std::list<IProgress *> iProgresslist;
public:
FileSplitter(const std::string &filePath, int fileNumber, int id)
:filePath(filePath), fileNumber(fileNumber), MethodID(id)
{
strategy = Splitter::createSplitter(MethodID);
}
~FileSplitter()
{
delete strategy;
auto it = iProgresslist.begin();
while (it != iProgresslist.end())
{
delete (*it);
it++;
}
}
void Split()
{
// 1. 读取文件
// 2. 分批像小文件中写入
if (strategy == nullptr)
return;
for (size_t i = 0; i < fileNumber; i++)
{
strategy->DoSplit(filePath, i);
float progressValue = fileNumber;
progressValue = (i + 1) / progressValue;
onProgress(progressValue);
}
}
void addIProgress(IProgress *iprogress)
{
iProgresslist.push_back(iprogress);
}
void eraseIprogress(IProgress *iprogress)
{
iProgresslist.erase(iprogress);
}
protected:
virtual void onProgress(float vaule)
{
auto it = iProgresslist.begin();
while (it != iProgresslist.end())
{
(*it)->DoProgress(vaule);
it++;
}
}
};