using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace IgniteEngine.IO
{
///
/// Class that reads data from a file.
///
public class ScriptReader : IDisposable
{
///
/// Returns true if the script has rows to read.
///
public bool HasRows => rows.Count > 0;
///
/// The current row that we are reading.
///
private ScriptRow currentRow;
///
/// The queued rows to read.
///
private readonly ConcurrentQueue rows;
///
/// Creates a new instance of the class.
///
/// The rows to read.
public ScriptReader(List rows)
{
if (rows == null)
{
this.rows = new ConcurrentQueue();
return;
}
this.rows = new ConcurrentQueue(rows);
}
///
/// Disposes the reader.
///
public void Dispose()
{
// empty...
}
///
/// Gets a boolean from the row.
///
/// The index to get the value from.
public bool GetBoolean(int index)
{
return bool.Parse(currentRow[index]);
}
///
/// Gets a byte from the row.
///
/// The index to get the value from.
public byte GetByte(int index)
{
return byte.Parse(currentRow[index]);
}
///
/// Gets a 16-bit integer from the row.
///
/// The index to get the value from.
public short GetInt16(int index)
{
return short.Parse(currentRow[index]);
}
///
/// Gets a 32-bit integer from the row.
///
/// The index to get the value from.
public int GetInt32(int index)
{
return int.Parse(currentRow[index]);
}
///
/// Gets a 64-bit integer from the row.
///
/// The index to get the value from.
public long GetInt64(int index)
{
return long.Parse(currentRow[index]);
}
///
/// Gets a signed byte from the row.
///
/// The index to get the value from.
public sbyte GetSByte(int index)
{
return sbyte.Parse(currentRow[index]);
}
///
/// Gets an unsigned 16-bit integer from the row.
///
/// The index to get the value from.
public ushort GetUInt16(int index)
{
return ushort.Parse(currentRow[index]);
}
///
/// Gets an unsigned 32-bit integer from the row.
///
/// The index to get the value from.
public uint GetUInt32(int index)
{
return uint.Parse(currentRow[index]);
}
///
/// Gets an unsigned 64-bit integer from the row.
///
/// The index to get the value from.
public ulong GetUInt64(int index)
{
return ulong.Parse(currentRow[index]);
}
///
/// Gets a string from the row.
///
/// The index to get the value from.
public string GetString(int index)
{
return currentRow[index];
}
///
/// Tries to read a row from the file.
///
/// True if the row was read successfully.
public bool Read()
{
return rows.TryDequeue(out currentRow) && currentRow != null;
}
}
}