home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / commodity / newedit18b / newedit_source.lha / Routines.c < prev   
C/C++ Source or Header  |  1994-05-30  |  3KB  |  125 lines

  1. #include <ctype.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <exec/types.h>
  6.  
  7. /* Function Prototypes */
  8. LONG next_word( UBYTE *buffer, LONG pos, LONG max );
  9. LONG prev_word( UBYTE *buffer, LONG pos );
  10. char *IncrementNum( char *target, char *string );
  11. void ToggleCase( UBYTE *character );
  12.  
  13. /* Maximum length of number substrings */
  14. #define MAXLEN 20
  15.  
  16. LONG next_word( UBYTE *buffer, LONG pos, LONG max )
  17. {
  18.     while ( pos < max && !isspace( buffer[ pos ] ) ) pos++;
  19.     while ( pos < max && isspace( buffer[ pos ] ) ) pos++;
  20.  
  21.     return pos;
  22. }
  23.  
  24. LONG prev_word( UBYTE *buffer, LONG pos )
  25. {
  26.     while ( pos > 0 && isspace( buffer[ pos - 1 ] ) ) pos--;
  27.     while ( pos > 0 && !isspace( buffer[ pos - 1 ] ) ) pos--;
  28.     return pos;
  29. }
  30.  
  31. char *IncrementNum( char *target, char *string )
  32. {
  33.     char *num_start, *num_end = NULL;
  34.     char numstr[ MAXLEN + 1 ];
  35.     int i, j = 0, pos, number;
  36.     BOOL FoundNumber = FALSE;
  37.  
  38.     /* Clear the target string */
  39.     target[0] = 0;
  40.  
  41.     /* Position the search after any :'s or /'s to ignore paths */
  42.     for ( pos = strlen( string ); pos > 0; pos-- )
  43.     {
  44.         if ( string[pos] == ':' || string[pos] == '/' )
  45.         {
  46.             pos++;
  47.             break;
  48.         }
  49.     }
  50.  
  51.     /* Locate the last number in the string */
  52.     for ( i = strlen( string ); i >= pos; i-- )
  53.     {
  54.         /* Locate a digit character */
  55.         if ( isdigit( string[i] ) != 0 )
  56.         {
  57.             /* Store a pointer to this location in string[] */
  58.             num_end = &string[i];
  59.  
  60.             /* Copy out the embedded number - including leading zeros */
  61.             while ( isdigit( string[i] ) != 0 )
  62.             {
  63.                 numstr[ j++ ] = string[i--]; // Copy the number
  64.  
  65.                 numstr[j] = 0; // Terminate the string
  66.  
  67.                 if ( j == MAXLEN ) break; // Only for as long as our temporary string
  68.             }
  69.             strrev( numstr ); // String will be in reverse
  70.             number = atoi( numstr ); // Convert to a decimal number
  71.             FoundNumber = TRUE;
  72.  
  73.             break; // Get outta this loop
  74.         }
  75.     }
  76.  
  77.     /* If we didn't find a number, abort */
  78.     if ( FoundNumber == FALSE ) return NULL;
  79.  
  80.     /* Find the start position of our number substring */
  81.     num_start = num_end - strlen( numstr ) + 1;
  82.  
  83.     /* Increment the number */
  84.     number++;
  85.  
  86.     /* Store length of the original number string - including leading zeros */
  87.     j = strlen( numstr );
  88.  
  89.     /* Calculate the length of the bit before the number */
  90.     i = num_start - string;
  91.  
  92.     /* Convert the new number back to a string */
  93.     stci_d( numstr, number );
  94.  
  95.     /* Copy the first part of string[] into our new destination */
  96.     strncat( target, string, i );
  97.  
  98.     /* Add leading zeros - if required */
  99.     if ( j > strlen( numstr ) )
  100.     {
  101.         for ( i = 0; i < j - strlen( numstr ); i++ )
  102.         {
  103.             strcat( target, "0" );
  104.         }
  105.     }
  106.  
  107.     /* Add the new number */
  108.     strcat( target, numstr );
  109.  
  110.     /* Add the remaining part of string[] */
  111.     strcat( target, num_start + j );
  112.  
  113.     return target;
  114. }
  115.  
  116. void ToggleCase( UBYTE *character )
  117. {
  118.     /* Only touch alphabetical characters */
  119.     if ( isalpha( *character ) != 0 )
  120.     {
  121.         if ( islower( *character ) != 0 ) *character = toupper( *character );
  122.         else *character = tolower( *character );
  123.     }
  124. }
  125.