home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 110 / af110sub.adf / DTConvert.lzx / DTConvert / misc.c < prev    next >
C/C++ Source or Header  |  2004-01-05  |  4KB  |  198 lines

  1.  
  2. /*
  3. **
  4. **  $VER: misc.c 1.4 (23.6.97)
  5. **  datatypes.library/Examples/DTConvert
  6. **
  7. **  Converts file into another format using datatypes
  8. **
  9. **  Misc functions
  10. **
  11. **  Written 1996/97 by Roland 'Gizzy' Mainz
  12. **
  13. */
  14.  
  15. /* project includes */
  16. #include "DTConvert.h"
  17.  
  18.  
  19. struct Node *GetNumNode( struct List *list, ULONG num )
  20. {
  21.     struct Node *cur     = NULL;
  22.     ULONG        currnum = 0UL;
  23.  
  24.     if( list )
  25.     {
  26.       for( cur = list -> lh_Head ; cur -> ln_Succ ; cur = cur -> ln_Succ )
  27.       {
  28.         if( currnum++ == num ) break;
  29.       }
  30.  
  31.       if( !(cur -> ln_Succ) ) cur = NULL;
  32.     }
  33.  
  34.     return( cur );
  35. }
  36.  
  37.  
  38. void mysprintf( STRPTR buffer, STRPTR fmt, ... )
  39. {
  40.     APTR args;
  41.  
  42.     args = (APTR)((&fmt) + 1);
  43.  
  44.     RawDoFmt( fmt, args, (void (*))"\x16\xc0\x4e\x75", buffer );
  45. }
  46.  
  47.  
  48. APTR AllocVecPooled( APTR poolheader, ULONG memsize )
  49. {
  50.     ULONG *memory;
  51.  
  52.     memsize += sizeof( ULONG );
  53.  
  54.     if( memory = (ULONG *)AllocPooled( poolheader, memsize ) )
  55.     {
  56.       *memory = memsize;
  57.  
  58.       memory++;
  59.     }
  60.  
  61.     return( (APTR)memory );
  62. }
  63.  
  64.  
  65. void FreeVecPooled( APTR poolheader, APTR mem )
  66. {
  67.     ULONG *memory;
  68.  
  69.     if( mem )
  70.     {
  71.       memory = (ULONG *)mem;
  72.  
  73.       memory--;
  74.  
  75.       FreePooled( poolheader, memory, (*memory) );
  76.     }
  77. }
  78.  
  79.  
  80. STRPTR GetLockName( BPTR lock, STRPTR name )
  81. {
  82.     ULONG  buffsize  = 64UL;
  83.     STRPTR buff      = NULL;
  84.     LONG   ioerr     = 0L;
  85.     ULONG  namelen   = ((name)?(strlen( name )):(0UL));
  86.  
  87.     for( ;; )
  88.     {
  89.       /* Allocate buffer for path, name and '/' */
  90.       if( buff = (STRPTR)AllocVec( (buffsize + namelen + 16UL), (MEMF_PUBLIC | MEMF_CLEAR) ) )
  91.       {
  92.         if( NameFromLock( lock, buff, (buffsize - 1UL) ) )
  93.         {
  94.           break;
  95.         }
  96.         else
  97.         {
  98.           ioerr = IoErr();
  99.  
  100.           FreeVec( (APTR)buff );
  101.           buff = NULL;
  102.  
  103.           if( ioerr == ERROR_LINE_TOO_LONG )
  104.           {
  105.             buffsize *= 2UL;
  106.           }
  107.           else
  108.           {
  109.             break;
  110.           }
  111.         }
  112.       }
  113.     }
  114.  
  115.     if( buff )
  116.     {
  117.       if( name )
  118.       {
  119.         if( !AddPart( buff, name, (buffsize + namelen + 14UL) ) )
  120.         {
  121.           FreeVec( buff );
  122.           buff = NULL;
  123.         }
  124.       }
  125.     }
  126.     else
  127.     {
  128.       SetIoErr( ioerr );
  129.     }
  130.  
  131.     return( buff );
  132. }
  133.  
  134.  
  135. void AttemptOpenLibrary( struct Library **library, STRPTR title, STRPTR libname, ULONG libversion )
  136. {
  137.     struct EasyStruct LibNotFoundES,
  138.                       LibWrongVersionES;
  139.  
  140.     LibNotFoundES . es_StructSize   = sizeof( struct EasyStruct );
  141.     LibNotFoundES . es_Flags        = 0UL;
  142.     LibNotFoundES . es_Title        = title;
  143.     LibNotFoundES . es_TextFormat   = "%s\nnot found.";
  144.     LibNotFoundES . es_GadgetFormat = "Retry|Cancel";
  145.  
  146.     LibWrongVersionES . es_StructSize   = sizeof( struct EasyStruct );
  147.     LibWrongVersionES . es_Flags        = 0UL;
  148.     LibWrongVersionES . es_Title        = title;
  149.     LibWrongVersionES . es_TextFormat   = "Requires at least\n%s version %lu";
  150.     LibWrongVersionES . es_GadgetFormat = "Cancel";
  151.  
  152.     if( (*library) == NULL )
  153.     {
  154.       for( ;; )
  155.       {
  156.         /* attemp to open shared library */
  157.         (*library) = OpenLibrary( libname, 0UL );
  158.  
  159.         if( (*library) )
  160.         {
  161.           /* check if we got at least "libversion" version of shared library */
  162.           if( ((*library) -> lib_Version) < libversion )
  163.           {
  164.             (void)EasyRequest( NULL, (&LibWrongVersionES), 0UL, libname, libversion );
  165.  
  166.             CloseLibrary( (*library) );
  167.             (*library) = NULL;
  168.           }
  169.  
  170.           break;
  171.         }
  172.  
  173.         /* prompt the user */
  174.         if( EasyRequest( NULL, (&LibNotFoundES), 0UL, libname ) == 0L )
  175.         {
  176.           /* user canceled */
  177.           break;
  178.         }
  179.       }
  180.     }
  181. }
  182.  
  183.  
  184. void ClearMsgPort( struct MsgPort *port )
  185. {
  186.     struct Message *msg;
  187.  
  188.     while( msg = GetMsg( port ) )
  189.     {
  190.       ReplyMsg( msg );
  191.     }
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.