Subversion Repositories fastphp

Rev

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