home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / nrdargs / src / CreateTTArray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-06  |  3.1 KB  |  178 lines

  1. /*
  2. **
  3. **    CreateTTArray.c 
  4. **    © 1998 by Stephan Rupprecht
  5. **
  6. **    email: stephan.rupprecht@primus-online.de
  7. **
  8. **    FREEWARE - I am not responsible for any damage 
  9. **    that is caused by the (mis)use of this program.
  10. **
  11. **    Compiler: MAXON-C++
  12. **
  13. **    NOTE: This code uses the pool functions of OS3
  14. **    to keep track on the allocated memory.
  15. **
  16. */
  17.  
  18. #include <exec/types.h>
  19. #include <exec/memory.h>
  20.  
  21. #include <pragma/exec_lib.h>
  22. #include <pragma/utility_lib.h>
  23.  
  24. #ifndef MAX_TEMPLATE_ITEMS
  25. #define MAX_TEMPLATE_ITEMS 100
  26. #endif
  27.  
  28. /****************************************************************************/
  29.  
  30. char **CreateTTArray( STRPTR template, LONG *array, APTR *pool, char **orig );
  31.  
  32. /****************************************************************************/
  33.  
  34. char **CreateTTArray( STRPTR template, LONG *array, APTR *poolp, char **orig )
  35. {
  36.     char     **tta, **oldtta = NULL;
  37.     APTR    pool;
  38.     
  39.     if( pool = *poolp = CreatePool( MEMF_ANY, 4096, 4096 ) )
  40.     {
  41.         if( oldtta = tta = AllocPooled( pool, sizeof(STRPTR) * (MAX_TEMPLATE_ITEMS+1) ) )
  42.         {
  43.             UBYTE    bufstart[32], 
  44.                     *buf = bufstart,
  45.                     kind = 0;
  46.                 
  47.             *tta++ = "DONOTWAIT";
  48.  
  49.             if( orig )
  50.             {
  51.                 STRPTR    p;
  52.                 
  53.                 while( p = *orig++ )
  54.                 {
  55.                     if( ! Strnicmp( "STARTPRI", p, sizeof( "STARTPRI" ) -1 ) )
  56.                     {
  57.                         *tta++ = p;
  58.                     }
  59.                     else if( ! Strnicmp( "TOOLPRI", p, sizeof( "TOOLPRI" ) -1 ) )
  60.                     {
  61.                         *tta++ = p;
  62.                     }
  63.                 }
  64.             }
  65.  
  66.             while( TRUE )
  67.             {
  68.                 UBYTE    ch = *template++;
  69.                 
  70.                 if( ch == '\0' || ch == ',' )
  71.                 {
  72.                     LONG    curr = *array;
  73.                     ULONG    allocsz;
  74.                     
  75.                     *buf = 0;
  76.                     
  77.                     allocsz = ( (ULONG)buf - (ULONG)bufstart ) + 16L;
  78.                     
  79.                     if( kind == '\0' && curr )
  80.                     {
  81.                         allocsz += strlen( curr );
  82.                     }
  83.                     
  84.                     if( *tta = AllocPooled( pool, allocsz ) )
  85.                     {
  86.                         STRPTR    data, fmt;
  87.                 
  88.                         switch( kind )
  89.                         {                        
  90.                             case 'M':
  91.                                 /*- not supported yet (does anyone need it?) -*/
  92.                                 fmt = "(%s=?)";
  93.                             break;
  94.                             
  95.                             case 'N':
  96.                                 fmt = curr ? "%s=%ld" : "(%s=?)";
  97.                                 if( curr )
  98.                                 {
  99.                                     data = (STRPTR) *(LONG **)curr;
  100.                                 }
  101.                             break;
  102.                             
  103.                             case 'T':
  104.                                 fmt = "%s=%s";
  105.                                 data = curr ? "YES" : "NO";
  106.                             break;
  107.                             
  108.                             case 'S':
  109.                                 fmt = curr ? "%s" : "(%s)";
  110.                             break;
  111.                             
  112.                             default:                                
  113.                                 fmt = curr ? "%s=%s" : "(%s=?)";
  114.                                 data = (STRPTR) curr;
  115.                             break;
  116.                         }
  117.                         
  118.                         sprintf( *tta++, fmt, bufstart, data );
  119.                     }
  120.                     else
  121.                     {                        
  122.                         oldtta = NULL;
  123.                         break;
  124.                     }
  125.                     
  126.                     if( ! ch )
  127.                     {
  128.                         break;
  129.                     }
  130.                     
  131.                     kind = 0;
  132.                     buf = bufstart;
  133.                     array++;
  134.                 }
  135.                 else if( ch == '=' )
  136.                 {
  137.                     buf = bufstart;
  138.                 }
  139.                 else if( ch == '/' )
  140.                 {
  141.                     while( ( ch = *template++ ) && ( ch != ',' ) )
  142.                     {
  143.                         switch( ch )
  144.                         {
  145.                             case 'M':
  146.                             case 'N':
  147.                             case 'T':
  148.                             case 'S':
  149.                                 kind = ch;
  150.                             break;
  151.                         }
  152.                     }
  153.                     
  154.                     template--;
  155.                 }
  156.                 else 
  157.                 {
  158.                     *buf++ = ch;
  159.                 }
  160.             }
  161.         }
  162.         
  163.         if( oldtta )
  164.         {
  165.             *tta = NULL;
  166.         }
  167.         else
  168.         {
  169.             DeletePool( pool );
  170.             *poolp = NULL;
  171.         }
  172.     }
  173.     
  174.     return oldtta;
  175. }
  176.  
  177. /****************************************************************************/
  178.