// Get command lines for module from Library file that corresponds to given dictionary entry. // The dictionary object holds the full name and path to the file. private libStatus GetModuleFromLibrary(DcDictionary dictionary, DictionaryEntry entry, ref String moduleData) { String str; if (!File.Exists(dictionary.fileName)) { str = String.Format("{0}\r\nFile may have been moved or deleted" + "since dictionaries were loaded.", libraryFileName); str += " Dictionaries will be automatically reloaded from specified library folder location."; MessageBox.Show(str, "Library File Not Found"); return(libStatus.LibraryNotFound); } FileStream inFile = new FileStream(dictionary.fileName, FileMode.Open, FileAccess.Read); inFile.Position = entry.recordOffset; String line = ""; moduleData = ""; //bool first = true; while (inFile.Position < entry.recordOffset + entry.elementSize) { line = ReadASCIILine(inFile, inFile.Position); if (line == ((char)0x1A).ToString()) { break; } moduleData += line + "\r\n"; } return(libStatus.ModuleLoaded); }
private int GetDictionary(FileStream inFile, DcDictionary dictionary) { Byte[] buffer = new Byte[100]; String str = ""; int entryCount = 0; int recordStartOffset = 0; int recordSizeInBytes = 0; dictionary.entries.Clear(); inFile.Read(buffer, 0, 32); // Assumes that two-byte binary values in the dictionary portion of the library are 16-bit and little endian. // All records in the library start and end on 128-byte boundaries, including the dictionary itself. // buffer[14] and [15] are the file offset (in multiples of 128 bytes) to the NEXT record in the library. // The first occurrence of the offset thus corresponds to the amount of space taken by the dictionary itself. int recordsStart = 128 * Convert.ToInt16(buffer[14] + buffer[15] * 256); for (int i = 32; i < recordsStart; i += 32) { DictionaryEntry entry = new DictionaryEntry(); long filePos = inFile.Position; inFile.Read(buffer, 0, 32); if (buffer[0] == 0xFF) { break; // no more dictionary entries } str = Encoding.ASCII.GetString(buffer, 1, 11); recordStartOffset = 128 * Convert.ToInt16(buffer[12] + buffer[13] * 256); recordSizeInBytes = 128 * Convert.ToInt16(buffer[14] + buffer[15] * 256); entry.name = str; entry.extension = str.Substring(8, 3).TrimEnd(' '); // extension is last 3 alphanumeric characters, if any. entry.recordOffset = recordStartOffset; entry.elementSize = recordSizeInBytes; entry.filePos = filePos; dictionary.entries.Add(entry); entryCount++; } dictionary.fileName = inFile.Name; dictionary.size = recordsStart; dictionary.entryCount = entryCount; return(entryCount); }
private Module FindModuleInLibraries(string moduleName, MatchType matchType) { int index = -1; string libFileName = ""; string moduleData = ""; DictionaryEntry entry = new DictionaryEntry(); Module module = new Module(); foreach (string libraryFileName in libraryFiles) { if (File.Exists(libraryFileName)) { libFileName = libraryFileName; FileStream inFile = new FileStream(libFileName, FileMode.Open, FileAccess.Read); DcDictionary dictionary = new DcDictionary(); GetDictionary(inFile, dictionary); index = FindModuleInDictionary(dictionary, moduleName, 0, matchType); inFile.Close(); if (index >= 0) { entry = dictionary.entries[index]; libStatus status = GetModuleFromLibrary(dictionary, entry, ref moduleData); break; } } } if (index >= 0) { Console.WriteLine("Found. Index = " + index + " in library file " + libFileName); module.processed = false; module.found = true; module.inLibrary = true; module.fName = libFileName; module.index = index; module.name = entry.name; module.data = moduleData; } return(module); }
// search dictionary entries for name starting from startIndex and return // index of entry if match found. Return -1 if match not found. // Using startIndex supports finding multiple matches. Setting startIndex to 0 will // return index of first match. private int FindModuleInDictionary(DcDictionary dictionary, String moduleName, int startIndex, MatchType matchType) { int index; bool found = false; for (index = startIndex; index < dictionary.entries.Count; index++) { if (matchType == MatchType.full && dictionary.entries[index].name == moduleName) { found = true; break; } else if (matchType == MatchType.partial && dictionary.entries[index].name.Contains(moduleName)) { found = true; break; } } if (!found) { return(-1); } return(index); }