TRF Language Model
wb-win.cpp
Go to the documentation of this file.
1 // You may obtain a copy of the License at
2 //
3 // http://www.apache.org/licenses/LICENSE-2.0
4 //
5 // Unless required by applicable law or agreed to in writing, software
6 // distributed under the License is distributed on an "AS IS" BASIS,
7 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8 // See the License for the specific language governing permissions and
9 // limitations under the License.
10 //
11 // Copyright 2014-2015 Tsinghua University
12 // Author: wb.th08@gmail.com (Bin Wang), ozj@tsinghua.edu.cn (Zhijian Ou)
13 //
14 // All h, cpp, cc, and script files (e.g. bat, sh, pl, py) should include the above
15 // license declaration. Different coding language may use different comment styles.
16 
17 
18 #include "wb-win.h"
19 #ifdef __linux
20 #include "wb-linux.h"
21 #else
22 #include <windows.h>
23 #endif
24 
25 namespace wb
26 {
27  string Title::m_global_title = "";
28  long long Title::m_precent_max = 0;
29  long long Title::m_precent_cur = 0;
30  int Title::m_precent_last = 0;
31  string Title::m_precent_label = "";
32 
33  void Title::SetGlobalTitle(const char *pstr)
34  {
35  m_global_title = pstr;
36  }
37  const char* Title::GetGlobalTitle()
38  {
39  return m_global_title.c_str();
40  }
41  void Title::Puts(const char *pstr)
42  {
43  char command[10 + cn_title_max_len];
44  sprintf(command, "title \"%s %s\"", m_global_title.c_str(), pstr);
45  system(command);
46  }
47  void Title::Precent(long long n, bool bNew /* = false */, long long nTotal /* = 100 */, const char* label /* = "Process" */)
48  {
49  bool bUpdate = false;
50  if (bNew)
51  {
52  m_precent_max = max(1LL, nTotal);
53  m_precent_cur = n;
54  m_precent_label = label;
56  bUpdate = true;
57  }
58  else
59  {
60  m_precent_cur = n;
61  int nNew = 100* m_precent_cur / m_precent_max;
62  if (nNew > m_precent_last)
63  {
64  m_precent_last = nNew;
65  bUpdate = true;
66  }
67  }
68 
69  if (bUpdate)
70  {
71  char outstr[cn_title_max_len];
72  sprintf(outstr, "%s%3d%%", m_precent_label.c_str(), m_precent_last);
73  Puts(outstr);
74  }
75  }
76  void Title::Precent(ifstream &ifile, bool bNew /* = false */, const char* label /* = "" */)
77  {
78  if (bNew)
79  {
80  size_t nCur = ifile.tellg();
81  ifile.seekg(0, ios_base::end);
82  Precent(nCur, true, ifile.tellg(), label);
83  ifile.seekg(nCur);
84  }
85  else
86  {
87  Precent(ifile.tellg());
88  }
89  }
90  void Title::Precent(FILE *fp, bool bNew /* = false */, const char* label /* = "" */)
91  {
92  if (bNew)
93  {
94  long long nCur = _ftelli64(fp);
95  _fseeki64(fp, 0, SEEK_END);
96  Precent(nCur, true, _ftelli64(fp), label);
97  _fseeki64(fp, nCur, SEEK_SET);
98  }
99  else
100  {
101  Precent(_ftelli64(fp));
102  }
103  }
104  void Title::Fraction(long long n, bool bNew /* = false */, long long nTotal /* = 100 */, const char* label /* = "Process" */)
105  {
106  if (bNew)
107  {
108  m_precent_max = max(1LL, nTotal);
109  m_precent_label = label;
110  }
111  m_precent_cur = n;
112 
113  char outstr[cn_title_max_len];
114  sprintf(outstr, "%s%d/%d", m_precent_label.c_str(), n, m_precent_max);
115  Puts(outstr);
116  }
117 
119  {
120  m_bWork = false;
121  m_nBeginTime = 0;
122  m_nEndTime = 0;
123  }
124 
126  {
127  m_bWork = false;
128  m_nBeginTime = 0;
129  m_nEndTime = 0;
130  }
131 
133  {
134  m_bWork = false;
135  m_nBeginTime = 0;
136  m_nEndTime = 0;
137  }
138  clock_t Clock::Begin()
139  {
140  m_bWork = true;
141  return (m_nBeginTime = clock());
142  }
143  clock_t Clock::End()
144  {
145  if (m_bWork == false)
146  return 0.0;
147  m_bWork = false;
148  m_nEndTime = clock();
149  return m_nEndTime - m_nBeginTime;
150  }
151  clock_t Clock::Get()
152  {
153  return clock() - m_nBeginTime;
154  }
155  void Clock::Sleep(clock_t n)
156  {
157  Begin();
158  while (Get() < n) {
159  }
160  }
161 
162 #ifndef __linux
163  Path::Path(const char *path/* =NULL */) : m_paFiles(NULL)
164  {
165  Reset(path);
166  }
168  {
169  Reset(NULL);
170  }
171  void Path::Reset(const char *path /* = NULL */)
172  {
173  // clean
174  m_input = "";
175  if (m_paFiles) {
176  char *pp;
177  while (m_paFiles->Out(&pp)) {
178  delete [] pp;
179  }
180  m_paFiles->Clean();
181  }
182 
183  // Reset
184  if (path) {
185  m_input = path;
186  SearchFiles();
187  }
188  }
189  bool Path::GetPath(char *path)
190  {
191  if (path == NULL)
192  return false;
193  if (m_paFiles == NULL)
194  {
195  SearchFiles();
196  }
197 
198  char *pTemp;
199  if (false == m_paFiles->Out(&pTemp))
200  return false;
201  strcpy(path, pTemp);
202  delete [] pTemp;
203  return true;
204  }
205  void Path::SearchFiles()
206  {
207  if (!m_paFiles)
208  m_paFiles = new Queue<char*>;
209  m_paFiles->Clean();
210 
211  // Find the paths
212  Array<char*> aPaths;
213  char *p = strtok(&m_input[0], "+\"");
214  while (p) {
215  aPaths.Add(p);
216  p = strtok(NULL, "+\"");
217  }
218 
219  // Find the files
220  for (int i = 0; i < aPaths.GetNum(); i++)
221  {
222  //process fileName
223  WIN32_FIND_DATAA fd;
224  BOOL bRed = TRUE;
225  HANDLE hFile = FindFirstFileA(aPaths[i], &fd);
226  while (hFile != INVALID_HANDLE_VALUE && bRed)
227  {
228  char *p = new char[MAX_PATH_LEN];
229  strcpy(p, aPaths[i]);
230  char *temp = strrchr(p, '\\');
231  if (temp == NULL)
232  temp = p;
233  else
234  temp++;
235  *temp = '\0';
236  strcat(p, fd.cFileName);
237 
238  m_paFiles->In(p);
239 
240  bRed = FindNextFileA(hFile, &fd);
241  }
242  }
243  }
244 #endif //__linux
245 
246  void Pause()
247  {
248  cout << "[Press any key to continue...]" << endl;
249  getch();
250  }
251  void outPrecent(long long n, bool bNew /*= false*/, long long nTotal /*= 100*/, const char* title /*= "Process"*/)
252  {
253  static int snLastPrecent = 0;
254  static long long snTotal = 100;
255  static char strTitle[500];
256 
257  static clock_t begtime = 0;
258 
259  bool bUpdate = false;
260  if (bNew)
261  {
262  if (nTotal > 0)
263  snTotal = nTotal;
264  snLastPrecent = n * 100 / snTotal;
265  strcpy(strTitle, title);
266  bUpdate = true;
267 
268  begtime = clock();
269  }
270  else
271  {
272 
273  int nNew = (int)(1.0*n / snTotal * 100);
274  if (nNew > snLastPrecent)
275  {
276  snLastPrecent = nNew;
277 
278  bUpdate = true;
279  }
280  }
281 
282  if (bUpdate)
283  {
284  if (bNew)
285  cout << title << ":";
286  else
287  cout << "\b\b\b\b";
288 
289  cout.width(3);
290  cout << snLastPrecent << "%";
291  }
292  }
293 
294  void outPrecent(ifstream &ifile, bool bNew, const char* title)
295  {
296  if (bNew)
297  {
298  ifile.seekg(0, ios_base::end);
299  outPrecent(0, true, ifile.tellg(), title);
300  ifile.seekg(0, ios_base::beg);
301  }
302  else
303  {
304  outPrecent(ifile.tellg());
305  }
306  }
307 
308  void outPrecent(FILE* fp, bool bNew, const char* title)
309  {
310  if (bNew)
311  {
312  long long iCur = _ftelli64(fp);
313  _fseeki64(fp, 0, SEEK_END);
314  outPrecent(iCur, true, _ftelli64(fp), title);
315  _fseeki64(fp, iCur, SEEK_SET);
316  }
317  else
318  {
319  outPrecent(_ftelli64(fp));
320  }
321  }
322 
323 }
bool Out(T *p_pT)
Move a value outof queue.
Definition: wb-vector.h:451
void outPrecent(long long n, bool bNew, long long nTotal, const char *title)
print precent in the cmd window
Definition: wb-win.cpp:251
static long long m_precent_cur
for Precent function, the current value
Definition: wb-win.h:70
A queue based the dynamic memory mangement.
Definition: wb-vector.h:431
static void SetGlobalTitle(const char *pstr)
set the global title
Definition: wb-win.cpp:33
const int cn_title_max_len
Definition: wb-win.h:58
Clock(void)
Definition: wb-win.cpp:118
clock_t Get()
get the time, but don&#39;t stop recording
Definition: wb-win.cpp:151
Queue< char * > * m_paFiles
store all the files find in the queue
Definition: wb-win.h:144
~Clock(void)
Definition: wb-win.cpp:125
void Clean()
clean the queue. Donot release the memory
Definition: wb-vector.h:443
static const char * GetGlobalTitle()
get the global title
Definition: wb-win.cpp:37
static string m_precent_label
the precent output label
Definition: wb-win.h:72
void Pause()
pause
Definition: wb-win.cpp:246
static long long m_precent_max
for Precent function, the maxiumn value
Definition: wb-win.h:69
clock_t Begin()
begin to record
Definition: wb-win.cpp:138
#define MAX_PATH_LEN
Definition: wb-win.h:127
Provide the toolkits for cmd window of window platform.
static string m_global_title
the global title. All the other information are follow the global title
Definition: wb-win.h:68
string m_input
stroe the input string
Definition: wb-win.h:143
static void Fraction(long long n=m_precent_cur+1, bool bNew=false, long long nTotal=100, const char *label="")
output rate to title
Definition: wb-win.cpp:104
static void Puts(const char *pStr)
output string to title
Definition: wb-win.cpp:41
static int m_precent_last
record the last output precent value (from 0 to 100)
Definition: wb-win.h:71
int GetNum() const
Get Array number.
Definition: wb-vector.h:240
void Add(T t)
Add a value to the tail of array.
Definition: wb-vector.h:242
static void Precent(long long n=m_precent_cur+1, bool bNew=false, long long nTotal=100, const char *label="")
output precent to title
Definition: wb-win.cpp:47
Path(const char *path=NULL)
Definition: wb-win.cpp:163
void Reset(const char *path=NULL)
reset the path
Definition: wb-win.cpp:171
clock_t End()
record end and return the time
Definition: wb-win.cpp:143
bool GetPath(char *path)
Get pathes.
Definition: wb-win.cpp:189
void In(T p_t)
Add a value into queue.
Definition: wb-vector.h:445
void Clean()
clean the clock
Definition: wb-win.cpp:132
define all the code written by Bin Wang.
Definition: wb-file.cpp:21
Dynamic array.
Definition: wb-vector.h:205
void Sleep(clock_t n)
wait for sveral millissecond
Definition: wb-win.cpp:155