/// <summary> /// Populates properties on the current object reading information from the /// provided binary reader. /// </summary> /// <param name="binaryReader">The binary reader to read</param> public void Read(BinaryReader binaryReader) { SectorNumber = binaryReader.ReadInt32(); // Read the full sized field from the file and tidy it up SectorName = BinaryFileHelper.CharArrayToString(binaryReader.ReadChars(SectorNameFieldSize)); NotesOffset = binaryReader.ReadInt32(); NotesLength = binaryReader.ReadInt32(); }
/// <summary> /// Writes property values to a binary file using the binary writer /// </summary> /// <param name="binaryWriter">The instantiated binary writer used to write property information</param> /// <exception cref="SectorDescriptionWriteException"><c>SectorDescriptionWriteException</c>.</exception> public void Write(BinaryWriter binaryWriter) { try { binaryWriter.Write(SectorNumber); // Write the contents of the string containing the Sector Name and any trailing zeros binaryWriter.Write(BinaryFileHelper.StringToCharArray(SectorName, SectorNameFieldSize)); binaryWriter.Write(NotesOffset); binaryWriter.Write(NotesLength); } catch (Exception eek) { Console.WriteLine("Error encountered writing sector description: {0} {1} = \"{2}\"", SectorNumber, SectorName, eek.ToString()); } }
/// <summary> /// Write the supplied list of string to the supplied Binary Writer with each item separated by commas /// and, if supplied, surrounded by a field delimiter e.g. a double quote (") /// </summary> /// <param name="binaryWriter">The Binary Writer to be written</param> /// <param name="stringList">The list of strings to be written</param> /// <param name="delimiter">The delimiter with which to surround each data item string</param> /// <param name="dataDelimiter">The character that should delimit the data items within the record</param> /// <returns>Whether (true) or not (false) the write succeeded</returns> public static bool WriteCommaSeparatedRecord(BinaryWriter binaryWriter, List <string> stringList, char itemDelimiter, char dataDelimiter) { bool success = true; for (int index = 0; index < stringList.Count; ++index) { string itemValue = stringList[index]; string itemValueWrite = itemDelimiter + itemValue + itemDelimiter; // Only add a comma at the beginning of items that need it if (index > 0) { // Precede the data with a comma itemValueWrite = dataDelimiter + itemValueWrite; } binaryWriter.Write(BinaryFileHelper.StringToCharArray(itemValueWrite)); } return(success); }
/// <summary> /// Writes property values to a binary file using the binary writer /// </summary> /// <param name="binaryWriter">The instantiated binary writer used to write property information</param> /// <exception cref="SectorInformationWriteException"><c>SectorInformationWriteException</c>.</exception> public void Write(BinaryWriter binaryWriter) { try { SectorDescription.Write(binaryWriter); long currentPosition = binaryWriter.BaseStream.Position; binaryWriter.BaseStream.Position = SectorDescription.NotesOffset; binaryWriter.Write(BinaryFileHelper.StringToCharArray(Notes, SectorDescription.NotesLength)); // Position back to after the end of the SectorDescriptor binaryWriter.BaseStream.Position = currentPosition; } catch (Exception eek) { var message = String.Format("Error encountered writing sector information: {0} {1} = {2}", Number, Name, eek.ToString()); } }
/// <summary> /// Write the specified items from the supplied list to the supplied Binary Writer /// </summary> /// <param name="binaryWriter">The Binary Writer to be written</param> /// <param name="stringList">The list of strings to be written</param> /// <param name="firstItemIndex">The index of the first item within the List to be written</param> /// <param name="itemCount">The count of items from the List to be written</param> /// <param name="dataDelimiter">The character that should delimit the data items within the record</param> /// <returns></returns> public static bool WriteDelimitedDataRecord(BinaryWriter binaryWriter, List <string> stringList, int firstItemIndex, int itemCount, char dataDelimiter) { bool success = true; for (int listItemCount = 0, index = firstItemIndex; listItemCount < itemCount; ++listItemCount, ++index) { string itemValue = stringList[index]; string itemValueWrite = itemValue; // Only add a delimiter at the beginning of items that need it if (index > firstItemIndex) { // Precede the data with a delimiter itemValueWrite = dataDelimiter + itemValueWrite; } if (!String.IsNullOrEmpty(itemValueWrite)) { binaryWriter.Write(BinaryFileHelper.StringToCharArray(itemValueWrite)); } } return(success); }
} // ConsumeWhiteSpace /// <summary> /// Skips over all whitespace and comments starting at the current location of the Binary Reader /// </summary> /// <param name="binaryReader">The Binary Reader in which all whitespace is to be "skipped over" starting at the current position</param> /// <param name="whitespaceChars">A string containing all characters that may be considered to be whitespace</param> /// <param name="commentDeclarator">The character that may be considered to preced a comment</param> /// <returns>false if no more data is available from the Binary Reader or true otherwise</returns> public static bool ConsumeWhiteSpaceAndComments(BinaryReader binaryReader, string whitespaceChars, char commentDeclarator) { bool success = true; try { do { BinaryFileHelper.ConsumeWhiteSpace(binaryReader, whitespaceChars); if (binaryReader.PeekChar() == commentDeclarator) { BinaryFileHelper.ReadTextToEndOfLine(binaryReader); } } while ((binaryReader.PeekChar() == ' ') || (binaryReader.PeekChar() == commentDeclarator)); success = !IsWhiteSpace(whitespaceChars, (char)binaryReader.PeekChar()); } catch (Exception eek) { success = false; } return(success); } // ConsumeWhiteSpaceAndComments