» Help

Code Snippets Database

Control Box
 
 

Requested snippets

This page displays 4 requested snippets.

You can choose a category, perform a search or generate a Pascal unit containing the snippets by using the Control Box.

IsValidURLProtocol
Checks if the given URL is valid per RFC1738. Returns true if valid and false if not.
function IsValidURLProtocol(const URL: string): Boolean;
const
  Protocols: array[1..10] of string = (
    // Array of valid protocols - per RFC 1738
    'ftp://', 'http://', 'gopher://', 'mailto:', 'news:', 'nntp://',
    'telnet://', 'wais://', 'file://', 'prospero://'
  );
var
  I: Integer;   // loops thru known protocols
begin
  // Scan array of protocols checking for a match with start of given URL
  Result := False;
  for I := Low(Protocols) to High(Protocols) do
    if Pos(Protocols[I], SysUtils.LowerCase(URL)) = 1 then
    begin
      Result := True;
      Exit;
    end;
end;
Kind of Snippet: Routine
Required units: SysUtils.
Required snippets: None.
Supported Compilers:
 D2   D3   D4   D5   D6   D7  D2005 D2006 D2007 D2009 D2010  DXE  DXE2 Free
Pascal
Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Grey LED Grey LED Green LED
IsNumeric
Checks if a string is a numeric value. If AllowFloat is true then the string may contain a floating point number, otherwise it must be an integer. If TrimWhiteSpace is True any white space surrounding Value is trimmed before testing.
function IsNumeric(Value: string; const AllowFloat: Boolean;
  const TrimWhiteSpace: Boolean = True): Boolean;
var
  ValueInt: Int64;      // dummy integer value
  ValueFloat: Extended; // dummy float value
begin
  if TrimWhiteSpace then
    Value := SysUtils.Trim(Value);
  // Check for valid integer
  Result := SysUtils.TryStrToInt64(Value, ValueInt);
  if not Result and AllowFloat then
    // Wasn't valid as integer, try float
    Result := SysUtils.TryStrToFloat(Value, ValueFloat);
end;
Kind of Snippet: Routine
Required units: SysUtils.
Required snippets: None.
See also: None.
Supported Compilers:
 D2   D3   D4   D5   D6   D7  D2005 D2006 D2007 D2009 D2010  DXE  DXE2 Free
Pascal
Red LED Red LED Red LED Red LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Grey LED Green LED
Floating point numbers can be in normal or scientific notation. Integers can be expressed as decimal or hexadecimal numbers. Hex numbers must be prefixed by either '$' or '0x'.
Pause
Pauses for a specified number of milliseconds before returning. Performs a busy wait.
procedure Pause(const ADelay: LongWord);
var
  StartTC: Windows.DWORD;   // tick count when routine called
  CurrentTC: Int64;         // tick count at each loop iteration
begin
  StartTC := GetTickCount;
  repeat
    ProcessMessages;
    CurrentTC := Windows.GetTickCount;
    if CurrentTC < StartTC then
      // tick count has wrapped around: adjust it
      CurrentTC := CurrentTC + High(Windows.DWORD);
    Windows.Sleep(5);
  until CurrentTC - StartTC >= ADelay;
end;
Kind of Snippet: Routine
Required units: Windows.
Required snippets: ProcessMessages.
See also: Delay.
Supported Compilers:
 D2   D3   D4   D5   D6   D7  D2005 D2006 D2007 D2009 D2010  DXE  DXE2 Free
Pascal
Red LED Red LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Grey LED Grey LED Green LED
Modifications to handle potential clock roll over and sleeping suggested by Montor.
Delay
Delays processing for a given number of milliseconds without processing any messages.
procedure Delay(ADelay: Integer);
var
  StartTC: Windows.DWORD;   // tick count when routine called
  CurrentTC: Int64;         // tick count at each loop iteration
begin
  StartTC := Windows.GetTickCount;
  repeat
    CurrentTC := Windows.GetTickCount;
    if CurrentTC < StartTC then
      // tick count has wrapped around: adjust it
      CurrentTC := CurrentTC + High(Windows.DWORD);
  until CurrentTC - StartTC >= ADelay;
end;
Kind of Snippet: Routine
Required units: Windows.
Required snippets: None.
See also: Pause.
Supported Compilers:
 D2   D3   D4   D5   D6   D7  D2005 D2006 D2007 D2009 D2010  DXE  DXE2 Free
Pascal
Red LED Red LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Grey LED Grey LED Green LED
Modified in line with Pause routine per suggestions by Montor. This routine is aimed at console applications and threads that have no message queue. Normal GUI applications should not use this routine.