public override string Act() { Active = true; bool check = Control.EDFPlusHandle.ValidFormat; if (!Control.EDFPlusHandle.ValidFormat) { throw new ActionCannotDoWhatDoBeDo(@Control.EDFPlusHandle.FileName + " is not in a valid EDF+ format! Most commonly this is because of illegal ASCII characters in the header. Only ASCII characters 32-126 are allowed (EDF specifications of 1992)."); } //Seems that the autoread function of the library is not alway accurate! if (!Control.EDFPlusHandle.TALRead) { Control.EDFPlusHandle.ReadTALsToMemory(); } EdfPlusAnnotationList list = Control.EDFPlusHandle.TAL; foreach (EdfPlusAnnotation annotation in list) { int trigger = ParseAnnotation(annotation.Annotation); if (trigger >= 0) { annotation.Annotation = trigger.ToString(); } else { annotation.Annotation = annotation.Annotation += ":NaN"; } } Trigger[] PresentationLogTriggers = Control.PresentationLogHandle.Triggers; Trigger[] EDFPlusTriggers = Control.EDFPlusHandle.Triggers; if (PresentationLogTriggers.Length == 0 || EDFPlusTriggers.Length == 0) { throw new ActionCannotDoWhatDoBeDo("Either the Presentation (#" + PresentationLogTriggers.Length + ") or EDF+ Trigger #(" + EDFPlusTriggers.Length + ") list came up empty. Check your parsing configuration!"); } Active = false; return("Action: Parsed triggers on " + @Path.GetFileName(Control.EDFPlusHandle.FileName) + " and " + @Path.GetFileName(Control.PresentationLogHandle.FileName)); }
public override string Act() { Active = true; if (Control.EDFPlusHandle.TALRead) { throw new ActionCannotDoWhatDoBeDo("TAL are already read in, no use going over the origin buffer! Call open files such that this does not happen!"); } UTF8Encoding encoding = new UTF8Encoding(); byte[] FirstFlagArray = encoding.GetBytes(FirstFlag.ToCharArray()); int FirstCounter = 0; byte[] SecondFlagArray = encoding.GetBytes(SecondFlag.ToCharArray()); int SecondCounter = 0; char[] rep = { ReplacementChar }; byte[] replacement = encoding.GetBytes(rep, 0, 1); Stream ByteStream = Control.EDFPlusHandle.MemoryHandle; long dataBlockOffset = Control.EDFPlusHandle.AnnotationSignalOffsetBytes; if (ByteStream.Position != dataBlockOffset) { ByteStream.Seek(dataBlockOffset, SeekOrigin.Begin); } BinaryReader br = new BinaryReader(ByteStream, encoding); BinaryWriter bw = new BinaryWriter(ByteStream, encoding); bool FirstHit = false; int replacements = 0; // While loop is simple. See if the bytes match up with the firstflag, then... when armed... // check for the secondflag and if THAT is armed replace the following byte. while (ByteStream.Position < ByteStream.Length) { byte c = br.ReadByte(); if (FirstHit) { if (c == SecondFlagArray[SecondCounter]) { if (SecondCounter == SecondFlagArray.Length - 1) { ByteStream.WriteByte(replacement[0]); FirstHit = false; replacements++; SecondCounter = 0; FirstCounter = 0; } else { SecondCounter++; } } else { SecondCounter = 0; } } else { if (c == FirstFlagArray[FirstCounter]) { if (FirstCounter == FirstFlagArray.Length - 1) { FirstHit = true; FirstCounter = 0; } else { FirstCounter++; } } else { FirstCounter = 0; } } } Active = false; if (replacements == 0) { throw new ActionCannotDoWhatDoBeDo("Did not fix any of the faulty bytes in the BioTrace Fix. Recheck the flags!"); } Control.EDFPlusHandle.ReadTALsToMemory(); EdfPlusAnnotationList list = Control.EDFPlusHandle.TAL; return("Action: Annotations fixed in " + @Path.GetFileName(Control.EDFPlusHandle.FileName)); }