Announcement

Collapse
No announcement yet.

Trying to wrap text instead of cutting it off

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trying to wrap text instead of cutting it off

    Hi im trying to write a function that reads a text file from SD and instead of cutting a word off midway when it hits end of screen, it instead will go to the new line at a white space.
    What I attempted to code was for the each character to be loaded in, verified that it was not a whitespace, placed into a "word" buffer until a whitespace was hit. The length of this "word" is then checked to see if it may be placed in the remaining columns available without being cutoff, if it cant, it will increment to the next row and write the "word" there.

    The problem im running into is that as soon as I write a character into the "word" buffer, it doesn't seem to be correct. It seems the word buffer just contains zeros.
    I've attached my code if anyone can make sense of it, or if not, offer me an alternative idea.

    Thanks,
    HTML Code:
    row := 1;
    col := 0;
    listhnd := file_Open("L1.TXT", 'r');
    file_Size(listhnd, &szH, &szL);
    i := 0;
    repeat
    char := file_GetC(listhnd);
    
    if ( char != 0x20 )  // if not a whitespace
    to(wordbuf[0]); print(char); //print first character of word to buffer
    j:=1;
    i++;
    while(char != 0x20)
    char := file_GetC(listhnd);
    to(wordbuf[j]); print(char);
    i++;
    j++;
    wend
    // once loop is finished, we have the completed word in wordbuf, along with the number of characters given by j-1
    if ( ((j-1)+col) = 1000);
    Edit: FYI for some reason forums showing < as lt (lessthan) and > gt( greaterthan)

  • #2


    Well looks like I got it working, I finally noticed my massive mistake in trying to use the to(outstream) command instead of just saying wordbuf[0] := char; works for the most part now.

    [HTML]
    row := 1;
    col := 1;
    // List of items
    //txt_FontID(Font_Data);
    //font := file_LoadFunction("farial.4FN");

    listhnd := file_Open("L1.TXT", 'r');
    file_Size(listhnd, &szH, &szL);
    i := 0;
    char := file_GetC(listhnd); //for some reason the beginning of the file has period characters or something so I must go thru the first few characters without using them
    char := file_GetC(listhnd);
    char := file_GetC(listhnd);
    repeat
    char := file_GetC(listhnd);
    if (char != 32) // if not whitespace
    wordbuf[0] := char;
    j := 1;
    i++;
    while(char != 32)
    //print("test4");
    char := file_GetC(listhnd);
    wordbuf[j] := char;
    i++;
    j++;
    wend
    if ( ((j-1)+col)

    Comment


    • #3


      So despite it mostly working, I am having some strange problems. For one, I'm unsure how to know when the repeat loop should stop.. I was thinking a null terminator, so I tried that but ran into issues. The main problem seems to be that the loop will freeze after a certain amount of text is printed. For instance, if I have the text file L1.TXT with contents:
      HTML Code:
      This is a test of the LCD, first a file is loaded onto the SD Card. The file is pointed to and read into a buffer. This buffer is then read onto the screen to be printed. Once printing is finished printing will end. Test will now END.
      It prints fine until the last sentence. It always stops after "now", it will never print the final "END." And it does indeed freeze here, as any commands after the loop will not execute. However, If i lessen the iterations by changing the until(i >=50); the text prints fine and the loop doesn't freeze, but of course this is not a solution since the entire text file is not printed.

      Im wondering what could be causing this issue, its seems rather strange..

      Regards,
      Mike

      Comment


      • #4


        Dunno, shouldn't you just loop until i = &szL and then print the final contents of wordbuf at the end
        Mark

        Comment


        • #5


          Hmm, even then the loop still freezes at the same point. I also tested by increasing the length of the file. say I add another sentence, "Here is the final end", it will print "final" but then freeze there.

          I'm wondering if its something that it encounters at the end of the file that screws it up.

          Comment


          • #6


            Wow what an obvious problem, finally noticed that my while loop condition checked to see if the character was not a space. The problem is, once you get to the end of the file, its just zeros, so the loop was just repeating forever. By adding an && (char != 0) to the loop condition this fixed the problem!

            Comment

            Working...
            X