/*************************************************************************/ /// <summary> /// /// </summary> /// <param name="str"></param> /// <param name="chunkSize"></param> /// <returns></returns> public static IEnumerable <string> Chunk(string str, int chunkSize) { if (str.Length < chunkSize) { string[] s = new string[] { str }; return(s); } else { try { List <string> s = new List <string>(); for (int i = 0; i < str.Length; i += chunkSize) { s.Add(str.Substring(i, chunkSize)); } return(s); } catch { ExceptionTrap.Trap("Error reading data! Check for the correct number of characters and for Hex only inputs."); return(null); } } }
/*********************************************************************/ /// <summary> /// Calculates the checksum for the S19 line (uses _RawLine) /// </summary> /// <param name="write">Write the checksum the S19 object when /// completed, else just performs the calculation</param> /// <returns>Result of the S19 calculation</returns> private byte CalculateChecksum(bool write) { try { /* remove the first and last bytes and checksum the rest */ byte csum = 0; List <String> bytes = (List <String>)Util.Chunk(_RawLine.Substring(2, _RawLine.Length - 4), 2); foreach (string s in bytes) { csum += System.Convert.ToByte(s, 16); } csum = (byte)~csum; /* perform the one's complement */ /* if required, save off the checksum value */ if (write) { _Checksum = csum; } return(csum); } catch { ExceptionTrap.Trap("Error calculating checksum!"); return(0); } }
/*********************************************************************/ /// <summary> /// /// </summary> public bool Output(string outFile) { try { InstantTimer t = new InstantTimer(); System.IO.StreamWriter file; using (file = new System.IO.StreamWriter(outFile, false)) { foreach (S19Line line in SRecordLines) { string outLine = line.ToString(); file.WriteLine(outLine); } } file.Close(); //SRecordizer.LogIt(LogView.LogType.Info, "Saved File \'" + outFile + "\' Ok! (Time = " + t.Stop() +" ms)"); /* update the file name to the new file name - will match for save, but not for save as */ FileInfo fi = new FileInfo(outFile); _FileName = fi.Name; return(true); } catch { ExceptionTrap.Trap("Error writing S19 file. Your changes have not been saved!"); return(false); } }
/*********************************************************************/ /// <summary> /// /// </summary> public void ParseFile() { try { SRecordLines = new List <S19Line>(); bool flag24BitAddrUsed = false, flag32BitAddrUsed = false; /* the default is 16-bit */ string line; StreamReader reader = new StreamReader(_FileName); while ((line = reader.ReadLine()) != null) { /* create the new line */ S19Line newLine = new S19Line(line); /* count the occurance of each instruction for analysis */ switch (newLine.Instruction) { case S19Line.S19Instruction.S2: case S19Line.S19Instruction.S8: flag24BitAddrUsed = true; break; case S19Line.S19Instruction.S3: case S19Line.S19Instruction.S7: flag32BitAddrUsed = true; break; } /* add the new line to the S-Record object */ SRecordLines.Add(newLine); } reader.Close(); UpdateLineNumbering(); /* Set the address length to use */ if (flag32BitAddrUsed) { _MaxAddressLength = LEN_32_BIT_ADDR; } else if (flag24BitAddrUsed) { _MaxAddressLength = LEN_24_BIT_ADDR; } else { _MaxAddressLength = LEN_16_BIT_ADDR; } } catch { ExceptionTrap.Trap("Error Parsing S19 File!!"); } }
/*********************************************************************/ /// <summary> /// Updates the S19 line object given a raw data string /// </summary> /// <param name="line"></param> public void UpdateLine(string line) { try { _RawLine = line; string strInstr = line.Substring(IDX_INSTRUCTION, LEN_INSTRUCTION); _Size = System.Convert.ToByte(line.Substring(IDX_LINELENGTH, IDX_LINELENGTH), 16); _Checksum = System.Convert.ToByte(line.Substring(line.Length - LEN_CSUM, LEN_CSUM), 16); switch (strInstr) { case "S0": this._Instruction = S19Instruction.S0; this._Address = System.Convert.ToUInt32(line.Substring(IDX_ADDR, LEN_S0_ADDR), 16); this._Data = line.Substring(IDX_S0_DATA, line.Length - LEN_CSUM - IDX_S0_DATA); this._AddressLength = LEN_S0_ADDR; break; case "S1": this._Instruction = S19Instruction.S1; this._Address = System.Convert.ToUInt32(line.Substring(IDX_ADDR, LEN_S1_ADDR), 16); this._Data = line.Substring(IDX_S1_DATA, line.Length - LEN_CSUM - IDX_S1_DATA); this._AddressLength = LEN_S1_ADDR; break; case "S2": this._Instruction = S19Instruction.S2; this._Address = System.Convert.ToUInt32(line.Substring(IDX_ADDR, LEN_S2_ADDR), 16); this._Data = line.Substring(IDX_S2_DATA, line.Length - LEN_CSUM - IDX_S2_DATA); this._AddressLength = LEN_S2_ADDR; break; case "S3": this._Instruction = S19Instruction.S3; this._Address = System.Convert.ToUInt32(line.Substring(IDX_ADDR, LEN_S3_ADDR), 16); this._Data = line.Substring(IDX_S3_DATA, line.Length - LEN_CSUM - IDX_S3_DATA); this._AddressLength = LEN_S3_ADDR; break; case "S5": this._Instruction = S19Instruction.S5; this._Address = System.Convert.ToUInt32(line.Substring(IDX_ADDR, LEN_S5_ADDR), 16); this._Data = line.Substring(IDX_S5_DATA, line.Length - LEN_CSUM - IDX_S5_DATA); this._AddressLength = LEN_S5_ADDR; break; case "S7": this._Instruction = S19Instruction.S7; this._Address = System.Convert.ToUInt32(line.Substring(IDX_ADDR, LEN_S7_ADDR), 16); this._Data = line.Substring(IDX_S7_DATA, line.Length - LEN_CSUM - IDX_S7_DATA); this._AddressLength = LEN_S7_ADDR; break; case "S8": this._Instruction = S19Instruction.S8; this._Address = System.Convert.ToUInt32(line.Substring(IDX_ADDR, LEN_S8_ADDR), 16); this._Data = line.Substring(IDX_S8_DATA, line.Length - LEN_CSUM - IDX_S8_DATA); this._AddressLength = LEN_S8_ADDR; break; case "S9": this._Instruction = S19Instruction.S9; this._Address = System.Convert.ToUInt32(line.Substring(IDX_ADDR, LEN_S9_ADDR), 16); this._Data = line.Substring(IDX_S9_DATA, line.Length - LEN_CSUM - IDX_S9_DATA); this._AddressLength = LEN_S9_ADDR; break; default: this._Instruction = S19Instruction.Unknown; break; } } catch { ExceptionTrap.Trap("Error reading S19 data!"); } }