using System;
using Microsoft.SPOT;

namespace CPF
{
    public static class NumberParser
    {
        // Convert a byte array to an integer
        public static int BytesToInt(byte[] s, int length, ref int pos, out bool eol, out bool successful)
        {
            int     r = 0;
            byte    ch = 0;
            bool    negative = false;

            eol = false;
            successful = true;

            for (; pos < length;)
            {
                ch = s[pos++];
                if (ch == IridiumStrings.BLANK[0])
                    continue;
                if (ch == IridiumStrings.COMMA[0])
                    break;
                if (ch == IridiumStrings.CR[0])
                    break;
                if ((ch == IridiumStrings.MINUS[0]) && (negative == false))
                {
                    negative = true;
                    continue;
                }
                if ((IridiumStrings.ZERO[0] <= ch) && (ch <= IridiumStrings.NINE[0]))
                {
                    r *= 10;
                    r += (int)(ch - IridiumStrings.ZERO[0]);
                }
                else
                {
                    successful = false;
                    return 0;
                }
            }

            if ((ch == IridiumStrings.CR[0]) || (pos == length))
                eol = true;
            if (negative)
                return -r;
            else
                return r;
        }

        // Convert a byte array to an integer
        public static double BytesToDouble(byte[] s, int length, ref int pos, out bool eol, out bool successful)
        {
            byte    ch          = 0;
            bool    negative    = false;
            bool    afterDot    = false;
            double  r           = 0F;
            double  m           = 0.1F;

            eol = false;
            successful = true;

            for (; pos < length; )
            {
                ch = s[pos++];
                if (ch == IridiumStrings.BLANK[0])
                    continue;

                if ((ch == IridiumStrings.MINUS[0]) && (negative == false))
                {
                    negative = true;         
                    continue;
                }

                if ((ch == IridiumStrings.DOT[0]) && (afterDot == false))
                {
                    afterDot = true;
                    continue;
                }

                if (ch == IridiumStrings.COMMA[0])
                    break;

                if (ch == IridiumStrings.CR[0])
                    break;
                
                if ((IridiumStrings.ZERO[0] <= ch) && (ch <= IridiumStrings.NINE[0]))
                {
                    if (!afterDot)
                    {
                        r *= 10F;
                        r += (double)(ch - IridiumStrings.ZERO[0]);
                    }
                    else
                    {
                        r += ((double)(ch - IridiumStrings.ZERO[0])) * m;
                        m /= 10F;
                    }
                }
                else
                {
                    successful = false;
                    return 0;
                }
            }

            if ((ch == IridiumStrings.CR[0]) || (pos == length))
                eol = true;

            if (negative)
                return -r;
            else
                return r;
        }

    }
}
