vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / ape_reader / MAClib / MACProgressHelper.cpp
blobb04ad4bdcd5b135a440535595102f48bbec0ccf8
1 #include "All.h"
2 #include "MACProgressHelper.h"
4 #include <OS.h>
6 CMACProgressHelper::CMACProgressHelper(int nTotalSteps, int * pPercentageDone, APE_PROGRESS_CALLBACK ProgressCallback, int * pKillFlag)
8 m_pKillFlag = pKillFlag;
10 m_bUseCallback = FALSE;
11 if (ProgressCallback != NULL)
13 m_bUseCallback = TRUE;
14 m_CallbackFunction = ProgressCallback;
17 m_pPercentageDone = pPercentageDone;
19 m_nTotalSteps = nTotalSteps;
20 m_nCurrentStep = 0;
21 m_nLastCallbackFiredPercentageDone = 0;
23 UpdateProgress(0);
26 CMACProgressHelper::~CMACProgressHelper()
31 void CMACProgressHelper::UpdateProgress(int nCurrentStep, BOOL bForceUpdate)
33 // update the step
34 if (nCurrentStep == -1)
35 m_nCurrentStep++;
36 else
37 m_nCurrentStep = nCurrentStep;
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;
44 // update the percent done pointer
45 if (m_pPercentageDone)
47 *m_pPercentageDone = nPercentageDone;
50 // fire the callback
51 if (m_bUseCallback)
53 if (bForceUpdate || (nPercentageDone - m_nLastCallbackFiredPercentageDone) >= 1000)
55 m_CallbackFunction(nPercentageDone);
56 m_nLastCallbackFiredPercentageDone = nPercentageDone;
61 int CMACProgressHelper::ProcessKillFlag(BOOL bSleep)
63 // process any messages (allows repaint, etc.)
64 if (bSleep)
66 PUMP_MESSAGE_LOOP
69 if (m_pKillFlag)
71 while (*m_pKillFlag == KILL_FLAG_PAUSE)
73 snooze(50*1000); // SHINTA: Replacement of Windows' SLEEP()
74 PUMP_MESSAGE_LOOP
77 if ((*m_pKillFlag != KILL_FLAG_CONTINUE) && (*m_pKillFlag != KILL_FLAG_PAUSE))
79 return -1;
83 return ERROR_SUCCESS;