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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
//
// Copyright (C) 2010,2011 Bartek thindil Jasicki
//
// This file is part of Grubng
//
// Grubng is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
using System;
namespace urlsdb
{
/// <summary>
/// Class provide various utility functions.
/// </summary>
internal sealed class Utils : IDisposable
{
/// <summary>
/// If true, don't show messages.
/// </summary>
bool quiet;
/// <summary>
/// Amount of data to process.
/// </summary>
int amount;
/// <summary>
/// Current processed amount of data.
/// </summary>
int curamount;
/// <summary>
/// Timer to update informations about work progress.
/// </summary>
System.Timers.Timer progress;
/// <summary>
/// Length of message to display during progress informations.
/// </summary>
int length;
/// <summary>
/// Message to show with work progress informations.
/// </summary>
string message;
/// <summary>
/// If true, all disposable fields are disposed.
/// </summary>
bool disposed;
/// <summary>
/// Standard class constructor
/// </summary>
/// <param name="q">
/// A <see cref="System.Boolean"/> if true, don't show messages.
/// </param>
public Utils(bool q)
{
this.quiet = q;
Console.Clear();
}
/// <summary>
/// Function show message to user.
/// </summary>
/// <param name="message">
/// A <see cref="System.String"/> message to show to user.
/// </param>
/// <param name="newline">
/// A <see cref="System.Boolean"/> if true, add new line after message.
/// </param>
public void Message(string message, bool newline)
{
if (this.quiet)
{
return;
}
if (this.disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
if (newline)
{
Console.WriteLine(message);
}
else
{
Console.Write(message);
}
}
/// <summary>
/// Function start timer for show work progress informations.
/// </summary>
/// <param name="a">
/// A <see cref="System.Int32"/> amount of data to process.
/// </param>
/// <param name="msg">
/// A <see cref="System.String"/> message to show with progress informations.
/// </param>
public void ProgressStart(int a, string msg)
{
if (this.disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
this.amount = a;
this.curamount = 0;
this.length = 0;
this.message = msg;
this.progress = new System.Timers.Timer();
this.progress.Interval = 1000;
this.progress.Elapsed += new System.Timers.ElapsedEventHandler(OnProgress);
this.progress.Start();
}
/// <summary>
/// Function stop timer for show work progress informations.
/// </summary>
public void ProgressStop()
{
if (this.disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
if (this.amount > 0)
{
this.curamount = this.amount;
}
this.OnProgress(null, null);
this.progress.Stop();
}
/// <summary>
/// Timer for show work progress informations.
/// </summary>
/// <param name="sender">
/// A <see cref="System.Object"/> unused.
/// </param>
/// <param name="e">
/// A <see cref="System.Timers.ElapsedEventArgs"/> unused.
/// </param>
void OnProgress(object sender, System.Timers.ElapsedEventArgs e)
{
if (this.quiet)
{
return;
}
double percent = 0;
string msg;
int newpos = Console.CursorLeft - this.length;
if (this.amount > 0)
{
percent = Math.Ceiling(((double)this.curamount / (double)this.amount * 100));
msg = "(" + percent.ToString() + " % done)";
}
else
{
msg = this.curamount.ToString() + " " + this.message;
}
this.length = msg.Length;
Console.SetCursorPosition(newpos, Console.CursorTop);
this.Message(msg, false);
}
/// <summary>
/// Function write message to log files.
/// </summary>
/// <param name="message">
/// A <see cref="System.String"/> message to write to log file.
/// </param>
public static void WriteLog(string message)
{
string date = DateTime.UtcNow.ToString("yyyy/MM/dd/HH:mm:ss");
using (System.IO.StreamWriter logstream = new System.IO.StreamWriter("error.log", true))
{
logstream.WriteLine(date + " " + message);
logstream.Close();
}
}
/// <summary>
/// Function dispose unmanaged resources.
/// </summary>
public void Dispose()
{
if (this.progress != null)
{
this.progress.Dispose();
}
this.disposed = true;
}
/// <summary>
/// Property set/read private field curamount.
/// </summary>
public int Curamount
{
set {this.curamount = value;}
get {return this.curamount;}
}
}
}
|