size_t StatGetter::GetAvgSizeAllFiles() const
{
    const int totalFilesCount = GetTotalFilesCount();

    if (totalFilesCount == 0)
        return 0;

    return GetTotalFilesSize() / totalFilesCount;
}
void vmsTpDownloadMgr::GetSectionInfo(int nIndex, vmsSectionInfo *sect)
{
	vmsAUTOLOCKSECTION (m_csDownload);
	
	UINT64 uTotal = GetTotalFilesSize ();
	int ns = GetNumberOfSections();
	UINT64 uPerPiece = uTotal / ns;
	
	sect->uDStart = nIndex * uPerPiece;
	sect->uDEnd = nIndex == ns-1 ? uTotal : sect->uDStart + uPerPiece - 1;
	sect->uDCurrent = GetDownloadedBytesCount();
}
void StatGetter::FillWidgetTable()
{
    tableWidget_->setColumnCount(2);
    tableWidget_->setHorizontalHeaderLabels(QStringList() << "Name" << "Value");
    // количество групп + общая статистика + количество каталогов в текущем
    const int groupCount = statTree_.size() + 1;
    const int blankLineCount = groupCount - 1;
    const int rowOnGroup = 4;
    const int spaceForDirsInCurDir = 2;
    const int rowCount = groupCount * rowOnGroup + blankLineCount +
            spaceForDirsInCurDir;
    tableWidget_->setRowCount(rowCount);

    int rowNumber = 0;

    for (auto statPair : statTree_)
    {
        const QString groupName = statPair.first.isEmpty() ?
                    "*no extention*" :
                    "*." + statPair.first;

        AddTableRow(rowNumber, groupName);
        AddTableRow(rowNumber, "Files count", &statPair.second.count);
        AddTableRow(rowNumber, "Files size", &statPair.second.size);
        const size_t avgSize = statPair.second.size / statPair.second.count;
        AddTableRow(rowNumber, "Avg size", &avgSize);
        ++rowNumber;
    }

    AddTableRow(rowNumber, "Total");
    const size_t totalFilesCount = GetTotalFilesCount();
    AddTableRow(rowNumber, "Total files count", &totalFilesCount);
    const size_t totalFilesSize = GetTotalFilesSize();
    AddTableRow(rowNumber, "Total files count", &totalFilesSize);
    const size_t avgSizeAllFiles = GetAvgSizeAllFiles();
    AddTableRow(rowNumber, "Avg by all files size", &avgSizeAllFiles);
    ++rowNumber;

    AddTableRow(rowNumber, "Subdirs count", &subdirsInCurPathCount_);
}
void vmsTpDownloadMgr::getObjectItselfStateBuffer(LPBYTE pb, LPDWORD pdwSize, bool bSaveToStorage)
{
	vmsAUTOLOCKSECTION (m_csDownload);

	#define CHECK_SIZE(need) {if (lpBuffer != NULL && *pdwSize < (UINT)(lpBuffer + need - pb)) return;}

	LPBYTE lpBuffer = pb;

	DWORD dwNeedSize;
	dwNeedSize = sizeof (int) + m_info.strTorrentUrl.GetLength();
	dwNeedSize += sizeof (int) + m_info.strOutputPath.GetLength();
	dwNeedSize += sizeof (int) + m_info.strFileName.GetLength();
	dwNeedSize += sizeof (int); 
	dwNeedSize += sizeof (int); 
	dwNeedSize += sizeof (UINT64); 

	dwNeedSize += sizeof (m_info.bDone);
	dwNeedSize += sizeof (m_info.fPercentDone);
	dwNeedSize += sizeof (m_info.nDownloadedBytes);
	
	
	
	

	*pdwSize = dwNeedSize;

	if (pb == NULL) {
		return;
	}

	int i;

	
	i = strlen(m_info.strTorrentUrl);
	CHECK_SIZE (sizeof (int));
	CopyMemory (lpBuffer, &i, sizeof (int));
	lpBuffer += sizeof (int);
	CHECK_SIZE (i);
	CopyMemory (lpBuffer, m_info.strTorrentUrl, i);
	lpBuffer += i;
	
	
	i = strlen(m_info.strOutputPath);
	CHECK_SIZE (sizeof (int));
	CopyMemory (lpBuffer, &i, sizeof (int));
	lpBuffer += sizeof (int);
	CHECK_SIZE (i);
	CopyMemory (lpBuffer, m_info.strOutputPath, i);
	lpBuffer += i;

	
	i = strlen(m_info.strFileName);
	CHECK_SIZE (sizeof (int));
	CopyMemory (lpBuffer, &i, sizeof (int));
	lpBuffer += sizeof (int);
	CHECK_SIZE (i);
	CopyMemory (lpBuffer, m_info.strFileName, i);
	lpBuffer += i;
	

	
	i = GetStreamingSpeed (false);
	CHECK_SIZE (sizeof (int));
	CopyMemory (lpBuffer, &i, sizeof (int));
	lpBuffer += sizeof (int);

	
	m_info.nNumSections = GetNumberOfSections (false);
	CHECK_SIZE (sizeof (int));
	CopyMemory (lpBuffer, &m_info.nNumSections, sizeof (int));
	lpBuffer += sizeof (int);

	
	UINT64 fileSize = GetTotalFilesSize (false);
	CHECK_SIZE (sizeof (UINT64));
	CopyMemory (lpBuffer, &fileSize, sizeof (UINT64));
	lpBuffer += sizeof (UINT64);

	m_info.bDone = IsDone();
	CHECK_SIZE (sizeof (m_info.bDone));
	CopyMemory (lpBuffer, &m_info.bDone, sizeof (m_info.bDone));
	lpBuffer += sizeof (m_info.bDone);

	m_info.fPercentDone = GetPercentDone(false);
	CHECK_SIZE (sizeof (m_info.fPercentDone));
	CopyMemory (lpBuffer, &m_info.fPercentDone, sizeof (m_info.fPercentDone));
	lpBuffer += sizeof (m_info.fPercentDone);

	m_info.nDownloadedBytes = GetDownloadedBytesCount(false);
	CHECK_SIZE (sizeof (m_info.nDownloadedBytes));
	CopyMemory (lpBuffer, &m_info.nDownloadedBytes, sizeof (m_info.nDownloadedBytes));
	lpBuffer += sizeof (m_info.nDownloadedBytes);

	*pdwSize = lpBuffer - pb;

	return;
}