Example #1
0
        /// <summary>
        /// Opens a file
        /// </summary>
        /// <param name="mode">File open mode</param>
        public void Open(FileOpenMode mode)
        {
            if (IsOpen)
            {
                throw new IOException($"File {FilePath} is already opened");
            }

            if (!File.Exists(FilePath))
            {
                throw new FileNotFoundException("Cannot open the file", FilePath);
            }

            var microfocusResult = VisionLibrary.V6_open(FilePath, (int)mode);

            // Open sucessful
            if (microfocusResult.StatusCode.IsOkStatus() &&
                microfocusResult.Result != IntPtr.Zero)
            {
                FilePointer     = microfocusResult.Result;
                CurrentOpenMode = mode;
                return;
            }

            // Error
            throw new VisionFileException((int)microfocusResult.StatusCode, $"Error {(int)microfocusResult.StatusCode} opening file {FilePath}");
        }
Example #2
0
        /// <summary>
        /// Read a record without lock
        /// </summary>
        /// <param name="keyValue">Value of the key</param>
        /// <param name="keyIndex">Index of the key</param>
        /// <returns>readed record, null if not found</returns>
        public IVisionRecord Read(IVisionRecord keyValue, int keyIndex = 0)
        {
            if (!IsOpen)
            {
                throw new IOException($"File not opened. File name: {FilePath}");
            }
            EnsureKeyIndexIsValid(keyIndex);

            var content = new byte[FileDefinition.MaxRecordSize];

            Array.Copy(keyValue.GetRawContent(), content, keyValue.GetRawContent().Length);

            var microfocusResult = VisionLibrary.V6_read(FilePointer, content, keyIndex, withLock: false);

            // OK
            if (microfocusResult.StatusCode.IsOkStatus())
            {
                var result = new MicrofocusVisionRecord(FileDefinition, DataConverter);
                result.SetRawContent(content);
                return(result);
            }

            // NOT FOUND
            if (microfocusResult.StatusCode == MicrofocusFileStatusCodes.NotFound)
            {
                return(null);
            }

            // Error
            throw new VisionFileException((int)microfocusResult.StatusCode, $"Error {(int)microfocusResult.StatusCode} on read on file {FilePath}");
        }
Example #3
0
        /// <summary>
        /// Read the previous record with lock
        /// </summary>
        /// <returns>Previous record or null if no more records</returns>
        public IVisionRecord ReadPreviousLock()
        {
            if (!IsOpen)
            {
                throw new IOException($"File not opened. File name: {FilePath}");
            }
            if (CurrentOpenMode == FileOpenMode.Input)
            {
                throw new IOException($"File {FilePath} cannot be locked since it was open in read-only mode");
            }

            var content = new byte[FileDefinition.MaxRecordSize];


            var microfocusResult = VisionLibrary.V6_previous(FilePointer, content, withLock: true);

            // OK
            if (microfocusResult.StatusCode.IsOkStatus())
            {
                var result = new MicrofocusVisionRecord(FileDefinition, DataConverter);
                result.SetRawContent(content);
                return(result);
            }

            // NOT FOUND
            if (microfocusResult.StatusCode == MicrofocusFileStatusCodes.NotFound)
            {
                return(null);
            }

            // Error
            throw new VisionFileException((int)microfocusResult.StatusCode, $"Error {(int)microfocusResult.StatusCode} on read previous lock on file {FilePath}");
        }
Example #4
0
        /// <summary>
        /// Read the next record without lock
        /// </summary>
        /// <returns>Next record or null if no more records</returns>
        public IVisionRecord ReadNext()
        {
            if (!IsOpen)
            {
                throw new IOException($"File not opened. File name: {FilePath}");
            }

            var content = new byte[FileDefinition.MaxRecordSize];

            var microfocusResult = VisionLibrary.V6_next(FilePointer, content, withLock: false);

            // OK
            if (microfocusResult.StatusCode.IsOkStatus())
            {
                var result = new MicrofocusVisionRecord(FileDefinition, DataConverter);
                result.SetRawContent(content);
                return(result);
            }

            // NOT FOUND
            if (microfocusResult.StatusCode == MicrofocusFileStatusCodes.NotFound)
            {
                return(null);
            }

            // Error
            throw new VisionFileException((int)microfocusResult.StatusCode, $"Error {(int)microfocusResult.StatusCode} on read next on file {FilePath}");
        }
Example #5
0
 /// <summary>
 /// Unlock the last locked record
 /// </summary>
 public void Unlock()
 {
     if (!IsOpen)
     {
         throw new IOException($"File not opened. File name: {FilePath}");
     }
     VisionLibrary.V6_unlock(FilePointer);
 }
Example #6
0
 /// <summary>
 /// Closes the file
 /// </summary>
 public void Close()
 {
     if (IsOpen)
     {
         var microfocusResult = VisionLibrary.V6_close(FilePointer);
         if (!microfocusResult.StatusCode.IsOkStatus())
         {
             throw new VisionFileException((int)microfocusResult.StatusCode, $"Error {(int)microfocusResult.StatusCode} closing file {FilePath}");
         }
         CurrentOpenMode = null;
         FilePointer     = IntPtr.Zero;
     }
 }
Example #7
0
        /// <summary>
        /// Delete e record
        /// </summary>
        /// <param name="record">Record to delete</param>
        public void Delete(IVisionRecord record)
        {
            if (!IsOpen)
            {
                throw new IOException($"File not opened. File name: {FilePath}");
            }
            if (CurrentOpenMode == FileOpenMode.Input)
            {
                throw new IOException($"File {FilePath} cannot be deleted since it was open in read-only mode");
            }

            var microfocusResult = VisionLibrary.V6_delete(FilePointer, record.GetRawContent());

            if (!microfocusResult.StatusCode.IsOkStatus())
            {
                throw new VisionFileException((int)microfocusResult.StatusCode, $"Error {(int)microfocusResult.StatusCode} on delete on file {FilePath}");
            }
        }
Example #8
0
        /// <summary>
        /// Set a pointer for the start
        /// </summary>
        /// <param name="keyIndex">Index of the key to use</param>
        /// <param name="record">Record to use for start (null = start from the beginning)</param>
        /// <param name="mode">Vision start mode</param>
        /// <returns>True if the starts is ok, otherwise false</returns>
        public bool Start(int keyIndex = 0, IVisionRecord record = null, FileStartMode mode = FileStartMode.GreaterOrEqual)
        {
            if (!IsOpen)
            {
                throw new IOException($"File not opened. File name: {FilePath}");
            }
            EnsureKeyIndexIsValid(keyIndex);

            var recordToUse = record ?? new MicrofocusVisionRecord(FileDefinition, DataConverter);

            var microfocusResult = VisionLibrary.V6_start(FilePointer, recordToUse.GetRawContent(), keyIndex, 0, (int)mode);

            if (microfocusResult.StatusCode.IsOkStatus())
            {
                return(true);
            }
            if (microfocusResult.StatusCode == MicrofocusFileStatusCodes.NotFound)
            {
                return(false);
            }

            throw new VisionFileException((int)microfocusResult.StatusCode, $"Error {(int)microfocusResult.StatusCode} on start on file {FilePath}");
        }