Difference between revisions of "sysutils"

From Lazarus wiki
(expand)
(Caveats: give program code better title, allegedly insert new external links [spam detection false positive])
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{sysutils}}
 
{{sysutils}}
  
The [[Unit|unit]] '''<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight>''' shipped with the [[FPC|FPC’s]] default [[RTL|run-time library]] provides a plethora of system utilities.
+
The [[Unit|unit]] '''<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight>''' shipped with the [[FPC|FPC’s]] default [[RTL|run-time library]] provides many system utilities.
 
It attempts to be as compatible to [[Delphi|Delphi’s]] <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit as possible.
 
It attempts to be as compatible to [[Delphi|Delphi’s]] <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit as possible.
However, the FPC version is available on [[Platform list|all platforms the FPC supports]].
+
However, the FPC version is available on [[Platform list|all platforms that the FPC supports]].
You will not find any Windows-related routines or other highly platform-specific functionality.
+
It does not contain any Windows-related routines or other highly platform-specific functionality.
  
== notable functionality ==
+
== Notable functionality ==
 
* [[Format function|<syntaxhighlight lang="pascal" inline>format</syntaxhighlight>]]
 
* [[Format function|<syntaxhighlight lang="pascal" inline>format</syntaxhighlight>]]
 
* [[Executing External Programs#SysUtils.ExecuteProcess|<syntaxhighlight lang="pascal" inline>executeProcess</syntaxhighlight> for executing external programs]]
 
* [[Executing External Programs#SysUtils.ExecuteProcess|<syntaxhighlight lang="pascal" inline>executeProcess</syntaxhighlight> for executing external programs]]
* several routines making [[TDateTime|<syntaxhighlight lang="pascal" inline>tDateTime</syntaxhighlight>]] usable
+
* several routines ensuring productive use of [[TDateTime|<syntaxhighlight lang="pascal" inline>tDateTime</syntaxhighlight>]]
 
* {{Doc|package=RTL|unit=sysutils|identifier=typehelpers|text=type helpers for all basic data types}}
 
* {{Doc|package=RTL|unit=sysutils|identifier=typehelpers|text=type helpers for all basic data types}}
 +
* [[FreeAndNil|<syntaxhighlight lang="pascal" inline>freeAndNil</syntaxhighlight>]]
 +
* routines to access [[Command line parameters and environment variables#Environment variables|environment variables]]
  
== caveats ==
+
== Caveats ==
{{Doc|package=RTL|unit=system|identifier=runtimeerrors|text=If the <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit is included}}, all [[runtime error|run-time errors]] become [[Exceptions|exceptions]], thus virtually forcing you to use a [[Compiler Mode|compiler mode]] (or [[modeswitch|mode switch]]) that allows exception treatment.
+
{{Doc|package=RTL|unit=system|identifier=runtimeerrors|text=If the <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit is included}}, all [[runtime error|run-time errors]] become [[Exceptions|exceptions]], which virtually forces you to use a [[Compiler Mode|compiler mode]] (or [[modeswitch|mode switch]]) that allows exception treatment.
You will need to include <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> in every unit that may throw an exception just to familiarize with the exception classes, even though the unit itself does not use any actual code of it.
+
To catch an exception by its name you will need to include <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight>, even though the module itself does not use any of the included system utilities.
  
If [[Size Matters|size matters]], using <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> is by design not the smartest choice.
+
Changing run-time errors to exceptions has a ''global'' effect.
 +
The following program will terminate with an uncaught ''exception'', even though it does not list <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> in its <syntaxhighlight lang="delphi" inline>uses</syntaxhighlight>-clause:
 +
The <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit is ''implicitly'' included via the {{Doc|package=RTL|unit=strutils|text=<syntaxhighlight lang="pascal" inline>strUtils</syntaxhighlight> unit}}:
 +
<syntaxhighlight lang="pascal">
 +
program implicitSysUtilsCaveat(input, output, stdErr);
 +
uses
 +
strUtils;
 +
var
 +
x: file of char;
 +
c: char;
 +
begin
 +
// deliberately cause an error for demonstration purposes
 +
read(x, c);
 +
end.
 +
</syntaxhighlight>
  
== see also ==
+
Also, if [[Size Matters|size matters]], using <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> is by design not the smartest choice.
 +
 
 +
== See also ==
 
* {{Doc|package=RTL|unit=sysutils|text=<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> reference}}
 
* {{Doc|package=RTL|unit=sysutils|text=<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> reference}}
 
* [http://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils Delphi’s <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit]
 
* [http://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils Delphi’s <syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit]
 
* [[libc library|<syntaxhighlight lang="c" inline>libc</syntaxhighlight> library]] <!-- similar in design -->
 
* [[libc library|<syntaxhighlight lang="c" inline>libc</syntaxhighlight> library]] <!-- similar in design -->
 
* [[System unit|<syntaxhighlight lang="pascal" inline>system</syntaxhighlight> unit]]
 
* [[System unit|<syntaxhighlight lang="pascal" inline>system</syntaxhighlight> unit]]

Latest revision as of 18:48, 6 February 2021

English (en) français (fr)

The unit sysUtils shipped with the FPC’s default run-time library provides many system utilities. It attempts to be as compatible to Delphi’s sysUtils unit as possible. However, the FPC version is available on all platforms that the FPC supports. It does not contain any Windows-related routines or other highly platform-specific functionality.

Notable functionality

Caveats

If the sysUtils unit is included, all run-time errors become exceptions, which virtually forces you to use a compiler mode (or mode switch) that allows exception treatment. To catch an exception by its name you will need to include sysUtils, even though the module itself does not use any of the included system utilities.

Changing run-time errors to exceptions has a global effect. The following program will terminate with an uncaught exception, even though it does not list sysUtils in its uses-clause: The sysUtils unit is implicitly included via the strUtils unit:

program implicitSysUtilsCaveat(input, output, stdErr);
uses
	strUtils;
var
	x: file of char;
	c: char;
begin
	// deliberately cause an error for demonstration purposes
	read(x, c); 
end.

Also, if size matters, using sysUtils is by design not the smartest choice.

See also