Click here to Skip to main content
Licence 
First Posted 19 Sep 2005
Views 22,440
Downloads 291
Bookmarked 11 times

Text Formatter

By Doga Arinir | 19 Sep 2005
A class to block the size of a given text.
2 votes, 28.6%
1

2

3
2 votes, 28.6%
4
3 votes, 42.9%
5
3.05/5 - 7 votes
μ 3.05, σa 3.17 [?]

Introduction

I wrote this code back in times where most of the user interfaces were text based. But some years ago, I again had to use this code for formatting emails which were automatically generated and sent to our customers. So I grabbed the change, and rewrote the whole thing in C++. The outcome of this refactoring process is an easy to use class called TextFormatter which provides row justification with a modifiable block size. It also has a special table formatting feature which avoids using tabs between its columns, thus providing a character based column size control and alignment.

How does it work?

The idea behind the row justification is, to automatically wrap the words if the length of one row is greater than the given block size. The diagram below shows the technique of wrapping the line into parts where each part's length is less than the block size. This job is done in makeRowsUnderBlockMax().

Once each row isn't greater than block size anymore, spaces are inserted between words by calculating the amount of characters needed to let the row fit exactly into the block size.

Using the class

The constructor of TextFormatter expects an output stream and a block size for row justifying. You can use either output streams. After an instance of this class is created you can call print(const char *text) as often as you want. This operation will print the given text to the output stream while row justifying it. By calling createTable(), you initiate table formatting. Before you can print the rows of the table, you have to define the column size and alignment. Please refer to the example below. You are also able to print text while you are printing tables. Just modify the code below and try it out. Hope you find this class useful.

void CTextformatterDialogDlg::OnButTesttable() 
{
    std::stringstream out;
    TF::TextFormatter tf(out, m_ColumnWidthControl.GetPos());
    tf.print(standard_text);

    tf.newLines(3);

    tf.createTable();
    tf.addColumn("Name", 10); tf.addColumn("First Name", 14); 
    tf.addColumn("Age", 6);
    tf.setColumnAlignment(2, TF::TextFormatter::right);
    tf.printHeader(true);

    tf.printRow(3, "Kandinsky", "Wassily", "139");
    tf.printRow(3, "Marc", "Franz", "125");
    tf.printRow(3, "Mueller", "Otto", "1131");
    tf.printRow(3, "Dali", "Salvador", "101");
    tf.printRowLine();
    tf.print("Mr.Mueller's age was slightly modified.");

    tf.newLines(5);

    tf.setBlockMax(m_ColumnWidthControl.GetPos()*2);
    tf.print(standard_text);

    m_Output = out.str().c_str();

    this->UpdateData(FALSE);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Doga Arinir

Chief Technology Officer
W3L
Germany Germany

Member
-Since 1th August 2007: Chief Technology Officer of W3L
-2002/08/01-2007/07/31: PhD student
-1997/10/15-2002/07/31: Studied Electrical Engineering and Computer Science

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionGreat ,Did u wrote it in c#??? Pinmemberqueen_Of_Hearts10:06 21 Feb '06  
Hi,
Can u convert this code "rows justification" from C++ to C#.
If that PLZ send me on "pure_eyes_cs@yahoo.com
" .
Wish I receive ur reply quickly.
jessy
 
Welcome
GeneralStandalone method PinmemberRavi Bhavnani15:30 19 Sep '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 19 Sep 2005
Article Copyright 2005 by Doga Arinir
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid