home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / stefanb_src / libskeleton.c next >
Encoding:
C/C++ Source or Header  |  1996-03-03  |  5.7 KB  |  210 lines

  1. Needed defines (do a search and replace in your editor):
  2.  
  3. XXX           Library base name, e.g. screennotify
  4.  
  5. XXX_NAME      Library base name with ".library" appended
  6.               Defined in <libraries/XXX.h>, e.g.
  7.  
  8.                   #define SCREENNOTIFY_NAME "screennotify.library"
  9.  
  10. XXX_VERSION   Library version
  11.               Defined in <libraries/XXX.h>, e.g.
  12.  
  13.                   #define SCREENNOTIFY_VERSION 1
  14.  
  15. XXX_REVISION  Library revision
  16.               Defined in "XXX.h", e.g.
  17.  
  18.                   #define SCREENNOTIFY_VERSION 1
  19.  
  20. XXXBase       Library base data structure
  21.               Defined in "XXX.h", e.g.
  22.  
  23.                   struct ScreenNotifyBase {
  24.                    struct Library snb_Library;
  25.                    UWORD          snb_Pad;
  26.                    BPTR           snb_Segment;
  27.                    /* Other private library global data */
  28.                   },
  29.  
  30. xxxb          Pointer to library base in functions, e.g. snb
  31.               (Abbreviation of library base data structure name)
  32.  
  33.  
  34. Include files:
  35.  
  36. "XXX.h"                  Local include file for library source.
  37.                          Contains all private library declarations.
  38.  
  39. <clib/XXX_protos.h>      Library function prototypes
  40.  
  41. <libraries/XXX.h>        Public library include file.
  42.                          Contains all public library declarations.
  43.  
  44. <pragmas/XXX_pragmas.h>  Library function inline call declarations
  45.  
  46.  
  47. Other files:
  48.  
  49. FD/XXX_lib.fd            Library function offsets & register deklaration file
  50.  
  51. /*
  52.  * XXX.c V0.0.00
  53.  *
  54.  * Library routines
  55.  *
  56.  * (c) 1995-96 Stefan Becker
  57.  */
  58.  
  59. #include "XXX.h"
  60.  
  61. /*
  62.  * Object file dummy entry point
  63.  */
  64. static ULONG Dummy(void)
  65. {
  66.  return(0);
  67. }
  68.  
  69. /* Library name and ID string */
  70. #define INTTOSTR(a) #a
  71. static const char LibraryName[] = XXX_NAME;
  72. static const char LibraryID[]   = "$VER: " XXX_NAME " "
  73.                                   INTTOSTR(XXX_VERSION) "."
  74.                                   INTTOSTR(XXX_REVISION)
  75.                                   " (" __COMMODORE_DATE__ ")\r\n";
  76.  
  77. /* Standard library function prototypes */
  78. __geta4 static struct Library *LibraryInit(__A0 BPTR, __A6 struct Library *);
  79. __geta4 static struct Library *LibraryOpen(__A6 struct XXXBase *);
  80. __geta4 static BPTR            LibraryClose(__A6 struct XXXBase *);
  81. __geta4 static BPTR            LibraryExpunge(__A6 struct XXXBase *);
  82.         static ULONG           LibraryReserved(void);
  83.  
  84. /* Library specific function prototypes */
  85. <...>
  86.  
  87. /* ROMTag structure */
  88. static const struct Resident ROMTag = { RTC_MATCHWORD, &ROMTag, &ROMTag + 1, 0,
  89.  XXX_VERSION, NT_LIBRARY, 0, LibraryName, LibraryID, LibraryInit
  90. };
  91.  
  92. /* Library functions table */
  93. static const APTR LibraryVectors[] = {
  94.  /* Standard functions */
  95.  (APTR) LibraryOpen,
  96.  (APTR) LibraryClose,
  97.  (APTR) LibraryExpunge,
  98.  (APTR) LibraryReserved,
  99.  
  100.  /* Library specific functions */
  101.  (APTR) <1st library specific function> /* Reserve this one for ARexx! */
  102.  (APTR) <2nd library specific function>
  103.  ....
  104.  
  105.  /* End of table */
  106.  (APTR) -1
  107. };
  108.  
  109. /* Global library bases */
  110. struct Library *SysBase;
  111. struct XXXBase *XXXBase;
  112.  
  113. /* Initialize library */
  114. __geta4 static struct Library *LibraryInit(__A0 BPTR Segment,
  115.                                            __A6 struct Library *ExecBase)
  116. {
  117.  struct XXXBase *xxxb = NULL;
  118.  
  119.  /* Initialize SysBase */
  120.  SysBase = ExecBase;
  121.  
  122.  if (xxxb = (struct XXXBase *) MakeLibrary(LibraryVectors, NULL, NULL,
  123.                                             sizeof(struct XXXBase), NULL)) {
  124.   /* Initialize libray structure */
  125.   xxxb->xxxb_Library.lib_Node.ln_Type = NT_LIBRARY;
  126.   xxxb->xxxb_Library.lib_Node.ln_Name = LibraryName;
  127.   xxxb->xxxb_Library.lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  128.   xxxb->xxxb_Library.lib_Version      = XXX_VERSION;
  129.   xxxb->xxxb_Library.lib_Revision     = XXX_REVISION;
  130.   xxxb->xxxb_Library.lib_IdString     = (APTR) LibraryID;
  131.   xxxb->xxxb_Segment                  = Segment;
  132.  
  133.   <.... get other resources for the library ...>
  134.  
  135.   /* Add the library to the system */
  136.   AddLibrary((struct Library *) wbsb);
  137.  
  138.   /* Set global library base pointer */
  139.   XXXBase = xxxb;
  140.  }
  141.  
  142.  /* Return new library pointer */
  143.  return((struct Library *) xxxb);
  144. }
  145.  
  146. /* Standard library function: Open. Called in Forbid() state */
  147. __geta4 static struct Library *LibraryOpen(__A6 struct XXXBase *xxxb)
  148. {
  149.  /* Oh another user :-) */
  150.  xxxb->xxxb_Library.lib_OpenCnt++;
  151.  
  152.  /* Reset delayed expunge flag */
  153.  xxxb->xxxb_Library.lib_Flags &= ~LIBF_DELEXP;
  154.  
  155.  /* Return library pointer */
  156.  return(&xxxb->xxxb_Library);
  157. }
  158.  
  159. /* Standard library function: Close. Called in Forbid() state */
  160. __geta4 static BPTR LibraryClose(__A6 struct XXXBase *xxxb)
  161. {
  162.  BPTR rc = NULL;
  163.  
  164.  /* Open count greater zero, only one user and delayed expunge bit set? */
  165.  if ((xxxb->xxxb_Library.lib_OpenCnt > 0) &&
  166.      (--xxxb->xxxb_Library.lib_OpenCnt == 0) &&
  167.      (xxxb->xxxb_Library.lib_Flags & LIBF_DELEXP))
  168.  
  169.   /* Yes, try to remove the library */
  170.   rc = LibraryExpunge(xxxb);
  171.  
  172.  /* Return library segment if expunge was successful */
  173.  return(rc);
  174. }
  175.  
  176. /* Standard library function: Expunge. Called in Forbid() state */
  177. __geta4 static BPTR LibraryExpunge(__A6 struct XXXBase *xxxb)
  178. {
  179.  BPTR rc = NULL;
  180.  
  181.  /* Does anybody use library now? */
  182.  if (xxxb->xxxb_Library.lib_OpenCnt > 0)
  183.  
  184.   /* No, library still in use -> set delayed expunge flag */
  185.   xxxb->xxxb_Library.lib_Flags |= LIBF_DELEXP;
  186.  
  187.  else {
  188.   /* Yes, remove library */
  189.   Remove(&xxxb->xxxb_Library.lib_Node);
  190.  
  191.   <... Free other library resources ...>
  192.  
  193.   /* Return library segment */
  194.   rc = xxxb->xxxb_Segment;
  195.  
  196.   /* Free memory for library base */
  197.   FreeMem((void *) ((ULONG) xxxb - xxxb->xxxb_Library.lib_NegSize),
  198.           xxxb->xxxb_Library.lib_NegSize + xxxb->xxxb_Library.lib_PosSize);
  199.  }
  200.  
  201.  /* Return library segment if expunge was successful */
  202.  return(rc);
  203. }
  204.  
  205. /* Reserved function, returns NULL */
  206. static ULONG LibraryReserved(void)
  207. {
  208.  return(0);
  209. }
  210.