Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GribFile" /> class. File read rights are shared between processes.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <exception cref="System.IO.IOException">Could not open file. See inner exception for more detail.</exception>
        /// <exception cref="System.IO.FileLoadException">The file is empty.</exception>
        public GribFile(string fileName)
        {
            FileInfo fi = new FileInfo(fileName);

            // need a better check
            if (fi.Length < 4)
            {
                throw new FileLoadException("This file is empty or invalid.");
            }

            _pFileHandleProxy = GribApiNative.CreateFileHandleProxy(fileName);

            if (_pFileHandleProxy == IntPtr.Zero)
            {
                throw new IOException("Could not open file. See inner exception for more detail.", new Win32Exception(Marshal.GetLastWin32Error()));
            }

            _fileHandleProxy = (FileHandleProxy)Marshal.PtrToStructure(_pFileHandleProxy, typeof(FileHandleProxy));

            FileName  = fileName;
            Reference = new HandleRef(this, _fileHandleProxy.File);
            Context   = GribApiProxy.GribContextGetDefault();

            // set the message count here; the result seems to be connected to the message iterator so
            // that after you begin iterating messages, the count decreases until it reaches 1.
            int count = 0;

            GribApiProxy.GribCountInFile(Context, this, out count);
            MessageCount = count;
        }