Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PercentPrinter.cpp « Console « UI « 7zip - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 34e4814365218d37346e0bdeb8b4dca1905eab7c (plain)
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
// PercentPrinter.cpp

#include "StdAfx.h"

#include "Common/IntToString.h"
#include "Common/String.h"

#include "PercentPrinter.h"

static const char *kPrepareString = "    ";
static const char *kCloseString = "\b\b\b\b    \b\b\b\b";
// static const char *kPercentFormatString =  "\b\b\b\b%3I64u%%";
static const char *kPercentFormatString1 = "\b\b\b\b";
static const int kNumDigits = 3;

CPercentPrinter::CPercentPrinter(UInt64 minStepSize):
  m_MinStepSize(minStepSize),
  m_PrevValue(0),
  m_CurValue(0),
  m_Total(1),
  m_ScreenPos(0),
  m_StringIsPrinted(false)
{
  for (int i = 0; i < kNumPercentSpaces; i++)
    m_Spaces[i] = ' ';
  m_Spaces[kNumPercentSpaces] = '\0';
}

void CPercentPrinter::PreparePrint()
{
  if (m_ScreenPos < kNumPercentSpaces)
    (*OutStream) << (m_Spaces + m_ScreenPos);
  m_ScreenPos  = kNumPercentSpaces;
  (*OutStream) << kPrepareString;
}

void CPercentPrinter::ClosePrint()
{
  (*OutStream) << kCloseString;
  m_StringIsPrinted = false;
}

void CPercentPrinter::PrintString(const char *s)
{
  m_ScreenPos += MyStringLen(s);
  (*OutStream) << s;
}

void CPercentPrinter::PrintString(const wchar_t *s)
{
  m_ScreenPos += MyStringLen(s);
  (*OutStream) << s;
}

void CPercentPrinter::PrintNewLine()
{
  m_ScreenPos = 0;
  (*OutStream) << "\n";
  m_StringIsPrinted = false;
}

void CPercentPrinter::SetRatio(UInt64 doneValue)
  { m_CurValue = doneValue; }

void CPercentPrinter::RePrintRatio()
{
  if (m_Total == 0)
    return;
  UInt64 ratio = m_CurValue * 100 / m_Total;
  char temp[32 + kNumDigits] = "    "; // for 4 digits;
  ConvertUInt64ToString(ratio, temp + kNumDigits);
  int len = (int)strlen(temp + kNumDigits);
  temp[kNumDigits + len] = '%';
  temp[kNumDigits + len + 1] = '\0';
  int pos = (len > kNumDigits)? kNumDigits : len;
  (*OutStream) << kPercentFormatString1;
  (*OutStream) << (temp + pos);
  OutStream->Flush(); 
  m_PrevValue = m_CurValue;
  m_StringIsPrinted = true;
}

void CPercentPrinter::PrintRatio()
{
  if (m_CurValue < m_PrevValue + m_MinStepSize || !m_StringIsPrinted)
    return;
  RePrintRatio();
}