summaryrefslogtreecommitdiff
path: root/plugins/Utils.pas/inouttext.pas
blob: d4013a815e4af5d609eb17821c38b1df0b6e9a54 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
unit inouttext;

interface

type
  TTextExport = class
    private
      FCurrent:PAnsiChar;
      FItems  :array of PAnsiChar;
      FItem   :integer;
      FIndent :integer;
      FLineLen:integer;
      FDoTab  :boolean;

      function AddTab:boolean;
      function GetItems(i:integer):PAnsiChar;

    public
      constructor Create(cnt:integer);
      destructor  Destroy; override;

      function  NextItem:integer;
      procedure EndItem;
      procedure addText (name:PAnsichar; value:PAnsiChar);
      procedure addTextW(name:PAnsichar; value:PWideChar);
      procedure addDWord(name:PAnsichar; value:cardinal);
      procedure addFlag (name:PAnsichar; value:boolean=true);
      procedure addNewLine;
      procedure ShiftRight;
      procedure ShiftLeft;

      property Items[i:integer]:PAnsiChar read GetItems;
  end;

implementation

uses Common;

const
  BUFSIZE = 65536;

constructor TTextExport.Create(cnt:integer);
begin
  SetLength(FItems,cnt);
  FIndent :=0;
  FDoTab  :=true;
  FLineLen:=0;
  FItem   :=-1;
end;

destructor TTextExport.Destroy;
var
  i:integer;
begin
  for i:=0 to HIGH(FItems) do
    mFreeMem(FItems[i]);
  FItems:=nil;

  inherited;
end;

function TTextExport.GetItems(i:integer):PAnsiChar;
begin
  if (i>=0) and (i<HIGH(FItems)) then
    result:=FItems[i]
  else
    result:=nil;
end;

function TTextExport.NextItem:integer;
begin
  inc(FItem);
  mGetMem(FItems[FItem],BUFSIZE);
  FCurrent:=FItems[FItem];
  result:=FItem;
end;

procedure TTextExport.EndItem;
var
  p:PAnsiChar;
begin
  if FItem>=0 then
  begin
    FCurrent^:=#0;
    p:=FItems[FItem];
    StrDup(FItems[FItem],p);
    mFreeMem(p);
  end;
end;

function TTextExport.AddTab:boolean;
var
  i:integer;
begin
  result:=FDoTab;
  if FDoTab then
  begin
    for i:=0 to FIndent-1 do
    begin
      FCurrent^:=' '; inc(FCurrent); inc(FLineLen);
    end;
    FDoTab:=false;
  end;
end;

procedure TTextExport.addText(name:PAnsichar; value:PAnsiChar);
begin
  if not AddTab then
    if (FCurrent-1)^ <> #10 then //!!
    begin
      FCurrent^:=' '; inc(FCurrent); inc(FLineLen);
    end;

  FCurrent:=StrCopyE(FCurrent,name); FCurrent^:='='; inc(FCurrent); inc(FLineLen, StrLen(name)+1);
  // check and make borders ', " or []
  FCurrent^:='"'; inc(FCurrent); inc(FLineLen);
  // delim-start
  FCurrent:=StrCopyE(FCurrent,value);
  // delim-end
  FCurrent^:='"'; inc(FCurrent); inc(FLineLen);
end;

procedure TTextExport.addTextW(name:PAnsichar; value:PWideChar);
var
  pc:PAnsiChar;
begin
  if not AddTab then
    if (FCurrent-1)^ <> #10 then //!!
    begin
      FCurrent^:=' '; inc(FCurrent); inc(FLineLen);
    end;

  FCurrent:=StrCopyE(FCurrent,name); FCurrent^:='='; inc(FCurrent); inc(FLineLen, StrLen(name)+1);
  // check and make borders ', " or []
  FCurrent^:='"'; inc(FCurrent); inc(FLineLen);
  // delim-start
  FCurrent:=StrCopyE(FCurrent,WideToUTF8(value, pc));
  // delim-end
  FCurrent^:='"'; inc(FCurrent); inc(FLineLen);

  mFreeMem(pc);
end;

procedure TTextExport.addDWord(name:PAnsichar; value:cardinal);
var
  p:PAnsiChar;
begin
  if not AddTab then
  begin
    FCurrent^:=' '; inc(FCurrent); inc(FLineLen);
  end;
  FCurrent:=StrCopyE(FCurrent,name);
  inc(FLineLen,StrLen(name));
  FCurrent^:='='; inc(FCurrent); inc(FLineLen);
  p:=IntToStr(FCurrent,value);
  inc(FLineLen,StrLen(p));
  FCurrent:=StrEnd(p);
end;

procedure TTextExport.addFlag(name:PAnsichar; value:boolean=true);
begin
  // if set "name=0/1", change here
  if value then
  begin
    if not AddTab then
    begin
      FCurrent^:=' '; inc(FCurrent); inc(FLineLen);
    end;
    FCurrent:=StrCopyE(FCurrent,name);
    inc(FLineLen,StrLen(name));
  end;
end;

procedure TTextExport.addNewLine;
begin
  FCurrent^:=#13; inc(FCurrent);
  FCurrent^:=#10; inc(FCurrent);
  FDoTab  :=true;
  FLineLen:=0;
end;

procedure TTextExport.ShiftRight;
begin
  inc(FIndent,2);
end;

procedure TTextExport.ShiftLeft;
begin
  dec(FIndent,2);
  if FIndent<0 then
    FIndent:=0;
end;

end.