Skip to content

Commit 7f63710

Browse files
Fixes
added more error reports and fixed bug
1 parent ab154e4 commit 7f63710

File tree

1 file changed

+63
-77
lines changed
  • MIDI-Keyboard/programChapter

1 file changed

+63
-77
lines changed

MIDI-Keyboard/programChapter/Run.cs

+63-77
Original file line numberDiff line numberDiff line change
@@ -23,78 +23,77 @@ public static bool Run_(bool runPogram)
2323

2424
//load data
2525
if (Program.values.Count < 1) {
26-
Console.WriteLine("loading from file...");
27-
array = File.ReadAllLines("data.txt");
28-
chanel = int.Parse(array[0].Split(',')[0]);
26+
if (File.Exists("data.txt") && (array = File.ReadAllLines("data.txt")).Length > 2) {
27+
Console.WriteLine("loading from file...");
28+
chanel = int.Parse(array[0].Split(',')[0]);
2929

30-
//color
31-
if (array[0].Split(',').Length > 1) {
32-
string[] ColorArray = array[0].Split(',');
30+
//color
31+
if (array[0].Split(',').Length > 1) {
32+
string[] ColorArray = array[0].Split(',');
3333

34-
InputPort midiOut = new InputPort();
35-
midiOut.OpenOut(int.Parse(ColorArray[1].Trim()));
34+
InputPort midiOut = new InputPort();
35+
midiOut.OpenOut(int.Parse(ColorArray[1].Trim()));
3636

37-
//send output
38-
for (int i = 2; i + 1 < ColorArray.Length; i += 2) {
39-
midiOut.MidiOutMsg((byte)int.Parse(ColorArray[i].Trim()), (byte)int.Parse(ColorArray[i + 1].Trim()));
40-
}
37+
//send output
38+
for (int i = 2; i + 1 < ColorArray.Length; i += 2) {
39+
midiOut.MidiOutMsg((byte)int.Parse(ColorArray[i].Trim()), (byte)int.Parse(ColorArray[i + 1].Trim()));
40+
}
4141

42-
midiOut.CloseOut();
43-
}
42+
midiOut.CloseOut();
43+
}
4444

45-
//load keys
46-
for (int lineID = 0; lineID < array.Length; lineID++) {
47-
if (array[lineID] != string.Concat(chanel)) {
48-
string[] loadedValues = array[lineID].Split(new char[] { ',' });
49-
int[] tempValues = new int[loadedValues.Length];
50-
StringBuilder log = new StringBuilder();
51-
for (int i = 0; i < tempValues.Length; i++) {
52-
string value = loadedValues[i].Trim();
53-
if (!int.TryParse(value, out tempValues[i])) {
54-
string[] convertValue;
55-
if ((convertValue = value.Split('\'')).Length > 1) {
56-
tempValues[i] = GetID(convertValue[1][0]);
57-
if (convertValue[0][convertValue[0].Length - 1] == '-') {
58-
if (i < 2 || i > 1 && tempValues[1] != 1) {
59-
error = true;
60-
Error("Error at line {0} -> \"{1}\", Negativ value not allowed: \"{2}\".", lineID, array[lineID], value);
61-
} else
62-
tempValues[i] = -tempValues[i];
63-
}
64-
} else if ((convertValue = value.Split('"')).Length > 1) {
65-
tempValues[i] = GetID(convertValue[1][0]);
66-
if (convertValue[0][convertValue[0].Length - 1] == '-') {
67-
if (i < 2 || i > 1 && tempValues[1] != 1) {
68-
error = true;
69-
Error("Error at line {0} -> \"{1}\", Negativ value not allowed: \"{2}\".", lineID, array[lineID], value);
70-
} else
71-
tempValues[i] = -tempValues[i];
72-
}
73-
} else if (value.Length == 1) {
74-
tempValues[i] = GetID(value[0]);
75-
} else {
76-
if (value.Length == 2 && value[0] == '-') {
77-
tempValues[i] = -GetID(value[1]);
45+
//load keys
46+
for (int lineID = 0; lineID < array.Length; lineID++) {
47+
if (array[lineID] != string.Concat(chanel)) {
48+
string[] loadedValues = array[lineID].Split(new char[] { ',' });
49+
int[] tempValues = new int[loadedValues.Length];
50+
StringBuilder log = new StringBuilder();
51+
for (int i = 0; i < tempValues.Length; i++) {
52+
string value = loadedValues[i].Trim();
53+
if (!int.TryParse(value, out tempValues[i])) {
54+
string[] convertValue;
55+
if ((convertValue = value.Split('\'')).Length > 1) {
56+
tempValues[i] = GetID(convertValue[1][0]);
57+
if (convertValue[0][convertValue[0].Length - 1] == '-') {
58+
if (i < 2 || i > 1 && tempValues[1] != 1) {
59+
error = true;
60+
Error("Error at line {0} -> \"{1}\", Negativ value not allowed: \"{2}\".", lineID, array[lineID], value);
61+
} else
62+
tempValues[i] = -tempValues[i];
63+
}
64+
} else if ((convertValue = value.Split('"')).Length > 1) {
65+
tempValues[i] = GetID(convertValue[1][0]);
66+
if (convertValue[0][convertValue[0].Length - 1] == '-') {
67+
if (i < 2 || i > 1 && tempValues[1] != 1) {
68+
error = true;
69+
Error("Error at line {0} -> \"{1}\", Negativ value not allowed: \"{2}\".", lineID, array[lineID], value);
70+
} else
71+
tempValues[i] = -tempValues[i];
72+
}
73+
} else if (value.Length == 1) {
74+
tempValues[i] = GetID(value[0]);
7875
} else {
79-
error = true;
80-
Error("Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineID, array[lineID], value.Length > 1 ? "s" : "", value);
76+
if (value.Length == 2 && value[0] == '-') {
77+
tempValues[i] = -GetID(value[1]);
78+
} else {
79+
error = true;
80+
Error("Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineID, array[lineID], value.Length > 1 ? "s" : "", value);
81+
}
8182
}
8283
}
84+
log.Append(value);
85+
log.Append(' ');
8386
}
84-
log.Append(value);
85-
log.Append(' ');
87+
Program.values.Add(tempValues);
88+
Console.WriteLine(log);
8689
}
87-
Program.values.Add(tempValues);
88-
Console.WriteLine(log);
8990
}
91+
} else {
92+
error = true;
93+
Error("Error -> No data.");
9094
}
9195
} else {
92-
error = true;
93-
Console.ForegroundColor = ConsoleColor.Red;
94-
Console.WriteLine("Error -> No data.");
95-
Console.ResetColor();
96-
Console.WriteLine("Press any key to continue...");
97-
Console.ReadKey();
96+
Console.WriteLine("Data already loaded in memory...");
9897
}
9998

10099
string[] valuesHex = new string[Program.values.Count];
@@ -187,8 +186,6 @@ public static bool Run_(bool runPogram)
187186
}
188187
Console.WriteLine(valueHex + "\n");
189188
old2 = value;
190-
191-
192189
}
193190
Program.waitHandle.Reset();
194191
}
@@ -249,21 +246,10 @@ private static void Error(string s, object[] os)
249246
Console.WriteLine("Press any key to ignore and continue...");
250247
Console.ReadKey();
251248
}
252-
private static void Error(string s, object o1)
253-
{
254-
Error(s, new object[] { o1 });
255-
}
256-
private static void Error(string s, object o1, object o2)
257-
{
258-
Error(s, new object[] { o1, o2 });
259-
}
260-
private static void Error(string s, object o1, object o2, object o3)
261-
{
262-
Error(s, new object[] { o1, o2, o3 });
263-
}
264-
private static void Error(string s, object o1, object o2, object o3, object o4)
265-
{
266-
Error(s, new object[] { o1, o2, o3, o4 });
267-
}
249+
private static void Error(string s, object o1) => Error(s, new object[] { o1 });
250+
private static void Error(string s, object o1, object o2) => Error(s, new object[] { o1, o2 });
251+
private static void Error(string s, object o1, object o2, object o3) => Error(s, new object[] { o1, o2, o3 });
252+
private static void Error(string s, object o1, object o2, object o3, object o4) => Error(s, new object[] { o1, o2, o3, o4 });
253+
268254
}
269255
}

0 commit comments

Comments
 (0)