Autor Beitrag
Nils:D
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 131

Debian, Win XP
Delphi 7 Arch.
BeitragVerfasst: Do 09.08.07 13:33 
Hi,

ich habe ein eigenes kleines Skriptformat und einen TreeView, der von vornherein 3 Einträge in der ersten Ebene hat.
Erklärung zum Skript:
ausblenden Quelltext
1:
[L=Sprache]					

Die 3 Nodes, welche von vornherein bestehen sind C, C++ und Pascal, daher gibt diese Angabe an, welcher der 3 genommen werden soll.

ausblenden Quelltext
1:
2:
3:
4:
#Template1 
{ 
  abcdef ghijkl 
}

# drückt aus, dass der Name der Vorlage in dieser Zeile steht. Die Codevorlage befindet sich logischerweise zwischen den geschweiften Klammern.

Kompletter Skript
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
[L=C]
#Template1
{
abcdef
ghijkl
}
#Template2
{
qrstuvwxyz
asdasdasda
}
[/L]
[L=C++]
#Cpp1
asd
#Cpp2
bla
[/L]


Problem
Nun kommen alle Vorlagen in den ersten Node, da for j := 0 to tv.Items.Count-1 do immer von vorne anfängt. Deshalb hatte ich vorrübergehend eine Sicherheitsvariable c eingeführt:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var c : Integer;
begin
  c := 0;
  for j := c to tv.Items.Count-1 do
  begin
    s := StringReplace(sl[j], ']', '', [rfReplaceAll]);
    s := Copy(s, 4, Length(s));
    if tv.Items[j].Text = s then
    begin
      up := tv.Items[j];
      inc(c);
      Break;
    end;
  end;

Warum hat die Variable keinerlei Auswirkungen im Vergleich zum kompletten Code ?

Kompletter Code
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:
type
  TVorgabe = class
  private
    sl : TStrings;
    ka : Integer; // {
    ke : Integer; // }
    tv : TTreeView;
    procedure FindeTemplates;
  public
    constructor Create(s : String; TreeView : TTreeView);
  end;

implementation

constructor TVorgabe.Create(s : String; TreeView : TTreeView);
begin
  sl := TStringList.Create;
  sl.LoadFromFile(s);

  tv := TreeView;
  
  FindeTemplates;
  ...
end;

procedure TVorgabe.FindeTemplates;
var i, j : Integer;
    up   : TTreeNode;    // Sprachnode (=Root-Node)
    s    : String;
begin
  ka := -1;
  ke := -1;
  for i := 0 to sl.Count-1 do
  begin
    if Copy(sl[i], 1, 3) = '[L=' then
    begin
      for j := 0 to tv.Items.Count-1 do
      begin
        s := StringReplace(sl[j], ']', '', [rfReplaceAll]);
        s := Copy(s, 4, Length(s));
        if tv.Items[j].Text = s then
        begin
          up := tv.Items[j];
          Break;
        end;
      end;
      
      for j := i+1 to sl.Count-1 do
      begin
        if sl[j] = '[/L]' then
          Break
        else if sl[j][1] = '{' then
          ka := j
        else if sl[j][1] = '}' then
          ke := j
        else if sl[j][1] = '#' then
          tv.Items.AddChild(up, Copy(sl[j], 2, Length(sl[j])));
      end;
    end;
  end;
end;
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 09.08.07 13:54 
Welche Auswirkung sollte "c" denn haben? :gruebel:
Kann sogar sein, das Delphi die komplett weg optimiert. Oder bin ich blind?
Nils:D Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 131

Debian, Win XP
Delphi 7 Arch.
BeitragVerfasst: Do 09.08.07 14:02 
Wenn eine Sprache gefunden und bearbeitet wurde, wird c erhöht uns somit sollte die entsprechende Sprache, da die Schleife erst später beginnt, ausgelassen werden.
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 09.08.07 14:06 
Du machst dafür zwar ein Break, aber setzt vor der Schleife c wieder auf "0". imho, kann das einfach keine Wirkung haben.

Durch das Break kommst Du zwar aus der Schleife raus, aber im Endeffekt fängst Du trotzdem wieder bei "0" an.
Nils:D Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 131

Debian, Win XP
Delphi 7 Arch.
BeitragVerfasst: Do 09.08.07 15:02 
Oh, da habe ich oben ein wenig geschlampt, ich denke, mit diesem Code sollte klar sein, wie ich es wirklich mit der c-Variable meinte:
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:
procedure TVorgabe.FindeTemplates;
var i, j, c : Integer;
    up   : TTreeNode;    // Sprachnode (=Root-Node)
    s    : String;
begin
  ka := -1;
  ke := -1;
  c  := 0;
  for i := 0 to sl.Count-1 do
  begin
    if Copy(sl[i], 1, 3) = '[L=' then
    begin
      for j := c to tv.Items.Count-1 do
      begin
        s := StringReplace(sl[j], ']', '', [rfReplaceAll]);
        s := Copy(s, 4, Length(s));
        if tv.Items[j].Text = s then
        begin
          up := tv.Items[j];
          inc(c);
          Break;
        end;
      end;
      
      for j := i+1 to sl.Count-1 do
      begin
        if sl[j] = '[/L]' then
          Break
        else if sl[j][1] = '{' then
          ka := j
        else if sl[j][1] = '}' then
          ke := j
        else if sl[j][1] = '#' then
          tv.Items.AddChild(up, Copy(sl[j], 2, Length(sl[j])));
      end;
    end;
  end;
end;
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: So 12.08.07 09:23 
Du kannst den Startwert der For-Schleife nicht zur Laufzeit (während die Schleife läuft) ändern. Dafür gibt es aber while-Schleifen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
X := 0;
while (X < Blup.Count) do
begin
  TueEtwas;
  if Erfolgreich then
    inc(X);

  inc(X);
end;

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)