Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JComLib.WriteManager"/> class for the
 /// given IO device, record index and format string.
 /// </summary>
 /// <param name="iodevice">The device to read from</param>
 /// <param name="record">The index of the record to write (for direct access)</param>
 /// <param name="formatString">Format string</param>
 public WriteManager(int iodevice, int record, string formatString)
 {
     _file = IOFile.Get(iodevice);
     if (_file == null) {
         _file = new IOFile(iodevice);
         _file.Open();
     }
     if (record >= 1) {
         _file.RecordIndex = record;
     }
     _file.IsFormatted = !string.IsNullOrEmpty(formatString);
     _format = new FormatManager(formatString);
 }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JComLib.ReadManager"/> class for the
 /// given IO device, record index and format string.
 /// </summary>
 /// <param name="iodevice">The device to read from</param>
 /// <param name="record">The index of the record to read (for direct access)</param>
 /// <param name="formatString">Format string</param>
 public ReadManager(int iodevice, int record, string formatString)
 {
     _file = IOFile.Get(iodevice);
     if (_file == null) {
         _file = new IOFile(iodevice);
         _file.Open();
     }
     if (record >= 1) {
         _file.RecordIndex = record;
     }
     if (formatString == "*") {
         formatString = string.Empty;
         _file.IsFormatted = true;
     } else {
         _file.IsFormatted = !string.IsNullOrEmpty(formatString);
     }
     _format = new FormatManager(formatString);
     _format.BlanksAsZero = (_file.Blank == 'Z');
     _readIndex = -1;
 }
Esempio n. 3
0
        /// <summary>
        /// FORTRAN file open.
        /// </summary>
        /// <param name="iodevice">The device number</param>
        /// <param name="iostat">A reference variable that will be set to the I/O status</param>
        /// <param name="filename">An optional file name</param>
        /// <param name="status">Specifies how the file is to be opened: OLD, NEW, UNKNOWN or SCRATCH</param>
        /// <param name="access">Specifies the access mode: DIRECT or SEQUENTIAL</param>
        /// <param name="form">Specifies the file formatting: FORMATTED or UNFORMATTED</param>
        /// <param name="recl">For formatted files, specifies the record size in units</param>
        /// <param name="blank">Specifies how blanks are treated in input: NULL or ZERO</param>
        /// <returns>A zero value if the operation succeeds, or -1 if the operation fails</returns>
        public static int OPEN(int iodevice, ref int iostat, string filename, string status, string access, string form, int recl, string blank)
        {
            char blankChar;

            // Default IOStat is no error
            iostat = 0;

            // Parse BLANK specifier.
            if (blank == null) {
                blankChar = ' ';
            } else {
                string fixedBlank = blank.ToUpper().Trim();
                if (fixedBlank == "NULL") {
                    blankChar = ' ';
                } else if (fixedBlank == "ZERO") {
                    blankChar = '0';
                } else {
                    iostat = IOError.IllegalBlank;
                    return -1;
                }
            }

            IOFile iofile = IOFile.Get(iodevice);
            if (iofile != null) {

                // If the FILE= specifier is not included in the OPEN statement, the file to be connected to the
                // unit is the same as the file to which the unit is connected.
                if (filename == null) {
                    return 0;
                }

                // If the file to be connected to the unit is the same as the file
                // to which the unit is connected, only the BLANK= specifier may
                // have a value different from the one currently in effect. Execution
                // of the OPEN statement causes the new value of the BLANK= specifier
                // to be in effect. The position of the file is unaffected.
                string fixedFilename = filename.Trim();
                if (string.Compare(fixedFilename, iofile.Path, StringComparison.CurrentCultureIgnoreCase) == 0) {
                    iofile.Blank = blankChar;
                    return 0;
                }

                // If the file to be connected to the unit is not the same as the file to which the unit is connected,
                // the effect is as if a CLOSE statement (12.10.2) without a STATUS= specifier had been executed for
                // the unit immediately prior to the execution of the OPEN statement.
                iofile.Handle.Close();
            } else {
                iofile = new IOFile(iodevice);
            }

            // Consult the status to determine how to open the file. A missing status is treated
            // as UNKNOWN.
            string fixedStatus = (status == null) ? "UNKNOWN" : status.ToUpper().Trim();

            if ((fixedStatus == "OLD" || fixedStatus == "NEW" || fixedStatus == "UNKNOWN") && filename != null) {
                bool fileExists = File.Exists(filename.Trim());
                if (fixedStatus == "UNKNOWN") {
                    fixedStatus = (fileExists) ? "OLD" : "NEW";
                }
                if (fixedStatus == "OLD") {
                    if (!fileExists) {
                        iostat = IOError.FileNotFound;
                        return -1;
                    }
                    iofile.IsNew = false;
                }
                if (fixedStatus == "NEW") {
                    if (fileExists) {
                        iostat = IOError.FileAlreadyExists;
                        return -1;
                    }
                    iofile.IsNew = true;
                }
                iofile.IsScratch = false;
            }

            // SCRATCH creates a temporary file. Filename cannot be specified here
            else if (fixedStatus == "SCRATCH") {
                if (filename != null) {
                    iostat = IOError.FilenameSpecified;
                    return -1;
                }
                filename = Path.GetTempFileName();
                iofile.IsScratch = true;
            }

            // STATUS must be UNKNOWN if we get here
            else if (fixedStatus != "UNKNOWN") {
                iostat = IOError.IllegalStatus;
                return -1;
            }

            // Check access. It must either be SEQUENTIAL or DIRECT. By
            // default we assume sequential access.
            if (access == null) {
                iofile.IsSequential = true;
            } else {
                string fixedAccess = access.ToUpper().Trim();
                if (fixedAccess == "SEQUENTIAL") {
                    iofile.IsSequential = true;
                } else if (fixedAccess == "DIRECT") {
                    iofile.IsSequential = false;
                } else {
                    iostat = IOError.IllegalAccess;
                    return -1;
                }
            }

            // Check whether the file is being written formatted or
            // unformatted. Access matters here too.
            if (form == null) {
                iofile.IsFormatted = iofile.IsSequential;
            } else {
                string fixedForm = form.ToUpper().Trim();
                if (fixedForm == "FORMATTED") {
                    iofile.IsFormatted = true;
                } else if (fixedForm == "UNFORMATTED") {
                    iofile.IsFormatted = false;
                } else {
                    iostat = IOError.IllegalForm;
                    return -1;
                }
            }

            // Save the record length
            iofile.RecordLength = recl;
            iofile.RecordIndex = 1;
            iofile.Blank = blankChar;
            iofile.Unit = iodevice;

            if (filename != null) {
                iofile.Path = filename.Trim();
            }

            if (!iofile.Open()) {
                iostat = IOError.CannotOpen;
                return -1;
            }
            return 0;
        }
Esempio n. 4
0
 /// <summary>
 /// ENDFILE keyword.
 /// For sequential files, writes an end-of-file record to the file and positions the file
 /// after this record (the terminal point). For direct access files, truncates the file
 /// after the current record.
 /// </summary>
 /// <param name="iodevice">The device number</param>
 /// <param name="iostat">A reference variable that will be set to the I/O status</param>
 /// <returns>A zero value if the operation succeeds, or -1 if the operation fails</returns>
 public static int ENDFILE(int iodevice, ref int iostat)
 {
     iostat = 0;
     IOFile iofile = IOFile.Get(iodevice);
     if (iofile == null) {
         iofile = new IOFile(iodevice);
         iofile.Open();
     }
     if (!iofile.EndFile()) {
         iostat = IOError.EndfileError;
         return -1;
     }
     return 0;
 }