| 1 |
{ |
| 2 |
Copyright 1999-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Various basic utilities. |
| 17 |
|
| 18 |
@unorderedList( |
| 19 |
@item(Lists, using generics derived from standard Generics.Collections. |
| 20 |
Lists of primitives, like @link(TIntegerList) and @link(TFloatList), |
| 21 |
and a helpful class to define a list of structures @link(TStructList).) |
| 22 |
|
| 23 |
@item(Basic operations on numbers (compare, randomize).) |
| 24 |
|
| 25 |
@item(Some OS-dependent things.) |
| 26 |
|
| 27 |
@item(Some compatibility things, to plug the differences between |
| 28 |
various versions of FPC and Delphi.) |
| 29 |
|
| 30 |
@item(Filenames operations. They do not touch actual files |
| 31 |
(operations on files are in @link(CastleFilesUtils).)) |
| 32 |
) |
| 33 |
|
| 34 |
This unit is a bag for simple and useful things. |
| 35 |
Let's not overload this unit with too many trivial utilities. |
| 36 |
When adding new utilities, first consider more specific units, |
| 37 |
like @link(CastleFilesUtils), @link(CastleStringUtils) and @link(CastleClassUtils). |
| 38 |
This unit does not depend on the standard @code(Classes) unit (use @link(CastleClassUtils) |
| 39 |
for utilities on top of @code(Classes)). |
| 40 |
|
| 41 |
Initialization of this unit does some generally-useful things: |
| 42 |
|
| 43 |
@unorderedList( |
| 44 |
@item(Makes AnsiString have UTF-8 encoding when compiled with FPC. |
| 45 |
|
| 46 |
See @url(https://castle-engine.io/coding_conventions#_most_code_should_use_just_string_and_be_prepared_that_it_is_8_bit_on_fpc_and_16_bit_on_delphi_only_if_writing_to_stream_explicitly_use_8_bit_ansistring_in_usual_case_when_you_write_utf_8 |
| 47 |
more docs about our approach to String handling in CGE, for both FPC and Delphi). |
| 48 |
Basically: |
| 49 |
|
| 50 |
@unorderedList( |
| 51 |
@item With FPC, we follow Lazarus convention, and String = AnsiString (8-bit) = contains UTF-8. |
| 52 |
|
| 53 |
@item With Delphi, we follow Delphi convention, and String = UnicodeString (16-bit) = contains UTF-16. |
| 54 |
) |
| 55 |
|
| 56 |
This way you can just use String throughout your code, and everything will just work. |
| 57 |
|
| 58 |
This way your applications will behave the same, whether they use Delphi, |
| 59 |
or FPC with LCL (which happens if you use TCastleControl on Lazarus form), |
| 60 |
or FPC without LCL (which happens if you use TCastleWindow with FPC). |
| 61 |
|
| 62 |
Some code in CGE has to assume that String is encoded like this -- e.g. TCastleAbstractFont |
| 63 |
iterates over Unicode characters in a String, so it assumes that the encoding is as above, |
| 64 |
for FPC and Delphi. |
| 65 |
) |
| 66 |
) |
| 67 |
} |
| 68 |
|
| 69 |
unit CastleUtils; |
| 70 |
|
| 71 |
{$I castleconf.inc} |
| 72 |
|
| 73 |
interface |
| 74 |
|
| 75 |
uses |
| 76 |
{$ifdef MSWINDOWS} Windows, {$ifndef FPC} ShlObj, {$endif} {$endif} |
| 77 |
{$ifdef UNIX} {$ifdef FPC} BaseUnix, Unix, Dl, {$else} Posix.Unistd, {$endif} {$endif} |
| 78 |
{$ifndef FPC} Classes, {$endif} |
| 79 |
Variants, SysUtils, Math, Generics.Collections; |
| 80 |
|
| 81 |
{$define read_interface} |
| 82 |
|
| 83 |
{ include everything } |
| 84 |
|
| 85 |
{$I castleutils_types.inc} |
| 86 |
{$I castleutils_delphi_compatibility.inc} |
| 87 |
{$I castleutils_platform.inc} |
| 88 |
{$I castleutils_miscella.inc} |
| 89 |
{$I castleutils_struct_list.inc} |
| 90 |
{$I castleutils_primitive_lists.inc} |
| 91 |
{$I castleutils_program_exit.inc} |
| 92 |
{$ifdef UNIX} {$I castleutils_os_specific_unix.inc} {$endif} |
| 93 |
{$ifdef MSWINDOWS} {$I castleutils_os_specific_windows.inc} {$endif} |
| 94 |
{$I castleutils_math.inc} |
| 95 |
{$I castleutils_filenames.inc} |
| 96 |
{$I castleutils_pointers.inc} |
| 97 |
{$I castleutils_read_write.inc} |
| 98 |
|
| 99 |
{$undef read_interface} |
| 100 |
|
| 101 |
implementation |
| 102 |
|
| 103 |
{$define read_implementation} |
| 104 |
|
| 105 |
{$I castleutils_types.inc} |
| 106 |
{$I castleutils_delphi_compatibility.inc} |
| 107 |
{$I castleutils_platform.inc} |
| 108 |
{$I castleutils_miscella.inc} |
| 109 |
{$I castleutils_struct_list.inc} |
| 110 |
{$I castleutils_primitive_lists.inc} |
| 111 |
{$I castleutils_program_exit.inc} |
| 112 |
{$I castleutils_math.inc} |
| 113 |
{$I castleutils_filenames.inc} |
| 114 |
|
| 115 |
{ We cannot just have |
| 116 |
windows/castleutils_os_specific.inc and |
| 117 |
unix/castleutils_os_specific.inc (same filename on all platforms), |
| 118 |
and depend on paths to choose proper one: |
| 119 |
For Lazarus package this would prevent maintaining single .lpk file, |
| 120 |
see ../packages/README. } |
| 121 |
{$ifdef UNIX} {$I castleutils_os_specific_unix.inc} {$endif} |
| 122 |
{$ifdef MSWINDOWS} {$I castleutils_os_specific_windows.inc} {$endif} |
| 123 |
|
| 124 |
{$I castleutils_pointers.inc} |
| 125 |
{$I castleutils_read_write.inc} |
| 126 |
|
| 127 |
{$undef read_implementation} |
| 128 |
|
| 129 |
initialization |
| 130 |
InitializationOSSpecific; |
| 131 |
|
| 132 |
{ CASTLE_TEST_DECIMAL_SEPARATOR_COMMA should only be defined for test |
| 133 |
purposes. It allows to deliberately test CGE behavior by simulating |
| 134 |
a system where the DecimalSeparator is a comma, not a dot. |
| 135 |
E.g. FPC on Windows or Delphi on Windows with "Regional settings" to Polish |
| 136 |
look like this. |
| 137 |
This forces CGE to use things like FormatDot instead of Format carefully. |
| 138 |
|
| 139 |
Never define CASTLE_TEST_DECIMAL_SEPARATOR_COMMA for a real application, |
| 140 |
in a real application you want the default format settings to reflect |
| 141 |
the user's preferences. } |
| 142 |
{$ifdef CASTLE_TEST_DECIMAL_SEPARATOR_COMMA} |
| 143 |
{$ifdef FPC} DefaultFormatSettings {$else} FormatSettings {$endif} |
| 144 |
.DecimalSeparator := ','; |
| 145 |
{$endif} |
| 146 |
|
| 147 |
{ FPC includes backslash in AllowDirectorySeparators also on non-Windows, |
| 148 |
so backslash will be considered as directory separator by |
| 149 |
Include/ExcludeTrailingPathDelimiter. This is IMHO very stupid, |
| 150 |
since normal OS routines on Unix *do not* consider backslash to be any |
| 151 |
special character in a filename, it certainly does not separate dirs. |
| 152 |
So Include/ExcludeTrailingPathDelimiter are basically buggy by default. |
| 153 |
|
| 154 |
Fortunately we can fix it by globally changing AllowDirectorySeparators. } |
| 155 |
{$ifdef FPC} |
| 156 |
{$ifndef MSWINDOWS} |
| 157 |
AllowDirectorySeparators := AllowDirectorySeparators - ['\']; |
| 158 |
{$endif} |
| 159 |
{$endif} |
| 160 |
|
| 161 |
{ Set UTF-8 in AnsiStrings, just like Lazarus |
| 162 |
(see initialization of lazarus/components/lazutils/fpcadds.pas in Lazarus sources) } |
| 163 |
SetMultiByteConversionCodePage(CP_UTF8); |
| 164 |
// SetMultiByteFileSystemCodePage(CP_UTF8); not needed, this is the default under Windows |
| 165 |
|
| 166 |
{$ifdef FPC} |
| 167 |
SetMultiByteRTLFileSystemCodePage(CP_UTF8); |
| 168 |
{$endif} |
| 169 |
|
| 170 |
{$ifndef FPC} |
| 171 |
DefaultFormatSettings := TFormatSettings.Create; |
| 172 |
{$endif} |
| 173 |
finalization |
| 174 |
FinalizationOSSpecific; |
| 175 |
end. |