Ejemplo n.º 1
0
        /// <summary>
        /// Read plan information in and store in a list. </summary>
        /// <param name="filename"> filename containing plan information </param>
        /// <exception cref="Exception"> if an error occurs </exception>
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: public static java.util.List<StateMod_Plan> readStateModFile(String filename) throws Exception
        public static IList <StateMod_Plan> readStateModFile(string filename)
        {
            string                routine  = "StateMod_Plan.readStateModFile";
            string                iline    = null;
            IList <string>        v        = new List <string>(9);
            IList <StateMod_Plan> thePlans = new List <StateMod_Plan>();
            int linecount = 0;

            StateMod_Plan aPlan = null;
            StreamReader  @in   = null;

            Message.printStatus(2, routine, "Reading plan file: " + filename);
            int size       = 0;
            int errorCount = 0;

            try
            {
                @in = new StreamReader(IOUtil.getPathUsingWorkingDir(filename));
                IList <string> commentsBeforeData = new List <string>();
                while (!string.ReferenceEquals((iline = @in.ReadLine()), null))
                {
                    ++linecount;
                    // check for comments
                    if (iline.StartsWith("#>", StringComparison.Ordinal) || (iline.Trim().Length == 0))
                    {
                        // Special dynamic header comments written by software and blank lines - no need to keep
                        continue;
                    }
                    else if (iline.StartsWith("#", StringComparison.Ordinal))
                    {
                        // Comment prior to a plan - do not trim so that input/output comparisons can be made but
                        // do remove the initial comment character
                        commentsBeforeData.Add(iline.Substring(1));
                        continue;
                    }

                    if (Message.isDebugOn)
                    {
                        Message.printDebug(50, routine, "line: " + iline);
                    }
                    // Break the line using whitespace, while allowing for quoted strings...
                    v    = StringUtil.breakStringList(iline, " \t", StringUtil.DELIM_ALLOW_STRINGS | StringUtil.DELIM_SKIP_BLANKS);
                    size = 0;
                    if (v != null)
                    {
                        size = v.Count;
                    }
                    if (size < 11)
                    {
                        Message.printStatus(2, routine, "Ignoring line " + linecount + " not enough data values.  Have " + size + " expecting " + 11);
                        ++errorCount;
                        continue;
                    }
                    // Uncomment if testing...
                    //Message.printStatus ( 2, routine, "" + v );

                    // Allocate new plan node and set the values
                    aPlan = new StateMod_Plan();
                    aPlan.setID(v[0].Trim());
                    aPlan.setName(v[1].Trim());
                    aPlan.setCgoto(v[2].Trim());
                    aPlan.setSwitch(v[3].Trim());
                    aPlan.setIPlnTyp(v[4].Trim());
                    aPlan.setPeffFlag(v[5].Trim());
                    int peffFlag = aPlan.getPeffFlag();
                    aPlan.setIPrf(v[6].Trim());
                    aPlan.setIPfail(v[7].Trim());
                    aPlan.setPsto1(v[8].Trim());
                    aPlan.setPsource(v[9].Trim());
                    aPlan.setIPAcc(v[10].Trim());

                    // Read the efficiencies...

                    if (peffFlag == 1)
                    {
                        iline = @in.ReadLine();
                        ++linecount;
                        if (string.ReferenceEquals(iline, null))
                        {
                            throw new IOException("Unexpected end of file after line " + linecount + " - expecting 12 efficiency values.");
                        }
                        v    = StringUtil.breakStringList(iline, " \t", StringUtil.DELIM_ALLOW_STRINGS | StringUtil.DELIM_SKIP_BLANKS);
                        size = 0;
                        if (v != null)
                        {
                            size = v.Count;
                        }
                        if (size != 12)
                        {
                            Message.printStatus(2, routine, "Ignoring line " + linecount + " not enough data values.  Have " + size + " expecting " + 12);
                            ++errorCount;
                        }
                        else
                        {
                            for (int iEff = 0; iEff < 12; iEff++)
                            {
                                string val = v[0].Trim();
                                try
                                {
                                    aPlan.setPeff(iEff, double.Parse(val));
                                }
                                catch (Exception)
                                {
                                    Message.printStatus(2, routine, "Efficiencies on line " + linecount + " value \"" + val + "\" is not a number.");
                                    ++errorCount;
                                }
                            }
                        }
                    }

                    // Set the comments

                    if (commentsBeforeData.Count > 0)
                    {
                        // Set comments that have been read previous to this line.  First, attempt to discard
                        // comments that do not below with the operational right.  For now, search backward for
                        // "EndHeader" and "--e" which indicate the end of the header.  If found, discard the comments prior
                        // to this because they are assumed to be file header comments, not comments for a specific right.
                        // Only do this for the first right because the user may actually want to include the header
                        // information in their file periodically to help with formatting
                        string comment;
                        if (thePlans.Count == 0)
                        {
                            for (int iComment = commentsBeforeData.Count - 1; iComment >= 0; --iComment)
                            {
                                comment = commentsBeforeData[iComment].ToUpper();
                                if ((comment.IndexOf("ENDHEADER", StringComparison.Ordinal) >= 0) || (comment.IndexOf("--E", StringComparison.Ordinal) >= 0))
                                {
                                    // Remove the comments above the position.
                                    while (iComment >= 0)
                                    {
                                        commentsBeforeData.RemoveAt(iComment--);
                                    }
                                    break;
                                }
                            }
                        }
                        aPlan.setCommentsBeforeData(commentsBeforeData);
                    }
                    // Always clear out for next right...
                    commentsBeforeData = new List <string>(1);

                    // Set the plan to not dirty because it was just initialized...

                    aPlan.setDirty(false);

                    // Add the plan to the vector of plans
                    thePlans.Add(aPlan);
                }
            }
            catch (Exception e)
            {
                Message.printWarning(3, routine, "Error reading line " + linecount + " \"" + iline + "\" uniquetempvar.");
                Message.printWarning(3, routine, e);
                throw e;
            }
            finally
            {
                if (@in != null)
                {
                    @in.Close();
                }
            }
            if (errorCount > 0)
            {
                throw new Exception("There were " + errorCount + " errors processing the data - refer to log file.");
            }
            return(thePlans);
        }