loading a Unicode file line by line

General TRichView support forum. Please post your questions here
Sergey Tkachenko
Site Admin
Posts: 17522
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Sorry, I do not understand what's wrong.
In your example:
Item[0] = 'LETHAL' IsFromNewLine = True, because the document starts from a new line, IsFromNewLine(0) is always true.
Item[1] = character IsFromNewLine = False, because the character contunues the same paragraph as the previous item.
Item[2] = ' WEAPON 4.' IsFromNewLine = False, because this text continues the same paragraph as the previous item.
Item[3] = 'Riggs, are you...' IsFromNewLine = True, because this text starts a new paragraph.
When exporting to text, of all these items, #13#10 must be inserted only before the 3rd item.
Cosmin3
Posts: 54
Joined: Sat Apr 05, 2008 12:04 pm

Post by Cosmin3 »

I don't understand why a text line is loosing NewLine at the end by inserting a character in it. But visual in ScalRichView is still a text line.
And I said that this is happenning on all items (lines) not only at the first.
The problem is that I load also subtitles: text is loaded in ScaleRichView and frames/times in an integer array.
The lines from the original subtitle are composed by one or more items.
Until now I used to synchronize lines from RichVieEdit with that array using IsFromNewLine and was working well.
The code looks like that (for a Unicode SubRip subtitle):

Code: Select all

            try
               begin
                  tf := TFileStream.Create(FileName, fmCreate or fmShareExclusive);
                  ws := #65279;
                  if tf.Write(Pointer(ws)^, 2) <> 2 then
                     i := -1
                  else
                     if srvEditor.RichViewEdit.ItemCount > 0 then
                     begin
                        ws := '';
                        SubCnt := -1;
                        for i := 0 to srvEditor.RichViewEdit.ItemCount - 2 do
                        begin
                           if not srvEditor.RichViewEdit.IsFromNewLine(i) then
                              ws := ws + srvEditor.RichViewEdit.GetItemTextW(i)
                           else
                           begin
                              Inc(SubCnt);
                              ws := IntToStr(SubCnt + 1) + #13#10 +
                                 FormatDateTime('hh:nn:ss' + Char(44 + (SrtType - 1) * 2) + 'zzz', 1.0 * SubTimes[SubCnt][1] / 86400000) +
                                 ' --> ' +
                                 FormatDateTime('hh:nn:ss' + Char(44 + (SrtType - 1) * 2) + 'zzz', 1.0 * SubTimes[SubCnt][2] / 86400000) +
                                 #13#10 + StringReplace(ws + srvEditor.RichViewEdit.GetItemTextW(i), '|', #13#10, [rfReplaceAll]) + #13#10#13#10;
                              if tf.Write(Pointer(ws)^, 2 * Length(ws)) <> 2 * Length(ws) then
                                 Break;
                              ws := '';
                           end;
                        end;
                        if i = srvEditor.RichViewEdit.ItemCount - 1 then
                        begin
                           Inc(SubCnt);
                           ws := IntToStr(SubCnt + 1) + #13#10 +
                              FormatDateTime('hh:nn:ss' + Char(44 + (SrtType - 1) * 2) + 'zzz', 1.0 * SubTimes[SubCnt][1] / 86400000) +
                              ' --> ' +
                              FormatDateTime('hh:nn:ss' + Char(44 + (SrtType - 1) * 2) + 'zzz', 1.0 * SubTimes[SubCnt][2] / 86400000) +
                              #13#10 + StringReplace(ws + srvEditor.RichViewEdit.GetItemTextW(i), '|', #13#10, [rfReplaceAll]) + #13#10;
                           if tf.Write(Pointer(ws)^, 2 * Length(ws)) = 2 * Length(ws) then
                              Inc(i);
                        end;
                     end
                     else
                        i := 1;
                  tf.Free;
                  Result := i >= srvEditor.RichViewEdit.ItemCount;
               end;
            finally
               ws := '';
            end;
            if not Result then
               CustomMessage('Write error', MB_ICONERROR or MB_OK);
I'm saving lines instead of all text at once because I noticed it's faster this way (it takes time to append all the lines in a string).
I don't know how to implement your code but still keeping the synchronization between text and that array...
Sergey Tkachenko
Site Admin
Posts: 17522
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Sorry, what do you think IsFromNewLine means? Probably, you misunderstand it, and so there is a confusion.

Actually, it means "this item starts a paragraph" (or a line inside paragraph, added with Shift+Enter).
In your example, there are 2 paragraphs, so there are must be only 2 items having IsFromNewLine = True.
In the first paragraph ("LETHAL", character, "WEAPON 4"), the first paragraph item is "LETHAL", and it has IsFromNewLine=True.
In the second paragraph, there is only one item, and it has IsFromNewLine=True.
Sergey Tkachenko
Site Admin
Posts: 17522
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

In your code, I do not understand the statement

Code: Select all

if i = srvEditor.RichViewEdit.ItemCount - 1 then ...
Why do you need to process the last item specially? This item is not necessary starts a paragraph.
Cosmin3
Posts: 54
Joined: Sat Apr 05, 2008 12:04 pm

Post by Cosmin3 »

It's not 100% necessary but I used it to avoid adding a #13#10 at the end. It can be removed if you want and set first cycle to ItemCount - 1.

Later edit: I have solved the problem. Thank you for all your patience...
Pieter Zijlstra
Posts: 42
Joined: Sat Oct 08, 2005 3:56 pm
Location: The Netherlands
Contact:

Post by Pieter Zijlstra »

Would you be so kind to tell us what it was?
Cosmin3
Posts: 54
Joined: Sat Apr 05, 2008 12:04 pm

Post by Cosmin3 »

I'm not so good of explaining in English but if you want I will show you my code...
Post Reply