~ubuntu-branches/ubuntu/utopic/mricron/utopic

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
{ ******************************************************************
                        Pascal string routines
  ****************************************************************** }

unit ustrings;

interface

uses
  utypes;

function LTrim(S : String) : String;
{ ------------------------------------------------------------------
  Removes leading blanks
  ------------------------------------------------------------------ }

function RTrim(S : String) : String;
{ ------------------------------------------------------------------
  Removes trailing blanks
  ------------------------------------------------------------------ }

function Trim(S : String) : String;
{ ------------------------------------------------------------------
  Removes leading and trailing blanks
  ------------------------------------------------------------------ }

function StrChar(N : Byte; C : Char) : String;
{ ------------------------------------------------------------------
  Returns a string made of character C repeated N times
  ------------------------------------------------------------------ }

function RFill(S : String; L : Byte) : String;
{ ------------------------------------------------------------------
  Completes string S with trailing blanks for a total length L
  ------------------------------------------------------------------ }

function LFill(S : String; L : Byte) : String;
{ ------------------------------------------------------------------
  Completes string S with leading blanks for a total length L
  ------------------------------------------------------------------ }

function CFill(S : String; L : Byte) : String;
{ ------------------------------------------------------------------
  Completes string S with leading blanks
  to center the string on a total length L
  ------------------------------------------------------------------ }

function Replace(S : String; C1, C2 : Char) : String;
{ ------------------------------------------------------------------
  Replaces in string S all the occurences
  of character C1 by character C2
  ------------------------------------------------------------------ }

function Extract(S : String; var Index : Byte; Delim : Char) : String;
{ ------------------------------------------------------------------
  Extracts a field from a string. Index is the position of the first
  character of the field. Delim is the character used to separate
  fields (e.g. blank, comma or tabulation). Blanks immediately
  following Delim are ignored. Index is updated to the position of
  the next field.
  ------------------------------------------------------------------ }

procedure Parse(S : String; Delim : Char; Field : PStrVector; var N : Byte);
{ ------------------------------------------------------------------
  Parses a string into its constitutive fields. Delim is the field
  separator. The number of fields is returned in N. The fields are
  returned in Field^[0]..Field^[N - 1]. Field must be dimensioned in
  the calling program.
  ------------------------------------------------------------------ }

procedure SetFormat(NumLength, MaxDec  : Integer;
                    FloatPoint, NSZero : Boolean);
{ ------------------------------------------------------------------
  Sets the numeric format

    NumLength  = Length of numeric field
    MaxDec     = Max. number of decimal places
    FloatPoint = True for floating point notation
    NSZero     = True to write non significant zero's
  ------------------------------------------------------------------ }

function FloatStr(X : Float) : String;
{ ------------------------------------------------------------------
  Converts a real to a string according to the numeric format
  ------------------------------------------------------------------ }

function IntStr(N : LongInt) : String;
{ ------------------------------------------------------------------
  Converts an integer to a string
  ------------------------------------------------------------------ }

function CompStr(Z : Complex) : String;
{ ------------------------------------------------------------------
  Converts a complex number to a string
  ------------------------------------------------------------------ }

implementation

const
  gNumLength  : Integer = 10;
  gMaxDec     : Integer =  4;
  gFloatPoint : Boolean = False;
  gNSZero     : Boolean = False;

  function LTrim(S : String) : String;
  begin
    if S <> '' then
      repeat
        if S[1] = ' ' then Delete(S, 1, 1);
      until S[1] <> ' ';
    LTrim := S;
  end;

  function RTrim(S : String) : String;
  var
    L1 : Byte;
  begin
    if S <> '' then
      repeat
        L1 := Length(S);
        if S[L1] = ' ' then Delete(S, L1, 1);
      until S[L1] <> ' ';
    RTrim := S;
  end;

  function Trim(S : String) : String;
  begin
    Trim := LTrim(RTrim(S));
  end;

  function StrChar(N : Byte; C : Char) : String;
  var
    I : Byte;
    S : String;
  begin
    S := '';
    for I := 1 to N do
      S := S + C;
    StrChar := S;
  end;

  function RFill(S : String; L : Byte) : String;
  var
    L1 : Byte;
  begin
    L1 := Length(S);
    if L1 >= L then
      RFill := S
    else
      RFill := S + StrChar(L - L1, ' ');
  end;

  function LFill(S : String; L : Byte) : String;
  var
    L1 : Byte;
  begin
    L1 := Length(S);
    if L1 >= L then
      LFill := S
    else
      LFill := StrChar(L - L1, ' ') + S;
  end;

  function CFill(S : String; L : Byte) : String;
  var
    L1 : Byte;
  begin
    L1 := Length(S);
    if L1 >= L then
      CFill := S
    else
      CFill := StrChar((L - L1) div 2, ' ') + S;
  end;

  function Replace(S : String; C1, C2 : Char) : String;
  var
    S1 : String;
    K : Byte;
  begin
    S1 := S;
    K := Pos(C1, S1);
    while K > 0 do
      begin
        S1[K] := C2;
        K := Pos(C1, S1);
      end;
    Replace := S1;
  end;

  function Extract(S : String; var Index : Byte; Delim : Char) : String;
  var
    I, L : Byte;
  begin
    I := Index;
    L := Length(S);

    { Search for Delim }
    while (I <= L) and (S[I] <> Delim) do
      Inc(I);

    { Extract field }
    if I = Index then
      Extract := ''
    else
      Extract := Copy(S, Index, I - Index);

    { Skip blanks after Delim }
    repeat
      Inc(I);
    until (I > L) or (S[I] <> ' ');

    { Update Index }
    Index := I;
  end;

  procedure Parse(S : String; Delim : Char; Field : PStrVector; var N : Byte);
  var
    I, Index, L : Byte;
  begin
    I := 0;
    Index := 1;
    L := Length(S);
    repeat
      Field^[I] := Extract(S, Index, Delim);
      Inc(I);
    until Index > L;
    N := I;
  end;

  procedure SetFormat(NumLength, MaxDec  : Integer;
                      FloatPoint, NSZero : Boolean);
  begin
    if (NumLength >= 1) and (NumLength <= 80) then gNumLength := NumLength;
    if (MaxDec    >= 0) and (MaxDec    <= 20) then gMaxDec    := MaxDec;

    gFloatPoint := FloatPoint;
    gNSZero     := NSZero;
  end;

  function RemZero(S : String) : String;
  var
    I      : Integer;
    S1, S2 : String;
    C      : Char;
  begin
    I := Pos('.', S);

    if I = 0 then
      begin
        RemZero := S;
        Exit
      end;

    I := Pos('E', S);
    if I = 0 then I := Pos('e', S);

    if I > 0 then
      begin
        S1 := Copy(S, 1, I - 1);
        S2 := Copy(S, I, Length(S) - I + 1)
      end
    else
      begin
        S1 := S;
        S2 := ''
      end;

    repeat
      I := Length(S1);
      C := S1[I];
      if (C = '0') or (C = '.') then S1 := Copy(S1, 1, I - 1)
    until C <> '0';

    RemZero := S1 + S2
  end;

  function FloatStr(X : Float) : String;
  var
    S : String;
  begin
    if gFloatPoint then
      begin
        Str(X:Pred(gNumLength), S);
        S := ' ' + S;
      end
    else
      Str(X:gNumLength:gMaxDec, S);

    if not gNSZero then
      S := RemZero(S);

    FloatStr := S;
  end;

  function IntStr(N : LongInt) : String;
  var
    S : String;
  begin
    Str(N:(gNumLength - gMaxDec - 1), S);
    IntStr := S;
  end;

  function CompStr(Z : Complex) : String;
  var
    S : String;
  begin
    if Z.Y >= 0.0 then S := ' + ' else S := ' - ';
    CompStr := FloatStr(Z.X) + S + FloatStr(Abs(Z.Y)) + ' * i';
  end;

end.