ddc
OperationMeter.h
Go to the documentation of this file.
1 // DDC originally by Alexey Sokirko
2 // Changes and modifications 2011-2014 by Bryan Jurish
3 //
4 // This file is part of DDC.
5 //
6 // DDC is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // DDC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with DDC. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // OperationMeter.h: interface for the COperationMeter class.
20 //
22 
23 #ifndef __OPERATION_METER_RML_H__
24 #define __OPERATION_METER_RML_H__
25 
26 #if _MSC_VER > 1000
27 #pragma once
28 #endif // _MSC_VER > 1000
29 
30 #ifndef WIN32
31 typedef const char* LPCSTR;
32 #endif
33 
34 #include "assert.h"
35 
36 //----------------------------------------------------------------------------
37 // COperationMeter abstract class
38 //----------------------------------------------------------------------------
40 {
41 public:
42  virtual ~COperationMeterRML() {}
43 
44  DWORD GetMaxPos() const { return m_maxPos; }
45  DWORD GetPos() const { return m_curPos; }
46 
47  bool SetMaxPos( DWORD pos, DWORD count = 50 )
48  {
49  if( pos!=m_maxPos && (pos>=0 || m_curPos!=0) )
50  {
51  m_maxPos = pos;
52  SetStepCount(count); // коррекция шага
53  UpdateMaxPos();
54  // if( m_curPos>m_maxPos )
55  {
56  // m_curPos = m_maxPos;
57  m_curPos = m_pos = 0;
58  UpdatePos();
59  }
60  return true;
61  }
62  return false;
63  }
64 
65  bool SetPos( DWORD pos )
66  {
67  if( pos!=m_curPos && pos<=m_maxPos )
68  {
69  m_curPos = pos;
70  if( pos>=m_pos+GetStep() || pos<=m_pos-GetStep() || pos==m_maxPos )
71  {
72  m_pos = pos;
73  UpdatePos();
74  return true;
75  }
76  }
77  return false;
78  }
79 
80  DWORD GetStep() const
81  {
82  assert(m_step!=0 && m_step<=m_maxPos);
83  return m_step;
84  }
85 
86  void SetStep( DWORD step = 0)
87  {
88  m_step = (step==0 || step>=m_maxPos) ? max(m_maxPos/50,(DWORD)1) : step;
89  }
90 
91  void SetStepCount( DWORD count )
92  {
93  assert(count>0);
94  m_step = max(m_maxPos/count,(DWORD)1);
95  }
96 
97  bool AddPos( DWORD pos = 1)
98  {
99  assert(pos>0);
100  return SetPos(pos+m_curPos);
101  }
102 
103  virtual void SetInfo( LPCSTR info ) {}
104 
105  //__declspec(property(get=GetMaxPos, put=SetMaxPos)) DWORD MaxPos;
106  //__declspec(property(get=GetPos, put=SetPos)) DWORD Pos;
107 protected:
109 
110  virtual void UpdateMaxPos()=0;
111  virtual void UpdatePos()=0;
112 private:
117 
118 };
119 
120 //----------------------------------------------------------------------------
122 {
123 public:
125  virtual ~CSimpleMeterRML() {};
126 
127  virtual void SetInfo( LPCSTR info ) { printf("\n%s",info); }
128 protected:
129  virtual void UpdateMaxPos() {}
130  virtual void UpdatePos() { printf("\r %i%% ",GetPos()*100/GetMaxPos()); }
131 };
132 
133 //----------------------------------------------------------------------------
135 {
136 public:
137  CFileMeterRML() : m_fp(0) {}
138  virtual ~CFileMeterRML() {};
139 
140  bool SetFileMaxPos( FILE* fp )
141  {
142  if( fp!=NULL )
143  {
144  m_fp = fp;
145  int fileLen = 0;
146  if( fseek(fp,0,SEEK_END)==0 )
147  {
148  fileLen = ftell(fp);
149  if( fileLen>0 )
150  {
151  rewind(fp);
152  return SetMaxPos(fileLen);
153  }
154  }
155  }
156  return false;
157  }
158 
159  bool SetFilePos()
160  {
161  if( GetMaxPos()>0 && m_fp!=NULL )
162  return SetPos(ftell(m_fp));
163  return false;
164  }
165 
166 private:
167 
168  FILE* m_fp;
169 };
170 
171 
172 #endif
173 
174 /*--- emacs style variables ---
175  * Local Variables:
176  * mode: C++
177  * c-file-style: "ellemtel"
178  * c-basic-offset: 4
179  * tab-width: 8
180  * indent-tabs-mode: nil
181  * End:
182  */
COperationMeterRML()
Definition: OperationMeter.h:108
virtual ~COperationMeterRML()
Definition: OperationMeter.h:42
Definition: OperationMeter.h:39
CFileMeterRML()
Definition: OperationMeter.h:137
DWORD m_step
Definition: OperationMeter.h:116
virtual void UpdateMaxPos()
Definition: OperationMeter.h:129
DWORD GetPos() const
Definition: OperationMeter.h:45
CSimpleMeterRML()
Definition: OperationMeter.h:124
DWORD GetStep() const
Definition: OperationMeter.h:80
bool SetMaxPos(DWORD pos, DWORD count=50)
Definition: OperationMeter.h:47
virtual void SetInfo(LPCSTR info)
Definition: OperationMeter.h:127
virtual void UpdatePos()
Definition: OperationMeter.h:130
virtual void UpdatePos()=0
bool AddPos(DWORD pos=1)
Definition: OperationMeter.h:97
virtual void UpdateMaxPos()=0
virtual ~CFileMeterRML()
Definition: OperationMeter.h:138
bool SetPos(DWORD pos)
Definition: OperationMeter.h:65
virtual ~CSimpleMeterRML()
Definition: OperationMeter.h:125
FILE * m_fp
Definition: OperationMeter.h:168
DWORD m_curPos
Definition: OperationMeter.h:114
void SetStep(DWORD step=0)
Definition: OperationMeter.h:86
bool SetFileMaxPos(FILE *fp)
Definition: OperationMeter.h:140
void SetStepCount(DWORD count)
Definition: OperationMeter.h:91
virtual void SetInfo(LPCSTR info)
Definition: OperationMeter.h:103
const char * LPCSTR
Definition: OperationMeter.h:31
DWORD GetMaxPos() const
Definition: OperationMeter.h:44
DWORD m_pos
Definition: OperationMeter.h:115
bool SetFilePos()
Definition: OperationMeter.h:159
Definition: OperationMeter.h:121
uint32_t DWORD
Definition: utilit.h:105
DWORD m_maxPos
Definition: OperationMeter.h:113
Definition: OperationMeter.h:134