Subversion Repositories fastphp

Rev

Rev 22 | Rev 25 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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