示例#1
0
        public static IEnumerable <char> Decode(ICodecIO s, ICharLookup map)
        {
            var chars = StreamAsChars(s, map.TreatAsBinary);
            var iter  = ChangeBase(chars, map, CodecBase256.Self);

            return(iter);
        }
示例#2
0
        public static IEnumerable <char> ChangeBase(ICodecIO s, ICharLookup inMap, ICharLookup outMap)
        {
            var chars = StreamAsChars(s, inMap.TreatAsBinary);
            var iter  = ChangeBase(chars, inMap, outMap);

            return(iter);
        }
示例#3
0
        static IEnumerable <int> ChangeBaseInternal(IEnumerable <char> src, ICharLookup inMap, ICharLookup outMap)
        {
            //the charOut/charsIn calculation is a bit magical
            // i'm not exactly sure why this works out but it does
            int charsOut = inMap.BytesIn * outMap.CharsOut;
            int charsIn  = inMap.CharsOut * outMap.BytesIn;

            var unInMap = new GlifMap(inMap);

            int[] inArr   = new int[charsIn];
            int[] outArr  = new int[charsOut];
            int   inBase  = inMap.Base();
            int   outBase = outMap.Base();

            var  iter = src.GetEnumerator();
            bool done = false;

            while (!done)
            {
                Array.Clear(inArr, 0, charsIn);
                Array.Clear(outArr, 0, charsOut);

                int inCount = 0;
                while (inCount < charsIn)
                {
                    if (iter.MoveNext())
                    {
                        int digit = unInMap.Map(iter.Current);

                        //reverse incoming bigendgian order
                        int iIndex = charsIn - inCount - 1;
                        inArr[iIndex] = digit;
                    }
                    else
                    {
                        done = true;
                        break;
                    }
                    inCount++;
                }

                //base calculation is done in little endian
                ChangeBase(inBase, outBase, inArr, ref outArr);

                //reverse again to restore big endianess
                for (int o = charsOut - 1; o >= 0; o--)
                {
                    yield return(outArr[o]);
                }
            }
        }
示例#4
0
        void Init(ICharLookup map)
        {
            int @base = map.Base();

            lookup = new Dictionary <char, int>(@base);
            for (int b = 0; b < @base; b++)
            {
                lookup.Add(map.Map(b), b);
            }
            if (map.IncludePadding)
            {
                Padding = map.Padding;
            }
        }
示例#5
0
        static void TestDecode(string inText, string outText, ICharLookup codec)
        {
            var    s    = new CodecIO(new StringReader(inText));
            var    enu  = TuckBytes.Decode(s, codec);
            string test = String.Concat(enu);

            //Assert.AreEqual(outText.Length,test.Length);
            for (int c = 0; c < outText.Length; c++)
            {
                char t = test[c];
                if (Char.IsControl(t))
                {
                    continue;
                }
                Trace.WriteLine(c + " [" + outText[c] + "," + t + "]");
                Assert.AreEqual(outText[c], test[c]);
            }
        }
示例#6
0
 public CodecIO(Mode mode, string fileName, ICharLookup codec)
 {
     IOMode = mode;
     if (mode == Mode.Read)
     {
         Data = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
         if (!codec.TreatAsBinary)
         {
             Reader = new StreamReader(Data);
         }
     }
     else if (mode == Mode.Write)
     {
         Data = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.Read);
         if (!codec.TreatAsBinary)
         {
             Writer = new StreamWriter(Data);
         }
     }
 }
示例#7
0
        static void DoConversion(ICodecIO dataIn, ICodecIO dataOut, ICharLookup codecIn, ICharLookup codecOut)
        {
            IEnumerable <char> charsOut = null;

            // direct conversion is better, but only works sometimes
            bool shouldDirectConvert =
                Options.DirectConvert ||              //override
                Options.BaseIn == Base.Base256 ||
                Options.BaseOut == Base.Base256
            ;

            Log.Debug("shouldDirectConvert = " + shouldDirectConvert);
            Log.Debug("BaseIn = " + Options.BaseIn);
            Log.Debug("BaseOut = " + Options.BaseOut);

            if (shouldDirectConvert)
            {
                charsOut = TuckBytes.ChangeBase(dataIn, codecIn, codecOut);
            }
            else
            {
                var binary = TuckBytes.ChangeBase(dataIn, codecIn, CodecBase256.Self);
                charsOut = TuckBytes.ChangeBase(dataIn, CodecBase256.Self, codecOut);
            }

            int count = Options.CharactersPerLine - 1;

            foreach (char c in charsOut)
            {
                dataOut.Write(c);
                if (!Options.DisableNewLines && !codecOut.TreatAsBinary)
                {
                    if (count <= 0)
                    {
                        dataOut.Write('\n');
                        count = Options.CharactersPerLine;
                    }
                    count--;
                }
            }
        }
示例#8
0
        public static IEnumerable <char> ChangeBase(IEnumerable <char> src, ICharLookup inMap, ICharLookup outMap)
        {
            //To deal with the padding, this hangs on to the previous array of converted characters
            // so that we can take any trailing 0 values and either remove or convert to padding
            //TODO maybe merge this with ChangeBaseInternal since ChangeBaseInternal is now farily simple
            int charsOut = inMap.BytesIn * outMap.CharsOut;
            var valIter  = ChangeBaseInternal(src, inMap, outMap);

            int[] currArr = new int[charsOut];
            int[] lastArr = null;
            int   pos     = 0;

            foreach (int val in valIter)
            {
                currArr[pos] = val;
                if (lastArr != null)
                {
                    char c = outMap.Map(lastArr[pos]);
                    yield return(c);
                }
                pos++;
                if (pos >= charsOut)
                {
                    pos = 0;
                    if (lastArr == null)
                    {
                        lastArr = new int[charsOut];
                    }
                    int[] temp = currArr;
                    currArr = lastArr;
                    lastArr = temp;
                    Array.Clear(currArr, 0, charsOut);
                }
            }

            if (lastArr != null)
            {
                int pad = 0;
                for (int v = charsOut - 1; v >= 0; v--)
                {
                    if (lastArr[v] == 0)
                    {
                        pad++;
                    }
                }
                //output padding if there are any non-zero entries in the last array
                if (pad < charsOut)
                {
                    for (int i = 0; i < charsOut - pad; i++)
                    {
                        char c = outMap.Map(lastArr[i]);
                        yield return(c);
                    }
                    if (outMap.IncludePadding)
                    {
                        for (int p = 0; p < pad; p++)
                        {
                            yield return(outMap.Padding);
                        }
                    }
                }
            }

            if (!outMap.TreatAsBinary)
            {
                yield return('\n');
            }
        }
示例#9
0
 public GlifMap(ICharLookup map)
 {
     Init(map);
 }
示例#10
0
 public FightTracker(ISpellLookup spells, ICharLookup chars)
 {
     Chars = chars;
     Buffs = new BuffTracker(spells);
 }
示例#11
0
 public static int Base(this ICharLookup map)
 {
     return((int)Math.Ceiling(Math.Pow(2.0, 8.0 * map.BytesIn / map.CharsOut)));
 }