|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +static class TextFileHelper |
| 6 | +{ |
| 7 | + // This method creates three files at the paths |
| 8 | + // ex0P, ex1P and ex2P |
| 9 | + // that will serve for our tests. |
| 10 | + public static void Init( |
| 11 | + string ex0P, |
| 12 | + string ex1P, |
| 13 | + string ex2P, |
| 14 | + bool over = false |
| 15 | + ) |
| 16 | + { |
| 17 | + if ( |
| 18 | + !over |
| 19 | + && ( |
| 20 | + File.Exists(ex0P) |
| 21 | + || File.Exists(ex1P) |
| 22 | + || File.Exists(ex0P) |
| 23 | + ) |
| 24 | + ) |
| 25 | + { |
| 26 | + Console.WriteLine( |
| 27 | + "One of the path already contains a file." |
| 28 | + + "\nCall the method with an additional parameter" |
| 29 | + + "\nset to \"true\" to override the existing file." |
| 30 | + ); |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + try |
| 35 | + { |
| 36 | + StreamWriter ex0 = new StreamWriter(ex0P); |
| 37 | + ex0.Write( |
| 38 | + "This is the first demo file\n# This line starts with an \"#\").\n(An isolated parenthesis" |
| 39 | + ); |
| 40 | + ex0.Close(); |
| 41 | + } |
| 42 | + catch |
| 43 | + { |
| 44 | + Console.WriteLine( |
| 45 | + "There was an error creating ex0" |
| 46 | + ); |
| 47 | + } |
| 48 | + try |
| 49 | + { |
| 50 | + StreamWriter ex1 = new StreamWriter(ex1P); |
| 51 | + ex1.Write( |
| 52 | + "(This is the second demo file" |
| 53 | + + "\n)There are parenthesis in it, but no pound (\"#\") character on the first line." |
| 54 | + ); |
| 55 | + ex1.Close(); |
| 56 | + } |
| 57 | + catch |
| 58 | + { |
| 59 | + Console.WriteLine( |
| 60 | + "There was an error creating ex1" |
| 61 | + ); |
| 62 | + } |
| 63 | + try |
| 64 | + { |
| 65 | + StreamWriter ex2 = new StreamWriter(ex2P); |
| 66 | + ex2.Write( |
| 67 | + "# Just a comment )" |
| 68 | + + "\nand unbalanced parentheses (" |
| 69 | + ); |
| 70 | + ex2.Close(); |
| 71 | + } |
| 72 | + catch |
| 73 | + { |
| 74 | + Console.WriteLine( |
| 75 | + "There was an error creating ex2" |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void Display(string ex0P) |
| 82 | + { |
| 83 | + if (!File.Exists(ex0P)) |
| 84 | + { |
| 85 | + Console.WriteLine( |
| 86 | + "Nothing to display: the file does not exists." |
| 87 | + ); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + try |
| 92 | + { |
| 93 | + StreamReader sw = new StreamReader(ex0P); |
| 94 | + string cLine = sw.ReadLine(); |
| 95 | + while (cLine != null) |
| 96 | + { |
| 97 | + Console.WriteLine(cLine); |
| 98 | + cLine = sw.ReadLine(); |
| 99 | + } |
| 100 | + } |
| 101 | + catch |
| 102 | + { |
| 103 | + Console.WriteLine( |
| 104 | + "An error was thrown from Display method." |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public static void DisplayNoComment(string ex0P) |
| 111 | + { |
| 112 | + if (!File.Exists(ex0P)) |
| 113 | + { |
| 114 | + Console.WriteLine( |
| 115 | + "Nothing to display: the file does not exists." |
| 116 | + ); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + try |
| 121 | + { |
| 122 | + StreamReader sw = new StreamReader(ex0P); |
| 123 | + string cLine = sw.ReadLine(); |
| 124 | + while (cLine != null) |
| 125 | + { |
| 126 | + if (!cLine.StartsWith("#")) |
| 127 | + { |
| 128 | + Console.WriteLine(cLine); |
| 129 | + } |
| 130 | + cLine = sw.ReadLine(); |
| 131 | + } |
| 132 | + } |
| 133 | + catch |
| 134 | + { |
| 135 | + Console.WriteLine( |
| 136 | + "An error was thrown from DisplayNoComment method." |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static void Reverse( |
| 143 | + string ex0P, |
| 144 | + string ex1P, |
| 145 | + bool over = false |
| 146 | + ) |
| 147 | + { |
| 148 | + if (!File.Exists(ex0P)) |
| 149 | + { |
| 150 | + Console.WriteLine( |
| 151 | + "There is a problem: the source file does not exist." |
| 152 | + ); |
| 153 | + } |
| 154 | + else if (File.Exists(ex1P) && !over) |
| 155 | + { |
| 156 | + Console.WriteLine( |
| 157 | + "There is a problem: the target file already exists." |
| 158 | + + "\nCall the method with an additional parameter" |
| 159 | + + "\nset to \"true\" to override the existing file." |
| 160 | + ); |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + List<string> contentex0P = new List<string>(); |
| 165 | + try |
| 166 | + { |
| 167 | + StreamReader sw0 = new StreamReader(ex0P); |
| 168 | + string cLine = sw0.ReadLine(); |
| 169 | + while (cLine != null) |
| 170 | + { |
| 171 | + contentex0P.Add(cLine); |
| 172 | + cLine = sw0.ReadLine(); |
| 173 | + } |
| 174 | + sw0.Close(); |
| 175 | + |
| 176 | + StreamWriter sr1 = new StreamWriter(ex1P); |
| 177 | + for (int i = contentex0P.Count - 1; i >= 0; i--) |
| 178 | + { |
| 179 | + sr1.WriteLine(contentex0P[i]); |
| 180 | + } |
| 181 | + sr1.Close(); |
| 182 | + } |
| 183 | + catch |
| 184 | + { |
| 185 | + Console.WriteLine( |
| 186 | + "An error was thrown from the Reverse method." |
| 187 | + ); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + public static void Concat( |
| 193 | + string ex0P, |
| 194 | + string ex1P, |
| 195 | + string ex2P, |
| 196 | + bool over = false |
| 197 | + ) |
| 198 | + { |
| 199 | + if (!File.Exists(ex0P) || !File.Exists(ex1P)) |
| 200 | + { |
| 201 | + Console.WriteLine( |
| 202 | + "There is a problem: one of the source file does not exist." |
| 203 | + ); |
| 204 | + } |
| 205 | + else if (File.Exists(ex2P) && !over) |
| 206 | + { |
| 207 | + Console.WriteLine( |
| 208 | + "There is a problem: the target file already exists." |
| 209 | + + "\nCall the method with an additional parameter" |
| 210 | + + "\nset to \"true\" to override the existing file." |
| 211 | + ); |
| 212 | + } |
| 213 | + else |
| 214 | + { |
| 215 | + List<string> contentex0P = new List<string>(); |
| 216 | + try |
| 217 | + { |
| 218 | + StreamReader sw0 = new StreamReader(ex0P); |
| 219 | + string cLine = sw0.ReadLine(); |
| 220 | + while (cLine != null) |
| 221 | + { |
| 222 | + contentex0P.Add(cLine); |
| 223 | + cLine = sw0.ReadLine(); |
| 224 | + } |
| 225 | + sw0.Close(); |
| 226 | + |
| 227 | + StreamReader sw1 = new StreamReader(ex1P); |
| 228 | + cLine = sw1.ReadLine(); |
| 229 | + while (cLine != null) |
| 230 | + { |
| 231 | + contentex0P.Add(cLine); |
| 232 | + cLine = sw1.ReadLine(); |
| 233 | + } |
| 234 | + sw1.Close(); |
| 235 | + |
| 236 | + StreamWriter sr1 = new StreamWriter(ex2P); |
| 237 | + for (int i = 0; i < contentex0P.Count; i++) |
| 238 | + { |
| 239 | + sr1.WriteLine(contentex0P[i]); |
| 240 | + } |
| 241 | + sr1.Close(); |
| 242 | + } |
| 243 | + catch |
| 244 | + { |
| 245 | + Console.WriteLine( |
| 246 | + "An error was thrown from the Concat method." |
| 247 | + ); |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + public static bool Balanced(string ex0P) |
| 253 | + { |
| 254 | + int count = -1; |
| 255 | + if (!File.Exists(ex0P)) |
| 256 | + { |
| 257 | + Console.WriteLine( |
| 258 | + "There is a problem: the source file does not exist." |
| 259 | + ); |
| 260 | + } |
| 261 | + else |
| 262 | + { |
| 263 | + try |
| 264 | + { |
| 265 | + StreamReader sw0 = new StreamReader(ex0P); |
| 266 | + count = 0; |
| 267 | + char[] cLineChar; |
| 268 | + string cLine = sw0.ReadLine(); |
| 269 | + while (cLine != null) |
| 270 | + { |
| 271 | + cLineChar = cLine.ToCharArray(); |
| 272 | + for (int i = 0; i < cLineChar.Length; i++) |
| 273 | + { |
| 274 | + if (cLineChar[i] == '(') |
| 275 | + { |
| 276 | + count++; |
| 277 | + } |
| 278 | + if (cLineChar[i] == ')') |
| 279 | + { |
| 280 | + if (count <= 0) |
| 281 | + { |
| 282 | + return false; |
| 283 | + } |
| 284 | + else |
| 285 | + { |
| 286 | + count--; |
| 287 | + } |
| 288 | + } |
| 289 | + } |
| 290 | + cLine = sw0.ReadLine(); |
| 291 | + } |
| 292 | + sw0.Close(); |
| 293 | + } |
| 294 | + catch |
| 295 | + { |
| 296 | + Console.WriteLine( |
| 297 | + "An error was thrown from the Concat method." |
| 298 | + ); |
| 299 | + } |
| 300 | + } |
| 301 | + return count == 0; |
| 302 | + } |
| 303 | +} |
0 commit comments