home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / develop / gadtoolsbox / gengtxsource / source / dupstrings.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  8KB  |  299 lines

  1. /*
  2. **      $Filename: DupStrings.c $
  3. **      $Release: 1.0 $
  4. **      $Revision: 38.1 $
  5. **
  6. **      Get all strings from the file into a seperate list
  7. **      filtering out all double strings.
  8. **
  9. **      (C) Copyright 1992 Jaba Development.
  10. **          Written by Jan van den Baard.
  11. **/
  12.  
  13. #include "GenGTXSource.h"
  14.  
  15. Prototype BOOL BuiltDuplicates( void );
  16. Prototype VOID FreeDuplicates( void );
  17. Prototype UWORD GetStringNumber( struct StringList *, UBYTE * );
  18. Prototype UWORD GetArrayNumber( struct ArrayList *, UBYTE ** );
  19. Prototype UWORD CountArray( UBYTE ** );
  20.  
  21. /*
  22.  *      Count the number of strings in an array.
  23.  */
  24. UWORD CountArray( UBYTE **array )
  25. {
  26.     UWORD       i = 0;
  27.  
  28.     while ( array[ i ] ) i++;
  29.  
  30.     return( i - 1 );
  31. }
  32.  
  33. /*
  34.  *      Compare two string arrays.
  35.  */
  36. BOOL CompareArrays( UBYTE **array1, UBYTE **array2 )
  37. {
  38.     UWORD           ac1, ac2, i;
  39.  
  40.     ac1 = CountArray( array1 );
  41.     ac2 = CountArray( array2 );
  42.  
  43.     if ( ac1 != ac2 ) return( FALSE );
  44.  
  45.     for ( i = 0; i < ac1; i++ ) {
  46.         if ( strcmp( array1[ i ], array2[ i ] ))
  47.             return( FALSE );
  48.     }
  49.  
  50.     return( TRUE );
  51. }
  52.  
  53. /*
  54.  *      Find a string.
  55.  */
  56. struct StringNode *FindString( struct StringList *list, UBYTE *string )
  57. {
  58.     struct StringNode       *sn;
  59.  
  60.     for ( sn = list->sl_First; sn->sn_Next; sn = sn->sn_Next ) {
  61.         if ( ! strcmp( sn->sn_String, string ))
  62.             return( sn );
  63.     }
  64.     return( NULL );
  65. }
  66.  
  67. /*
  68.  *      Find an array.
  69.  */
  70. struct ArrayNode *FindArray( struct ArrayList *list, UBYTE **array )
  71. {
  72.     struct ArrayNode        *an;
  73.  
  74.     for ( an = list->al_First; an->an_Next; an = an->an_Next ) {
  75.         if ( CompareArrays( an->an_Array, array ))
  76.             return( an );
  77.     }
  78.     return( NULL );
  79. }
  80.  
  81. /*
  82.  *      Add a string.
  83.  */
  84. BOOL AddString( struct StringList *list, UBYTE *string )
  85. {
  86.     struct StringNode       *sn;
  87.  
  88.     if ( string == ( UBYTE * )NM_BARLABEL )
  89.         return( TRUE );
  90.  
  91.     if ( ! FindString( list, string )) {
  92.         if ( sn = ( struct StringNode * )AllocVecItem( Chain, sizeof( struct StringNode ), MEMF_PUBLIC )) {
  93.             sn->sn_String = string;
  94.             AddTail(( struct List * )list, ( struct Node * )sn );
  95.             return( TRUE );
  96.         }
  97.     } else
  98.         return( TRUE );
  99.     return( FALSE );
  100. }
  101.  
  102. /*
  103.  *      Add an array.
  104.  */
  105. BOOL AddArray( struct ArrayList *list, UBYTE **array )
  106. {
  107.     struct ArrayNode        *an;
  108.  
  109.     if ( ! FindArray( list, array )) {
  110.         if ( an = ( struct ArrayNode * )AllocVecItem( Chain, sizeof( struct ArrayNode ), MEMF_PUBLIC )) {
  111.             an->an_Array = array;
  112.             AddTail(( struct List * )list, ( struct Node * )an );
  113.             while ( *array ) {
  114.                 if ( ! AddString( &Strings, *array ))
  115.                     return( FALSE );
  116.                 array++;
  117.             }
  118.             return( TRUE );
  119.         }
  120.     } else
  121.         return( TRUE );
  122.     return( FALSE );
  123. }
  124.  
  125. /*
  126.  *      Get the ordinal string number.
  127.  */
  128. UWORD GetStringNumber( struct StringList *list, UBYTE *string )
  129. {
  130.     struct StringNode       *sn;
  131.  
  132.     sn = FindString( list, string );
  133.  
  134.     return( sn->sn_Number );
  135. }
  136.  
  137. /*
  138.  *      Get the ordinal array number.
  139.  */
  140. UWORD GetArrayNumber( struct ArrayList *list, UBYTE **array )
  141. {
  142.     struct ArrayNode        *an;
  143.  
  144.     an = FindArray( list, array );
  145.  
  146.     return( an->an_Number );
  147. }
  148.  
  149. /*
  150.  *      Number the duplicates.
  151.  */
  152. Local VOID NumberDuplicates( void )
  153. {
  154.     struct StringNode       *sn;
  155.     struct ArrayNode        *an;
  156.     UWORD                    num;
  157.  
  158.     for ( sn = Strings.sl_First, num = 0; sn->sn_Next; sn = sn->sn_Next, num++ )
  159.         sn->sn_Number = num;
  160.  
  161.     for ( an = Arrays.al_First, num = 0; an->an_Next; an = an->an_Next, num++ )
  162.         an->an_Number = num;
  163. }
  164.  
  165. /*
  166.  *      Built the string and array list.
  167.  */
  168. BOOL BuiltDuplicates( void )
  169. {
  170.     struct ProjectWindow            *pw;
  171.     struct IntuiText                *it;
  172.     struct ExtNewGadget             *eng;
  173.     struct ExtNewMenu               *menu, *item, *sub;
  174.     UBYTE                          **array, *string;
  175.  
  176.    /*
  177.     *   First of all add the screen title to the string list.
  178.     */
  179.     if ( ! AddString( &Strings, &GuiInfo.gui_ScreenTitle[ 0 ] ))
  180.         goto memoryError;
  181.  
  182.     for ( pw = Windows.wl_First; pw->pw_Next; pw = pw->pw_Next ) {
  183.        /*
  184.         *   Add the Window title to the string list.
  185.         */
  186.         if ( strlen( &pw->pw_WindowTitle[ 0 ] )) {
  187.             if ( ! AddString( &Strings, &pw->pw_WindowTitle[ 0 ] ))
  188.                 goto memoryError;
  189.         }
  190.  
  191.        /*
  192.         *   Add the Window Screen title to the string list.
  193.         */
  194.         if ( strlen( &pw->pw_ScreenTitle[ 0 ] )) {
  195.             if ( ! AddString( &Strings, &pw->pw_ScreenTitle[ 0 ] ))
  196.                 goto memoryError;
  197.         }
  198.  
  199.        /*
  200.         *   Add the IntuiTexts to the string list.
  201.         */
  202.         if ( it = pw->pw_WindowText ) {
  203.             while ( it ) {
  204.                 if ( ! AddString( &Strings, it->IText ))
  205.                     goto memoryError;
  206.                 it = it->NextText;
  207.             }
  208.         }
  209.  
  210.        /*
  211.         *   Add the Gadget texts to the string list and
  212.         *   the MX or CYCLE arrays to the array list.
  213.         */
  214.         for ( eng = pw->pw_Gadgets.gl_First; eng->en_Next; eng = eng->en_Next ) {
  215.             if ( strlen( &eng->en_GadgetText[ 0 ] )) {
  216.                 if ( ! AddString( &Strings, &eng->en_GadgetText[ 0 ] ))
  217.                     goto memoryError;
  218.             }
  219.  
  220.             switch ( eng->en_Kind ) {
  221.                 case    MX_KIND:
  222.                     if ( array = ( UBYTE ** )GetTagData( GTMX_Labels, NULL, eng->en_Tags )) {
  223.                         if ( ! AddArray( &Arrays, array ))
  224.                             goto memoryError;
  225.                     }
  226.                     break;
  227.                 case    CYCLE_KIND:
  228.                     if ( array = ( UBYTE ** )GetTagData( GTCY_Labels, NULL, eng->en_Tags )) {
  229.                         if ( ! AddArray( &Arrays, array ))
  230.                             goto memoryError;
  231.                     }
  232.                     break;
  233.                 case    STRING_KIND:
  234.                     if ( string = ( UBYTE * )GetTagData( GTST_String, NULL, eng->en_Tags )) {
  235.                         if ( ! AddString( &Strings, string ))
  236.                             goto memoryError;
  237.                     }
  238.                     break;
  239.                 case    TEXT_KIND:
  240.                     if ( string = ( UBYTE * )GetTagData( GTTX_Text, NULL, eng->en_Tags )) {
  241.                         if ( ! AddString( &Strings, string ))
  242.                             goto memoryError;
  243.                     }
  244.                     break;
  245.                 case    SLIDER_KIND:
  246.                     if ( string = ( UBYTE * )GetTagData( GTSL_LevelFormat, NULL, eng->en_Tags )) {
  247.                         if ( ! AddString( &Strings, string ))
  248.                             goto memoryError;
  249.                     }
  250.                     break;
  251.             }
  252.         }
  253.  
  254.        /*
  255.         * Add the menu, item and subitem titles and CommKey's to the string list.
  256.         */
  257.         for ( menu = pw->pw_Menus.ml_First; menu->em_Next; menu = menu->em_Next ) {
  258.             if ( ! AddString( &Strings, &menu->em_MenuTitle[ 0 ] ))
  259.                 goto memoryError;
  260.             for ( item = menu->em_Items->ml_First; item->em_Next; item = item->em_Next ) {
  261.                 if ( ! AddString( &Strings, &item->em_MenuTitle[ 0 ] ))
  262.                     goto memoryError;
  263.                 if ( strlen( &item->em_CommKey[ 0 ] )) {
  264.                     if ( ! AddString( &Strings, &item->em_CommKey[ 0 ] ))
  265.                         goto memoryError;
  266.                 }
  267.                 for ( sub = item->em_Items->ml_First; sub->em_Next; sub = sub->em_Next ) {
  268.                     if ( ! AddString( &Strings, &sub->em_MenuTitle[ 0 ] ))
  269.                         goto memoryError;
  270.                     if ( strlen( &sub->em_CommKey[ 0 ] )) {
  271.                         if ( ! AddString( &Strings, &sub->em_CommKey[ 0 ] ))
  272.                             goto memoryError;
  273.                     }
  274.                 }
  275.             }
  276.         }
  277.     }
  278.     NumberDuplicates();
  279.     return( TRUE );
  280.  
  281.     memoryError:
  282.     Print( STRING( MSG_OUT_OF_MEMORY ));
  283.     return( FALSE );
  284. }
  285.  
  286. /*
  287.  *      Free up the duplicates.
  288.  */
  289. VOID FreeDuplicates( void )
  290. {
  291.     struct Node         *node;
  292.  
  293.     while ( node = RemHead(( struct List * )&Strings ))
  294.         FreeVecItem( Chain, node );
  295.  
  296.     while ( node = RemHead(( struct List * )&Arrays ))
  297.         FreeVecItem( Chain, node );
  298. }
  299.