Recent

Author Topic: Linux programme - Pos worked, then later it did not  (Read 893 times)

stephanos

  • Jr. Member
  • **
  • Posts: 79
Linux programme - Pos worked, then later it did not
« on: September 17, 2025, 08:26:20 pm »
Dear All

I have a funny situation that has developed with Pos.  In a graphical programme I want to use it multiple times to find #10.  I am using a string that has received multiple lines of text from memo.text
Code: Pascal  [Select][+][-]
  1. MemoString := Memo1.Lines.Text;
The actual content is:
Quote
   Alasdair Roberts/Farewell Sorrow/Farewell Sorrow.mp3
    Didgeridoo Dreaming - Aboriginal Spiritual Music 1/The First Kangaroo.mp3
   Donald Fagen/The Nightfly/Track 02.mp3
   Agua de Pena\ROMARIAS\BAILE DA NOITE.mp3
Using Pos and searching on the #10 that must be present at the end of each line I can find and extract the first line of text
Tag := Pos(#10, MemoString); // Tag is 0 at start.  Pos returns zero if not found
path2MP3 := Copy(MemoString, 1, (Tag-1));

I concat an existing string with path2MP3
Code: Pascal  [Select][+][-]
  1. path2Musicfolder := concat(path2Musicfolder, path2MP3);
I have checked the output using a label caption and the content is correct
Code: Pascal  [Select][+][-]
  1. Label3.Caption := path2Musicfolder;
Output of:
“/media/stephanos/Sport Go/Music/Alasdair Roberts/Farewell Sorrow/Farewell Sorrow.mp3”

I became suspicious that I was not able to use a third party tool called BASS to retrieve the play time of this MP3 file.  So I did some checking.  I added a ‘[’ to the beginning of the file and a ‘]’ to the end and output to a label caption again.  The output was over 2 lines
Line 1:  [/media/stephanos/Sport Go/Music/Alasdair Roberts/Farewell Sorrow/Farewell Sorrow.mp3
Line 2: ]
Simple I thought, I still have a #10 at the end, and I can remove it, just as I did with MemoString.

First find the position of #10 in the string.  I tried 2 methods and got a return of zero for both.
Code: Pascal  [Select][+][-]
  1. Label3.Caption := IntToStr(Pos(#10, path2Musicfolder));
and
Code: Pascal  [Select][+][-]
  1. Tag := Pos(#10, path2Musicfolder);
  2. Label3.Caption := IntToStr(Tag);

This is very peculiar.  Pos could not find the #10 that has to be present.  It must be present because I had output over 2 lines.

Any help appreciated

stephanos

  • Jr. Member
  • **
  • Posts: 79
Re: Linux programme - Pos worked, then later it did not
« Reply #1 on: September 17, 2025, 08:28:42 pm »
Sorry too many errors in above I will repost

stephanos

  • Jr. Member
  • **
  • Posts: 79
Re: Linux programme - Pos worked, then later it did not
« Reply #2 on: September 17, 2025, 08:34:28 pm »
Dear All

I have a funny situation that has developed with Pos.  In a graphical programme I want to use it multiple times to find #10.  I am using a string that has received multiple lines of text from memo.text
Code: Pascal  [Select][+][-]
  1. MemoString := Memo1.Lines.Text;
The actual content is:
Quote
   Alasdair Roberts/Farewell Sorrow/Farewell Sorrow.mp3
    Didgeridoo Dreaming - Aboriginal Spiritual Music 1/The First Kangaroo.mp3
   Donald Fagen/The Nightfly/Track 02.mp3
   Agua de Pena\ROMARIAS\BAILE DA NOITE.mp3
Using Pos and searching on the #10 that must be present at the end of each line I can find and extract the first line of text
Quote
Tag := Pos(#10, MemoString); // Tag is 0 at start.  Pos returns zero if not found
path2MP3 := Copy(MemoString, 1, (Tag-1));

I concat an existing string with path2MP3
Code: Pascal  [Select][+][-]
  1. path2Musicfolder := concat(path2Musicfolder, path2MP3);
I have checked the output using a label caption and the content is correct
Code: Pascal  [Select][+][-]
  1. Label3.Caption := path2Musicfolder;
Output of:
Quote
   /media/stephanos/Sport Go/Music/Alasdair Roberts/Farewell Sorrow/Farewell Sorrow.mp3

I became suspicious that I was not able to use a third party tool called BASS to retrieve the play time of this MP3 file.  So I did some checking.  I added a ‘[’ to the beginning of the string and a ‘]’ to the end,and output to a label caption again.  The output was over 2 lines
Line 1:  [/media/stephanos/Sport Go/Music/Alasdair Roberts/Farewell Sorrow/Farewell Sorrow.mp3
Line 2: ]
Simple I thought, I still have a #10 at the end, and I can remove it using Pos.

First find the position of #10 in the string.  I tried 2 methods and got a return of zero for both.
Code: Pascal  [Select][+][-]
  1. Label3.Caption := IntToStr(Pos(#10, path2Musicfolder));
and
Code: Pascal  [Select][+][-]
  1. Tag := Pos(#10, path2Musicfolder);
  2. Label3.Caption := IntToStr(Tag);

This is very peculiar.  Pos could not find the #10 that has to be present.  It must be present because I had output over 2 lines.

Any help appreciated

cdbc

  • Hero Member
  • *****
  • Posts: 2464
    • http://www.cdbc.dk
Re: Linux programme - Pos worked, then later it did not
« Reply #3 on: September 17, 2025, 08:42:28 pm »
Hi
Just for fun -- Try looking for #13  ...maybe they be playing tricks on you  ;D
Another approach would be:
Code: Pascal  [Select][+][-]
  1. function PickFirstLineOf(const aStr: string): string;
  2. begin
  3.   if aStr = '' then exit('');
  4.   with TStringlist.Create do try
  5.     Text:= aStr;
  6.     Result:= Strings[0];
  7.   finally Free; end;
  8. end;
  9.  
...Just saying, 'Let the tools given to you, work for you  8)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

Bart

  • Hero Member
  • *****
  • Posts: 5612
    • Bart en Mariska's Webstek
Re: Linux programme - Pos worked, then later it did not
« Reply #4 on: September 17, 2025, 10:55:05 pm »
Why using Pos() to find the next line?
You already have Memo1.Lines.
Just iterate over them:
Code: Pascal  [Select][+][-]
  1.   for i := 0 to Memo1.Lines.Count-1 do
  2.     DoSomethingWith(Memo1.Lines[i]);
  3.  

Bart
« Last Edit: September 18, 2025, 10:39:40 am by Bart »

jamie

  • Hero Member
  • *****
  • Posts: 7302
Re: Linux programme - Pos worked, then later it did not
« Reply #5 on: September 17, 2025, 11:37:20 pm »
He's been told countless times to not use the "TEXT" like this.

 :o
The only true wisdom is knowing you know nothing

Thausand

  • Sr. Member
  • ****
  • Posts: 392
Re: Linux programme - Pos worked, then later it did not
« Reply #6 on: September 18, 2025, 01:37:06 pm »
When know then easy, when not know difficult  ;D

@stephanos
See no-print character difficult. Then choice not use print character for see.

Same is when copy-paste text. You write this is text you have in program (have copy in post) but is no true. Then other person not can help find error.

You also have describe what happen but situation not reproduce for other.

Then not find answer when ask help.

TMemo have property Lines (default property) type TStrings and is act same TStringList (is ancestor type TStrings), they same.

When use property TMemo Text then stuff happen that not know (I not want know because no relevant). I want know characters for Line and no Text.

When TMemo.Lines then all line is individual store/access and (when all good) have no non-print character. Character stop when Line break and break line not store in Line. Then no require search non-print character and make solution more easy.

Code: Pascal  [Select][+][-]
  1. ...
  2. var
  3.   index:integer;
  4.   songers:TStringList;
  5.   Path2musicfolder:string;
  6.   Song:string;
  7.  
  8. Path2musicfolder:='/media/stephanos/Sport Go/Music';
  9. songer:=TStringList.Create;
  10.  
  11. for index:=0 to Memo1.Lines.Count-1 do
  12. begin
  13.   // 1st: Path2Mp3:=Alasdair Roberts/Farewell Sorrow/Farewell Sorrow.mp3
  14.   Path2Mp3:=Memo1.Lines[index];
  15.  
  16.   // song  := '/media/stephanos/Sport Go/Music/Alasdair Roberts/Farewell Sorrow/Farewell Sorrow.mp3'
  17.   song:= ConcatPaths(Path2musicfolder, Path2Mp3);
  18.  
  19.   // if want make clean begin/end. Is alert when name have space or valid non-print begin/end
  20.   song:=trim(song);
  21.  
  22.   // add for songer, now have copy memo line
  23.   songer.Add(song);
  24. end;
  25.  
  26. // now have use songer
  27. for song in songer do
  28. var
  29.   p:string;
  30. begin
  31.   // have indicate #13 #10 when write
  32.   if song.contains(#13) or song.contains(#10) then p:='XXX ' else p := '';
  33.  
  34.   // write terminal if can
  35.   writeln(p + ' ' + song);
  36.  
  37.   // write memo for show
  38.   Memo1.Lines.Append(p + ' ' + song);
  39. end;
  40.  
  41. songer.Free;
  42. ...
  43.  

Can have other problem when unicode and problem path separator but is simple example for show how can make work. I not have all understand what explain do. Please have share work code when need help more.

I have make copy songer for lines TMemo but can have omit then use memo direct. I no understand explain and what is result want.
« Last Edit: September 18, 2025, 01:41:27 pm by Thausand »

stephanos

  • Jr. Member
  • **
  • Posts: 79
Re: Linux programme - Pos worked, then later it did not
« Reply #7 on: September 18, 2025, 08:59:58 pm »
Dear cdbc and all

Spot on.  #13, not #10 – Solved
Thanks
Case closed, but another coming soon

Fred vS

  • Hero Member
  • *****
  • Posts: 3715
    • StrumPract is the musicians best friend
Re: Linux programme - Pos worked, then later it did not
« Reply #8 on: September 19, 2025, 01:48:38 am »
Spot on.  #13, not #10 – Solved

Instead of #13 you may use lineending constant, it is cross-platform.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

ccrause

  • Hero Member
  • *****
  • Posts: 1080
Re: Linux programme - Pos worked, then later it did not
« Reply #9 on: September 19, 2025, 07:46:59 am »
Spot on.  #13, not #10 – Solved

Instead of #13 you may use lineending constant, it is cross-platform.
Since the OP is working with a TStrings decendant, rather use the linebreak property for internal consistency.

CM630

  • Hero Member
  • *****
  • Posts: 1522
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Linux programme - Pos worked, then later it did not
« Reply #10 on: September 19, 2025, 07:52:07 am »
...
Instead of #13 you may use lineending constant, it is cross-platform.
For TMemo it might be OS dependent, but I am pretty sure that for TRichMemo it is not. So if one decides to convert later, it might be a problem.


@stephanos, I avoid TStringList as much as possible, but it is an essential part of Lazarus (and maybe other languages), and sooner or later you will find out that you are going nowhere without it. I would advise you to learn how to use it.
The way you are handling the current situation (as many people already told you) is far from the best.
Лазар 4,2 32 bit (sometimes 64 bit); FPC3,2,2

 

TinyPortal © 2005-2018