/** * Static function to insert key/value pair into specified IniSection */ public static Boolean TryInsertValue(IniSection section, String valueName, String value) { if ((section == null) || (section.sectionValues == null)) { return false; } else { UInt32 valueNum; valueName = valueName.ToUpper(); // Hex values must be prefixed by "0x" value = value.ToLower(); if (value.StartsWith("0x")) { if (!UInt32.TryParse(value.Replace("0x", ""), NumberStyles.HexNumber, null, out valueNum) ) { return false; } else { section.sectionValues[valueName] = valueNum; } } else if (UInt32.TryParse(value, out valueNum)) { section.sectionValues[valueName] = valueNum; } else { section.sectionValues[valueName] = value; } } return true; }
public void InsertSection(IniSection section) { IniSection sec = GetSectionByName(section.sectionName); if (sec == null) { // Section by this name does not exist sections.Add(sec); } else { // Section by this name exists, simply insert values foreach (DictionaryEntry de in section.sectionValues) { sec.InsertValue((String)de.Key, (String)de.Value); } } }
public static String ToString(IniSection section) { StringWriter sw = new StringWriter(new StringBuilder()); sw.WriteLine("[{0}]",section.sectionName); foreach (DictionaryEntry de in section.sectionValues) { sw.WriteLine("{0} = {1}",de.Key,de.Value); } return sw.GetStringBuilder().ToString(); }
/** * Static function to build an INI data string * * @param sections is an array of IniSection objects */ public static String ToString(IniSection[] sections) { StringWriter sw = new StringWriter(new StringBuilder()); foreach(IniSection currSection in sections) { sw.WriteLine(currSection.ToString()); sw.WriteLine(); } return sw.GetStringBuilder().ToString(); }
/** * Function to Parse an input data stream containing INI data * * @param iniStream is an input Stream object */ public static IniFile Parse(Stream iniStream) { IniFile myFile = new IniFile(); List<String> streamLines = new List<String>(); List<IniSection> streamSections = new List<IniSection>(); StreamReader iniSR; IniSection currSec = new IniSection(); Boolean inASection = false; Regex iniSecHdr = new Regex("\\[[A-Za-z0-9_]*\\]"); try { iniSR = new StreamReader(iniStream); } catch (Exception e) { Console.WriteLine(e.Message); throw e; } // Get lines of data from the stream while (!iniSR.EndOfStream) { streamLines.Add(iniSR.ReadLine()); } // Parse actually line contents for (int i=0; i<streamLines.Count; i++) { // Get current line from the streamLines List String currLine = (streamLines[i]).Trim(); // Ignore comment and empty lines if ( (currLine.StartsWith(";")) || (currLine.Equals("")) ) { continue; } // If we find a section header, begin a new section Match m = iniSecHdr.Match(currLine); if (m.Success) { if (inASection) { streamSections.Add(currSec); } inASection = true; currSec = new IniSection(); currSec.sectionName = m.Value.ToUpper().Trim('[', ']'); currSec.sectionValues = new Hashtable(); Debug.DebugMSG("INI Section: {0}", currSec.sectionName); continue; } // If we find key/value paramter pairs, parse the value and key if (currLine.Contains("=")) { // Split the name at the '=' sign String[] paramAndValue = currLine.Split(new char[1] { '=' }, StringSplitOptions.RemoveEmptyEntries); // Trim the param name and value paramAndValue[0] = paramAndValue[0].Trim().ToUpper(); paramAndValue[1] = paramAndValue[1].Trim(); // Hex values must be prefixed by "0x" if (paramAndValue[1].StartsWith("0x") || paramAndValue[1].StartsWith("0X")) { currSec.sectionValues[paramAndValue[0]] = UInt32.Parse(paramAndValue[1].Replace("0x", ""), NumberStyles.AllowHexSpecifier); } else { UInt32 value; if (UInt32.TryParse(paramAndValue[1], out value)) { currSec.sectionValues[paramAndValue[0]] = value; } else { currSec.sectionValues[paramAndValue[0]] = paramAndValue[1]; } } Debug.DebugMSG("\t{0} = {1}", paramAndValue[0], currSec.sectionValues[paramAndValue[0]]); continue; } // Any other lines throw an error throw new Exception(String.Format("Bad INI data at line {0}: {1}.", i, currLine)); } // End of parsing INI // Add last section to return value if (inASection) { streamSections.Add(currSec); } // Return parsed ini sections myFile.sections = streamSections; return myFile; }
public override String ToString() { return(IniSection.ToString(this)); }
/** * Function to Parse an input data stream containing INI data * * @param iniStream is an input Stream object */ public static IniFile Parse(Stream iniStream) { IniFile myFile = new IniFile(); List <String> streamLines = new List <String>(); List <IniSection> streamSections = new List <IniSection>(); StreamReader iniSR; IniSection currSec = new IniSection(); Boolean inASection = false; Regex iniSecHdr = new Regex("\\[[A-Za-z0-9_]*\\]"); try { iniSR = new StreamReader(iniStream); } catch (Exception e) { Console.WriteLine(e.Message); throw e; } // Get lines of data from the stream while (!iniSR.EndOfStream) { streamLines.Add(iniSR.ReadLine()); } // Parse actually line contents for (int i = 0; i < streamLines.Count; i++) { // Get current line from the streamLines List String currLine = (streamLines[i]).Trim(); // Ignore comment and empty lines if ((currLine.StartsWith(";")) || (currLine.Equals(""))) { continue; } // If we find a section header, begin a new section Match m = iniSecHdr.Match(currLine); if (m.Success) { if (inASection) { streamSections.Add(currSec); } inASection = true; currSec = new IniSection(); currSec.sectionName = m.Value.ToUpper().Trim('[', ']'); currSec.sectionValues = new Hashtable(); Debug.DebugMSG("INI Section: {0}", currSec.sectionName); continue; } // If we find key/value paramter pairs, parse the value and key if (currLine.Contains("=")) { // Split the name at the '=' sign String[] paramAndValue = currLine.Split(new char[1] { '=' }, StringSplitOptions.RemoveEmptyEntries); // Trim the param name and value paramAndValue[0] = paramAndValue[0].Trim().ToUpper(); paramAndValue[1] = paramAndValue[1].Trim(); // Hex values must be prefixed by "0x" if (paramAndValue[1].StartsWith("0x") || paramAndValue[1].StartsWith("0X")) { currSec.sectionValues[paramAndValue[0]] = UInt32.Parse(paramAndValue[1].Replace("0x", ""), NumberStyles.AllowHexSpecifier); } else { UInt32 value; if (UInt32.TryParse(paramAndValue[1], out value)) { currSec.sectionValues[paramAndValue[0]] = value; } else { currSec.sectionValues[paramAndValue[0]] = paramAndValue[1]; } } Debug.DebugMSG("\t{0} = {1}", paramAndValue[0], currSec.sectionValues[paramAndValue[0]]); continue; } // Any other lines throw an error throw new Exception(String.Format("Bad INI data at line {0}: {1}.", i, currLine)); } // End of parsing INI // Add last section to return value if (inASection) { streamSections.Add(currSec); } // Return parsed ini sections myFile.sections = streamSections; return(myFile); }
public static retType InsertAISCommandViaINI(AISGen devAISGen, IniSection sec) { #region Handle Input Binary and Object Files if (sec.sectionName.Equals("INPUTFILE", StringComparison.OrdinalIgnoreCase)) { String fileName = null; Boolean useEntryPoint = false; UInt32 loadAddr = 0xFFFFFFFF; UInt32 entryPointAddr = 0xFFFFFFFF; foreach (DictionaryEntry de in sec.sectionValues) { // File name for binary section data if (((String)de.Key).Equals("FILENAME", StringComparison.OrdinalIgnoreCase)) { fileName = (String) sec.sectionValues["FILENAME"]; } // Binary section's load address in the memory map if (((String)de.Key).Equals("LOADADDRESS", StringComparison.OrdinalIgnoreCase)) { loadAddr = (UInt32) sec.sectionValues["LOADADDRESS"]; } // Binary section's entry point address in the memory map if (((String)de.Key).Equals("ENTRYPOINTADDRESS", StringComparison.OrdinalIgnoreCase)) { entryPointAddr = (UInt32) sec.sectionValues["ENTRYPOINTADDRESS"]; } // Option to specify that this entry point should be used for AIS if (((String)de.Key).Equals("USEENTRYPOINT", StringComparison.OrdinalIgnoreCase)) { if (((String)sec.sectionValues["USEENTRYPOINT"]).Equals("YES", StringComparison.OrdinalIgnoreCase)) { useEntryPoint = true; } else if (((String)sec.sectionValues["USEENTRYPOINT"]).Equals("TRUE", StringComparison.OrdinalIgnoreCase)) { useEntryPoint = true; } } } if (fileName == null) { Console.WriteLine("ERROR: File name must be provided in INPUTFILE section."); return retType.FAIL; } // Insert the file into the AIS image if ( loadAddr != 0xFFFFFFFF ) { // binary image if ( entryPointAddr != 0xFFFFFFFF ) { devAISGen.InsertAISObjectFile(fileName, loadAddr, entryPointAddr); } else { devAISGen.InsertAISObjectFile(fileName, loadAddr); } } else { if ( entryPointAddr != 0xFFFFFFFF ) { devAISGen.InsertAISObjectFile(fileName,true); } else { devAISGen.InsertAISObjectFile(fileName,useEntryPoint); } } return retType.SUCCESS; } #endregion #region Handle ROM and AIS Extra Functions // Handle ROM functions if (devAISGen.ROMFunc != null) { for (UInt32 j = 0; j < devAISGen.ROMFunc.Length; j++) { if (sec.sectionName.Equals(devAISGen.ROMFunc[j].iniSectionName, StringComparison.OrdinalIgnoreCase)) { UInt32 funcIndex = j; UInt32[] args = new UInt32[ (UInt32)devAISGen.ROMFunc[funcIndex].numParams ]; for (Int32 k = 0; k < devAISGen.ROMFunc[funcIndex].numParams; k++) { Debug.DebugMSG("\tParam name: {0}, Param num: {1}, Value: {2}\n", devAISGen.ROMFunc[funcIndex].paramNames[k], k, sec.sectionValues[devAISGen.ROMFunc[funcIndex].paramNames[k].ToUpper()]); try { args[k] = (UInt32) sec.sectionValues[devAISGen.ROMFunc[funcIndex].paramNames[k].ToUpper()]; } catch { Console.WriteLine("WARNING: INI Section {0} is malformed - {1} parameter not provided. Ignoring section contens.",sec.sectionName, devAISGen.ROMFunc[funcIndex].paramNames[k].ToUpper()); return retType.SUCCESS; } } devAISGen.InsertAISFunctionExecute((UInt16) funcIndex, (UInt16) devAISGen.ROMFunc[funcIndex].numParams, args); return retType.SUCCESS; } } } // Handle AISExtras functions if (devAISGen.AISExtraFunc != null) { for (UInt32 j = 0; j < devAISGen.AISExtraFunc.Length; j++) { if (sec.sectionName.Equals(devAISGen.AISExtraFunc[j].iniSectionName, StringComparison.OrdinalIgnoreCase)) { UInt32 funcIndex = j; UInt32[] args = new UInt32[ (UInt32)devAISGen.AISExtraFunc[j].numParams ]; // Load the AIS extras file if needed { IniSection tempSec = new IniSection(); tempSec.sectionName = "INPUTFILE"; tempSec.sectionValues = new Hashtable(); tempSec.sectionValues["FILENAME"] = devAISGen.AISExtraFunc[funcIndex].aisExtraFileName; EmbeddedFileIO.ExtractFile(Assembly.GetExecutingAssembly(), devAISGen.AISExtraFunc[funcIndex].aisExtraFileName, true); InsertAISCommandViaINI(devAISGen, tempSec); Debug.DebugMSG("AISExtras file loaded.\n"); // Use symbols to get address for AISExtra functions and parameters for (Int32 k = 0; k < devAISGen.AISExtraFunc.Length; k++) { ObjectFile tempFile = FindFileWithSymbol(devAISGen, devAISGen.AISExtraFunc[funcIndex].funcName); if (tempFile == null) { // Try looking for underscore version tempFile = FindFileWithSymbol(devAISGen, "_" + devAISGen.AISExtraFunc[funcIndex].funcName); } if (tempFile != null) { ObjectSymbol tempSym = tempFile.symFind(devAISGen.AISExtraFunc[funcIndex].funcName); if (tempSym == null) { // Try looking for underscore version tempSym = tempFile.symFind("_"+devAISGen.AISExtraFunc[funcIndex].funcName); } if (tempSym != null) { devAISGen.AISExtraFunc[funcIndex].funcAddr = (UInt32) tempSym.value; ObjectSection tempObjSec = tempFile.secFind(".params"); if (tempObjSec == null) { Console.WriteLine(".params section not found in file {0}.", devAISGen.AISExtraFunc[funcIndex].aisExtraFileName); return retType.FAIL; } else { devAISGen.AISExtraFunc[funcIndex].paramAddr = (UInt32) tempObjSec.runAddr; } } else { Console.WriteLine("AIS extra function, {0}, not found in file {1}.", devAISGen.AISExtraFunc[funcIndex].funcName, devAISGen.AISExtraFunc[funcIndex].aisExtraFileName); return retType.FAIL; } } else { // The function name was not found - that's a big problem with our // device specific AISGen class. Console.WriteLine("AIS extra function, {0}, not found in file {1}.", devAISGen.AISExtraFunc[funcIndex].funcName, devAISGen.AISExtraFunc[funcIndex].aisExtraFileName); return retType.FAIL; } } } Debug.DebugMSG("Found required sections and symbols in AISExtras file.\n"); // Validate input parameters for (Int32 k = 0; k < devAISGen.AISExtraFunc[funcIndex].numParams; k++) { try { args[k] = (UInt32) sec.sectionValues[devAISGen.AISExtraFunc[funcIndex].paramNames[k].ToUpper()]; } catch { Console.WriteLine("WARNING: INI Section {0} is malformed - {1} parameter not provided. Ignoring section contens.",sec.sectionName, devAISGen.ROMFunc[funcIndex].paramNames[k].ToUpper()); return retType.SUCCESS; } } Debug.DebugMSG("Input parameter validation for AISExtras function is complete.\n"); // Write SET command for each input parameter for (Int32 k = 0; k < devAISGen.AISExtraFunc[funcIndex].numParams; k++) { devAISGen.InsertAISSet( (UInt32)AisSetType.INT, // Write type field (32-bit only) (UInt32) (devAISGen.AISExtraFunc[funcIndex].paramAddr + (k * 4)), args[k], (UInt32)0x0 ); // Write Sleep value (should always be zero) } // Now that params are set, Jump to function devAISGen.InsertAISJump(devAISGen.AISExtraFunc[funcIndex].funcAddr); return retType.SUCCESS; } } } #endregion #region Handle AIS Command Sections if (sec.sectionName.Equals("AIS_EnableCRC", StringComparison.OrdinalIgnoreCase)) { devAISGen.InsertAISEnableCRC(); } else if (sec.sectionName.Equals("AIS_DisableCRC", StringComparison.OrdinalIgnoreCase)) { devAISGen.InsertAISDisableCRC(); } else if (sec.sectionName.Equals("AIS_RequestCRC", StringComparison.OrdinalIgnoreCase)) { UInt32 crcValue = 0x00000000; Int32 seekValue = -12; foreach (DictionaryEntry de in sec.sectionValues) { if (((String)de.Key).Equals("CRCValue", StringComparison.OrdinalIgnoreCase)) { crcValue = (UInt32)sec.sectionValues["CRCVALUE"]; } if (((String)de.Key).Equals("SEEKValue", StringComparison.OrdinalIgnoreCase)) { seekValue = (Int32)sec.sectionValues["SEEKVALUE"]; } } if (devAISGen.InsertAISRequestCRC(crcValue, seekValue) != retType.SUCCESS) { Console.WriteLine("WARNING: Final function register AIS command failed."); } } else if (sec.sectionName.Equals("AIS_Jump", StringComparison.OrdinalIgnoreCase)) { String symbolName = ""; UInt32 address = 0x00000000; foreach (DictionaryEntry de in sec.sectionValues) { if (((String)de.Key).Equals("LOCATION", StringComparison.OrdinalIgnoreCase)) { symbolName = sec.sectionValues["LOCATION"].ToString(); } } // See if string is number (address) if (UInt32.TryParse(symbolName, out address)) { if (devAISGen.InsertAISJump(address) != retType.SUCCESS) { Console.WriteLine("WARNING: AIS Jump to {0} was not inserted.",symbolName); } } else { if (devAISGen.InsertAISJump(symbolName) != retType.SUCCESS) { Console.WriteLine("WARNING: AIS Jump to {0} was not inserted.",symbolName); } } } else if (sec.sectionName.Equals("AIS_JumpClose", StringComparison.OrdinalIgnoreCase)) { String symbolName = ""; UInt32 address = 0x00000000; foreach (DictionaryEntry de in sec.sectionValues) { if (((String)de.Key).Equals("ENTRYPOINT", StringComparison.OrdinalIgnoreCase)) { symbolName = (String)sec.sectionValues["ENTRYPOINT"]; } } if (symbolName == "") { devAISGen.InsertAISJumpClose(devAISGen.entryPoint); } else { // See if string is number (address) if (UInt32.TryParse(symbolName, out address)) { if (devAISGen.InsertAISJumpClose(address) != retType.SUCCESS) { Console.WriteLine("WARNING: AIS Jump to {0} was not inserted.",symbolName); } } else { if (devAISGen.InsertAISJumpClose(symbolName) != retType.SUCCESS) { Console.WriteLine("WARNING: AIS Jump to {0} was not inserted.",symbolName); } } } } else if (sec.sectionName.Equals("AIS_Set", StringComparison.OrdinalIgnoreCase)) { UInt32 type = 0x00000000; UInt32 address = 0x00000000; UInt32 data = 0x00000000; UInt32 sleep = 0x00000000; foreach (DictionaryEntry de in sec.sectionValues) { if (sec.sectionValues["TYPE"].GetType() == typeof(String)) { if (((String)de.Key).Equals("TYPE", StringComparison.OrdinalIgnoreCase)) { if (! UInt32.TryParse((String)sec.sectionValues["TYPE"], out type)) { try { type = (UInt32)Enum.Parse(typeof(AisSetType),(String)sec.sectionValues["TYPE"]); } catch (ArgumentException e) { Console.WriteLine((String)sec.sectionValues["TYPE"] + " is not allowed specifier for SET type."); Console.WriteLine(e.Message); return retType.FAIL; } } } } else { type = (UInt32)sec.sectionValues["TYPE"]; } if (((String)de.Key).Equals("ADDRESS", StringComparison.OrdinalIgnoreCase)) { address = (UInt32)sec.sectionValues["ADDRESS"]; } if (((String)de.Key).Equals("DATA", StringComparison.OrdinalIgnoreCase)) { data = (UInt32)sec.sectionValues["DATA"]; } if (((String)de.Key).Equals("SLEEP", StringComparison.OrdinalIgnoreCase)) { sleep = (UInt32)sec.sectionValues["SLEEP"]; } } devAISGen.InsertAISSet(type, address, data, sleep); } else if (sec.sectionName.Equals("AIS_SectionFill", StringComparison.OrdinalIgnoreCase)) { UInt32 address = 0x00000000; UInt32 size = 0x00000000; UInt32 type = 0x00000000; UInt32 pattern = 0x00000000; foreach (DictionaryEntry de in sec.sectionValues) { if (((String)de.Key).Equals("ADDRESS", StringComparison.OrdinalIgnoreCase)) { address = (UInt32)sec.sectionValues["ADDRESS"]; } if (((String)de.Key).Equals("SIZE", StringComparison.OrdinalIgnoreCase)) { size = (UInt32)sec.sectionValues["SIZE"]; } if (((String)de.Key).Equals("TYPE", StringComparison.OrdinalIgnoreCase)) { type = (UInt32)sec.sectionValues["TYPE"]; } if (((String)de.Key).Equals("PATTERN", StringComparison.OrdinalIgnoreCase)) { pattern = (UInt32)sec.sectionValues["PATTERN"]; } } devAISGen.InsertAISSectionFill( address, size, type, pattern); } else if (sec.sectionName.Equals("AIS_FastBoot", StringComparison.OrdinalIgnoreCase)) { devAISGen.InsertAISFastBoot(); } else if (sec.sectionName.Equals("AIS_ReadWait", StringComparison.OrdinalIgnoreCase)) { UInt32 address = 0x00000000; UInt32 mask = 0xFFFFFFFF; UInt32 data = 0xFFFFFFFF; foreach (DictionaryEntry de in sec.sectionValues) { if (((String)de.Key).Equals("ADDRESS", StringComparison.OrdinalIgnoreCase)) { address = (UInt32)sec.sectionValues["ADDRESS"]; } if (((String)de.Key).Equals("MASK", StringComparison.OrdinalIgnoreCase)) { mask = (UInt32)sec.sectionValues["MASK"]; } if (((String)de.Key).Equals("DATA", StringComparison.OrdinalIgnoreCase)) { data = (UInt32)sec.sectionValues["DATA"]; } } devAISGen.InsertAISReadWait(address, mask, data); } else if (sec.sectionName.Equals("AIS_SeqReadEnable", StringComparison.OrdinalIgnoreCase)) { devAISGen.InsertAISSeqReadEnable(); } else if (sec.sectionName.Equals("AIS_FinalFunctionReg", StringComparison.OrdinalIgnoreCase)) { String finalFxnName = ""; foreach (DictionaryEntry de in sec.sectionValues) { if (((String)de.Key).Equals("FINALFXNSYMBOLNAME", StringComparison.OrdinalIgnoreCase)) { finalFxnName = (String)sec.sectionValues["FINALFXNSYMBOLNAME"]; } } if (devAISGen.InsertAISFinalFxnReg(finalFxnName) != retType.SUCCESS) { Console.WriteLine("WARNING: Final function register AIS command failed."); } } else if ( (sec.sectionName.Equals("GENERAL", StringComparison.OrdinalIgnoreCase)) || (sec.sectionName.Equals("SECURITY", StringComparison.OrdinalIgnoreCase)) ) { // Ignore General/Security section here since it should have already been processed } else { // Any other sections names should be ignored with warning Console.WriteLine("WARNING: Unrecognized INI section, {0}. Ignoring...", sec.sectionName ); } #endregion return retType.SUCCESS; }