File: syneditlines.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (428 lines) | stat: -rw-r--r-- 10,986 bytes parent folder | download | duplicates (6)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.

Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.

-------------------------------------------------------------------------------}

(*
  This is a wrapper around Synedits internal Storage. It allows TString based
  access to the Text.
  It relieves SynEditTextBase from having to support the full TStrings interface.
  It also hides all of the other methods that SynEditTextBase provides
*)

unit SynEditLines;

{$I synedit.inc}

interface

uses
  Classes, SysUtils, FileUtil, LazUtf8Classes, FPCAdds, SynEditTextBuffer;

type

  TSavedNotification = Procedure of Object;

  TSynLinesFileLineEndType =
    ( sfleSystem,
      sfleLoaded,
      sfleCrLf,
      sfleCr,
      sfleLf
    );

  { TSynEditLines }

  TSynEditLines = class(TStrings)
  private
    FFileLineEndType: TSynLinesFileLineEndType;
    FFileWriteLineEndType: TSynLinesFileLineEndType;
    FTextBuffer: TSynEditStringList;
    FOnSaved: TSavedNotification;
    function GetTextChangeStamp: int64;
  protected
    function Get(Index: integer): string; override;
    function GetCapacity: integer; override;
    function GetCount: integer; override;
    function GetObject(Index: integer): TObject; override;
    procedure Put(Index: integer; const S: string); override;
    procedure PutObject(Index: integer; AObject: TObject); override;
    procedure SetCapacity(NewCapacity: integer); override;
    procedure SetUpdateState(Updating: Boolean); override;
  public
    constructor Create(ATextBuffer: TSynEditStringList; OnSaved: TSavedNotification);
    function Add(const S: string): integer; override;
    procedure AddStrings(AStrings: TStrings); override;
    procedure Clear; override;
    procedure Delete(Index: integer); override;
    procedure Insert(Index: integer; const S: string); override;
    procedure Assign(Source: TPersistent); override;
    procedure LoadFromFile(const FileName: string); override;
    procedure SaveToFile(const FileName: string); override;
    property FileLineEndType: TSynLinesFileLineEndType read FFileLineEndType;
    property FileWriteLineEndType: TSynLinesFileLineEndType
             read FFileWriteLineEndType write FFileWriteLineEndType;
    property TextChangeStamp: int64 read GetTextChangeStamp;
  end;

implementation

type

{ TSynEditFiler }

  TSynEditFiler = class(TObject)
  private
    FLineEndString: String;
    FLineEndLen: Integer;
    FLineEndType: TSynLinesFileLineEndType;
    procedure SetLineEndType(const AValue: TSynLinesFileLineEndType);
  protected
    fBuffer: PChar;
    fBufPtr: Cardinal;
    fBufSize: Cardinal;
    fFiler: TFileStreamUtf8;
    procedure Flush; virtual;
    procedure SetBufferSize(NewSize: Cardinal);
  public
    constructor Create;
    destructor Destroy; override;
  public
    property LineEndType: TSynLinesFileLineEndType read FLineEndType write SetLineEndType;
  end;

constructor TSynEditFiler.Create;
const
  kByte = 1024;
begin
  inherited Create;
  LineEndType := sfleSystem;
  SetBufferSize(16 * kByte);
  fBuffer[0] := #0;
end;

destructor TSynEditFiler.Destroy;
begin
  Flush;
  fFiler.Free;
  SetBufferSize(0);
  inherited Destroy;
end;

procedure TSynEditFiler.SetLineEndType(const AValue: TSynLinesFileLineEndType);
begin
  FLineEndType := AValue;
  case FLineEndType of
    sfleCrLf: FLineEndString := #13#10;
    sfleCr:   FLineEndString := #13;
    sfleLf:   FLineEndString := #10;
    else
      FLineEndString := LineEnding;
  end;
  FLineEndLen := length(FLineEndString);
end;

procedure TSynEditFiler.Flush;
begin
end;

procedure TSynEditFiler.SetBufferSize(NewSize: Cardinal);
begin
  if NewSize <> fBufSize then begin
    ReallocMem(fBuffer, NewSize);
    fBufSize := NewSize;
  end;
end;

{ TSynEditFileReader }

type
  TSynEditFileReader = class(TSynEditFiler)
  protected
    fFilePos: TStreamSeekType;
    fFileSize: TStreamSeekType;
    procedure FillBuffer;
  public
    constructor Create(const FileName: string);
    function EOF: boolean;
    function ReadLine: string;
  end;

constructor TSynEditFileReader.Create(const FileName: string);
begin
  inherited Create;
  fFiler := TFileStreamUtf8.Create(FileName, fmOpenRead{ ToDo: or fmShareDenyWrite});
  fFileSize := fFiler.Size;
  fFiler.Seek(0, soFromBeginning);
end;

function TSynEditFileReader.EOF: boolean;
begin
  Result := (fBuffer[fBufPtr] = #0) and (fFilePos >= fFileSize);
end;

procedure TSynEditFileReader.FillBuffer;
var
  Count: Cardinal;
begin
  if fBufPtr >= fBufSize - 1 then
    fBufPtr := 0;
  Count := fFileSize - fFilePos;
  if Count >= fBufSize - fBufPtr then
    Count := fBufSize - fBufPtr - 1;
  fFiler.ReadBuffer(fBuffer[fBufPtr], Count);
  fBuffer[fBufPtr + Count] := #0;
  fFilePos := fFilePos + Count;
  fBufPtr := 0;
end;

function TSynEditFileReader.ReadLine: string;
var
  E, P, S: PChar;
begin
  Result := '';
  repeat
    S := PChar(@fBuffer[fBufPtr]);
    if S[0] = #0 then begin
      FillBuffer;
      S := PChar(@fBuffer[0]);
    end;
    E := PChar(@fBuffer[fBufSize]);
    P := S;
    while P + 2 < E do begin
      case P[0] of
        #10, #13:
          begin
            SetString(Result, S, P - S);
            // a single #13 is used in Mac OS files
            if (P[0] = #13) then begin
              if (P[1] = #10) then begin
                FLineEndType := sfleCrLf;
                inc(P);
              end
              else
                FLineEndType := sfleCr;
            end
            else
              FLineEndType := sfleLf;
            Inc(P);
            fBufPtr := P - fBuffer;
            exit;
          end;
        #0:
          if fFilePos >= fFileSize then begin
            fBufPtr := P - fBuffer;
            SetString(Result, S, P - S);
            exit;
          end;
      end;
      Inc(P);
    end;
    // put the partial string to the start of the buffer, and refill the buffer
    Inc(P);
    if S > fBuffer then
      StrLCopy(fBuffer, S, P - S);
    fBufPtr := P - S;
    fBuffer[fBufPtr] := #0;
    // if line is longer than half the buffer then grow it first
    if 2 * Cardinal(P - S) > fBufSize then
      SetBufferSize(fBufSize + fBufSize);
  until FALSE;
end;

{ TSynEditFileWriter }

type
  TSynEditFileWriter = class(TSynEditFiler)
  protected
    procedure Flush; override;
  public
    constructor Create(const FileName: string);
    procedure WriteLine(const S: string);
  end;

constructor TSynEditFileWriter.Create(const FileName: string);
begin
  inherited Create;
  fFiler := TFileStreamUtf8.Create(FileName, fmCreate);
  fFiler.Seek(0, soFromBeginning);
end;

procedure TSynEditFileWriter.Flush;
begin
  if fBufPtr > 0 then begin
    fFiler.WriteBuffer(fBuffer[0], fBufPtr);
    fBufPtr := 0;
  end;
end;

procedure TSynEditFileWriter.WriteLine(const S: string);
var
  L: Cardinal;
begin
  {$PUSH}{$HINTS OFF} // Lots of mixing signed/unsigned
  L := Length(S);
  repeat
    if fBufPtr + L + FLineEndLen <= fBufSize then begin
      if L > 0 then begin
        Move(S[1], fBuffer[fBufPtr], L);
        fBufPtr := fBufPtr + L;
      end;
      Move(FLineEndString[1], fBuffer[fBufPtr], FLineEndLen);
      Inc(fBufPtr, FLineEndLen);
      exit;
    end;
    Flush;
    if L + FLineEndLen > fBufSize then
      SetBufferSize(L + FLineEndLen);
  until FALSE;
  {$POP}
end;

{ TSynEditLines }

constructor TSynEditLines.Create(ATextBuffer: TSynEditStringList; OnSaved: TSavedNotification);
begin
  inherited Create;
  FTextBuffer := ATextBuffer;
  FFileWriteLineEndType := sfleSystem;
  FFileLineEndType := sfleSystem;
  FOnSaved := OnSaved;
end;

function TSynEditLines.GetTextChangeStamp: int64;
begin
  Result:=FTextBuffer.TextChangeStamp;
end;

function TSynEditLines.Get(Index: integer): string;
begin
  Result := FTextBuffer[Index];
end;

function TSynEditLines.GetCapacity: integer;
begin
  Result := FTextBuffer.Capacity;
end;

function TSynEditLines.GetCount: integer;
begin
  Result := FTextBuffer.Count;
end;

function TSynEditLines.GetObject(Index: integer): TObject;
begin
  Result := FTextBuffer.Objects[Index];
end;

procedure TSynEditLines.Put(Index: integer; const S: string);
begin
  FTextBuffer[Index] := S;
end;

procedure TSynEditLines.PutObject(Index: integer; AObject: TObject);
begin
  FTextBuffer.Objects[Index] := AObject;
end;

procedure TSynEditLines.SetCapacity(NewCapacity: integer);
begin
  FTextBuffer.Capacity := NewCapacity;
end;

procedure TSynEditLines.SetUpdateState(Updating: Boolean);
begin
  if Updating then
    FTextBuffer.BeginUpdate
  else
    FTextBuffer.EndUpdate;
end;

function TSynEditLines.Add(const S: string): integer;
begin
  Result := FTextBuffer.Add(S);
end;

procedure TSynEditLines.AddStrings(AStrings: TStrings);
begin
  FTextBuffer.AddStrings(AStrings);
end;

procedure TSynEditLines.Clear;
begin
  FTextBuffer.Clear;
end;

procedure TSynEditLines.Delete(Index: integer);
begin
  FTextBuffer.Delete(Index);
end;

procedure TSynEditLines.Insert(Index: integer; const S: string);
begin
  FTextBuffer.Insert(Index, S);
end;

procedure TSynEditLines.Assign(Source: TPersistent);
begin
  FTextBuffer.Assign(Source);
end;

procedure TSynEditLines.LoadFromFile(const FileName: string);
var
  Reader: TSynEditFileReader;
begin
  Reader := TSynEditFileReader.Create(FileName);
  try
    BeginUpdate;
    try
      Clear;
      while not Reader.EOF do
        Add(Reader.ReadLine);
    finally
      EndUpdate;
    end;
  finally
    FFileLineEndType := Reader.LineEndType;
    Reader.Free;
  end;
end;

procedure TSynEditLines.SaveToFile(const FileName: string);
var
  Writer: TSynEditFileWriter;
  i: integer;
begin
  Writer := TSynEditFileWriter.Create(FileName);
  try
    if FFileWriteLineEndType = sfleLoaded then
      Writer.LineEndType := FFileLineEndType
    else
      Writer.LineEndType := FFileWriteLineEndType;
    for i := 0 to Count - 1 do
      Writer.WriteLine(Get(i));
  finally
    Writer.Free;
  end;
  If Assigned(FOnSaved) then FOnSaved();
end;

end.