public static unsafe void CropBinary(XElement signalentry, double time_start, double time_end) { StreamDataType datatype = SignalEntry.GetBinDataType(signalentry); int channels = SignalEntry.GetNumChannels(signalentry); int samplestructsize = channels * (int)SignalEntry.GetDataTypeBytes(datatype); double samplespersec = SignalEntry.GetSampleRate(signalentry); MapFile mapfile = new MapFile(SignalEntry.GetId(signalentry), false); // Hier Dateigröße und Anzahl der Samples nochmal ausrechnen. // Kann ja sein, dass die Datei am Ende ein bissel kaputt ist. // (Datei wird dann auf ganze Sampleanzahl abgeschnitten) Int64 filesamples = mapfile.filesize / samplestructsize; Int64 filesize = filesamples * samplestructsize; Int64 sample_start = (Int64)(time_start * samplespersec); Int64 sample_end = (Int64)(time_end * samplespersec); if (sample_start >= filesamples) { return; } if (sample_end > filesamples) { sample_end = filesamples; } Int64 srcoffs = sample_start * samplestructsize; Int64 size = (Int64)((sample_end - sample_start) * samplestructsize); // Sampledaten nach vorne kopieren if (sample_start > 0) { mapfile.MemMove(0, (int)srcoffs, (int)size); // XXX memmove gibts gibts nur als 32bit version?? } mapfile.Dispose(); mapfile = null; // Datei hinten abschneiden (truncate) if (size < filesize) { FileStream fs = new FileStream(SignalEntry.GetId(signalentry), FileMode.Open, FileAccess.Write); fs.SetLength(size); fs.Close(); fs.Dispose(); fs = null; } }
private static void OpenMap(string filename) { //Check for quotation marks string safeFileName = filename; if ((safeFileName.StartsWith("\"") || safeFileName.StartsWith("\'")) && (safeFileName.EndsWith("\"") || safeFileName.EndsWith("\'"))) { safeFileName = filename.Substring(1, filename.Length - 2); } else if (safeFileName.StartsWith("file:\\") && filename.Length >= 7) { safeFileName = filename.Substring(7); } //Check if (!File.Exists(safeFileName)) { Console.WriteLine("Unable to find file."); return; } //Open try { //Dispose if (selectedMap != null) { selectedMap.Dispose(); } selectedMap = new MapFile(); //Load selectedMap.Load(safeFileName); //Write Console.WriteLine($"{selectedMap.Name} is now the selected map."); } catch (Exception ex) { Console.WriteLine($"Error opening {filename}:"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }
private void mapToolStripMenuItem_Click(object sender, EventArgs e) { //Check if (sender is ToolStripItem mapItem) { //Open m_MapFile = new MapFile(); m_MapFile.Load(mapItem.Name); //Load listBox1.Items.Clear(); foreach (IndexEntry entry in m_MapFile.IndexEntries) { listBox1.Items.Add($"{entry.Id} {entry.Filename}.{entry.Root}"); } //Set tagCountToolStripTextBox.Text = $"{m_MapFile.IndexEntries.Count} tags"; //Dispose m_MapFile.Dispose(); } }
/// <summary> /// Crops the binary. /// </summary> /// <param name="signalentry">The signalentry.</param> /// <param name="time_start">The time_start.</param> /// <param name="time_end">The time_end.</param> private static unsafe void CropBinary(XElement signalentry, double time_start, double time_end) { String fileName = SignalEntry.GetId(signalentry); StreamDataType datatype = SignalEntry.GetBinDataType(signalentry); int channels = SignalEntry.GetNumChannels(signalentry); int samplestructsize = channels * (int)SignalEntry.GetDataTypeBytes(datatype); double samplespersec = SignalEntry.GetSampleRate(signalentry); FileInfo fileInfo = new FileInfo(fileName); // Hier Dateigröße und Anzahl der Samples nochmal ausrechnen. // Kann ja sein, dass die Datei am Ende ein bissel kaputt ist. // (Datei wird dann auf ganze Sampleanzahl abgeschnitten) long filesamples = fileInfo.Length / samplestructsize; long filesize = filesamples * samplestructsize; long sample_start = (long)(time_start * samplespersec); long sample_end = (long)(time_end * samplespersec); if (sample_start >= filesamples) { return; } if (sample_end > filesamples) { sample_end = filesamples; } long size = (long)((sample_end - sample_start) * samplestructsize); // Datei vorne abschneiden if (sample_start > 0) { long srcoffs = sample_start * samplestructsize; try //Try it first with fast memory mapping { MapFile mapfile = new MapFile(fileName, false); mapfile.MemMove(0, (int)srcoffs, (int)size); // XXX memmove gibts gibts nur als 32bit version?? mapfile.Dispose(); mapfile = null; } catch (Exception) //If file is too large for memory mapping do it manually { FileStream inFile = new FileStream(fileName, FileMode.Open, FileAccess.Read); FileStream outFile = new FileStream(fileName + ".tmp", FileMode.OpenOrCreate, FileAccess.Write); int blocksize = 10240; inFile.Seek(srcoffs, SeekOrigin.Begin); while (true) { byte[] buffer = new byte[blocksize]; int data = inFile.Read(buffer, 0, blocksize); if (data <= 0) { break; // } outFile.Write(buffer, 0, data); if (data < blocksize) { break; } } inFile.Close(); outFile.Close(); File.Delete(fileName); File.Move(fileName + ".tmp", fileName); } } // Datei hinten abschneiden (truncate) if (size < filesize) { FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Write); fs.SetLength(size); fs.Close(); fs.Dispose(); fs = null; } }