Ineffable21
Ineffable21

Reputation: 456

Getting cyrillic string in richedit with Delphi

I have a formatted text on a wordpad file(rtf). I'm trying to open it on a richedit on a delphi form. The problem is that the string is in cyrillic(Bulgarian) and it's saved with weird hieroglyphs or whatever those are "Âëåçå ïîòðåáèòåë". Is there a way to transfer/translate the hieroglyphs to the richedit, so they can appear as proper text?

This function I use to check if the file is empty so I can then enter the first rtf tag, or remove the closing tag, so I can add more text in there without breaking the file

function FileIsEmpty(const FileName: String): Boolean;
var
  fad: TWin32FileAttributeData;
begin
  Result := GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @fad) and
            (fad.nFileSizeLow = 0) and (fad.nFileSizeHigh = 0);
end;

This is the code I use to format the text and also give it to the file:

procedure FormatLogAndAddToFile(richEditLog : TRichEdit; richEditTextColor : TRichEdit);

var
  i : integer;
  s, c, finalText : string;
  sString : TStringList;

begin
  with frmMain do
  begin
    sString := TStringList.Create;
    sString.LoadFromFile('C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\TestFormating.rtf');
    if Pos('{\rtf}', sString.Strings[0]) <> 0 then
    begin
      sString.Delete(0);
    end
    else
    begin
      sString.Delete(sString.Count - 1);
    end;
    sString.SaveToFile('C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\TestFormating.rtf');
    sString.free;

    AssignFile(logFile, 'C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\TestFormating.rtf');
    Append(logFile);

    if FileIsEmpty('C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\TestFormating.rtf') = True then
    begin
      WriteLn(logFile, '{\rtf\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}}');
    end;

    for i := 0 to richEditLog.Lines.Count do
    begin
      s := richEditLog.Lines[i];
      c := richEditTextColor.Lines[i];

      if c = 'blue' then
      begin
        finalText := '{\colortbl ;\red0\green128\blue255;\red255\green0\blue0;}' +
                       '\viewkind4\uc1 \pard\sa200\sl276\slmult1\cf1\f0\fs32\lang9 ' + s + '\cf2\par';
      end
      else if c = 'red' then
      begin
        finalText := '{\colortbl ;\red255\green0\blue0;}' +
                       '\viewkind4\uc1 \pard\sa200\sl276\slmult1\cf1\f0\fs32\lang9 ' + s + '\par';
      end
      else if c = 'green' then
      begin
        finalText := '{\colortbl ;\red0\green128\blue128;\red255\green0\blue0;}' +
                       '\viewkind4\uc1 \pard\sa200\sl276\slmult1\cf1\f0\fs32\lang9 ' + s + '\cf2\par';
      end;

      WriteLn(logFile, finalText);
    end;

    WriteLn(logFile, '}');
    CloseFile(logFile);
  end;
end;

This is the code I use to add the log lines to the file. I also have little bit of code that checks if the file has lines with a date that is entered on a TDateEdit, so I can only get log from the date I've entered.

procedure OpenLogInRichEdit(dateFilter : Boolean; searchDate : tDate);

var
  sTime : string;
  dateExists : Boolean;
  I : integer;
  
begin
  with frmMain do
  begin
    dateExists := false;
    frmLogSearch.tLogRichEdit.Clear;
    frmLogSearch.tLogRichEdit.Lines.LoadFromFile('C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\TestFormating.rtf');

    sTime := DateTimeToStr(searchDate); 

    if dateFilter then
    begin
      for I := 0 to frmLogSearch.tLogRichEdit.Lines.Count do
      begin
        if Pos(sTime, frmLogSearch.tLogRichEdit.Lines[i]) <> 0 then
        begin
          frmLogSearch.tLogRichEdit.Lines.Delete(i);
          dateExists := True;
        end;
      end;

      if dateExists = false then
      begin
        ShowMessage('No log from this day!');
      end;
    end;
  end;
end;

This is how I add the text to the richedits I use later for the procedure FormatLogAndAddToFile.

dateTimeNow := Now;

  logText.Lines.Add('<' + DateTimeToStr(dateTimeNow) + '> Изтрита е поръчка');
  logTextColor.Lines.Add('red');

And this is how I eventually call the procedures. First the procedure to get the formatted log to the richedits

OpenLogInRichEdit(tcxCheckBoxDate.Checked, tcxDate.Date);

And this is the procedure to format the text and give it to the file

LogFileUse.FormatLogAndAddToFile(logText, logTextColor);

Thanks to the comments I've managed to make it work. I've changed the code above. Instead of having 'fcharset0' as a tag, I now have 'fcharset1' and I also changed 'lang9' to 'lang1026' and now I save it properly to the file and it opens perfectly!

Upvotes: -2

Views: 464

Answers (1)

Torbins
Torbins

Reputation: 2216

If all this scary code is here only to add colored lines to the file, than you should use TRichEdit.SelAttributes with friends: Colorful text in the same line in TRichEdit This way TRichEdit will be able to correctly handle encoding. And if you need some fancy file header or footer, that you do not want to create from code, than you can create empty rtf-file with required header/footer, and use it as a template.

Upvotes: 0

Related Questions