Ejemplo n.º 1
0
        private static int wrapSimply(IQueueInterface <String> words, int columnLength, string outputFilename)
        {
            StreamWriter sw = null;

            try
            {
                sw = new StreamWriter(outputFilename);
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("Cannot create or open " + outputFilename +
                                  " for writing.  Using standard output instead.");
                sw = new StreamWriter(Console.OpenStandardOutput());
            }

            int col             = 1;
            int spacesRemaining = 0;            // Running count of spaces left at the end of lines

            while (!words.isEmpty())
            {
                string str = words.peek();
                int    len = str.Length;
                // See if we need to wrap to the next line
                if (col == 1)
                {
                    sw.Write(str);
                    col += len;
                    words.pop();
                }
                else if ((col + len) >= columnLength)
                {
                    // go to the next line
                    sw.WriteLine();
                    spacesRemaining += (columnLength - col) + 1;
                    col              = 1;
                }
                else
                {
                    sw.Write(" ");
                    sw.Write(str);
                    col += (len + 1);
                    words.pop();
                }
            }
            sw.WriteLine();
            sw.Flush();
            sw.Close();
            return(spacesRemaining);
        }
Ejemplo n.º 2
0
        public static int WrapSimply(IQueueInterface <String> words, int columnLength, String outputFilename)
        {
            StreamWriter _out;

            try
            {
                _out = new StreamWriter(outputFilename);
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("Cannot create or open " + outputFilename +
                                  " for writing.  Using standard output instead.");
                _out = new StreamWriter(Stream.Null);
            }

            int col             = 1;
            int spacesRemaining = 0;

            // Running count of spaces left at the end of lines

            try
            {
                while (!words.isEmpty())
                {
                    String str = words.peek();
                    int    len = str.Length;
                    // See if we need to wrap to the next line
                    if (col == 1)
                    {
                        _out.Write(str);
                        col += len;
                        words.dequeue();
                    }
                    else if ((col + len) >= columnLength)
                    {
                        // go to the next line
                        _out.WriteLine();
                        spacesRemaining += (columnLength - col) + 1;
                        col              = 1;
                    }
                    else
                    {                       // Typical case of printing the next word on the same line
                        _out.Write(" ");
                        _out.Write(str);
                        col += (len + 1);
                        words.dequeue();
                    }
                }
            }
            catch (QueueUnderflowException e)
            {
                Console.WriteLine("Exception occured : The queue was empty");
            }
            finally
            {
                _out.WriteLine();
                _out.Flush();
                _out.Close();
            }
            return(spacesRemaining);
        }