반응형

std::string StoreSCPService::GetDirectoryPath(std::string filePath)
{
	std::string dirPath;
	const size_t last_slash_idx = filePath.rfind('\\');
	if (std::string::npos != last_slash_idx)
	{
		dirPath = filePath.substr(0, last_slash_idx);
	}

	return dirPath;
}

std::string StoreSCPService::GetUniqueFilePath(std::string filePath)
{
	std::string dirPath = GetDirectoryPath(filePath);

	std::string fileName = base_name<std::string>(filePath);

	std::string newFileName = GetUniqueFileName(filePath, fileName);

	std::string newFilePath = string_format("%s%s", dirPath.c_str(), newFileName.c_str());

	return newFilePath;
}

std::string StoreSCPService::GetFileExtension(const std::string& FileName)
{
	if (FileName.find_last_of(".") != std::string::npos)
		return FileName.substr(FileName.find_last_of(".") + 1);
	return "";
}

std::string StoreSCPService::GetUniqueFileName(std::string filePath, std::string filename, int i)
{
	std::string dirPath = GetDirectoryPath(filePath);
	std::string name = base_name<std::string>(filePath);
	std::string path = filePath;

	if (name != filename)
	{
		path = string_format("%s%s", dirPath.c_str(), filename.c_str());
	}

	if (IsExist(path))
	{
		std::string ext = GetFileExtension(name);

		size_t lastindex = name.find_last_of(".");
		name = name.substr(0, lastindex);

		i++;

		std::string newFileName = string_format("%s_(%d).%s", name.c_str(), i, ext.c_str());

		filename = GetUniqueFileName(filePath, newFileName, i);
	}

	return filename;
}

 

반응형

+ Recent posts