home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / iii-29 / dillo.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  8KB  |  322 lines

  1. /*
  2. **  dillo.c
  3. **
  4. **  A code module of armadillo.library, which implements the main library
  5. **  functions.  Strictly do-nothing code for example.
  6. **
  7. **  © Copyright 1993, Commodore-Amiga Inc. All Rights Reserved.
  8. **  Written by John Wiederhirn
  9. **
  10. */
  11.  
  12. #include    <exec/types.h>
  13. #include    <exec/memory.h>
  14. #include    <clib/exec_protos.h>
  15. #include    <pragmas/exec_pragmas.h>
  16.  
  17. extern struct ExecBase *SysBase;    /* olsen 30-Jan-96 */
  18.  
  19. #include    "dillo.h"
  20. #include    "dillo_protos.h"
  21.  
  22. /* The next prototype is for a static function which is only available
  23. ** to code inside the library itself.
  24. */
  25.  
  26. static void ClearDilloName(struct Armadillo * );
  27.  
  28.  
  29. /* The following global data item becomes part of the near data section
  30. ** for each library client.  Since this library is designed to give a
  31. ** different library base to each client, this data item is unique per
  32. ** client.
  33. **
  34. ** It holds the number of armadillos a given client has open at once.
  35. */
  36.  
  37. ULONG TotalDillos = 0L;
  38.  
  39. /* In contrast to the previous global data item, the following goes in
  40. ** the far data section and is global to all library clients.  Read access
  41. ** doesn't need arbitration, but write access needs a semaphor or use of
  42. ** a Forbid()/Permit() pair (see CreateDillo() below).
  43. **
  44. ** It holds the number of times CreateDillo has been called overall.
  45. */
  46.  
  47. ULONG __far TotalDillosCreated = 0L;
  48.  
  49. /* This routine just allocates a `struct Armadillo', and increments
  50. ** the number of armadillos this client has by one.  If the allocation
  51. ** cannot be done, this routine returns NULL.
  52. */
  53.  
  54. struct Armadillo * __saveds __asm
  55. LIBCreateArmadillo( register __a6 struct Library *DilloBase )
  56. {
  57.     struct Armadillo *newdillo = NULL;
  58.  
  59.     if ( newdillo = AllocMem( sizeof(struct Armadillo), MEMF_CLEAR ))
  60.     {
  61.         /* Armadillo allocated, so increment number of dillos.
  62.         ** Note that to reference the client-unique data takes no
  63.         ** special coding.
  64.         */
  65.  
  66.         TotalDillos++;
  67.  
  68.         /* Since we've also added to the overall number created, we
  69.         ** need to also update the TotalDillosCreated variable in
  70.         ** the far data section.  That means a Forbid() and Permit()
  71.         ** around the action (which MUST complete).
  72.         */
  73.  
  74.         Forbid();
  75.         TotalDillosCreated++;
  76.         Permit();
  77.  
  78.     }
  79.  
  80.     /* And return either the address of the new armadillo, or else
  81.     ** return NULL if the allocation failed.
  82.     */
  83.  
  84.     return( newdillo );
  85. }
  86.  
  87.  
  88. /* This function wipes an existing Armadillo structure out of existance
  89. ** and decrements the number of Armadillos for this client.  Note that the
  90. ** number of Armadillos created overall does not go down.
  91. */
  92.  
  93. VOID __saveds __asm
  94. LIBDeleteArmadillo( register __a0 struct Armadillo *dillo,
  95.                     register __a6 struct Library *DilloBase )
  96. {
  97.     /* This routine is ``safe'' in that it can handle being given a NULL
  98.     ** pointer (in which case it does nothing).
  99.     */
  100.  
  101.     if ( dillo )
  102.     {
  103.         /* We do indeed appear to have an armadillo on our hands
  104.         ** so we decrement the overall count and deallocate the
  105.         ** memory it uses.
  106.         */
  107.  
  108.         TotalDillos--;
  109.  
  110.         FreeMem( dillo, sizeof( struct Armadillo ));
  111.     }
  112.  
  113.     return;
  114. }
  115.  
  116. /* This transfers the contents of a string up to 32 characters long into
  117. ** the name buffer of an Armadillo.  Any attempt to transfer more than 32
  118. ** characters gets truncated to 32 characters.  Returns FALSE if dillo
  119. ** was a NULL pointer, the pointer to the string was NULL, or the length
  120. ** of the transfer was to be 0L.
  121. */
  122.  
  123. BOOL __saveds __asm
  124. LIBNameArmadillo( register __a0 struct Armadillo *dillo,
  125.                   register __a1 STRPTR dname,
  126.                   register __d0 ULONG len,
  127.                   register __a6 struct Library *DilloBase )
  128. {
  129.     BOOL retval = FALSE;
  130.  
  131.     /* This routine is ``safe'' in that it can handle being given a NULL
  132.     ** pointer (in which case it does nothing).
  133.     */
  134.  
  135.     if ( dillo && dname && len )
  136.     {
  137.         CopyMem( (APTR) dname, (APTR) &(dillo->name), ((len>31L)?32L:len) );
  138.         retval = TRUE;
  139.     }
  140.  
  141.     return( retval );
  142. }
  143.  
  144.  
  145. /* Assigns a value to the weight field of an Armadillo structure.  It
  146. ** returns NULL if a NULL pointer is passed in or amt was 0L.
  147. */
  148.  
  149. BOOL __saveds __asm
  150. LIBFillArmadillo( register __a0 struct Armadillo *dillo,
  151.                   register __d0 ULONG amt,
  152.                   register __a6 struct Library *DilloBase )
  153. {
  154.     BOOL retval = FALSE;
  155.  
  156.     /* This routine is ``safe'' in that it can handle being given a NULL
  157.     ** pointer (in which case it does nothing).
  158.     */
  159.  
  160.     if ( dillo && amt )
  161.     {
  162.         dillo->weight = amt;
  163.         retval = TRUE;
  164.     }
  165.  
  166.     return( retval );
  167. }
  168.  
  169.  
  170. /* In homage to the Texas state animal, the roadkill armadillo, this function
  171. ** sets whether a given Armadillo is flattened or not.  Returns NULL if a
  172. ** NULL pointer was passed as the Armadillo structure.
  173. */
  174.  
  175. BOOL __saveds __asm
  176. LIBFlattenArmadillo( register __a0 struct Armadillo *dillo,
  177.                      register __d0 BOOL flatd,
  178.                      register __a6 struct Library *DilloBase )
  179. {
  180.     BOOL retval = FALSE;
  181.  
  182.     /* This routine is ``safe'' in that it can handle being given a NULL
  183.     ** pointer (in which case it does nothing).
  184.     */
  185.  
  186.     if ( dillo )
  187.     {
  188.         dillo->flat = flatd;
  189.         retval = TRUE;
  190.     }
  191.  
  192.     return( retval );
  193. }
  194.  
  195.  
  196. /* Returns whether or not the Armadillo has been flattened.  If a NULL
  197. ** pointer is passed in, this function returns FALSE (not really distinct).
  198. */
  199.  
  200. BOOL __saveds __asm
  201. LIBDilloFlat( register __a0 struct Armadillo *dillo,
  202.               register __a6 struct Library *DilloBase )
  203. {
  204.     BOOL retval = FALSE;
  205.  
  206.     /* This routine is ``safe'' in that it can handle being given a NULL
  207.     ** pointer (in which case it returns FALSE).
  208.     */
  209.  
  210.     if ( dillo )
  211.     {
  212.         retval = dillo->flat;
  213.     }
  214.  
  215.     return( retval );
  216. }
  217.  
  218.  
  219. /* Returns the weight of a given Armadillo or 0L if a NULL pointer
  220. ** is passed instead of an Armadillo (no pointer == no weight ).
  221. */
  222.  
  223. ULONG __saveds __asm
  224. LIBDilloWeight( register __a0 struct Armadillo *dillo,
  225.                 register __a6 struct Library *DilloBase )
  226. {
  227.     ULONG retval = 0L;
  228.  
  229.     /* This routine is ``safe'' in that it can handle being given a NULL
  230.     ** pointer (in which case it returns 0L).
  231.     */
  232.  
  233.     if ( dillo )
  234.     {
  235.         retval = dillo->weight;
  236.     }
  237.  
  238.     return( retval );
  239. }
  240.  
  241.  
  242. /* This function copies the name of an Armadillo into the caller-specified
  243. ** buffer (which MUST be at least 32 characters in length).  A NULL pointer
  244. ** for the Armadillo, buffer or len will get a FALSE return, otherwise a
  245. ** return of TRUE if the transfer occurred.
  246. */
  247.  
  248. BOOL __saveds __asm
  249. LIBDilloName( register __a0 struct Armadillo *dillo,
  250.               register __a1 STRPTR buf,
  251.               register __d0 ULONG len,
  252.               register __a6 struct Library *DilloBase )
  253. {
  254.     BOOL retval = FALSE;
  255.  
  256.     /* This routine is ``safe'' in that it can handle being given a NULL
  257.     ** pointer (in which case it does nothing).
  258.     */
  259.  
  260.     if ( dillo && buf && len)
  261.     {
  262.         CopyMem( (APTR) &(dillo->name), (APTR) buf, ((len>31L)?32L:len) );
  263.         retval = TRUE;
  264.     }
  265.  
  266.     return( retval );
  267. }
  268.  
  269.  
  270. /* Following are non-public but externally-accessible entry points. */
  271.  
  272. /* This routine clears out the contents of an Armadillo.  It also is an
  273. ** example of using a non-public non-ext.-accessible routine in a shared
  274. ** library.
  275. */
  276.  
  277. VOID __saveds __asm
  278. LIBClearDillo( register __a0 struct Armadillo *dillo,
  279.                register __a6 struct Library *DilloBase )
  280. {
  281.     /* This routine is ``safe'' in that it can handle being given a NULL
  282.     ** pointer (in which case it does nothing).
  283.     */
  284.  
  285.     if ( dillo )
  286.     {
  287.         dillo->flat = FALSE;
  288.         dillo->weight = 0L;
  289.         ClearDilloName( dillo );
  290.     }
  291. }
  292.  
  293.  
  294. /* This routine does an "unprotected" query (legal, since the access is
  295. ** read-only) of the TotalDillosCreated variable in the far data section.
  296. */
  297.  
  298. ULONG __saveds __asm
  299. LIBDilloBirths( register __a6 struct Library *DilloBase )
  300. {
  301.     return( TotalDillosCreated );
  302. }
  303.  
  304.  
  305. /* Following call is a non-public non-externally-accessible function */
  306.  
  307. /* This function is callable ONLY from within this module.  It clears out
  308. ** the name buffer for a given Armadillo.
  309. */
  310.  
  311. static VOID
  312. ClearDilloName( struct Armadillo *dillo )
  313. {
  314.     int i;
  315.  
  316.     /* This routine is NOT 'safe'.  Params must be pre-checked. */
  317.  
  318.     for(i=0;i<31;i++)
  319.         dillo->name[i] = '\0';
  320.  
  321. }
  322.