//
// JobPacket.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Runtime.InteropServices;
using System.Text;
using Ntp.Process;

namespace Ntp.Monitor
{
    /*
    [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 2 + 18 + 4*2 + 20)]
    public unsafe struct JobPacket
    {
        public ushort JobId;
        public fixed char Name [18];
        public bool InitialRun;
        public ushort Frequency;
        public ushort Prio;
        public ushort Runs;
        public fixed char Description [20];

        public JobPacket (JobDescription job)
        {
            JobId = Convert.ToUInt16 (job.JobId);

            fixed (JobPacket* ptr = &this) {
                int i = 0;
                foreach (char x in job.Name) {
                    ptr->Name [i++] = x;
                }
                ptr->Name [i] = '\0';
            }

            InitialRun = job.InitialRun;
            Frequency = Convert.ToUInt16 (job.Frequency);
            Prio = Convert.ToUInt16 (job.Priority);
            Runs = Convert.ToUInt16 (job.RunCount);

            fixed (JobPacket* ptr = &this) {
                int i = 0;
                foreach (char x in job.Description) {
                    ptr->Description [i++] = x;
                }
                ptr->Description [i] = '\0';
            }
        }

        public JobDescription ToJob ()
        {
            char[] chars = new char[18];
            fixed (JobPacket* ptr = &this) {
                for (int i = 0; i < 18; i++) {
                    chars [i] = Convert.ToChar (ptr->Name [i]);
                }
            }

            char[] descr = new char[20];

            fixed (JobPacket* ptr = &this) {
                for (int i = 0; i < 20; i++) {
                    // Access marking[] via the fixed pointer.
                    descr [i] = Convert.ToChar (ptr->Description [i]);
                }
            }

            return new JobObject {
                JobId = JobId,
                Name = new String(chars),
                InitialRun = InitialRun,
                Frequency = Frequency,
                Prio = Prio,
                RunCount = Runs,
                Description = new String(descr)
            };
        }
        // Fast.  Must be null terminated.
        public static string DecodeAsciiZ (byte[] buffer)
        {
            fixed (byte* bytes = &buffer[0]) {
                return new String ((sbyte*)bytes);
            }
        }
        // Safer
        public static string DecodeAscii (byte[] buffer)
        {
            int count = Array.IndexOf<byte> (buffer, 0, 0);
            if (count < 0)
                count = buffer.Length;
            return Encoding.ASCII.GetString (buffer, 0, count);
        }
    }
    */
}