CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Nov 2006
    Posts
    7

    tab to spaces conversion

    Hi All,

    This is a very basic q, but i am kinda stuck now in my code.
    My Appln reads from a file which contains tabs as well as spaces between words, Now when this application ( in various platforms) reads this file it treats the tab character (\t ot ^I) in various ways. like in Windows it is putting a box character or in SOlaris it is totlly ignoring , which is spoiling the alignment while displaying the file.

    Is there any way to convert this tab in right no of spaces independent of the platform as I have notice in some OS a tab is equal to 4 spaces & in some as 8.
    How can i recognize this thing & replace accordingly.

    THanks
    Nimisha

  2. #2
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: tab to spaces conversion

    I do not think that tab size is OS dictated; it is dictated by the application displaying the content. In your case it would be your application. What you need to do is to do some research to find out what the usual tab size is for the applications running on those OS and then put that knowledge in your software.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  3. #3
    Join Date
    Nov 2006
    Posts
    7

    Re: tab to spaces conversion

    Hi but for the same application, at one part of code when i replaced tab by 5 spaces it seemed fine but now for this particular file it is coming out to be 8 tabs & for one of the line 7 seems to be working fine.
    Y I am emphasizing on the no of spaces is bcoz this is affecting the allignment.

    hv searched quite extensively on net without any fruitfull result

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  5. #5
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: tab to spaces conversion

    Are you working with plain text display OR with one of the 'word' type editors using true type fonts?
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: tab to spaces conversion

    Quote Originally Posted by UnderDog
    I do not think that tab size is OS dictated; it is dictated by the application displaying the content. In your case it would be your application. What you need to do is to do some research to find out what the usual tab size is for the applications running on those OS and then put that knowledge in your software.
    More over, most of editors allow you to change the size of the TAB.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Nov 2006
    Posts
    7

    Re: tab to spaces conversion

    Quote Originally Posted by exterminator
    What is the problem with alignment? Can you post sample data (2/3 rows?)?

    well if suppose the tab is 7 spaces at 1 place
    & on 2nd line - 8 spaces then two lines which should occur vertically on top if each other, will get misallignment

  8. #8
    Join Date
    Nov 2006
    Posts
    7

    Re: tab to spaces conversion

    Quote Originally Posted by UnderDog
    Are you working with plain text display OR with one of the 'word' type editors using true type fonts?

    i am using Qt - a GUI language to output the display. this is just read the file line by line & displaying on a screen like object ( Listbox) Eg: like this one we are using here.

    I still dunno how to get the no of spaces for a tab in the current program..

  9. #9
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: tab to spaces conversion

    If it is a 'smart' display like this one, then the width of a tab is determined by a complicated editor algorithms - they look into stuff such as how to fit words in a given line etc. You can't really replace tab with spaces in such displays and hope it to work. You may 'visualy' determine that a tab looks like 4 spaces at one place and 7 at another place, while it is just that pagination algorithm working to change the tab size to keep the text looking proper. Also e.g. Word lets you define the tab stop. So the amount of white space a single tab character inserts depends on where in a line you insert a tab and where the nearest tab stop is. And then you will run into the situation whcih you descibe - a single tab is 4 spaces at some place, 8 places at others and 10 places at some other. Display your file in a plain text editor using Courier fonts and see what I mean.

    IMHO - you really dont want to be messing with tabs and spaces unless you are dealing with fixed width Courier font inside of a plain text editor. Such as Visual Studio C++ code editor. There is too much under the table stuff going on with True Type fonts and 'smart' displays. And you would run into things such as justification, orphan control, hyphenation logic etc.
    Last edited by UnderDog; November 13th, 2006 at 06:31 AM.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  10. #10
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: tab to spaces conversion

    Also please find out the root cause of the problem - those boxes. The tab should display like a tab in all the OS - period. I can understand that tabs may insert various amounts of white space depending on the application - but the tab is still a tab.

    Your problems looks like the tab characters in your source files are NOT the tab characters as defined by your character set used for display. e.g. it may not be ASCII code 9 (i.e. the integer value of that character in the source file many not be 9 - the code for tab). What you should be doing is doing replacement for the source tab (whatever the representation is) to tab character on your target machine. e.g. you have to do such sort of translations when you translate HTML to plain text because one of the ways to specify line breaks in HTML is like <BR> while for your display a line break may be <CR><LF> character pair.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  11. #11
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: tab to spaces conversion

    Quote Originally Posted by nimishm
    well if suppose the tab is 7 spaces at 1 place
    & on 2nd line - 8 spaces then two lines which should occur vertically on top if each other, will get misallignment
    Can you show a sample?

    I believe you should neither be using tabs nor fixed length spaces.. but you should use any of those along with text "padding". Everything that you are writing should be strictly within a fixed width.. and if it is not - you pad spaces to it and then write to the file.

    Worrying about how many spaces does each tab have on which platform is not relevant to your issue. That is why I asked - can you show a sample in [CODE] tags?

  12. #12
    Join Date
    Nov 2006
    Posts
    7

    Re: tab to spaces conversion

    Quote Originally Posted by UnderDog
    Also please find out the root cause of the problem - those boxes. The tab should display like a tab in all the OS - period. I can understand that tabs may insert various amounts of white space depending on the application - but the tab is still a tab.

    Your problems looks like the tab characters in your source files are NOT the tab characters as defined by your character set used for display. e.g. it may not be ASCII code 9 (i.e. the integer value of that character in the source file many not be 9 - the code for tab). What you should be doing is doing replacement for the source tab (whatever the representation is) to tab character on your target machine. e.g. you have to do such sort of translations when you translate HTML to plain text because one of the ways to specify line breaks in HTML is like <BR> while for your display a line break may be <CR><LF> character pair.

    hello..
    i checked the ascii value of the character & indeed it is 9. also while debugging in VC++. i cud see the character is displayed as small rectangle(box).

  13. #13
    Join Date
    Nov 2006
    Posts
    7

    Re: tab to spaces conversion

    Quote Originally Posted by exterminator
    Can you show a sample?

    I believe you should neither be using tabs nor fixed length spaces.. but you should use any of those along with text "padding". Everything that you are writing should be strictly within a fixed width.. and if it is not - you pad spaces to it and then write to the file.

    Worrying about how many spaces does each tab have on which platform is not relevant to your issue. That is why I asked - can you show a sample in [CODE] tags?

    the code is simple -

    QFile file(fileName);
    file.open(IO_ReadOnly);
    QTextStream textStream(&file);
    QString str_line = "";
    while (!textStream.atEnd())
    {
    str_line = textStream.readLine();
    str_line.stripWhiteSpace();
    txt = new PvBaseText(lb_TD_listCanvas);
    txt->dat = str_line;
    txt->setText(txt->dat);
    }



    bascially i am reading str_line (String) line by line from file & then displaying it in a canvas (txt).

  14. #14
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: tab to spaces conversion

    First of all, please use CODE tags:

    the code is simple -

    [CODE]
    QFile file(fileName);
    file.open(IO_ReadOnly);
    QTextStream textStream(&file);
    QString str_line = "";
    while (!textStream.atEnd())
    . .{
    . . . .str_line = textStream.readLine();
    . . . .str_line.stripWhiteSpace();
    . . . .txt = new PvBaseText(lb_TD_listCanvas);
    . . . .txt->dat = str_line;
    . . . .txt->setText(txt->dat);
    . .}

    [/CODE]

    bascially i am reading str_line (String) line by line from file & then displaying it in a canvas (txt).
    Secondly, the call to stripWhiteSpace isn't entirely helpful as it removes white space from the beginning of the string. Unnecessary white space at the end of the string likely won't matter as it won't really be visible in the control. In my opinion, I'd remove that call.

    Thirdly, have you set font in the control to a fixed-sized font? If not, then (as many people have said before) trying to replace tabs with spaces will be very unsuccessful.

    - Kevin
    Last edited by KevinHall; November 14th, 2006 at 05:08 AM.
    Kevin Hall

  15. #15
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: tab to spaces conversion

    Ooopss.... double post. Sorry.
    Last edited by KevinHall; November 14th, 2006 at 05:06 AM.
    Kevin Hall

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured