Pasti file Reader-Writer  1.0
Pasti files
MainWindow.xaml.cs
Go to the documentation of this file.
1 
26 using Microsoft.Win32;
27 using System;
28 using System.Collections.Generic;
29 using System.IO;
30 using System.Linq;
31 using System.Text;
32 using System.Threading.Tasks;
33 using System.Windows;
34 using System.Windows.Controls;
35 using System.Windows.Data;
36 using System.Windows.Documents;
37 using System.Windows.Input;
38 using System.Windows.Media;
39 using System.Windows.Media.Imaging;
40 using System.Windows.Navigation;
41 using System.Windows.Shapes;
42 
43 namespace Pasti {
47  public partial class MainWindow : Window {
48  Floppy _fd;
49  BufferWindow _trackWindow = null;
50  BufferWindow _sectorWindow = null;
51  bool _trackWindowOpen = false;
52  bool _sectorWindowOpen = false;
53 
54  public MainWindow() {
55  InitializeComponent();
56  string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
57  string progName = "Pasti STX File Reader-Writer " + version;
58  Title = progName;
59 
60  }
61 
62  private void btFileClick(object sender, RoutedEventArgs e) {
63  OpenFileDialog ofd = new OpenFileDialog();
64  ofd.Filter = "Pasti file|*.stx|All Files|*.*";
65  bool? ok = ofd.ShowDialog();
66  if (ok == true) {
67  fileName.Text = ofd.FileName;
68  processFile(ofd.FileName);
69  }
70 
71  }
72 
73 
74 
75  private void processFile(string file) {
76  PastiReader pasti = new PastiReader(infoBox);
77  _fd = new Floppy();
78  pasti.readPasti(file, _fd);
79  }
80 
81 
82 
83  private void btWriteClick(object sender, RoutedEventArgs e) {
84 
85  if (_fd == null) {
86  MessageBox.Show("Nothing to write", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
87  return;
88  }
89 
90  SaveFileDialog sfd = new SaveFileDialog();
91  sfd.Filter = "Pasti file|*.stx|All Files|*.*";
92  // askOutput:
93 
94  bool? ok = sfd.ShowDialog();
95  if (ok == true) {
96  fileName.Text = sfd.FileName;
97  //if (File.Exists(sfd.FileName)) {
98  // MessageBoxResult result = MessageBox.Show("Do you want to overwrite?", "File already exist", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.No);
99  // if (result == MessageBoxResult.Cancel) return;
100  // if (result == MessageBoxResult.No) goto askOutput;
101  //}
102  PastiWriter pasti = new PastiWriter(infoBox);
103  pasti.writePasti(sfd.FileName, _fd);
104  }
105  }
106 
107  private void btTrackClick(object sender, RoutedEventArgs e) {
108  if (_fd == null) {
109  tbStatus.Text = "Nothing to display";
110  return;
111  }
112  int trackNumber;
113  int sideNumber;
114  tbStatus.Clear();
115  if (Int32.TryParse(tbTrack.Text, out trackNumber) == false)
116  tbStatus.Text = "Invalid track number - please correct and try again";
117  if (Int32.TryParse(tbSide.Text, out sideNumber) == false)
118  tbStatus.Text = "Invalid side number - please correct and try again";
119 
120  if ((trackNumber < 0) || (trackNumber > 84) || (sideNumber < 0) || (sideNumber > 1))
121  tbStatus.Text = "Track or Side out of range - please correct and try again";
122  if (_fd == null)
123  tbStatus.Text = "Nothing to display";
124 
125  if (_trackWindowOpen)
126  _trackWindow.Close();
127 
128  _trackWindow = new BufferWindow();
129  _trackWindow.displayTrackBuffer(_fd, trackNumber, sideNumber);
130  _trackWindow.Closed += new EventHandler(trackWindowClosed);
131  _trackWindowOpen = true;
132  _trackWindow.Show();
133  }
134 
135  void trackWindowClosed(object sender, EventArgs e) {
136  _trackWindowOpen = false;
137  }
138 
139 
140  private void btSectorsClick(object sender, RoutedEventArgs e) {
141  if (_fd == null) {
142  tbStatus.Text = "Nothing to display";
143  return;
144  }
145  int trackNumber;
146  int sideNumber;
147  tbStatus.Clear();
148  if (Int32.TryParse(tbTrack.Text, out trackNumber) == false)
149  tbStatus.Text = "Invalid track number - please correct and try again";
150  if (Int32.TryParse(tbSide.Text, out sideNumber) == false)
151  tbStatus.Text = "Invalid side number - please correct and try again";
152 
153  if ((trackNumber < 0) || (trackNumber > 84) || (sideNumber < 0) || (sideNumber > 1))
154  tbStatus.Text = "Track or Side out of range - please correct and try again";
155  if (_fd == null)
156  tbStatus.Text = "Nothing to display";
157 
158  if (_sectorWindowOpen)
159  _sectorWindow.Close();
160 
161  _sectorWindow = new BufferWindow();
162  _sectorWindow.displaySectorBuffer(_fd, trackNumber, sideNumber);
163  _sectorWindow.Closed += new EventHandler(sectorWindowClosed);
164  _sectorWindowOpen = true;
165  _sectorWindow.Show();
166  }
167 
168 
169  void sectorWindowClosed(object sender, EventArgs e) {
170  _sectorWindowOpen = false;
171  }
172 
173 
174  private void btAllSectorsClick(object sender, RoutedEventArgs e) {
175  tbStatus.Clear();
176  if (_fd == null) {
177  tbStatus.Text = "Nothing to display";
178  return;
179  }
180 
181  SaveFileDialog sfd = new SaveFileDialog();
182  sfd.Filter = "Text file|*.txt|All Files|*.*";
183  if (sfd.ShowDialog() == true) {
184  displayAllSectorBuffer(_fd, sfd.FileName);
185  }
186 
187 
188  }
189 
190  public void displayAllSectorBuffer(Floppy floppy, string filename) {
191  StringBuilder sb = new StringBuilder();
192 
193  for (int track = 0; track < 84; track++) {
194  for (int side = 0; side < 2; side++) {
195  Track t = floppy.tracks[track, side];
196  if (t == null) continue;
197 
198  sb.Append(String.Format("Track {0:D2}.{1} has {2} sectors\n", track, side, t.sectorCount));
199  for (int sect = 0; sect < t.sectorCount; sect++) {
200  sb.Append(String.Format("Sector {0}", t.sectors[sect].id.number));
201  sb.Append(String.Format(" T={0} H={1} N={2} S={3} CRC={4:X4}",
202  t.sectors[sect].id.track, t.sectors[sect].id.side, t.sectors[sect].id.number, t.sectors[sect].id.size, t.sectors[sect].id.crc));
203 
204  if (t.sectors[sect].sectorData != null) {
205  sb.Append(String.Format(" has {0} bytes\n", t.sectors[sect].sectorData.Count()));
206  sb.Append(String.Format(" bitPosition {0}, Flags {1:X2}", t.sectors[sect].bitPosition, t.sectors[sect].fdcFlags));
207  sb.Append(String.Format(" {0} {1}\n", ((t.sectors[sect].fdcFlags & SectorDesc.CRC_ERR) == 0) ?
208  "Good CRC" : "Bad CRC", (t.sectors[sect].fuzzyData != null) ? "Has Fuzzy bytes" : ""));
209  saveBuffer(t.sectors[sect].sectorData, sb);
210  if (t.sectors[sect].fuzzyData != null) {
211  sb.Append(String.Format("\nFuzzy bytes for sector {0}\n", t.sectors[sect].fuzzyData.Count()));
212  saveBuffer(t.sectors[sect].fuzzyData, sb);
213  }
214  if (t.sectors[sect].timmingData != null) {
215  sb.Append(String.Format("\nTiming values for sector {0}\n", t.sectors[sect].timmingData.Count()));
216  byte[] timming = new byte[t.sectors[sect].timmingData.Count()];
217  for (int i = 0; i < t.sectors[sect].timmingData.Count(); i++)
218  timming[i] = (byte)t.sectors[sect].timmingData[i];
219  saveBuffer(timming, sb);
220  }
221 
222  }
223  else
224  sb.Append(" *** Sector has no data ***\n");
225  sb.Append("\n");
226  }
227  }
228  }
229  using (StreamWriter outputfile = new StreamWriter(filename)) {
230  outputfile.Write(sb.ToString());
231  }
232  }
233 
234 
235  public void saveBuffer(byte[] buffer, StringBuilder sb) {
236  for (int i = 0; i < buffer.Count(); i += 16) {
237  sb.Append(String.Format("{0:D5} ", i));
238 
239  string ascii = " ";
240  for (int j = 0; j < 16; j++) {
241  if ((i + j) < buffer.Count()) {
242  sb.Append(String.Format("{0:X2} ", buffer[i + j]));
243  char ch = Convert.ToChar(buffer[i + j]);
244  ascii += Char.IsControl(ch) ? "." : ch.ToString();
245  }
246  else
247  sb.Append(String.Format(" "));
248  } // one more line
249  sb.Append(ascii);
250  sb.Append(String.Format("\n"));
251  } // all lines
252  }
253 
254 
255  private void fileDrop(object sender, DragEventArgs e) {
256  string[] droppedFiles = null;
257  if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
258  droppedFiles = e.Data.GetData(DataFormats.FileDrop, true) as string[];
259  }
260 
261  if ((null == droppedFiles) || (!droppedFiles.Any())) { return; }
262  processFile(droppedFiles[0]);
263  }
264 
265 
266  private void dragEnter(object sender, DragEventArgs e) {
267  e.Handled = true;
268  }
269 
270  }
271 }
The reader class
Definition: PastiRead.cs:36
Track[,] tracks
Array of Tracks
Definition: FloppyStruct.cs:90
Interaction logic for MainWindow.xaml
ushort crc
CRC Value from address field
Definition: PastiStruct.cs:151
byte track
Track number from address field
Definition: PastiStruct.cs:143
uint sectorCount
Number of sectors for this track
Definition: FloppyStruct.cs:58
ushort bitPosition
position in the track of the sector address field in bits
Definition: FloppyStruct.cs:42
The Pasti Writer Class
Definition: PastiWrite.cs:39
Definition: App.xaml.cs:9
byte size
Size value from address field
Definition: PastiStruct.cs:149
PastiStatus readPasti(string fileName, Floppy fd)
Read a Pasti file and fills the Floppy structure
Definition: PastiRead.cs:68
Contains information about one Track
Definition: FloppyStruct.cs:53
void displaySectorBuffer(Floppy floppy, int track, int side)
Draw the content of the buffer
Interaction logic for TrackBuffer.xaml
Contains information about a complete Floppy disk
Definition: FloppyStruct.cs:88
IDField id
Address field content
Definition: FloppyStruct.cs:46
const byte CRC_ERR
CRC Error (in Data if RNF=0 else in ID)
Definition: PastiStruct.cs:221
Pasti File Sector Descriptor
Definition: PastiStruct.cs:173
byte number
Sector number from address field
Definition: PastiStruct.cs:147
Sector[] sectors
Array of Sectors
Definition: FloppyStruct.cs:55
byte[] sectorData
buffer for the sector data
Definition: FloppyStruct.cs:36
void displayTrackBuffer(Floppy floppy, int track, int side)
Display the content of the Track Information in a textbox
ushort[] timmingData
buffer for timing bytes if necessary
Definition: FloppyStruct.cs:40
WriteStatus writePasti(string fileName, Floppy fd)
Read a Pasti file and fills the Floppy structure
Definition: PastiWrite.cs:71
byte side
Head number from address field
Definition: PastiStruct.cs:145
byte[] fuzzyData
buffer for fuzzy mask bytes if necessary
Definition: FloppyStruct.cs:38
byte fdcFlags
Status returned by the FDC
Definition: FloppyStruct.cs:48