1#include "All.h"
2#include "MACProgressHelper.h"
3
4#include <OS.h>
5
6CMACProgressHelper::CMACProgressHelper(int nTotalSteps, int * pPercentageDone, APE_PROGRESS_CALLBACK ProgressCallback, int * pKillFlag)
7{
8    m_pKillFlag = pKillFlag;
9
10    m_bUseCallback = FALSE;
11    if (ProgressCallback != NULL)
12    {
13        m_bUseCallback = TRUE;
14        m_CallbackFunction = ProgressCallback;
15    }
16
17    m_pPercentageDone = pPercentageDone;
18
19    m_nTotalSteps = nTotalSteps;
20    m_nCurrentStep = 0;
21    m_nLastCallbackFiredPercentageDone = 0;
22
23    UpdateProgress(0);
24}
25
26CMACProgressHelper::~CMACProgressHelper()
27{
28
29}
30
31void CMACProgressHelper::UpdateProgress(int nCurrentStep, BOOL bForceUpdate)
32{
33    // update the step
34    if (nCurrentStep == -1)
35        m_nCurrentStep++;
36    else
37        m_nCurrentStep = nCurrentStep;
38
39    // figure the percentage done
40    float fPercentageDone = float(m_nCurrentStep) / float(max(m_nTotalSteps, 1));
41    int nPercentageDone = (int) (fPercentageDone * 1000 * 100);
42    if (nPercentageDone > 100000) nPercentageDone = 100000;
43
44    // update the percent done pointer
45    if (m_pPercentageDone)
46    {
47        *m_pPercentageDone = nPercentageDone;
48    }
49
50    // fire the callback
51    if (m_bUseCallback)
52    {
53        if (bForceUpdate || (nPercentageDone - m_nLastCallbackFiredPercentageDone) >= 1000)
54        {
55            m_CallbackFunction(nPercentageDone);
56            m_nLastCallbackFiredPercentageDone = nPercentageDone;
57        }
58    }
59}
60
61int CMACProgressHelper::ProcessKillFlag(BOOL bSleep)
62{
63    // process any messages (allows repaint, etc.)
64    if (bSleep)
65    {
66        PUMP_MESSAGE_LOOP
67    }
68
69    if (m_pKillFlag)
70    {
71        while (*m_pKillFlag == KILL_FLAG_PAUSE)
72        {
73			snooze(50*1000);	// SHINTA: Replacement of Windows' SLEEP()
74            PUMP_MESSAGE_LOOP
75        }
76
77        if ((*m_pKillFlag != KILL_FLAG_CONTINUE) && (*m_pKillFlag != KILL_FLAG_PAUSE))
78        {
79            return -1;
80        }
81    }
82
83    return ERROR_SUCCESS;
84}
85