utility.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. {
  2. This file is part of the Free Pascal run time library.
  3. A file in Amiga system run time library.
  4. Copyright (c) 1998-2003 by Nils Sjoholm
  5. member of the Amiga RTL development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. History:
  14. Added functions and procedures with array of const.
  15. For use with fpc 1.0.7. Thay are in systemvartags.
  16. 11 Nov 2002.
  17. Added the define use_amiga_smartlink.
  18. 13 Jan 2003.
  19. Update for AmigaOS 3.9.
  20. Added a few overlays.
  21. 06 Feb 2003.
  22. [email protected]
  23. }
  24. {$PACKRECORDS 2}
  25. unit utility;
  26. INTERFACE
  27. uses exec;
  28. Type
  29. pClockData = ^tClockData;
  30. tClockData = record
  31. sec : Word;
  32. min : Word;
  33. hour : Word;
  34. mday : Word;
  35. month : Word;
  36. year : Word;
  37. wday : Word;
  38. END;
  39. pHook = ^tHook;
  40. tHook = record
  41. h_MinNode : tMinNode;
  42. h_Entry : Pointer; { assembler entry point }
  43. h_SubEntry : Pointer; { often HLL entry point }
  44. h_Data : Pointer; { owner specific }
  45. END;
  46. {
  47. * Hook calling conventions:
  48. * A0 - pointer to hook data structure itself
  49. * A1 - pointer to parameter structure ("message") typically
  50. * beginning with a longword command code, which makes
  51. * sense in the context in which the hook is being used.
  52. * A2 - Hook specific address data ("object," e.g, GadgetInfo)
  53. *
  54. * Control will be passed to the routine h_Entry. For many
  55. * High-Level Languages (HLL), this will be an assembly language
  56. * stub which pushes registers on the stack, does other setup,
  57. * and then calls the function at h_SubEntry.
  58. *
  59. * The C standard receiving code is:
  60. * CDispatcher( hook, object, message )
  61. * struct Hook *hook;
  62. * APTR object;
  63. * APTR message;
  64. *
  65. * NOTE that register natural order differs from this convention
  66. * for C parameter order, which is A0,A2,A1.
  67. *
  68. * The assembly language stub for "vanilla" C parameter conventions
  69. * could be:
  70. _hookEntry:
  71. move.l a1,-(sp) ; push message packet pointer
  72. move.l a2,-(sp) ; push object pointer
  73. move.l a0,-(sp) ; push hook pointer
  74. move.l h_SubEntry(a0),a0 ; fetch C entry point ...
  75. jsr (a0) ; ... and call it
  76. lea 12(sp),sp ; fix stack
  77. rts
  78. * with this function as your interface stub, you can write
  79. * a Hook setup function as:
  80. SetupHook( hook, c_function, userdata )
  81. struct Hook *hook;
  82. ULONG (*c_function)();
  83. VOID *userdata;
  84. ULONG (*hookEntry)();
  85. hook->h_Entry = hookEntry;
  86. hook->h_SubEntry = c_function;
  87. hook->h_Data = userdata;
  88. * with Lattice C pragmas, you can put the C function in the
  89. * h_Entry field directly if you declare the function:
  90. ULONG __saveds __asm
  91. CDispatcher( register __a0 struct Hook *hook,
  92. register __a2 VOID *object,
  93. register __a1 ULONG *message );
  94. *
  95. ***}
  96. { Namespace definitions }
  97. Type
  98. { The named object structure }
  99. pNamedObject = ^tNamedObject;
  100. tNamedObject = record
  101. no_Object : Pointer; { Your pointer, for whatever you want }
  102. END;
  103. const
  104. { Tags for AllocNamedObject() }
  105. ANO_NameSpace = 4000; { Tag to define namespace }
  106. ANO_UserSpace = 4001; { tag to define userspace }
  107. ANO_Priority = 4002; { tag to define priority }
  108. ANO_Flags = 4003; { tag to define flags }
  109. { Flags for tag ANO_Flags }
  110. NSB_NODUPS = 0;
  111. NSB_CASE = 1;
  112. NSF_NODUPS = 1; { Default allow duplicates }
  113. NSF_CASE = 2; { Default to caseless... }
  114. { Control attributes for Pack/UnpackStructureTags() }
  115. { PackTable definition:
  116. *
  117. * The PackTable is a simple array of LONGWORDS that are evaluated by
  118. * PackStructureTags() and UnpackStructureTags().
  119. *
  120. * The table contains compressed information such as the tag offset from
  121. * the base tag. The tag offset has a limited range so the base tag is
  122. * defined in the first longword.
  123. *
  124. * After the first longword, the fields look as follows:
  125. *
  126. * +--------- 1 = signed, 0 = unsigned (for bits, 1=inverted boolean)
  127. * |
  128. * | +------ 00 = Pack/Unpack, 10 = Pack, 01 = Unpack, 11 = special
  129. * | / \
  130. * | | | +-- 00 = Byte, 01 = Integer, 10 = Long, 11 = Bit
  131. * | | | / \
  132. * | | | | | /----- For bit operations: 1 = TAG_EXISTS is TRUE
  133. * | | | | | |
  134. * | | | | | | /-------------------- Tag offset from base tag value
  135. * | | | | | | | \
  136. * m n n o o p q q q q q q q q q q r r r s s s s s s s s s s s s s
  137. * \ | | |
  138. * Bit offset (for bit operations) ----/ | |
  139. * \ |
  140. * Offset into data structure -----------------------------------/
  141. *
  142. * A -1 longword signifies that the next longword will be a new base tag
  143. *
  144. * A 0 longword signifies that it is the end of the pack table.
  145. *
  146. * What this implies is that there are only 13-bits of address offset
  147. * and 10 bits for tag offsets from the base tag. For most uses this
  148. * should be enough, but when this is not, either multiple pack tables
  149. * or a pack table with extra base tags would be able to do the trick.
  150. * The goal here was to make the tables small and yet flexible enough to
  151. * handle most cases.
  152. }
  153. const
  154. PSTB_SIGNED =31;
  155. PSTB_UNPACK =30; { Note that these are active low... }
  156. PSTB_PACK =29; { Note that these are active low... }
  157. PSTB_EXISTS =26; { Tag exists bit true flag hack... }
  158. PSTF_SIGNED = $80000000;
  159. PSTF_UNPACK = $40000000;
  160. PSTF_PACK = $20000000;
  161. PSTF_EXISTS = $4000000;
  162. {***************************************************************************}
  163. PKCTRL_PACKUNPACK = $00000000;
  164. PKCTRL_PACKONLY = $40000000;
  165. PKCTRL_UNPACKONLY = $20000000;
  166. PKCTRL_BYTE = $80000000;
  167. PKCTRL_WORD = $88000000;
  168. PKCTRL_LONG = $90000000;
  169. PKCTRL_UBYTE = $00000000;
  170. PKCTRL_UWORD = $08000000;
  171. PKCTRL_ULONG = $10000000;
  172. PKCTRL_BIT = $18000000;
  173. PKCTRL_FLIPBIT = $98000000;
  174. {***************************************************************************}
  175. { Macros used by the next batch of macros below. Normally, you don't use
  176. * this batch directly. Then again, some folks are wierd
  177. }
  178. {***************************************************************************}
  179. { Some handy dandy macros to easily create pack tables
  180. *
  181. * Use PACK_STARTTABLE() at the start of a pack table. You pass it the
  182. * base tag value that will be handled in the following chunk of the pack
  183. * table.
  184. *
  185. * PACK_ENDTABLE() is used to mark the end of a pack table.
  186. *
  187. * PACK_NEWOFFSET() lets you change the base tag value used for subsequent
  188. * entries in the table
  189. *
  190. * PACK_ENTRY() lets you define an entry in the pack table. You pass it the
  191. * base tag value, the tag of interest, the type of the structure to use,
  192. * the field name in the structure to affect and control bits (combinations of
  193. * the various PKCTRL_XXX bits)
  194. *
  195. * PACK_BYTEBIT() lets you define a bit-control entry in the pack table. You
  196. * pass it the same data as PACK_ENTRY, plus the flag bit pattern this tag
  197. * affects. This macro should be used when the field being affected is byte
  198. * sized.
  199. *
  200. * PACK_WORDBIT() lets you define a bit-control entry in the pack table. You
  201. * pass it the same data as PACK_ENTRY, plus the flag bit pattern this tag
  202. * affects. This macro should be used when the field being affected is Integer
  203. * sized.
  204. *
  205. * PACK_LONGBIT() lets you define a bit-control entry in the pack table. You
  206. * pass it the same data as PACK_ENTRY, plus the flag bit pattern this tag
  207. * affects. This macro should be used when the field being affected is longword
  208. * sized.
  209. *
  210. * EXAMPLE:
  211. *
  212. * ULONG packTable[] =
  213. * (
  214. * PACK_STARTTABLE(GA_Dummy),
  215. * PACK_ENTRY(GA_Dummy,GA_Left,Gadget,LeftEdge,PKCTRL_WORD|PKCTRL_PACKUNPACK),
  216. * PACK_ENTRY(GA_Dummy,GA_Top,Gadget,TopEdge,PKCTRL_WORD|PKCTRL_PACKUNPACK),
  217. * PACK_ENTRY(GA_Dummy,GA_Width,Gadget,Width,PKCTRL_UWORD|PKCTRL_PACKUNPACK),
  218. * PACK_ENTRY(GA_Dummy,GA_Height,Gadget,Height,PKCTRL_UWORD|PKCTRL_PACKUNPACK),
  219. * PACK_WORDBIT(GA_Dummy,GA_RelVerify,Gadget,Activation,PKCTRL_BIT|PKCTRL_PACKUNPACK,GACT_RELVERIFY)
  220. * PACK_ENDTABLE
  221. * );
  222. }
  223. { ======================================================================= }
  224. { ==== TagItem ========================================================== }
  225. { ======================================================================= }
  226. { This data type may propagate through the system for more general use.
  227. * In the meantime, it is used as a general mechanism of extensible data
  228. * arrays for parameter specification and property inquiry (coming soon
  229. * to a display controller near you).
  230. *
  231. * In practice, an array (or chain of arrays) of TagItems is used.
  232. }
  233. Type
  234. Tag = LongInt;
  235. pTag = ^Tag;
  236. pTagItem = ^tTagItem;
  237. tTagItem = record
  238. ti_Tag : Tag;
  239. ti_Data : LongInt;
  240. END;
  241. ppTagItem = ^pTagItem;
  242. { ---- system tag values ----------------------------- }
  243. CONST
  244. TAG_DONE = 0; { terminates array of TagItems. ti_Data unused }
  245. TAG_END = TAG_DONE;
  246. TAG_IGNORE = 1; { ignore this item, not END of array }
  247. TAG_MORE = 2; { ti_Data is pointer to another array of TagItems
  248. * note that this tag terminates the current array
  249. }
  250. TAG_SKIP = 3; { skip this AND the next ti_Data items }
  251. { differentiates user tags from control tags }
  252. TAG_USER = LongInt($80000000); { differentiates user tags from system tags}
  253. {* If the TAG_USER bit is set in a tag number, it tells utility.library that
  254. * the tag is not a control tag (like TAG_DONE, TAG_IGNORE, TAG_MORE) and is
  255. * instead an application tag. "USER" means a client of utility.library in
  256. * general, including system code like Intuition or ASL, it has nothing to do
  257. * with user code.
  258. *}
  259. { Tag filter logic specifiers for use with FilterTagItems() }
  260. TAGFILTER_AND = 0; { exclude everything but filter hits }
  261. TAGFILTER_NOT = 1; { exclude only filter hits }
  262. { Mapping types for use with MapTags() }
  263. MAP_REMOVE_NOT_FOUND = 0; { remove tags that aren't in mapList }
  264. MAP_KEEP_NOT_FOUND = 1; { keep tags that aren't in mapList }
  265. Type
  266. pUtilityBase = ^tUtilityBase;
  267. tUtilityBase = record
  268. ub_LibNode : tLibrary;
  269. ub_Language : Byte;
  270. ub_Reserved : Byte;
  271. END;
  272. function AddNamedObject(nameSpace : pNamedObject location 'a0';obj : pNamedObject location 'a1') : LongBool; syscall _UtilityBase 222;
  273. function AllocateTagItems(num : ULONG location 'd0') : pTagItem; syscall _UtilityBase 066;
  274. function AllocNamedObjectA(const name : STRPTR location 'a0';const TagList : pTagItem location 'a1') : pNamedObject; syscall _UtilityBase 228;
  275. procedure Amiga2Date(amigatime : ULONG location 'd0';resultat : pClockData location 'a0'); syscall _UtilityBase 120;
  276. procedure ApplyTagChanges(TagList : pTagItem location 'a0'; const ChangeList : pTagItem location 'a1'); syscall _UtilityBase 186;
  277. function AttemptRemNamedObject(obj : pNamedObject location 'a0') : LongInt; syscall _UtilityBase 234;
  278. function CallHookPkt(h : pHook location 'a0';obj: APTR location 'a2'; paramPkt : APTR location 'a1') : ULONG; syscall _UtilityBase 102;
  279. function CheckDate(const date : pClockData location 'a0') : ULONG; syscall _UtilityBase 132;
  280. function CloneTagItems(const tagList : pTagItem location 'a0') : pTagItem; syscall _UtilityBase 072;
  281. function Date2Amiga(const date : pClockData location 'a0') : ULONG; syscall _UtilityBase 126;
  282. procedure FilterTagChanges(changelist: PTagItem location 'a0'; oldvalues : pTagItem location 'a1';apply : ULONG location 'd0'); syscall _UtilityBase 054;
  283. function FilterTagItems(taglist : pTagItem location 'a0';const tagArray : pULONG location 'a1';logic : ULONG location 'd0') : ULONG; syscall _UtilityBase 096;
  284. function FindNamedObject(nameSpace : pNamedObject location 'a0';const name : STRPTR location 'a1';lastobject: pNamedObject location 'a2') : pNamedObject; syscall _UtilityBase 240;
  285. function FindTagItem(TagVal : Tag location 'd0';const TagList : pTagItem location 'a0') : pTagItem; syscall _UtilityBase 030;
  286. procedure FreeNamedObject(Obj : pNamedObject location 'a0'); syscall _UtilityBase 246;
  287. procedure FreeTagItems(TagList : pTagItem location 'a0'); syscall _UtilityBase 078;
  288. function GetTagData(tagval : Tag location 'd0';default : ULONG location 'd1';const TagList : pTagItem location 'a0') : ULONG; syscall _UtilityBase 036;
  289. function GetUniqueID : ULONG; syscall _UtilityBase 270;
  290. procedure MapTags(TagList : pTagItem location 'a0';const maplist : pTagItem location 'a1';IncludeMiss : ULONG location 'd0'); syscall _UtilityBase 060;
  291. function NamedObjectName(Obj : pNamedObject location 'a0') : STRPTR; syscall _UtilityBase 252;
  292. function NextTagItem(Item : ppTagItem location 'a0') : pTagItem; syscall _UtilityBase 048;
  293. function PackBoolTags(InitialFlags : ULONG location 'd0';const TagList: PTagItem location 'a0'; const boolmap : pTagItem location 'a1') : ULONG; syscall _UtilityBase 042;
  294. function PackStructureTags(packk: APTR location 'a0';const packTable : pULONG location 'a1';const TagList : pTagItem location 'a2') : ULONG; syscall _UtilityBase 210;
  295. procedure RefreshTagItemClones(cloneTagItem : pTagItem location 'a0'; const OriginalTagItems : pTagItem location 'a1'); syscall _UtilityBase 084;
  296. procedure ReleaseNamedObject(Obj : pNamedObject location 'a0'); syscall _UtilityBase 258;
  297. procedure RemNamedObject(Obj : pNamedObject location 'a0';Msg : pointer location 'a1'); syscall _UtilityBase 264;
  298. function SDivMod32( dividend: LongInt location 'd0'; divisor : LongInt location 'd1') : LongInt; syscall _UtilityBase 150;
  299. function SMult32(Arg1: LongInt location 'd0'; Arg2 : LongInt location 'd1') : LongInt; syscall _UtilityBase 138;
  300. function SMult64(Arg1: LongInt location 'd0'; Arg2 : LongInt location 'd1') : LongInt; syscall _UtilityBase 198;
  301. function Stricmp(const Str1: STRPTR location 'a0';const Str2 : STRPTR location 'a1') : LongInt; syscall _UtilityBase 162;
  302. function Strnicmp(const Str1: STRPTR location 'a0';const Str2 : STRPTR location 'a1';len : LongInt location 'd0') : LongInt; syscall _UtilityBase 168;
  303. function TagInArray(t : Tag location 'd0';const TagArray : pULONG location 'a0') : LongBool; syscall _UtilityBase 090;
  304. function ToLower(c : ULONG location 'd0') : Char; syscall _UtilityBase 180;
  305. function ToUpper(c : ULONG location 'd0') : Char; syscall _UtilityBase 174;
  306. function UDivMod32( dividend: ULONG location 'd0'; divisor : ULONG location 'd1') : ULONG; syscall _UtilityBase 156;
  307. function UMult32(Arg1: ULONG location 'd0'; Arg2 : ULONG location 'd1') : ULONG; syscall _UtilityBase 144;
  308. function UMult64(Arg1: ULONG location 'd0'; Arg2 : ULONG location 'd1') : ULONG; syscall _UtilityBase 204;
  309. function UnpackStructureTags(const pac: APTR location 'a0';const packTable: pULONG location 'a1';TagList : pTagItem location 'a2') : ULONG; syscall _UtilityBase 216;
  310. function AllocNamedObjectA(const name : string;const TagList : pTagItem) : pNamedObject;
  311. FUNCTION FindNamedObject(nameSpace : pNamedObject; CONST name : string; lastObject : pNamedObject) : pNamedObject;
  312. FUNCTION Stricmp(CONST string1 : string; CONST string2 : pCHAR) : LONGINT;
  313. FUNCTION Stricmp(CONST string1 : pCHAR; CONST string2 : string) : LONGINT;
  314. FUNCTION Stricmp(CONST string1 : string; CONST string2 : string) : LONGINT;
  315. FUNCTION Strnicmp(CONST string1 : string; CONST string2 : pCHAR; length : LONGINT) : LONGINT;
  316. FUNCTION Strnicmp(CONST string1 : pCHAR; CONST string2 : string; length : LONGINT) : LONGINT;
  317. FUNCTION Strnicmp(CONST string1 : string; CONST string2 : string; length : LONGINT) : LONGINT;
  318. IMPLEMENTATION
  319. function AllocNamedObjectA(const name : string;const TagList : pTagItem) : pNamedObject;
  320. begin
  321. AllocNamedObjectA := AllocNamedObjectA(PChar(RawByteString(name)),TagList);
  322. end;
  323. FUNCTION FindNamedObject(nameSpace : pNamedObject; CONST name : string; lastObject : pNamedObject) : pNamedObject;
  324. begin
  325. FindNamedObject := FindNamedObject(nameSpace,PChar(RawByteString(name)),lastObject);
  326. end;
  327. FUNCTION Stricmp(CONST string1 : string; CONST string2 : pCHAR) : LONGINT;
  328. begin
  329. Stricmp := Stricmp(PChar(RawbyteString(string1)),string2);
  330. end;
  331. FUNCTION Stricmp(CONST string1 : pCHAR; CONST string2 : string) : LONGINT;
  332. begin
  333. Stricmp := Stricmp(string1,PChar(RawbyteString(string2)));
  334. end;
  335. FUNCTION Stricmp(CONST string1 : string; CONST string2 : string) : LONGINT;
  336. begin
  337. Stricmp := Stricmp(PChar(RawbyteString(string1)),PChar(RawbyteString(string2)));
  338. end;
  339. FUNCTION Strnicmp(CONST string1 : string; CONST string2 : pCHAR; length : LONGINT) : LONGINT;
  340. begin
  341. Strnicmp := Strnicmp(PChar(RawbyteString(string1)),string2,length);
  342. end;
  343. FUNCTION Strnicmp(CONST string1 : pCHAR; CONST string2 : string; length : LONGINT) : LONGINT;
  344. begin
  345. Strnicmp := Strnicmp(string1,PChar(RawbyteString(string2)),length);
  346. end;
  347. FUNCTION Strnicmp(CONST string1 : string; CONST string2 : string; length : LONGINT) : LONGINT;
  348. begin
  349. Strnicmp := Strnicmp(PChar(RawbyteString(string1)),PChar(RawbyteString(string2)),length);
  350. end;
  351. end.