Recent

Author Topic: [SOLVED] SynEdit for Batch Files  (Read 4181 times)

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: SynEdit for Batch Files
« Reply #15 on: May 16, 2019, 05:29:06 pm »
Okay... read the two articles... lot of complexion, very little code examples except one.
This is the one I am trying.

I get error on this line...
ustr.Assign(UTF8ToConsole(SynEdit.Lines.Text));

Saying...
Identifier not found "UTF8ToConsole"

The only uses reference I could find is...
LConvEncoding
I put in my uses, but still get error,
I don't know what other uses I need, the two articles don't help.

Code: Pascal  [Select][+][-]
  1.  
  2. uses
  3.    LConvEncoding
  4.  
  5. uStr:=TStringList.Create;
  6. try
  7.    ustr.Assign(UTF8ToConsole(SynEdit.Lines.Text));
  8. finally
  9.    uStr.Free;
  10. end;
  11. uStr.SaveToFile(sDlg.Filename);
  12.  
  13.  
  14.  
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: SynEdit for Batch Files
« Reply #16 on: May 16, 2019, 05:42:31 pm »
Besides lconvencoding there is another utf8-related unit which you should memorize: lazutf8...

Similarly, for utf16-related functions you have a good chance in unit lazutf16.

And for unicode (=utf8 or utf16) you should know that there is a lazunicode.

Difficult to remember?
« Last Edit: May 16, 2019, 05:47:20 pm by wp »

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: SynEdit for Batch Files
« Reply #17 on: May 16, 2019, 05:53:53 pm »
Besides lconvencoding there is another utf8-related unit which you should memorize: lazutf8...

Similarly, for utf16-related functions you have a good chance in unit lazutf16.

And for unicode (=utf8 or utf16) you should know that there is a lazunicode.

Difficult to remember?

I have all 3 uses (LConvEncoding,lazutf8,lazunicode), and now get this error
Error: Incompatible type for arg no. 1: Got "AnsiString", expected "TPersistent"

Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: SynEdit for Batch Files
« Reply #18 on: May 16, 2019, 06:08:55 pm »
Code: Pascal  [Select][+][-]
  1.  
  2. uses
  3.    LConvEncoding
  4.  
  5. uStr:=TStringList.Create;
  6. try
  7.    ustr.Assign(UTF8ToConsole(SynEdit.Lines.Text));
  8. finally
  9.    uStr.Free;
  10. end;
  11. uStr.SaveToFile(sDlg.Filename);
  12.  

You cannot assign a String (which the SynEdit.Lines.Text is) to a TStrings (which is waht TStringList is).
You can however do
Code: Pascal  [Select][+][-]
  1.   uStr.Text := UTF8ToConsole(SynEdit.Lines.Text)

Furthermore, your code would instantly crash, since you call uStr.SaveToFile() after you have just freed uStr.

This might work:
Code: Pascal  [Select][+][-]
  1. uses
  2.   LazUtf8; //for Utf8ToConsole function
  3. ...
  4.   uStr:=TStringList.Create;
  5.   try
  6.     ustr.Text := UTF8ToConsole(SynEdit.Lines.Text);
  7.     uStr.SaveToFile(sDlg.Filename);  
  8.   finally
  9.     uStr.Free;
  10.   end;

Bart

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: SynEdit for Batch Files
« Reply #19 on: May 16, 2019, 07:11:50 pm »
Okay... makes sense.

Adjusted code... Still don't display right when i run the BAT file
BUT, NO compile errors in code!!!

Code: Pascal  [Select][+][-]
  1.  
  2. uStr:=TStringList.Create;
  3. try
  4.    uStr.Text := UTF8ToConsole(SynEdit.Lines.Text)
  5. finally
  6.    uStr.SaveToFile(sDlg.Filename);
  7.    uStr.Free;
  8. end;
  9.  
  10.  
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: SynEdit for Batch Files
« Reply #20 on: May 16, 2019, 08:10:39 pm »
Well, unless someone has a real solution to why SynEdit doesn't save exactly like Notepad, then this project is basically dead for now, at least as far as using SynEdit.

%)
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: SynEdit for Batch Files
« Reply #21 on: May 16, 2019, 10:43:59 pm »
Stop lamenting, don't blame SynEdit for not using your brain:

SynEdit is a Lazarus control, it uses UTF-8 encoding. Your batch file is encoded as ANSI, but not in one of the Windows code pages because they don't have the line-draw characters. Open the Lazarus character table ("Edit" > "Insert from character map") and switch to tab "ANSI". On my system the combobox shows that the character table is encoded as CP1252, yours is maybe different, but most probably you will not see the double line character in the cell at row 192 and column 13 - this is where the 'Í' sits. But when you scroll the combobox down to item "CP 437 - Original IBM PC Hardware" the line characters will be there, and the 'Í' will have converted to a '═'!

From this you learn that the line characters are on CP437.

So, all you have to do is: after reading the batch file you must convert the text from CP437 to UTF8 (use function CP437ToUTF8 in unit LConvEncoding), and SynEdit (or any other Lazarus control) will show the double-line characters.

Of course, after you edited the text in SynEdit and want to save the changed version you must convert back to CP437 before saving (UTF8ToCP437)

What I want to tell you is that when there are encoding issues for a particular file the first thing you have to do is to find out the encoding of the file instead of blindly trying code that you've seen somewhere. Use your brain!
« Last Edit: May 16, 2019, 10:49:52 pm by wp »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: SynEdit for Batch Files
« Reply #22 on: May 17, 2019, 02:08:03 am »
@WP,

 Thanks for mentioning those TABLES, I've never investigated that option in Lazarus. I generally only  look for
options I need at the time but that is a great little converter util  :)
The only true wisdom is knowing you know nothing

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: SynEdit for Batch Files
« Reply #23 on: May 17, 2019, 02:15:13 am »
@WP... thanks for your help.  ::)

Unfortunately I am treading on uncharted waters with all this UTF-8/Ansi/Unicode and stuff.
Remember, I have only been LAZ'in for two years now, and I have mostly just been doing regular characters.

I read the two wiki pages that were suggested, but it was so complicated and all over the place that I couldn't figure what I was supposed to do. Let alone know what to code without example code.

I didn't make SynEdit, so I do not know what it supports or don't... and don't forget... SynEdits documentation plain out rots because there is none.

Its hard to use my brain, when there ain't good info to learn.

Its the same concept as me trying to fly a plane without learning how the airplane flies.

Can't be done unless you have something to read and learn and be taught.

So, I downloaded your ZIp file, but it is missing Form1

If you don't mind, I would appreciate it if I could get that

 :D

« Last Edit: May 17, 2019, 01:00:49 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: SynEdit for Batch Files
« Reply #24 on: May 17, 2019, 03:38:22 am »
Hmm
Looks simple to me, I ran some test here...
Code: Pascal  [Select][+][-]
  1. // Include "LconvEncoding" in the uses list.
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. Var
  4.   S:TStringList;
  5. begin
  6.    S := TStringList.Create;
  7.    S.Text := UTF8ToCP437(SynEdit1.Lines.Text);
  8.    Caption := S.Text;
  9.    //S.SaveTofile(....); // Save here.
  10.    S.Free;
  11. end;                                
  12.  

That is Code page 437, the old IBM page but I am not so sure your console is showing this?
to load the file you do the opposite.

 To get the IBM chars, use the ALT+KeyCode in the synEDit, that will generate  a UTF8..

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   S:TStringList;
  4. begin
  5.   S := TstringList.Create;
  6.   //S.LoadFromFIle(....);
  7.   S.Text := 'Test Line'+#185; //Fake a load from a CP437 file
  8.   SynEdit1.Text := CP437ToUTF8(S.Text); // a Test line.
  9.   S.free;
  10. end;                                      
  11.  
« Last Edit: May 17, 2019, 03:46:41 am by jamie »
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: SynEdit for Batch Files
« Reply #25 on: May 17, 2019, 04:26:51 am »
That is Code page 437, the old IBM page but I am not so sure your console is showing this?

The OP is in "Sabattus, ME", which I assume means "Sabattus, Maine, USA", so it's not only possible but very probable that his console is set to CP437.

Anyway, ConsoleToUTF8() (and viceversa, from LazUTF8) should be doing the trick nicely; if it's not then there is a bug somewhere, either in his code or in Lazarus'.

ETA: I couldn't sleep (again! :() so I've made a small test/demo of how it may be done.

The "meat" of it is in the unit batfiles.pas which takes care of loading/saving any text file encoded in the console code-page. The rest (in the main form) is mostly window dressing ... except the part that shows (in the status bar) what exactly Lazarus thinks is your console's CP.

I couldn't test if it works because I'm in Linux (i.e. my console is UTF8 so nothing happens) but you can use it at least to see if the proposed solution works for you.

HTH!
« Last Edit: May 17, 2019, 06:08:37 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: SynEdit for Batch Files
« Reply #26 on: May 17, 2019, 09:38:19 am »
So, I downloaded your ZIp file, but it is missing Form1
Sorry. I work with Laz trunk which uses a different file format in some places, and I always forget to save projects in the old format. In the attachment, there is the corrected project.

Execuse my arrogant tone in the previous message.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: SynEdit for Batch Files
« Reply #27 on: May 17, 2019, 12:43:05 pm »
Okay.. will give this a shot.

So, yes... my code is cp1252

Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: SynEdit for Batch Files
« Reply #28 on: May 17, 2019, 01:22:41 pm »
Okay... it all works!!!

Wow... this was a big learning experience.
I didn't know about the "edit/insert char map, the whole code page based on location thing."

A lot to take in.

It may seem simple, but to a newbie there is so many little parts to sort out.

THANK YOU TO EVERYONE FOR YOUR HELP!!!
 O:-)
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

 

TinyPortal © 2005-2018