EventCSVReader.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.IO;
  6. using System.Globalization;
  7. namespace CSVReader
  8. {
  9. public class EventCSVReader
  10. {
  11. private string completeFileName;
  12. private char separator = ';';
  13. private long lastLength = 0;
  14. public EventCSVReader(string filename)
  15. {
  16. completeFileName = Path.Combine(".", "Assets", "CSVInput", filename);
  17. if (!File.Exists(completeFileName))
  18. {
  19. completeFileName = "Err";
  20. Debug.Log("Unable to find Event File " + filename);
  21. }
  22. }
  23. /// <summary>
  24. /// Reads the entire File that has been found when Constructing the class instance. No streams are left open after the use of this method.
  25. /// If instance construction was not successfull, only return null.
  26. /// </summary>
  27. /// <returns>
  28. /// Returns a field of EventObjects including all entries of the file
  29. /// </returns>
  30. public EventObject[] ReadFile()
  31. {
  32. if (completeFileName == "Err")
  33. {
  34. return null;
  35. }
  36. List<string> lines = new List<string>();
  37. try
  38. {
  39. using (StreamReader fs = new StreamReader(File.Open(completeFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
  40. {
  41. if (fs.BaseStream.Length == lastLength)
  42. {
  43. return new EventObject[0];
  44. }
  45. lastLength = fs.BaseStream.Length;
  46. string line;
  47. while ((line = fs.ReadLine()) != null)
  48. lines.Add(line);
  49. }
  50. }
  51. catch
  52. {
  53. Debug.Log("Unable to Read Lines from Event File");
  54. return null;
  55. }
  56. EventObject[] events = new EventObject[lines.Count];
  57. for (int i = 0; i < lines.Count; i++)
  58. {
  59. string[] elems = lines[i].Split(separator);
  60. if (elems.Length == 6)
  61. {
  62. try
  63. {
  64. events[i] = new EventObject(int.Parse(elems[0]),
  65. double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture),
  66. float.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture),
  67. float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture),
  68. float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture),
  69. (EventType)int.Parse(elems[5]));
  70. }
  71. catch (Exception e)
  72. {
  73. Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i);
  74. events[i] = null;
  75. }
  76. }
  77. else if (elems.Length == 7)
  78. {
  79. try
  80. {
  81. events[i] = new EventObject(int.Parse(elems[0]),
  82. double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture),
  83. float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture),
  84. float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture),
  85. float.Parse(elems[5].Replace(',', '.'), CultureInfo.InvariantCulture),
  86. (EventType)int.Parse(elems[6]));
  87. events[i].TimestampEnd = double.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture);
  88. }
  89. catch (Exception e)
  90. {
  91. Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i);
  92. events[i] = null;
  93. }
  94. }
  95. else if (elems.Length == 9)
  96. {
  97. try
  98. {
  99. events[i] = new EventObject(int.Parse(elems[0]),
  100. double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture),
  101. float.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture),
  102. float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture),
  103. float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture),
  104. (EventType)int.Parse(elems[8]));
  105. events[i].setIdealViewingPosition(float.Parse(elems[5].Replace(',', '.'), CultureInfo.InvariantCulture),
  106. float.Parse(elems[6].Replace(',', '.'), CultureInfo.InvariantCulture),
  107. float.Parse(elems[7].Replace(',', '.'), CultureInfo.InvariantCulture));
  108. }
  109. catch (Exception e)
  110. {
  111. Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i);
  112. events[i] = null;
  113. }
  114. }
  115. else if (elems.Length == 10)
  116. {
  117. try
  118. {
  119. events[i] = new EventObject(int.Parse(elems[0]),
  120. double.Parse(elems[1].Replace(',', '.'), CultureInfo.InvariantCulture),
  121. float.Parse(elems[3].Replace(',', '.'), CultureInfo.InvariantCulture),
  122. float.Parse(elems[4].Replace(',', '.'), CultureInfo.InvariantCulture),
  123. float.Parse(elems[5].Replace(',', '.'), CultureInfo.InvariantCulture),
  124. (EventType)int.Parse(elems[9]));
  125. events[i].setIdealViewingPosition(float.Parse(elems[6].Replace(',', '.'), CultureInfo.InvariantCulture),
  126. float.Parse(elems[7].Replace(',', '.'), CultureInfo.InvariantCulture),
  127. float.Parse(elems[8].Replace(',', '.'), CultureInfo.InvariantCulture));
  128. events[i].TimestampEnd = double.Parse(elems[2].Replace(',', '.'), CultureInfo.InvariantCulture);
  129. }
  130. catch (Exception e)
  131. {
  132. Debug.Log("Eventfile Error \"" + e.Message + "\" at line " + i);
  133. events[i] = null;
  134. }
  135. }
  136. else
  137. {
  138. Debug.Log("Unclear length of line " + i);
  139. events[i] = null;
  140. }
  141. }
  142. return events;
  143. }
  144. }
  145. }