private FileAccess GetFileStreamAccess(AlgFileAccess access) { if (access == AlgFileAccess.Read) { return(System.IO.FileAccess.Read); } else { return(System.IO.FileAccess.ReadWrite); } }
/// <summary> /// Creates an algorithm file /// </summary> /// <param name="path">The file location of the file</param> /// <param name="fileMode">Creating a new file or Opening an exising file</param> /// <param name="fileAccess">Read or ReadWrite</param> /// <param name="numAlgs">The number of algorithms in the file. Required if creating a new file, ignored if opening a file</param> public AlgFile(string path, AlgFileMode fileMode, AlgFileAccess fileAccess, int numAlgs = -1) { FileAccess = fileAccess; if (string.IsNullOrEmpty(path)) { throw new ArgumentException("path is not a valid string"); } if (!Path.GetExtension(path).Equals(".alg")) { throw new ArgumentException("path is not the correct type of file"); } if (fileMode == AlgFileMode.Create && FileAccess == AlgFileAccess.Read) { throw new ArgumentException("Cannot create file with only read access"); } if (fileMode == AlgFileMode.Create && numAlgs <= 0) { throw new ArgumentException("number of algs is not positive"); } _stream = new FileStream(path, GetFileStreamMode(fileMode), System.IO.FileAccess.ReadWrite); if (fileMode == AlgFileMode.Create) { NumAlgs = numAlgs; _stream.SetLength((numAlgs + 1) * 32); _stream.Position = 0; byte[] header = Encoding.Unicode.GetBytes(headerString); _stream.Write(header, 0, 28); _stream.Write(BitConverter.GetBytes(numAlgs), 0, 4); } else { _stream.Position = 0; byte[] header = new byte[32]; _stream.Read(header, 0, 32); for (int k = 0; k < 14; k++) { char c = BitConverter.ToChar(header, 2 * k); if (c != headerString[k]) { throw new IOException("The file is not in the correct format"); } } NumAlgs = BitConverter.ToInt32(header, 28); } _isClosed = false; }