Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Text parsing - need method (https://www.delphipraxis.net/172158-text-parsing-need-method.html)

WojTec 16. Dez 2012 16:56

Delphi-Version: 2010

Text parsing - need method
 
Sample:
Code:
ROWS 7
COLS 1
WIDTH 140
HEIGHT 30
TEXTHEIGHT 12
SPACING 1
R:247, G:224, B:23  HV:0.00, SV:0.00, VV:0.00  PANTONE Yellow C
R:247, G:217, B:23  HV:0.00, SV:0.00, VV:0.00  PANTONE Yellow 012 C
R:237, G:110, B:0  HV:0.00, SV:0.00, VV:0.00  PANTONE Orange 021 C
R:245, G:64, B:41  HV:0.00, SV:0.00, VV:0.00  PANTONE Warm Red C
R:237, G:46, B:56  HV:0.00, SV:0.00, VV:0.00  PANTONE Red 032 C
R:207, G:3, B:92  HV:0.00, SV:0.00, VV:0.00  PANTONE Rubine Red C
R:230, G:0, B:148  HV:0.00, SV:0.00, VV:0.00  PANTONE Rhodamine Red C
R:186, G:31, B:181  HV:0.00, SV:0.00, VV:0.00  PANTONE Purple C
R:102, G:0, B:161  HV:0.00, SV:0.00, VV:0.00  PANTONE Violet C
R:41, G:5, B:161  HV:0.00, SV:0.00, VV:0.00  PANTONE Blue 072 C
R:23, G:23, B:150  HV:0.00, SV:0.00, VV:0.00  PANTONE Reflex Blue C
I need to parse input in this format to get RGB and color name. Just question: do you know method how to get these data?

DeddyH 16. Dez 2012 17:18

AW: Text parsing - need method
 
Just split each line e.g. using a TStrings-object (have a look at the DelimitedText-property). If the resulting list contains more than 8 lines, it seems to be color-data, which you can process further.

Bummi 16. Dez 2012 17:30

AW: Text parsing - need method
 
Delphi-Quellcode:
Type
TR=Record
  R:byte;
  G:Byte;
  B:Byte;
  HV:Double;
  SV:Double;
  VV:Double;
  Panthone:String;
End;

Function GetInfos(Const s:String):TR;
var
 sl:TStringList;
 Function SaveStrToInt(const st:String):Integer;
  begin
    Result := StrToInt(TRIM(st));
  end;
begin
   sl:=TStringList.Create;
   try
     sl.Delimiter :=',';
     sl.StrictDelimiter := true;
     sl.DelimitedText :=StringReplace(
                        StringReplace(
                        StringReplace(
                        StringReplace(s,'HV:',',HV:',[])
                        ,'PANTONE ',',PANTONE=',[])
                        ,', ',',',[rfReplaceAll])
                        ,':','=',[rfReplaceAll]);
     Result.r := SaveStrToInt(sl.Values['R']);
     Result.g := SaveStrToInt(sl.Values['G']);
     Result.b := SaveStrToInt(sl.Values['B']);
     Result.HV := StrToFloat(StringReplace(sl.Values['HV'],'.',DecimalSeparator,[]));
     Result.SV := StrToFloat(StringReplace(sl.Values['SV'],'.',DecimalSeparator,[]));
     Result.VV := StrToFloat(StringReplace(sl.Values['VV'],'.',DecimalSeparator,[]));

     Result.Panthone := sl.Values['PANTONE'];

   finally
     sl.Free;
   end;
end;

use as

Delphi-Quellcode:
var
  r:TR;
  I: Integer;
begin
  for I := 0 to Yourlist.Count - 1 do
    if pos('R:',Yourlist[i])=1 then
       begin
        r := getInfos(Yourlist[i]);
       // work with r  
       end;

p80286 16. Dez 2012 18:06

AW: Text parsing - need method
 
If you don't want to use the "delimitedText" try this way:
Delphi-Quellcode:
{pseudocode}
for i:=0 to lines.count-1 do begin
  if (pos('R:',line)>0) and
     (pos('G:',line)>0) and
     (pos('B:',line)>0) and
     (pos('PANTONE',line)>0) then
   GetDatafromLine(line)
  else
    donothing;
end;
Greetings
K-H

WojTec 16. Dez 2012 18:52

Re: Text parsing - need method
 
Guys, 'PANTONE*' is color name ;) RGB is color, HSV are not used (in all files I saw), after HSV is color name :) Sorry, I should write it early ;)

DeddyH 16. Dez 2012 18:59

AW: Text parsing - need method
 
So what? Just ignore the values you do not need, and you are done.

Furtbichler 16. Dez 2012 19:02

AW: Text parsing - need method
 
I think a regular expression might be a simpler approach to get the relevant data.

WojTec 16. Dez 2012 19:55

Re: Text parsing - need method
 
Hm, regex, but what lib for that?


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:17 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz