using System;
using Microsoft.SPOT;

namespace CPF
{
    class Utils
    {

        private static byte[] returnBytes = new byte[128];
        public static int getField(byte[] inBytes, byte inToken, int desiredField, ref byte[] returnField)
        {
            int currentField = 0;
            int returnFieldIndex = 0;
            byte token;
            bool inDesiredField = false;

            token = (byte)inToken;

            Array.Clear(returnBytes, 0, returnBytes.Length);
            for (int i = 0; i < inBytes.Length; i++)
            {
                if ((inBytes[i] == token) && !inDesiredField)
                    currentField++;
                else if (currentField == desiredField)
                {
                    inDesiredField = true;
                    if ((inBytes[i] == token) || (inBytes[i] == 10) || (inBytes[i] == 13))
                        return (1);
                    else
                    {
                        returnField[returnFieldIndex] = inBytes[i];
                        returnFieldIndex++;
                    }
                }
            }
            return (0);
        }
    }
}
