Advertisement
Guest User

Bitflag deconstructor

a guest
Aug 24th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////////////////////
  2. // Code by DRGC / Arcbinder
  3. // This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
  4. //////////////////////////////////////////////////////////////////////////////////////////////
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. using System.Globalization;
  10.  
  11. namespace FlagIdentifier
  12. {
  13.     class Program
  14.     {
  15.         // IDs for localised text
  16.         enum LocalisationID
  17.         {
  18.             EnterValue = 0,
  19.             InvalidInput,
  20.             InputOutOfBounds,
  21.             FlagsSet,
  22.             InputOverflow,
  23.         }
  24.  
  25.         //Contains strings which the program prints to console (Localise these)
  26.         static string[] LocalisationStrings =
  27.         {
  28.             "Enter flags value (-1 to Exit):",
  29.             "Input must be a non-zero, positive integer, try again",
  30.             "Entered value ( {0} ) higher than largest flag ( {1} ). Try again",
  31.             "These flags are set:",
  32.             "Input out of bounds of type {0}",
  33.         };
  34.  
  35.         // This array contains the names for your flags
  36.         static string[] FlagNames =
  37.             {
  38.                 "Flag Not Named",   // 2^0    = 1      = 1 << 0
  39.                 "Flag Not Named",   // 2^1    = 2      = 1 << 1
  40.                 "Flag Not Named",   // 2^2    = 4      = 1 << 2
  41.                 "Flag Not Named",   // 2^3    = 8      = 1 << 3
  42.                 "Flag Not Named",   // 2^4    = 16     = 1 << 4
  43.                 "Flag Not Named",   // 2^5    = 32     = 1 << 5
  44.                 "Flag Not Named",   // 2^6    = 64     = 1 << 6
  45.                 "Flag Not Named",   // 2^7    = 128    = 1 << 7
  46.                 "Flag Not Named",   // 2^8    = 256    = 1 << 8
  47.                 "Flag Not Named",   // 2^9    = 512    = 1 << 9
  48.                 "Flag Not Named",   // 2^10   = 1024   = 1 << 10
  49.             };
  50.  
  51.         static int Main()
  52.         {
  53.             // The flag that has been entered
  54.             int flagInt = 0;
  55.             // Highest valid flag (will change as flags are added to (or removed from) FlagNames)
  56.             int highestFlag = 1 << FlagNames.Length;
  57.            
  58.             // Loop until quit
  59.             while (flagInt == 0)
  60.             {
  61.                 // Error flag - set to true if input not in bounds of flagInt's type
  62.                 bool overflow = false;
  63.  
  64.                 // Prompt to enter flags value
  65.                 Console.Write(LocalisationStrings[(int)LocalisationID.EnterValue]);
  66.                
  67.                 // try to parse input
  68.                 try
  69.                 {
  70.                     flagInt = int.Parse(Console.ReadLine(), CultureInfo.CurrentCulture);
  71.                 }
  72.                 catch (OverflowException) // Input is out of bounds of a 32-bit integer
  73.                 {
  74.                     overflow = true;
  75.                     flagInt = 0xDEAD;
  76.                 }
  77.                 catch  // Input is not valid integer
  78.                 {
  79.                     flagInt = 0;
  80.                 }
  81.  
  82.                 if (flagInt <= 0)
  83.                 {
  84.                     if (flagInt != -1)
  85.                         Console.WriteLine(LocalisationStrings[(int)LocalisationID.InvalidInput]);
  86.                     else
  87.                         return 0;
  88.                 }
  89.                 else if (flagInt > highestFlag || overflow)
  90.                 {
  91.                     if (!overflow)
  92.                         Console.WriteLine(string.Format(CultureInfo.CurrentCulture, LocalisationStrings[(int)LocalisationID.InputOutOfBounds], flagInt, highestFlag));
  93.                     else
  94.                         Console.WriteLine(string.Format(CultureInfo.CurrentCulture, LocalisationStrings[(int)LocalisationID.InputOverflow], flagInt.GetType().ToString()));
  95.  
  96.                     flagInt = 0;
  97.                 }
  98.                 else
  99.                 {
  100.                     Console.WriteLine(LocalisationStrings[(int)LocalisationID.FlagsSet]);
  101.  
  102.                     // Generate flags list
  103.                     List<String> flags = new List<string>();
  104.                     for (int i = FlagNames.Length - 1; i >= 0; i--)
  105.                     {
  106.                         int flagValue = 1 << i;
  107.                         if (flagInt / flagValue != 0)
  108.                         {
  109.                             flagInt -= flagValue;
  110.                             flags.Insert(0, i + ": " + FlagNames[i]);
  111.                         }
  112.                     }
  113.  
  114.                     // Print flags
  115.                     Console.WriteLine();
  116.                     foreach (string s in flags)
  117.                     {
  118.                         Console.WriteLine(s);
  119.                     }
  120.                     Console.WriteLine();
  121.                 }
  122.                 flagInt = 0;
  123.             }
  124.             return -1; // Shouldn't be able to get here
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement