123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using System.IO;
- using System.Globalization;
- namespace CSVReader
- {
- public class EventCSVReader
- {
- private string completeFileName;
- private char separator = ';';
- private long lastLength = 0;
- public EventCSVReader(string filename)
- {
- completeFileName = Path.Combine(".", "Assets", "CSVInput", filename);
- if (!File.Exists(completeFileName))
- {
- completeFileName = "Err";
- Debug.Log("Unable to find Event File " + filename);
- }
- }
- /// <summary>
- /// Reads the entire File that has been found when Constructing the class instance. No streams are left open after the use of this method.
- /// If instance construction was not successfull, only return null.
- /// </summary>
- /// <returns>
- /// Returns a field of EventObjects including all entries of the file
- /// </returns>
- public EventObject[] ReadFile()
- {
- if (completeFileName == "Err")
- {
- return null;
- }
- List<string> lines = new List<string>();
- try
- {
- using (StreamReader fs = new StreamReader(File.Open(completeFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
- {
- if (fs.BaseStream.Length == lastLength)
- {
- return new EventObject[0];
- }
- lastLength = fs.BaseStream.Length;
- string line;
- while ((line = fs.ReadLine()) != null)
- lines.Add(line);
- }
- }
- catch
- {
- Debug.Log("Unable to Read Lines from Event File");
- return null;
- }
- EventObject[] events = new EventObject[lines.Count];
- for (int i = 0; i < lines.Count; i++)
- {
- string[] elems = lines[i].Split(separator);
- if (elems.Length == 6)
- {
- try
- {
- events[i] = new EventObject(int.Parse(elems[0]),
- double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture),
- (EventType)int.Parse(elems[5]));
- }
- catch (Exception e)
- {
- Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i);
- events[i] = null;
- }
- }
- else if (elems.Length == 7)
- {
- try
- {
- events[i] = new EventObject(int.Parse(elems[0]),
- double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[5].Replace(',', '.'), CultureInfo.InvariantCulture),
- (EventType)int.Parse(elems[6]));
- events[i].TimestampEnd = double.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture);
- }
- catch (Exception e)
- {
- Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i);
- events[i] = null;
- }
- }
- else if (elems.Length == 9)
- {
- try
- {
- events[i] = new EventObject(int.Parse(elems[0]),
- double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture),
- (EventType)int.Parse(elems[8]));
- events[i].setIdealViewingPosition(float.Parse(elems[5].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[6].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[7].Replace(',', '.'), CultureInfo.InvariantCulture));
- }
- catch (Exception e)
- {
- Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i);
- events[i] = null;
- }
- }
- else if (elems.Length == 10)
- {
- try
- {
- events[i] = new EventObject(int.Parse(elems[0]),
- double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[5].Replace(',', '.'), CultureInfo.InvariantCulture),
- (EventType)int.Parse(elems[9]));
- events[i].setIdealViewingPosition(float.Parse(elems[6].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[7].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[8].Replace(',', '.'), CultureInfo.InvariantCulture));
- events[i].TimestampEnd = double.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture);
- }
- catch (Exception e)
- {
- Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i);
- events[i] = null;
- }
- }
- else
- {
- Debug.Log("Unclear length of line " + i);
- events[i] = null;
- }
- }
- return events;
- }
- }
- }
|