/// <summary> /// Adds a file to the PVFS image /// </summary> /// <param name="localName">Local file name to read</param> /// <param name="imageName">Name to use in image file</param> public override bool AddFile(String localName, String imageName) { // Skip if can't be opened if (!File.Exists(localName)) { log.Add("\r\nERROR: Could not read " + localName); return(false); } // Set up the new file record PVFSFileRecord newFile = new PVFSFileRecord(); newFile.FileName = imageName; // Read the data in, escaping as it is read byte b; List <byte> data = new List <byte>(1024); FileStream fs = new FileStream(localName, FileMode.Open, FileAccess.Read); BinaryReader fin = new BinaryReader(fs); for (int i = 0; i < fs.Length; i++) { if (data.Count == data.Capacity) { data.Capacity *= 2; } b = fin.ReadByte(); if (b == PVFS_DLE || b == PVFS_ETX) { data.Add(PVFS_DLE); } data.Add(b); } fin.Close(); newFile.data = data.ToArray(); // Add the file and return log.Add(" " + imageName + ": " + newFile.data.Length + " bytes"); files.Add(newFile); return(true); }
/// <summary> /// Parses and indexes a file for dynamic variables /// </summary> /// <param name="file">The PVFSFileRecord to parse</param> /// <returns>An PVFSFileRecord of indexes, or null if no variables were found</returns> public PVFSFileRecord Parse(PVFSFileRecord file) { //return null; byte[] idxData = new byte[0]; UInt32 dynVarCntr=0; MatchCollection matches = parser.Matches(ascii.GetString(file.data)); foreach(Match m in matches) { int i = GetIndex(m.Value.Replace("~", "")); // .Replace(" ", "") Array.Resize(ref idxData, idxData.Length + 8); /* idxData[idxData.Length - 8] = (byte)m.Index; idxData[idxData.Length - 7] = (byte)(m.Index >> 8); idxData[idxData.Length - 6] = (byte)(m.Index >> 16); idxData[idxData.Length - 5] = (byte)(m.Index >> 24); idxData[idxData.Length - 4] = (byte)i; idxData[idxData.Length - 3] = (byte)(i >> 8); idxData[idxData.Length - 2] = (byte)(i >> 16); idxData[idxData.Length - 1] = (byte)(i >> 24); Array.Resize(ref file.dynVarOffsetAndIndexID, file.dynVarOffsetAndIndexID.Length + 8); file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 1] = (byte)(i >> 24); file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 2] = (byte)(i >> 16); file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 3] = (byte)(i >> 8); file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 4] = ((byte)i); file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 5] = (byte)(m.Index >> 24); file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 6] = (byte)(m.Index >> 16); file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 7] = (byte)(m.Index >> 8); file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 8] = (byte)m.Index; */ file.dynVarCntr = ++dynVarCntr; // offSetCounter = offSetCounter + 8; } /* if(parseItrtn == (UInt32)0x0) { file.fileRecordOffset = (UInt32)0x0; offSetCounter = (UInt32)0x0; } else { file.fileRecordOffset=tempFileRcrdLen; } file.fileRecordLength = 4 // 4 bytes for file record length itself + 2; //To store the hasIndex/isZipped flag //+(UInt32)file.FileName.Length + file.dynVarCntr*8; // *4 tempFileRcrdLen += file.fileRecordLength; */ parseItrtn++; // Determine if any matches were made if (idxData.Length == 0) return null; else { // Set up new file record PVFSFileRecord idxFile = new PVFSFileRecord(); idxFile.FileName = ""; idxFile.fileDate = file.fileDate; idxFile.isIndex = true; idxFile.data = idxData; return idxFile; } }
/// <summary> /// Adds a file to the PVFS image /// </summary> /// <param name="localName">Local file name to read</param> /// <param name="imageName">Name to use in image file</param> public override bool AddFile(String localName, String imageName) { // Skip if can't be opened if (!File.Exists(localName)) { log.Add("\r\nERROR: Could not read " + localName); return(false); } // Set up the new file record PVFSFileRecord newFile = new PVFSFileRecord(); newFile.FileName = imageName; newFile.fileDate = File.GetLastWriteTime(localName); // Read the data in FileStream fs = new FileStream(localName, FileMode.Open, FileAccess.Read); BinaryReader fin = new BinaryReader(fs); newFile.data = fin.ReadBytes((int)fs.Length); fin.Close(); // Parse the file if necessary PVFSFileRecord idxFile = null; if (this.FileMatches(localName, this.dynamicTypes)) { idxFile = dynVarParser.Parse(newFile); } // GZip the file if possible int gzipRatio = 0; if (idxFile == null && !this.FileMatches(localName, this.nonGZipTypes)) { MemoryStream ms = new MemoryStream(); GZipStream gz = new GZipStream(ms, CompressionMode.Compress, true); gz.Write(newFile.data, 0, newFile.data.Length); gz.Flush(); gz.Close(); // Only use zipped copy if it's smaller if (ms.Length < newFile.data.Length) { gzipRatio = (int)(100 - (100 * ms.Length / newFile.data.Length)); newFile.data = ms.ToArray(); newFile.isZipped = true; } } // Add the file and return if (idxFile == null) { log.Add(" " + imageName + ": " + newFile.data.Length + " bytes" + ((gzipRatio > 0) ? " (gzipped by " + gzipRatio + "%)" : "")); files.Add(newFile); } else { log.Add(" " + imageName + ": " + newFile.data.Length + " bytes, " + (idxFile.data.Length / 8) + " vars"); newFile.hasIndex = true; files.Add(newFile); // files.Add(idxFile); } return(true); }
/// <summary> /// Adds a file to the PVFS image /// </summary> /// <param name="localName">Local file name to read</param> /// <param name="imageName">Name to use in image file</param> public override bool AddFile(String localName, String imageName) { // Skip if can't be opened if (!File.Exists(localName)) { log.Add("\r\nERROR: Could not read " + localName); return false; } // Set up the new file record PVFSFileRecord newFile = new PVFSFileRecord(); newFile.FileName = imageName; // Read the data in, escaping as it is read byte b; List<byte> data = new List<byte>(1024); FileStream fs = new FileStream(localName, FileMode.Open, FileAccess.Read); BinaryReader fin = new BinaryReader(fs); for (int i = 0; i < fs.Length; i++) { if(data.Count == data.Capacity) data.Capacity *= 2; b = fin.ReadByte(); if (b == PVFS_DLE || b == PVFS_ETX) data.Add(PVFS_DLE); data.Add(b); } fin.Close(); newFile.data = data.ToArray(); // Add the file and return log.Add(" " + imageName + ": " + newFile.data.Length + " bytes"); files.Add(newFile); return true; }
/// <summary> /// Adds a file to the PVFS image /// </summary> /// <param name="localName">Local file name to read</param> /// <param name="imageName">Name to use in image file</param> public override bool AddFile(String localName, String imageName) { // Skip if can't be opened if (!File.Exists(localName)) { log.Add("\r\nERROR: Could not read " + localName); return false; } // Set up the new file record PVFSFileRecord newFile = new PVFSFileRecord(); newFile.FileName = imageName; newFile.fileDate = File.GetLastWriteTime(localName); // Read the data in FileStream fs = new FileStream(localName, FileMode.Open, FileAccess.Read); BinaryReader fin = new BinaryReader(fs); newFile.data = fin.ReadBytes((int)fs.Length); fin.Close(); // Parse the file if necessary PVFSFileRecord idxFile = null; if (this.FileMatches(localName, this.dynamicTypes)) { idxFile = dynVarParser.Parse(newFile); } // GZip the file if possible int gzipRatio = 0; if (idxFile == null && !this.FileMatches(localName, this.nonGZipTypes)) { MemoryStream ms = new MemoryStream(); GZipStream gz = new GZipStream(ms, CompressionMode.Compress, true); gz.Write(newFile.data, 0, newFile.data.Length); gz.Flush(); gz.Close(); // Only use zipped copy if it's smaller if (ms.Length < newFile.data.Length) { gzipRatio = (int)(100 - (100 * ms.Length / newFile.data.Length)); newFile.data = ms.ToArray(); newFile.isZipped = true; } } // Add the file and return if (idxFile == null) { log.Add(" " + imageName + ": " + newFile.data.Length + " bytes" + ((gzipRatio > 0) ? " (gzipped by " + gzipRatio + "%)" : "")); files.Add(newFile); } else { log.Add(" " + imageName + ": " + newFile.data.Length + " bytes, " + (idxFile.data.Length / 8) + " vars"); newFile.hasIndex = true; files.Add(newFile); // files.Add(idxFile); } return true; }
/// <summary> /// Parses and indexes a file for dynamic variables /// </summary> /// <param name="file">The PVFSFileRecord to parse</param> /// <returns>An PVFSFileRecord of indexes, or null if no variables were found</returns> public PVFSFileRecord Parse(PVFSFileRecord file) { //return null; byte[] idxData = new byte[0]; UInt32 dynVarCntr = 0; MatchCollection matches = parser.Matches(ascii.GetString(file.data)); foreach (Match m in matches) { int i = GetIndex(m.Value.Replace("~", "")); // .Replace(" ", "") Array.Resize(ref idxData, idxData.Length + 8); /* idxData[idxData.Length - 8] = (byte)m.Index; * idxData[idxData.Length - 7] = (byte)(m.Index >> 8); * idxData[idxData.Length - 6] = (byte)(m.Index >> 16); * idxData[idxData.Length - 5] = (byte)(m.Index >> 24); * idxData[idxData.Length - 4] = (byte)i; * idxData[idxData.Length - 3] = (byte)(i >> 8); * idxData[idxData.Length - 2] = (byte)(i >> 16); * idxData[idxData.Length - 1] = (byte)(i >> 24); * * * Array.Resize(ref file.dynVarOffsetAndIndexID, file.dynVarOffsetAndIndexID.Length + 8); * * file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 1] = (byte)(i >> 24); * file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 2] = (byte)(i >> 16); * file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 3] = (byte)(i >> 8); * file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 4] = ((byte)i); * * * file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 5] = (byte)(m.Index >> 24); * file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 6] = (byte)(m.Index >> 16); * file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 7] = (byte)(m.Index >> 8); * file.dynVarOffsetAndIndexID[file.dynVarOffsetAndIndexID.Length - 8] = (byte)m.Index; */ file.dynVarCntr = ++dynVarCntr; // offSetCounter = offSetCounter + 8; } /* * if(parseItrtn == (UInt32)0x0) * { * file.fileRecordOffset = (UInt32)0x0; * offSetCounter = (UInt32)0x0; * } * else * { * file.fileRecordOffset=tempFileRcrdLen; * } * * file.fileRecordLength = 4 // 4 bytes for file record length itself + 2; //To store the hasIndex/isZipped flag + //+(UInt32)file.FileName.Length + file.dynVarCntr*8; // *4 + + tempFileRcrdLen += file.fileRecordLength; */ parseItrtn++; // Determine if any matches were made if (idxData.Length == 0) { return(null); } else { // Set up new file record PVFSFileRecord idxFile = new PVFSFileRecord(); idxFile.FileName = ""; idxFile.fileDate = file.fileDate; idxFile.isIndex = true; idxFile.data = idxData; return(idxFile); } }