Esempio n. 1
0
        /// <summary> Main function with Output streams to stdOut and file. </summary>
        /// <param name="args">Program arguments</param>
        /// <param name="standardWriter">stdOut stream for reporting errors.</param>
        /// <param name="fileWriter">File stream for program data output.</param>
        public static void Run(string[] args, TextWriter standardWriter, TextWriter fileWriter = null)
        {
            output = standardWriter;
            long lineLenght = 0;

            try {
                if(args.Length != 3 || args[0] == "" || args[1] == "")
                    throw new Exception();

                long.TryParse(args[2], out lineLenght);
                if(lineLenght <= 0)
                    throw new Exception();
            }
            catch(Exception) {
                ReportArgumentError();
                return;
            }

            WordReader reader = null;
            WordJustifier justifier = null;
            try {
                TextWriter writer = null;
                if(fileWriter == null)
                    writer = new StreamWriter(args[1]);
                else
                    writer = fileWriter;

                reader = new WordReader(new StreamReader(args[0]));
                justifier = new WordJustifier(writer, lineLenght);

                ProcessWords(reader, justifier);
            }
            catch(FileNotFoundException) {
                ReportFileError();
            }
            catch(IOException) {
                ReportFileError();
            }
            catch(UnauthorizedAccessException) {
                ReportFileError();
            }
            catch(System.Security.SecurityException) {
                ReportFileError();
            }
            catch(Exception) {
                ReportFileError();
            }
            finally {
                if(reader != null) reader.Dispose();
                if(justifier != null) justifier.Dispose();
            }
        }
Esempio n. 2
0
        /// <summary> Main function with Output streams to stdOut and file. </summary>
        /// <param name="args">Program arguments</param>
        /// <param name="standardWriter">stdOut stream for reporting errors.</param>
        /// <param name="fileWriter">File stream for program data output.</param>
        public static void Run(string[] args, TextWriter standardWriter, TextWriter fileWriter = null)
        {
            output = standardWriter;
            long lineLenght = 0;

            try {
                if (args.Length != 3 || args[0] == "" || args[1] == "")
                {
                    throw new Exception();
                }

                long.TryParse(args[2], out lineLenght);
                if (lineLenght <= 0)
                {
                    throw new Exception();
                }
            }
            catch (Exception) {
                ReportArgumentError();
                return;
            }

            WordReader    reader    = null;
            WordJustifier justifier = null;

            try {
                TextWriter writer = null;
                if (fileWriter == null)
                {
                    writer = new StreamWriter(args[1]);
                }
                else
                {
                    writer = fileWriter;
                }

                reader    = new WordReader(new StreamReader(args[0]));
                justifier = new WordJustifier(writer, lineLenght);

                ProcessWords(reader, justifier);
            }
            catch (FileNotFoundException) {
                ReportFileError();
            }
            catch (IOException) {
                ReportFileError();
            }
            catch (UnauthorizedAccessException) {
                ReportFileError();
            }
            catch (System.Security.SecurityException) {
                ReportFileError();
            }
            catch (Exception) {
                ReportFileError();
            }
            finally {
                if (reader != null)
                {
                    reader.Dispose();
                }
                if (justifier != null)
                {
                    justifier.Dispose();
                }
            }
        }