Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CompletionInfo.cs « corcompare « tools « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ab497f31eb483f81b41498fb45cce05d3954459f (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Mono.Util.CorCompare.CompletionInfo
//
// Author(s):
//   Piers Haken (piersh@friskit.com)
//
// (C) 2001-2002 Piers Haken

using System;
using System.Reflection;
using System.Xml;
using System.Collections;

namespace Mono.Util.CorCompare 
{
	#region
	public struct CompletionType
	{
		private enum CompletionTypes
		{
			Present,
			Missing,
			Extra
		}
		private const int MASK_TYPE = 0x0f;
		private const int MASK_TODO = 0x10;
		//private const int MASK_ERROR = 0x20;
		private int m_type;

		private CompletionType (CompletionTypes type, bool fTodo)
		{
			m_type = (int) type;

			if (fTodo)
				m_type |= MASK_TODO;
		}

		public bool IsPresent
		{
			get { return Type == CompletionTypes.Present; }
		}
		public bool IsMissing
		{
			get { return Type == CompletionTypes.Missing; }
		}
		public bool IsExtra
		{
			get { return Type == CompletionTypes.Extra; }
		}
		public bool IsTodo
		{
			get { return (m_type & MASK_TODO) != 0; }
		}
		private CompletionTypes Type
		{
			get { return (CompletionTypes) (m_type & MASK_TYPE); }
		}

		public override string ToString ()
		{
			switch (Type)
			{
				case CompletionTypes.Missing:
					return "missing";
				case CompletionTypes.Extra:
					return "extra";
				case CompletionTypes.Present:
					return "present";
				default:
					throw new Exception ("Invalid CompletionType: "+Type);
			}
		}

		public static CompletionType Present
		{
			get { return new CompletionType (CompletionTypes.Present, false); }
		}
		public static CompletionType Missing
		{
			get { return new CompletionType (CompletionTypes.Missing, false); }
		}
		public static CompletionType Extra
		{
			get { return new CompletionType (CompletionTypes.Extra, false); }
		}
		public static CompletionType Compare (Object oMono, Object oMS)
		{
			if (oMono == null)
				return Missing;
			else if (oMS == null)
				return Extra;
			else
				return Present;
		}
	}

	/// <summary>
	/// 	Represents the amount of work done on a node
	/// </summary>
	/// <remarks>
	/// 	created by - Piersh
	/// 	created on - 3/2/2002 1:12:00 AM
	/// </remarks>

	public struct CompletionInfo
	{
		public int cPresent;
		public int cExtra;
		public int cMissing;
		public int cTodo;

		/// <summary>
		/// converts a CompletionTypes into a CompletionInfo
		/// sets the corresponding field to '1'
		/// </summary>
		/// <param name="ct">the CompletionTypes to convert</param>
		public CompletionInfo (CompletionType ct)
		{
			cPresent = cTodo = cMissing = cExtra = 0;
			if (ct.IsPresent)
				cPresent = 1;
			else if (ct.IsMissing)
				cMissing = 1;
			else if (ct.IsExtra)
				cExtra = 1;

			if (ct.IsTodo)
				cTodo = 1;
		}

		/// <summary>
		/// counts the total number of elements represented by this info
		/// </summary>
		public int cTotal
		{
			get
			{
				return cPresent + cTodo + cMissing;
			}
		}

		/// <summary>
		/// adds two CompletionInfos together
		/// </summary>
		/// <param name="m_nodeStatus"></param>
		public void Add (CompletionInfo m_nodeStatus)
		{
			cPresent += m_nodeStatus.cPresent;
			cTodo += m_nodeStatus.cTodo;
			cMissing += m_nodeStatus.cMissing;
			cExtra += m_nodeStatus.cExtra;
		}

		/// <summary>
		/// subtracts two CompletionInfos
		/// </summary>
		/// <param name="m_nodeStatus"></param>
		public void Sub (CompletionInfo m_nodeStatus)
		{
			cPresent -= m_nodeStatus.cPresent;
			cTodo -= m_nodeStatus.cTodo;
			cMissing -= m_nodeStatus.cMissing;
			cExtra -= m_nodeStatus.cExtra;
			if (cPresent < 0 || cTodo < 0 || cMissing < 0 || cExtra < 0)
				throw new Exception ("Completion underflow on subtract");
		}

		/// <summary>
		/// increments the corresponding field
		/// </summary>
		/// <param name="ct"></param>
		public void Add (CompletionType ct)
		{
			Add (new CompletionInfo (ct));
		}
		/// <summary>
		/// decrements the corresponding field
		/// </summary>
		/// <param name="ct"></param>
		public void Sub (CompletionType ct)
		{
			Sub (new CompletionInfo (ct));
		}

		/// <summary>
		/// adds appropriate 'missing', 'todo' & 'complete' attributes to an XmlElement
		/// </summary>
		/// <param name="elt"></param>
		public void SetAttributes (XmlElement elt)
		{
			elt.SetAttribute ("present", cPresent.ToString ());
			elt.SetAttribute ("missing", cMissing.ToString ());
			elt.SetAttribute ("extra", cExtra.ToString ());
			elt.SetAttribute ("todo", cTodo.ToString ());

			//int percentComplete = (cTotal == 0) ? 100 : (100 - 100 * (cMissing + cExtra) / cTotal);
			//elt.SetAttribute ("complete", percentComplete.ToString ());
		}
	}

	#endregion

	public enum PresenceTypes
	{
//		UNINITIALIZED = 0,
		Missing = 0,
		Present,
		Extra
	}

	public enum ErrorTypes
	{
		// TODO: order is important here... (see Status.SetError ())
//		UNINITIALIZED = 0,
		OK = 0,
		Todo,
		Warning,
		Error
	}

	public struct PresenceCounts
	{
		public int cMissing;
		public int cPresent;
		public int cExtra;

		public PresenceCounts (PresenceTypes type)
		{
			cMissing = cPresent = cExtra = 0;
			if (type == PresenceTypes.Missing)
				cMissing = 1;
			else if (type == PresenceTypes.Present)
				cPresent = 1;
			else if (type == PresenceTypes.Extra)
				cExtra = 1;
			else throw new Exception ("Invalid PresenceType");
		}
		public int Total
		{
			get { return cMissing + cPresent + cExtra; }
		}
		public void Add (PresenceCounts counts)
		{
			cMissing += counts.cMissing;
			cPresent += counts.cPresent;
			cExtra += counts.cExtra;
		}
		public void Sub (PresenceCounts counts)
		{
			cMissing -= counts.cMissing;
			cPresent -= counts.cPresent;
			cExtra -= counts.cExtra;

			if (cMissing < 0 || cPresent < 0 || cExtra < 0)
				throw new Exception ("Underflow");
		}
		public void Add (PresenceTypes type)
		{
			Add (new PresenceCounts (type));
		}
		public void Sub (PresenceTypes type)
		{
			Sub (new PresenceCounts (type));
		}
		public void SetAttributes (XmlElement elt, string strSuffix)
		{
			if (cMissing != 0)
				elt.SetAttribute ("missing"+strSuffix, cMissing.ToString ());
			if (cPresent != 0)
				elt.SetAttribute ("present"+strSuffix, cPresent.ToString ());
			if (cExtra != 0)
				elt.SetAttribute ("extra"+strSuffix, cExtra.ToString ());
		}
	}

	public struct ErrorCounts
	{
		public int cOK;
		public int cTodo;
		public int cWarning;
		public int cError;

		public ErrorCounts (ErrorTypes type)
		{
			cOK = cTodo = cWarning = cError = 0;
			if (type == ErrorTypes.OK)
				cOK = 1;
			else if (type == ErrorTypes.Todo)
				cTodo = 1;
			else if (type == ErrorTypes.Warning)
				cWarning = 1;
			else if (type == ErrorTypes.Error)
				cError = 1;
			else throw new Exception ("Invalid ErrorType");
		}
		public int Total
		{
			get { return cOK + cTodo + cWarning + cError; }
		}
		public void Add (ErrorCounts counts)
		{
			cOK += counts.cOK;
			cTodo += counts.cTodo;
			cWarning += counts.cWarning;
			cError += counts.cError;
		}
		public void Sub (ErrorCounts counts)
		{
			cOK -= counts.cOK;
			cTodo -= counts.cTodo;
			cWarning -= counts.cWarning;
			cError -= counts.cError;
			if (cOK < 0 || cTodo < 0 || cWarning < 0 || cError < 0)
				throw new Exception ("Underflow");
		}
		public void Add (ErrorTypes type)
		{
			Add (new ErrorCounts (type));
		}
		public void Sub (ErrorTypes type)
		{
			Sub (new ErrorCounts (type));
		}
		public void SetAttributes (XmlElement elt, string strSuffix)
		{
			if (cOK != 0)
				elt.SetAttribute ("ok"+strSuffix, cOK.ToString ());
			if (cTodo != 0)
				elt.SetAttribute ("todo"+strSuffix, cTodo.ToString ());
			if (cWarning != 0)
				elt.SetAttribute ("warning"+strSuffix, cWarning.ToString ());
			if (cError != 0)
				elt.SetAttribute ("error"+strSuffix, cError.ToString ());
		}
	}

	public struct Status
	{
		public PresenceTypes presence;
		public ErrorTypes error;

		public string PresenceName
		{
			get
			{
				if (presence == PresenceTypes.Missing)
					return "missing";
				else if (presence == PresenceTypes.Present)
					return "present";
				else if (presence == PresenceTypes.Extra)
					return "extra";
				else throw new Exception ("Invalid PresenceType");
			}
		}
		public string ErrorName
		{
			get
			{
				if (error == ErrorTypes.OK)
					return "OK";
				else if (error == ErrorTypes.Todo)
					return "todo";
				else if (error == ErrorTypes.Warning)
					return "warning";
				else if (error == ErrorTypes.Error)
					return "error";
				else throw new Exception ("Invalid ErrorType");
			}
		}
		public void SetAttributes (XmlElement elt)
		{
			if (presence != PresenceTypes.Present)
				elt.SetAttribute ("presence", PresenceName);
			if (error != ErrorTypes.OK)
				elt.SetAttribute ("error", ErrorName);
		}
	}

	public struct StatusCounts
	{
		public PresenceCounts presenceCounts;
		public ErrorCounts errorCounts;

		public void Add (StatusCounts statusCounts)
		{
			presenceCounts.Add (statusCounts.presenceCounts);
			errorCounts.Add (statusCounts.errorCounts);
			if (presenceCounts.Total != errorCounts.Total)
				throw new Exception ("invalid status counts");
		}
		public void Sub (StatusCounts statusCounts)
		{
			presenceCounts.Sub (statusCounts.presenceCounts);
			errorCounts.Sub (statusCounts.errorCounts);
			if (presenceCounts.Total != errorCounts.Total)
				throw new Exception ("invalid status counts");
		}
		public void Add (Status status)
		{
			presenceCounts.Add (status.presence);
			errorCounts.Add (status.error);
			if (presenceCounts.Total != errorCounts.Total)
				throw new Exception ("invalid status counts");
		}
		public void Sub (Status status)
		{
			presenceCounts.Sub (status.presence);
			errorCounts.Sub (status.error);
			if (presenceCounts.Total != errorCounts.Total)
				throw new Exception ("invalid status counts");
		}
		public void SetAttributes (XmlElement elt, string strSuffix)
		{
			presenceCounts.SetAttributes (elt, strSuffix);
			errorCounts.SetAttributes (elt, strSuffix);

			int cTotal = presenceCounts.cMissing + presenceCounts.cPresent;
			int cIncomplete =
				presenceCounts.cMissing +
				errorCounts.cTodo +
				errorCounts.cWarning +
				errorCounts.cError;

			if (presenceCounts.Total != errorCounts.Total)
				throw new Exception ("invalid status counts");

			if (cTotal != 0)
			{
				int percentComplete = 100 * (cTotal - cIncomplete) / cTotal;
				elt.SetAttribute ("complete" + strSuffix, percentComplete.ToString ());
			}
		}
	}

	public class NodeMessage
	{
		protected string msg;

		public NodeMessage (string _msg)
		{
			msg = _msg;
		}

		public string Message
		{
			get { return msg; }
		}
	}

	public class NodeStatus
	{
		public Status status;
		protected StatusCounts statusCountsChildren;
		protected StatusCounts statusCountsTotal;
		protected IList lstWarnings;

		public NodeStatus ()
		{
			status.error = ErrorTypes.OK;
		}

		/// <summary>
		/// Constructs a NodeStatus by comparing the presence of two objects
		/// it only sets the status.presence field
		/// </summary>
		/// <param name="objMono"></param>
		/// <param name="objMS"></param>
		public NodeStatus (Object objMono, Object objMS)
		{
			status.error = ErrorTypes.OK;
			statusCountsChildren = statusCountsTotal = new StatusCounts ();
			if (objMono == null)
				status.presence = PresenceTypes.Missing;
			else if (objMS == null)
				status.presence = PresenceTypes.Extra;
			else
				status.presence = PresenceTypes.Present;
		}
		public void Add (NodeStatus statusChild)
		{
			if ((int) statusChild.status.error > (int) status.error)
				status.error = statusChild.status.error;
			statusCountsTotal.Add (statusChild.statusCountsTotal);
			statusCountsChildren.Add (statusChild.statusCountsChildren);
		}
		public void AddChildren (NodeStatus statusChild)
		{
			statusCountsTotal.Add (statusChild.statusCountsTotal);
			statusCountsTotal.Add (statusChild.status);
			statusCountsChildren.Add (statusChild.status);
		}

		public void SubChildren (NodeStatus statusChild)
		{
			statusCountsTotal.Sub (statusChild.statusCountsTotal);
			statusCountsTotal.Sub (statusChild.status);
			statusCountsChildren.Sub (statusChild.status);
		}

		public void Add (StatusCounts statusCounts)
		{
			statusCountsChildren.Add (statusCounts);
			statusCountsTotal.Add (statusCounts);
		}

		public void Sub (StatusCounts statusCounts)
		{
			statusCountsChildren.Sub (statusCounts);
			statusCountsTotal.Sub (statusCounts);
		}

		public void Add (Status status)
		{
			statusCountsChildren.Add (status);
			statusCountsTotal.Add (status);
		}

		public void Sub (Status status)
		{
			statusCountsChildren.Sub (status);
			statusCountsTotal.Sub (status);
		}


		public bool IsMissing
		{
			get { return status.presence == PresenceTypes.Missing; }
		}
		public bool IsPresent
		{
			get { return status.presence == PresenceTypes.Present; }
		}
		public bool IsExtra
		{
			get { return status.presence == PresenceTypes.Extra; }
		}

		public void SetAttributes (XmlElement elt)
		{
			status.SetAttributes (elt);
			statusCountsChildren.SetAttributes (elt, "");
			statusCountsTotal.SetAttributes (elt, "_total");

			// add any warning messages
			if (lstWarnings != null)
			{
				XmlElement eltWarnings = elt.OwnerDocument.CreateElement ("warnings");
				elt.AppendChild (eltWarnings);
				foreach (NodeMessage msg in lstWarnings)
				{
					XmlElement eltWarning = elt.OwnerDocument.CreateElement ("warning");
					eltWarnings.AppendChild (eltWarning);
					eltWarning.SetAttribute ("text", msg.Message);
				}
			}

			//int percentComplete = (cTotal == 0) ? 100 : (100 - 100 * (cMissing + cExtra) / cTotal);
			//elt.SetAttribute ("complete", percentComplete.ToString ());
		}
		public void SetError (ErrorTypes errorNew)
		{
			// TODO: assumes order of error values
			if ((int) errorNew > (int) status.error)
				status.error = errorNew;
		}
		public void AddWarning (string strWarning)
		{
			if (lstWarnings == null)
				lstWarnings = new ArrayList ();
			lstWarnings.Add (new NodeMessage (strWarning));
			SetError (ErrorTypes.Warning);
		}
	}
}