Code:
#include <stdio.h>
#include <string.h>
void expand( char * , int );
void print_range( char , char );
int main( int argc , char **argv )
{
if( argc != 2 )
printf( "Usage: %s [shorthand notation]\n" , argv[ 0 ] );
else{
expand( argv[ 1 ] , strlen( argv[ 1 ] ));
printf( "\n" );
}
return 0;
}
void expand( char * string , int str_len ){
int i = 0 , partner = 0 , hyphen_pair = 0;
char * start = NULL , * end = NULL;
static char upper[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static char lower[27] = "abcdefghijklmnopqrstuvwxyz";
static char digits[11] = "0123456789";
while( i < str_len ){
if( string[ i ] == '-' && ( i == 0 || i == ( str_len - 1 ) ) )
printf( "-" ); // leading or trailing '-'
else /* parsing for "upper ranges". either right next to each other or '-' seperated is allowable */
if( ( start = strchr( upper , string[ i ] ) ) != NULL ){
end = NULL;
if( ( end = strchr( upper , string[ i + 1 ] ) ) != NULL ){
print_range( * start, * end );/* next char is also upper, allowable syntax by my "standards" */
partner++;
}
else if( ( end = strchr( lower , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is lower-case
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
partner++;
}
else if( ( end = strchr( digits , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is digit
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
partner++;
}
else
if( string[ i + 1 ] == '-' ){ // "upper-" found, checking data type of 2nd operand
hyphen_pair++;
if( ( end = strchr( upper , string[ i + 2 ] ) ) != NULL ){ // "upper-upper" found
print_range( * start , * end );
}
else if( ( end = strchr( lower , string[ i + 2 ] ) ) != NULL ) // "upper-lower" found; error
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
else if( ( end = strchr( digits , string[ i + 2 ] ) ) != NULL ) // "upper-digit" found; error
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
}
else{
printf( "\n[!] invalid character detected, '%c': ABORT!\n" , string[ i + 1 ] );
break; // error recovery is non-existent if invalid character is used (non alpha_numeric or not a hyphen(-)
}
start = NULL;
}else if( ( start = strchr( lower , string[ i ] ) ) != NULL ){
/* parsing for "lower ranges". either right next to each other or '-' seperated is allowable */
end = NULL;
if( ( end = strchr( lower , string[ i + 1 ] ) ) != NULL ){
print_range( * start, * end );/* next char is also lower, allowable syntax by my "standards" */
partner++;
}
else if( ( end = strchr( upper , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is upper-case; error
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
partner++;
}
else if( ( end = strchr( digits , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is digit; error
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
partner++;
}
else
if( string[ i + 1 ] == '-' ){ // "lower-" found, checking data type of 2nd operand
hyphen_pair++;
if( ( end = strchr( lower , string[ i + 2 ] ) ) != NULL ){ // "lower-lower" found
print_range( * start , * end );
}
else if( ( end = strchr( upper , string[ i + 2 ] ) ) != NULL ) // "lower-upper" found; error
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
else if( ( end = strchr( digits , string[ i + 2 ] ) ) != NULL ) // "lower-digit" found; error
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
}
else{
printf( "\n[!] invalid character detected, '%c': ABORT!\n" , string[ i + 1 ] );
break;
}
start = NULL;
}
else /* parsing for "digit ranges". either right next to each other or '-' seperated is allowable */
if( ( start = strchr( digits , string[ i ] ) ) != NULL ){
end = NULL;
if( ( end = strchr( digits , string[ i + 1 ] ) ) != NULL ){
print_range( * start, * end );/* next char is also digit, allowable syntax by my "standards" */
partner++;
}
else if( ( end = strchr( upper , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is upper-case; error
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
partner++;
}
else if( ( end = strchr( lower , string[ i + 1 ] ) ) != NULL ){ // 2nd operand is lower; error
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
partner++;
}
else
if( string[ i + 1 ] == '-' ){ // "digit-" found, checking data type of 2nd operand
hyphen_pair++;
if( ( end = strchr( digits , string[ i + 2 ] ) ) != NULL ){ // "digit-digit" found
print_range( * start , * end );
}
else if( ( end = strchr( upper , string[ i + 2 ] ) ) != NULL ) // "digit-upper" found; error
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
else if( ( end = strchr( lower , string[ i + 2 ] ) ) != NULL ) // "digit-lower" found; error
printf( "\n[!] mis-matched operands: %c%c\n" , * start , * end );
}
else{
printf( "[!] invalid character detected, '%c': ABORT!\n" , string[ i + 1 ] );
break;
}
start = NULL;
}
else{
printf( "[!] invalid character detected, '%c': ABORT!\n" , string[ i ] );
break;
}
////////////////////////////
if( partner ){//////////////
i += 2;/////////////////
partner = 0;//////////// this block sets the next starting position in string[]
}///////////////////////////
else if( hyphen_pair ){/////
i += 3;/////////////////
hyphen_pair = 0;////////
}///////////////////////////
else////////////////////////
i++;////////////////////
} ////////////////////////////
return;
}
void print_range( char start , char end ){
int i;
if( start < end ) /* fowards */
for( i = ( int ) start; i <= ( int ) end; ++i )
printf( "%c" , i );
else if( start > end ) /* backwards */
for( i = ( int ) start; i >= ( int ) end; --i )
printf( "%c" , i );
else
printf( "%c" , start );
return;
}
Thanks in advance for any and all help!