Autor Beitrag
Pepe
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 107

Win 98, Win 2000 Prof., Win XP Prof.
Delphi 2005 Prof.
BeitragVerfasst: So 08.01.06 19:06 
Hi, hab mal ein paar Funktionen geschrieben, die logische Such-Anfragen bearbeiten.
In guter Delphi manier muss dort natürlich alles umklammert werden...

z.B.: ((such1 AND such2) OR (such3))

Die ganzen Funktionen sind zum Durchsuchen von Strings gedacht!

benögtig wird zu dem die Explode-Funktion ausser FAQ :)

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
function check_klammern(ostr : String) : Boolean;
  var i,k : Integer;
  begin
      k := 0;
      for i := 1 to length(ostr) do
          begin
              if ostr[i] = '(' then k := k + 1;
              if ostr[i] = ')' then k := k - 1;
          end;
      if k = 0 then Result := True
          else Result := False;
      if ((Pos('(',ostr) = 0and (Pos(')',ostr) = 0)) then Result := False;
  end;

function log_suche_and(log_str, str : String) : Boolean;
  var log_str_parts : TStringDynArray;
      i : Integer;
  begin
      log_str_parts := Explode(' AND ',log_str);
      i := 0;
      Result := True;
      while ((i <= High(log_str_parts)) and (Result = True)) do
        begin
            if Pos(log_str_parts[i], str) = 0 then Result := False;
            i := i + 1;
        end;
  end;

function log_suche_or(log_str, str : String) : Boolean;
  var log_str_parts : TStringDynArray;
      i : Integer;
  begin
      //showmessage('asdf');
      log_str_parts := Explode(' OR ',log_str);
      i := 0;
      Result := False;
      while ((i <= High(log_str_parts)) and (Result = False)) do
        begin
            if Pos(log_str_parts[i], str) <> 0 then Result := True;
            i := i + 1;
        end;
  end;

function get_lgstr_part1(log_str : String) : Integer;
  var i : Integer;
  begin
      i := 1;
      while ((i <= length(log_str)) and ((copy(log_str,i,5) <> ' AND 'or (copy(log_str,i,4) <> ' OR ')) and (not check_klammern(copy(log_str,1,i)))) do
        i := i + 1;
      Result := i;
  end;

function get_lgstr_part2(log_str : String) : Integer;
  var i : Integer;
  begin
      i := length(log_str);
      while ((i >= 1and ((copy(log_str,i-5,5) <> ' AND 'or (copy(log_str,i-4,5) <> ' OR ')) and (not check_klammern(copy(log_str,i,length(log_str))))) do
        i := i - 1;
      Result := i;
  end;

function log_suche(log_str, str : String) : Boolean;
  var str_p1, str_p2 : Integer;
  begin
      if ((log_str[1] = '('or (log_str[length(log_str)] = ')')) then
        begin
            if log_str[1]='(' then log_str:=copy(log_str,2);
            if log_str[length(log_str)]=')' then log_str:=copy(log_str,1,length(log_str)-1);
        end;
      if ( Pos(' AND ',log_str) <> 0 ) and (Pos(' OR ',log_str) = 0 ) then
        Result := log_suche_and(log_str,str)
          else if ( Pos(' OR ',log_str) <> 0 ) and (Pos(' AND ',log_str) = 0 ) then
            Result := log_suche_or(log_str,str)
              else if ( Pos(' OR ',log_str) <> 0 ) and (Pos(' AND ',log_str) <> 0 ) then
                begin
                   str_p1 := get_lgstr_part1(log_str);
                   str_p2 := get_lgstr_part2(log_str);
                   Result:=False;
                   if copy(log_str,str_p1+1,4) = ' OR ' then
                      if (log_suche(copy(log_str,1,str_p1),str) or log_suche(copy(log_str,str_p2,length(log_str)),str)) then
                        Result := True
                          else Result := False;
                   if copy(log_str,str_p1+1,5) = ' AND ' then
                      if (log_suche(copy(log_str,1,str_p1),str) and log_suche(copy(log_str,str_p2,length(log_str)),str)) then
                        Result := True
                          else Result := False;
                end
                  else if Pos(log_str,str) <> 0 then
                    Result := True
                      else Result := False;

  end;


der Aufruf erfolglt mit:

ausblenden Delphi-Quelltext
1:
log_suche('((such1 AND such2) OR (such3))',String);					


der Rückgabewert ist vom Typ Boolean.

würde mich über Feadback sehr freuen!!!

Kritik und Anregungen zur Verbesserung bevorzugt ;)

MfG
Pepe