Skip to content

Commit 7c8e847

Browse files
small fix
1 parent 85f67f5 commit 7c8e847

File tree

2 files changed

+156
-125
lines changed

2 files changed

+156
-125
lines changed

MIDI-Keyboard/programChapter/OutViewerVisual.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Text;
56
using MIDIKeyboard.dataFolder;
67
using MIDIKeyboard.Miscellaneous;
@@ -44,9 +45,14 @@ public static bool OutViewerVisual_(bool runOutViewerVisual_)
4445
bool exists = Directory.Exists(profilePath);
4546
if (!exists)
4647
Directory.CreateDirectory(profilePath);
47-
string[] files = Directory.GetFiles(profilePath);
48+
var files = Directory.GetFiles(profilePath).ToList();
4849
Console.ForegroundColor = ConsoleColor.Black;
49-
for (int i = 0; i < files.Length; i++) {
50+
for (int i = 0; i < files.Count; i++) {
51+
if (files[i].Contains("-input")) {
52+
files.RemoveAt(i);
53+
i--;
54+
continue;
55+
}
5056
Console.BackgroundColor = i % 2 == 0 ? ConsoleColor.Gray : ConsoleColor.White;
5157
Console.WriteLine(i + ".\t" + files[i].Split('\\')[1].PadRight(32, ' '));
5258
}

MIDI-Keyboard/programChapter/run/LoadData.cs

+148-123
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Text;
45
using MIDIKeyboard.dataFolder;
6+
using MIDIKeyboard.Miscellaneous;
57
using static MIDIKeyboard.Miscellaneous.Miscellaneous;
68

79
namespace MIDIKeyboard.Run
@@ -46,7 +48,7 @@ private static bool SendOutputData()
4648
}
4749

4850
Console.WriteLine("No output data detected.");
49-
return true;
51+
return false;
5052
}
5153

5254
public static bool SendOutputDataZero()
@@ -73,159 +75,182 @@ private static bool LoadKeyData()
7375
{
7476
bool noErrors = true;
7577
Console.WriteLine("loading from file...");
76-
int chanel = int.Parse(rawData[0].Split(',')[0]);
77-
78-
for (int lineId = 1; lineId < rawData.Length; lineId++)
79-
if (rawData[lineId] != string.Concat(chanel)) {
80-
string[] loadedValues = rawData[lineId].Split(',');
81-
if (loadedValues.Length == 2) //add missing mode id
82-
loadedValues = new[]
83-
{loadedValues[0], loadedValues[1].Contains("\"") ? "1" : "0", loadedValues[1]};
84-
else if (loadedValues[1].Contains("\"")) { //if mode is a string, assume it as a macro argument
85-
string[] newLoadedValues = new string[loadedValues.Length + 1];
86-
newLoadedValues[0] = loadedValues[0];
87-
newLoadedValues[1] = "1";
88-
for (int i = 1; i < loadedValues.Length; i++)
89-
newLoadedValues[i + 1] = loadedValues[i];
90-
}
78+
//int chanel = int.Parse(rawData[0].Split(',')[0]);
79+
80+
int lineId = 1;
81+
//Settings.data.visual_profiles_path
82+
if (!rawData[1].Contains(",")) {
83+
lineId = 2;
84+
if (!File.Exists(Settings.data.visual_profiles_path + rawData[1].Trim())) {
85+
Error("Error at line 1 -> \"{0}\", Cannot find target profile.", rawData[1]);
86+
return false;
87+
}
88+
89+
string[] res = new string[rawData.Length-2];
90+
Array.Copy(rawData, 2, res, 0, res.Length);
91+
string[] profileStringArray = string.Join(",", File.ReadAllLines(Settings.data.visual_profiles_path + rawData[1].Trim(), Encoding.UTF8)).Split(',');
92+
string[] dataArray = string.Join(",", res).Split(',');
93+
string[] newRawData = new string[profileStringArray.Length + 2];
94+
newRawData[0] = rawData[0];
95+
newRawData[1] = rawData[1];
96+
for (int i = 0; i < profileStringArray.Length; i++) {
97+
newRawData[i + 2] = profileStringArray[i] + (dataArray[i].Trim().Length > 0 ? "," + dataArray[i] : "");
98+
}
9199

92-
int[] tempValues = new int[loadedValues.Length];
93-
var log = new StringBuilder();
94-
bool error = false;
95-
for (int i = 0; i < tempValues.Length; i++) {
96-
string value = loadedValues[i].Trim();
97-
if (!int.TryParse(value, out tempValues[i])) {
98-
if (i < 2) {
100+
rawData = newRawData;
101+
}
102+
103+
for (; lineId < rawData.Length; lineId++) {
104+
string[] loadedValues = rawData[lineId].Split(',');
105+
if (loadedValues.Length == 1) continue;
106+
if (loadedValues.Length == 2) //add missing mode id
107+
loadedValues = new[]
108+
{loadedValues[0], loadedValues[1].Contains("\"") ? "1" : "0", loadedValues[1]};
109+
else if (loadedValues[1].Contains("\"")) { //if mode is a string, assume it as a macro argument
110+
string[] newLoadedValues = new string[loadedValues.Length + 1];
111+
newLoadedValues[0] = loadedValues[0];
112+
newLoadedValues[1] = "1";
113+
for (int i = 1; i < loadedValues.Length; i++)
114+
newLoadedValues[i + 1] = loadedValues[i];
115+
}
116+
117+
int[] tempValues = new int[loadedValues.Length];
118+
var log = new StringBuilder();
119+
bool error = false;
120+
for (int i = 0; i < tempValues.Length; i++) {
121+
string value = loadedValues[i].Trim();
122+
if (!int.TryParse(value, out tempValues[i])) {
123+
if (i < 2) {
124+
noErrors = false;
125+
error = true;
126+
Error(
127+
"Error at line {0} -> \"{1}\", Value must be a full number: \"{2}\".", lineId,
128+
rawData[lineId], value);
129+
break;
130+
}
131+
132+
string[] convertValue;
133+
if ((convertValue = value.Split('\'')).Length > 1) { //'a' -> GetId("a")
134+
tempValues[i] = GetId(convertValue[1][0]);
135+
if (convertValue[0].Length > 0 && convertValue[0][0] == '-') {
136+
if (tempValues[1] != 1) {
137+
noErrors = false;
138+
error = true;
139+
Error(
140+
"Error at line {0} -> \"{1}\", Negative value not allowed: \"{2}\".",
141+
lineId,
142+
rawData[lineId], value);
143+
break;
144+
}
145+
146+
tempValues[i] = -tempValues[i];
147+
}
148+
} else if ((convertValue = value.Split('"')).Length > 1) {
149+
//"a" -> GetId("a") //"abc" -> GetId("a"), GetId("b"), GetId("c")
150+
151+
if (convertValue.Length > 4) {
99152
noErrors = false;
100153
error = true;
101154
Error(
102-
"Error at line {0} -> \"{1}\", Value must be a full number: \"{2}\".", lineId,
103-
rawData[lineId], value);
155+
"Error at line {0} -> \"{1}\", Multiple strings not allowed: \"{2}\".", lineId,
156+
rawData[lineId], value);
104157
break;
105158
}
106159

107-
string[] convertValue;
108-
if ((convertValue = value.Split('\'')).Length > 1) { //'a' -> GetId("a")
109-
tempValues[i] = GetId(convertValue[1][0]);
110-
if (convertValue[0][convertValue[0].Length - 1] == '-') {
111-
if (tempValues[1] != 1) {
112-
noErrors = false;
113-
error = true;
114-
Error(
115-
"Error at line {0} -> \"{1}\", Negative value not allowed: \"{2}\".",
116-
lineId,
117-
rawData[lineId], value);
118-
break;
119-
}
120-
121-
tempValues[i] = -tempValues[i];
160+
if (tempValues[1] != 1) {
161+
if (convertValue[0].Length != 0) {
162+
noErrors = false;
163+
error = true;
164+
Error(
165+
"Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineId,
166+
rawData[lineId], convertValue[0].Length > 1 ? "s" : "", convertValue[0]);
167+
break;
122168
}
123-
} else if ((convertValue = value.Split('"')).Length > 1) {
124-
//"a" -> GetId("a") //"abc" -> GetId("a"), GetId("b"), GetId("c")
125169

126-
if (convertValue.Length > 4) {
170+
if (convertValue[1].Length == 1) { tempValues[i] = GetId(convertValue[1][0]); }
171+
} else {
172+
if (convertValue[0].Length > 1 || (convertValue[0].Length != 0 &&
173+
(convertValue[0][0] != '-' &&
174+
convertValue[0][0] != '+'))) {
127175
noErrors = false;
128176
error = true;
129177
Error(
130-
"Error at line {0} -> \"{1}\", Multiple strings not allowed: \"{2}\".", lineId,
131-
rawData[lineId], value);
178+
"Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineId,
179+
rawData[lineId], convertValue[0].Length > 1 ? "s" : "", convertValue[0]);
132180
break;
133181
}
134182

135-
if (tempValues[1] != 1) {
136-
if (convertValue[0].Length != 0) {
137-
noErrors = false;
138-
error = true;
139-
Error(
140-
"Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineId,
141-
rawData[lineId], convertValue[0].Length > 1 ? "s" : "", convertValue[0]);
142-
break;
143-
}
144-
145-
if (convertValue[1].Length == 1) { tempValues[i] = GetId(convertValue[1][0]); }
183+
if (convertValue[0].Length == 1 && convertValue[1].Length == 1) {
184+
tempValues[i] = GetId(convertValue[1][0]);
185+
if (convertValue[0][0] != '-')
186+
tempValues[i] = -tempValues[i];
146187
} else {
147-
if (convertValue[0].Length > 1 || (convertValue[0].Length != 0 &&
148-
(convertValue[0][0] != '-' &&
149-
convertValue[0][0] != '+'))) {
150-
noErrors = false;
151-
error = true;
152-
Error(
153-
"Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineId,
154-
rawData[lineId], convertValue[0].Length > 1 ? "s" : "", convertValue[0]);
155-
break;
156-
}
188+
int length = convertValue[0].Length == 1
189+
? convertValue[1].Length - 1
190+
: convertValue[1].Length + convertValue[1].Length - 1;
157191

158-
if (convertValue[0].Length == 1 && convertValue[1].Length == 1) {
159-
tempValues[i] = GetId(convertValue[1][0]);
160-
if (convertValue[0][0] != '-')
161-
tempValues[i] = -tempValues[i];
162-
} else {
163-
int length = convertValue[0].Length == 1
164-
? convertValue[1].Length - 1
165-
: convertValue[1].Length + convertValue[1].Length - 1;
166-
167-
//copy old values to new array with a gap for the new values.
168-
int[] newTempV = new int[tempValues.Length + length];
169-
for (int j = 0; j < tempValues.Length; j++) {
170-
if (j < i) { newTempV[j] = tempValues[j]; } else if (j > i) {
171-
newTempV[j + length] = tempValues[j];
172-
}
192+
//copy old values to new array with a gap for the new values.
193+
int[] newTempV = new int[tempValues.Length + length];
194+
for (int j = 0; j < tempValues.Length; j++) {
195+
if (j < i) { newTempV[j] = tempValues[j]; } else if (j > i) {
196+
newTempV[j + length] = tempValues[j];
173197
}
198+
}
174199

175-
// insert new values
176-
if (convertValue[0].Length == 1) {
177-
if (convertValue[0][0] == '-')
178-
for (int j = 0; j < convertValue[1].Length; j++) {
179-
newTempV[j + i] = -GetId(convertValue[1][j]);
180-
}
181-
else
182-
for (int j = 0; j < convertValue[1].Length; j++) {
183-
newTempV[j + i] = GetId(convertValue[1][j]);
184-
}
185-
} else
200+
// insert new values
201+
if (convertValue[0].Length == 1) {
202+
if (convertValue[0][0] == '-')
186203
for (int j = 0; j < convertValue[1].Length; j++) {
187-
newTempV[j * 2 + i] = GetId(convertValue[1][j]);
188-
newTempV[j * 2 + i + 1] = -GetId(convertValue[1][j]);
204+
newTempV[j + i] = -GetId(convertValue[1][j]);
189205
}
206+
else
207+
for (int j = 0; j < convertValue[1].Length; j++) {
208+
newTempV[j + i] = GetId(convertValue[1][j]);
209+
}
210+
} else
211+
for (int j = 0; j < convertValue[1].Length; j++) {
212+
newTempV[j * 2 + i] = GetId(convertValue[1][j]);
213+
newTempV[j * 2 + i + 1] = -GetId(convertValue[1][j]);
214+
}
190215

191-
//apply new values
192-
tempValues = newTempV;
193-
i += length;
194-
}
195-
}
196-
} else if (value.Length == 1) //a -> GetId("a")
197-
tempValues[i] = GetId(value[0]);
198-
else { //-a -> -GetId("a")
199-
if (value.Length == 2 && value[0] == '-')
200-
tempValues[i] = -GetId(value[1]);
201-
else {
202-
noErrors = false;
203-
error = true;
204-
Error(
205-
"Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineId,
206-
rawData[lineId], value.Length > 1 ? "s" : "", value);
207-
break;
216+
//apply new values
217+
tempValues = newTempV;
218+
i += length;
208219
}
209220
}
221+
} else if (value.Length == 1) //a -> GetId("a")
222+
tempValues[i] = GetId(value[0]);
223+
else { //-a -> -GetId("a")
224+
if (value.Length == 2 && value[0] == '-')
225+
tempValues[i] = -GetId(value[1]);
226+
else {
227+
noErrors = false;
228+
error = true;
229+
Error(
230+
"Error at line {0} -> \"{1}\", Unexpected character{2}: \"{3}\".", lineId,
231+
rawData[lineId], value.Length > 1 ? "s" : "", value);
232+
break;
233+
}
210234
}
211-
212-
log.Append(value);
213-
log.Append(' ');
214235
}
215236

216-
if (!error)
217-
Program.values.Add(tempValues);
218-
219-
log.Append("-> ");
220-
foreach (int item in tempValues) {
221-
log.Append(item);
222-
log.Append(' ');
223-
}
237+
log.Append(value);
238+
log.Append(' ');
239+
}
224240

241+
if (!error)
242+
Program.values.Add(tempValues);
225243

226-
Console.WriteLine(log);
244+
log.Append("-> ");
245+
foreach (int item in tempValues) {
246+
log.Append(item);
247+
log.Append(' ');
227248
}
228249

250+
251+
Console.WriteLine(log);
252+
}
253+
229254
return noErrors;
230255
}
231256
}

0 commit comments

Comments
 (0)