TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Generics.cs
Go to the documentation of this file.
1 // TraceLab Component Library
2 // Copyright © 2012-2013 SEMERU
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
20 using System.Linq;
21 using System.Text;
22 
23 namespace TraceLab.Components.DevelopmentKit.IO
24 {
28  public static class Generics
29  {
35  public static string[] ImportStrings(string filename)
36  {
37  return File.ReadAllLines(filename);
38  }
39 
46  public static int[] ImportIntegers(string filename, bool skipInvalid = true)
47  {
48  List<int> list = new List<int>();
49  TextReader file = new StreamReader(filename);
50  string line;
51  while ((line = file.ReadLine()) != null)
52  {
53  int number;
54  // failure
55  if (!Int32.TryParse(line, out number))
56  {
57  // skip
58  if (skipInvalid)
59  {
60  continue;
61  }
62  // error
63  else
64  {
65  throw new DevelopmentKitException("Line in invalid format (" + filename + ")");
66  }
67  }
68  // success
69  else
70  {
71  list.Add(number);
72  }
73  }
74  file.Close();
75  return list.ToArray();
76  }
77 
84  public static double[] ImportDoubles(string filename, bool skipInvalid = true)
85  {
86  List<double> list = new List<double>();
87  TextReader file = new StreamReader(filename);
88  string line;
89  while ((line = file.ReadLine()) != null)
90  {
91  double number;
92  // failure
93  if (!Double.TryParse(line, out number))
94  {
95  // skip
96  if (skipInvalid)
97  {
98  continue;
99  }
100  // error
101  else
102  {
103  throw new DevelopmentKitException("Line in invalid format (" + filename + ")");
104  }
105  }
106  // success
107  else
108  {
109  list.Add(number);
110  }
111  }
112  file.Close();
113  return list.ToArray();
114  }
115  }
116 }