Subversion Repositories fastphp

Rev

Rev 8 | Rev 18 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
unit Functions;
2
 
3
interface
4
 
5
uses
6
  Windows, Messages, SysUtils, StrUtils, IniFiles, Classes, Forms, Variants, MsHTML,
8 daniel-mar 7
  SHDocVw_TLB, StdCtrls, SynEdit, ActiveX;
2 daniel-mar 8
 
9
function GetDosOutput(CommandLine: string; Work: string = ''): string;
10
function StrIPos(const SubStr, S: string): Integer;
11
function LoadFileToStr(const FileName: TFileName): AnsiString;
12
function LastPos(const SubStr, S: string): integer;
13
function IsTextHTML(s: string): boolean;
4 daniel-mar 14
function GetWordUnderCaret(AMemo: TSynEdit): string;
9 daniel-mar 15
function MyVarToStr(v: Variant): string;
2 daniel-mar 16
 
17
implementation
18
 
19
function GetDosOutput(CommandLine: string; Work: string = ''): string;
20
var
21
  SA: TSecurityAttributes;
22
  SI: TStartupInfo;
23
  PI: TProcessInformation;
24
  StdOutPipeRead, StdOutPipeWrite: THandle;
25
  WasOK: Boolean;
26
  Buffer: array[0..255] of AnsiChar;
27
  BytesRead: Cardinal;
28
  WorkDir: string;
29
  Handle: Boolean;
30
begin
31
  if Work = '' then Work := ExtractFilePath(ParamStr(0));
32
 
33
  Result := '';
34
  with SA do begin
35
    nLength := SizeOf(SA);
36
    bInheritHandle := True;
37
    lpSecurityDescriptor := nil;
38
  end;
39
  CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
40
  try
41
    with SI do
42
    begin
43
      FillChar(SI, SizeOf(SI), 0);
44
      cb := SizeOf(SI);
45
      dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
46
      wShowWindow := SW_HIDE;
47
      hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
48
      hStdOutput := StdOutPipeWrite;
49
      hStdError := StdOutPipeWrite;
50
    end;
51
    WorkDir := Work;
52
    Handle := CreateProcess(nil, PChar('cmd.exe /C "' + CommandLine + '"'),
53
                            nil, nil, True, 0, nil,
54
                            PChar(WorkDir), SI, PI);
55
    CloseHandle(StdOutPipeWrite);
56
    if Handle then
57
      try
58
        repeat
59
          WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
60
          if BytesRead > 0 then
61
          begin
62
            Buffer[BytesRead] := #0;
63
            Result := Result + Buffer;
64
          end;
65
        until not WasOK or (BytesRead = 0);
66
        WaitForSingleObject(PI.hProcess, INFINITE);
67
      finally
68
        CloseHandle(PI.hThread);
69
        CloseHandle(PI.hProcess);
70
      end;
71
  finally
72
    CloseHandle(StdOutPipeRead);
73
  end;
74
end;
75
 
76
function StrIPos(const SubStr, S: string): Integer;
77
begin
78
  Result := Pos(UpperCase(SubStr), UpperCase(S));
79
end;
80
 
81
function LoadFileToStr(const FileName: TFileName): AnsiString;
82
var
83
  FileStream : TFileStream;
84
 
85
begin
86
  Result:= '';
87
  FileStream:= TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
88
  try
89
    if FileStream.Size>0 then begin
90
      SetLength(Result, FileStream.Size);
91
      FileStream.Read(Result[1], FileStream.Size);
92
    end;
93
  finally
94
    FileStream.Free;
95
  end;
96
end;
97
 
98
function LastPos(const SubStr, S: string): integer;
99
var
100
  I, J, K: integer;
101
begin
102
  Result := 0;
103
  I := Length(S);
104
  K := Length(SubStr);
105
  if (K = 0) or (K > I) then
106
    Exit;
107
  while (Result = 0) and (I >= K) do
108
  begin
109
    J := K;
110
    if S[I] = SubStr[J] then
111
    begin
112
      while (J > 1) and (S[I + J - K - 1] = SubStr[J - 1]) do
113
        Dec(J);
114
      if J = 1 then
115
        Result := I - K + 1;
116
    end;
117
    Dec(I);
118
  end;
119
end;
120
 
121
function IsTextHTML(s: string): boolean;
122
 
123
  function _Tag(const tag: string): integer;
124
  begin
125
    result := 0;
126
    if (StrIPos('<'+tag+'>', s) > 0) then Inc(result);
127
    if (StrIPos('</'+tag+'>', s) > 0) then Inc(result);
128
    if (StrIPos('<'+tag+' />', s) > 0) then Inc(result);
129
    if (StrIPos('<'+tag+' ', s) > 0) then Inc(result);
130
  end;
131
 
132
var
133
  score: integer;
134
begin
135
  score := _Tag('html') + _Tag('body') + _Tag('p') + _Tag('a') + _Tag('b') +
136
           _Tag('i') + _Tag('u') + _Tag('li') + _Tag('ol') + _Tag('ul') +
137
           _Tag('img') + _Tag('div') + _Tag('hr') + _Tag('code') +
138
           _Tag('pre') + _Tag('blockquote') + _Tag('span');
139
  result := score >= 2;
140
end;
141
 
142
// Template: http://stackoverflow.com/questions/6339446/delphi-get-the-whole-word-where-the-caret-is-in-a-memo
4 daniel-mar 143
function GetWordUnderCaret(AMemo: TSynEdit): string;
2 daniel-mar 144
 
145
  function ValidChar(c: char): boolean;
146
  begin
8 daniel-mar 147
    result := CharInSet(c, ['a'..'z', 'A'..'Z', '0'..'9', '_']);
2 daniel-mar 148
  end;
149
 
150
var
151
   Line    : Integer;
152
   Column  : Integer;
153
   LineText: string;
154
   InitPos : Integer;
155
   EndPos  : Integer;
156
begin
157
   //Get the caret position
4 daniel-mar 158
   (*
159
   if AMemo is TMemo then
160
   begin
161
     Line   := AMemo.Perform(EM_LINEFROMCHAR,AMemo.SelStart, 0);
162
     Column := AMemo.SelStart - AMemo.Perform(EM_LINEINDEX, Line, 0);
163
   end;
164
   if AMemo is TSynEdit then
165
   begin
5 daniel-mar 166
   *)
4 daniel-mar 167
     Line := AMemo.CaretY-1;
168
     Column := AMemo.CaretX-1;
5 daniel-mar 169
   (*
4 daniel-mar 170
   end;
5 daniel-mar 171
   *)
4 daniel-mar 172
 
2 daniel-mar 173
   //Validate the line number
174
   if AMemo.Lines.Count-1 < Line then Exit;
175
 
176
   //Get the text of the line
177
   LineText := AMemo.Lines[Line];
178
 
5 daniel-mar 179
   if LineText = '' then exit('');
180
 
2 daniel-mar 181
   // Column zeigt auf das Zeichen LINKS vom Cursor!
182
 
183
   InitPos := Column;
184
   if not ValidChar(LineText[InitPos]) then Inc(InitPos);
185
   while (InitPos-1 >= 1) and ValidChar(LineText[InitPos-1]) do Dec(InitPos);
186
 
187
   EndPos := Column;
188
   while (EndPos+1 <= Length(LineText)) and ValidChar(LineText[EndPos+1]) do Inc(EndPos);
189
 
190
   //Get the text
191
   Result := Copy(LineText, InitPos, EndPos - InitPos + 1);
192
end;
193
 
9 daniel-mar 194
function MyVarToStr(v: Variant): string;
2 daniel-mar 195
var
9 daniel-mar 196
  _Lo, _Hi, i: integer;
2 daniel-mar 197
begin
9 daniel-mar 198
  if VarIsNull(v) then
199
  begin
200
    result := '';
201
  end
202
  else if VarIsArray(v) then
203
  begin
204
    _Lo := VarArrayLowBound(v, 1);
205
    _Hi := VarArrayHighBound(v, 1);
206
    result := '';
207
    for i := _Lo to _Hi do
208
    begin
209
      if v[i] = 0 then break;
210
      result := result + chr(integer(v[i]));
211
    end;
212
  end
213
  else
214
  begin
215
    // At least try it...
216
    result := VarToStr(v);
217
  end;
2 daniel-mar 218
end;
219
 
220
end.