

// A very simple text-formatting program: simply fills out each line with
// blanks so that the line extends to 75 chars.
//
// M. A. Sridhar
// Feb 13, 1994
// Redone with iostreams: 6/28/94

#include "base/string.h"
#include "base/strgseq.h"

#include <fstream.h>
#include <iomanip.h>
main ()
{
    short max_line_length = 72;
    CL_String line;
    while (line.ReadLine (cin)) {
        if (line.Size() >= max_line_length) {
            cout << line << endl;
            continue;
        }
#if defined(_MSC_VER)
        // This ifdef is a workaround for a Microsoft Visual C++ bug: the
        // default arguments to the Split method is not passed correctly. In
        // this instance, I should see " \t" as the (default) parameter to
        // the Split method. Instead, I get "CL_"!! And, needless to say,
        // this does not happen under other compilers. Hence this ifdef.
        CL_StringSequence words = line.Split (" \t");
#else
        CL_StringSequence words = line.Split ();
#endif
        register short word_count = words.Size();
        // Find the sum of the lengths of all the words
        if (word_count > 1) {
            short word_len_sum = 0;
            for (short i = 0; i < word_count; i++)
                word_len_sum += words[i].Size();
            short space = max_line_length - word_len_sum;
            short space_per_word = space / (word_count - 1);
            short leftover = space % (word_count - 1);
            for (i = 0; i < word_count - 1; i++) {
                cout << words[i];
                for (short j = 0; j < space_per_word; j++) cout << ' ';
                if (leftover) {
                    cout << ' ';
                    leftover--;
                }
            }
        }
        if (word_count >= 1)
            cout << words[word_count-1] << endl;
        else // Empty line
            cout << endl;
    }
    return 0;
}



        
