12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using System.IO;
- using System.Globalization;
- namespace CSVReader
- {
- public class SensorCSVReader
- {
- private string completeFileName;
- private char separator = ';';
- public SensorCSVReader(string filename)
- {
- completeFileName = Path.Combine(".", "Assets", "CSVInput", filename);
- if (!File.Exists(completeFileName))
- {
- completeFileName = "Err";
- Debug.Log("Unable to find Sensor 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 SensorObjects including all entries of the file
- /// </returns>
- public SensorObject[] ReadFile()
- {
- if (completeFileName == "Err")
- {
- return null;
- }
- string[] lines;
- try
- {
- lines = File.ReadAllLines(completeFileName);
- }
- catch (Exception e)
- {
- Debug.Log("Unable to Read Lines from Sensor File, " + e.Message);
- return null;
- }
- SensorObject[] sensors = new SensorObject[lines.Length];
- for (int i = 0; i < lines.Length; i++)
- {
- string[] elems = lines[i].Split(separator);
- try
- {
- sensors[i] = new SensorObject(int.Parse(elems[0]), float.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),
- float.Parse(elems[5].Replace(',', '.'), CultureInfo.InvariantCulture),
- float.Parse(elems[6].Replace(',', '.'), CultureInfo.InvariantCulture));
- }
- catch (Exception e)
- {
- Debug.Log("Sensorfile Error \"" + e.Message + "\" at line " + i);
- sensors[i] = null;
- }
- }
- return sensors;
- }
- }
- }
|