aboutsummaryrefslogtreecommitdiff
path: root/Source/Strz.pas
blob: 85ad4a7ba217fb50a0c1bae64945fe25fc060562 (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
unit Strz;

interface

uses
  Windows;

type
  CharSet = set of Char;

// General
function MyStrToInt(const S: String): Integer; // some special checking, no exceptions
function MyStrToFloat(const S: String): Extended; // some special checking, no exceptions
function StrToRect(const S: String): TRect;
function Long2Str(L : LongInt) : string;
function SearchAndReplace( S, Old, New:String ):String;
function WordCount(S : string; WordDelims : CharSet) : Integer;
  {-Given a set of word delimiters, return number of words in S}
function ExtractWord(N : Integer; S : string; WordDelims : CharSet) : string;
  {-Given a set of word delimiters, return the N'th word in S}

// Filez
function VerifyBackSlash( const FileName:String ):String;

implementation

uses
  Classes, SysUtils;

function StrToRect(const S: String): TRect;
var
  i: Integer;
begin
  Result := Rect(0, 0, 0, 0);
  if S='' then Exit;
  i := WordCount(S, [' ']);
  Result.Left := MyStrToInt(ExtractWord(1, S, [' ']));
  if i>1 then
    Result.Top := MyStrToInt(ExtractWord(2, S, [' ']));
  if i>2 then
    Result.Right := MyStrToInt(ExtractWord(3, S, [' ']));
  if i>3 then
    Result.Bottom := MyStrToInt(ExtractWord(4, S, [' ']));
end;

function MyStrToInt(const S: String): Integer;
var
  St: String;
begin
  Result := 0;
  if (S='') or (S='-') or (S='0') then Exit;
  try
    Result := StrToInt(S);
  except
    St := S;
    while Pos('.', St)>0 do
      St[Pos('.', St)] := ',';
    try Result := Trunc(StrToFloat(St)); except end;
  end;
end;

function MyStrToFloat(const S: String): Extended;
var
  St: String;
  i: Integer;
begin
  Result := 0;
  if (S='') or (S='-') or (S='0') then Exit;
  St := S;
  i := Pos('.', St);
  if i>0 then
    St[i] := ',';
  try
    Result := StrToFloat(St);
  except
  end;
end;

function VerifyBackSlash( const FileName:String ):String;
begin
  if FileName[Length(FileName)]='\' then
    Result := FileName
  else
    Result := FileName + '\';
end;

function Long2Str(L : LongInt) : string;
  {-Convert a long/word/integer/byte/shortint to a string}
var
  S : string;
begin
  Str(L, S);
  Long2Str := S;
end;

{-----}

  function WordCount(S : string; WordDelims : CharSet) : Integer;
    {-Given a set of word delimiters, return number of words in S}
  var
    {I,} Count : Integer;                       {!!.12}
    I, SLen : Word;                                {!!.12}
  begin
    Count := 0;
    I := 1;
    SLen:=Length(S);

    while I <= SLen do begin
      {skip over delimiters}
      while (I <= SLen) and (S[I] in WordDelims) do
        Inc(I);

      {if we're not beyond end of S, we're at the start of a word}
      if I <= SLen then
        Inc(Count);

      {find the end of the current word}
      while (I <= SLen) and not(S[I] in WordDelims) do
        Inc(I);
    end;

    WordCount := Count;
  end;

  function ExtractWord(N : Integer; S : string; WordDelims : CharSet) : string;
    {-Given a set of word delimiters, return the N'th word in S}
  var
    I : Word;                 {!!.12}
    Count, Len : Integer;
    SLen : Longint;
    Str : String;
  begin
    Count := 0;
    I := 1;
    Len := 0;
    Str := '';
    SLen := Length(S);

//    ExtractWord[0] := #0;

    while (I <= SLen) and (Count <> N) do begin
      {skip over delimiters}
      while (I <= SLen) and (S[I] in WordDelims) do
        Inc(I);

      {if we're not beyond end of S, we're at the start of a word}
      if I <= SLen then
        Inc(Count);

      {find the end of the current word}
      while (I <= SLen) and not(S[I] in WordDelims) do begin
        {if this is the N'th word, add the I'th character to Tmp}
        if Count = N then begin
          Inc(Len);
          SetLength(Str, Len);
//          ExtractWord[0] := Char(Len);
          Str[Len] := S[I];
//          ExtractWord[Len] := S[I];
        end;

        Inc(I);
      end;
    end;
    ExtractWord := Str;
  end;

function SearchAndReplace( S, Old, New:String ):String;
var
  Ts : String;
  i : Integer;
begin
  Ts := S;
  i := Pos(Old, Ts);
  while i>0 do begin
    Delete(Ts, i, Length(Old));
    Insert(New, Ts, i);
    i := Pos(Old, Ts);
  end;
  SearchAndReplace := Ts;
end;

end.