Changeset d287f3e
- Timestamp:
- Jul 8, 2024, 8:40:20 PM (3 months ago)
- Branches:
- master
- Children:
- 2f4c910
- Parents:
- c015e2d
- Location:
- libcfa/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/enum.cfa
rc015e2d rd287f3e 1 1 #include "enum.hfa" 2 2 #include "fstream.hfa" 3 #include <stdlib.h> // qsort 3 4 #include <string.h> 4 5 5 6 #pragma GCC visibility push(default) 6 7 8 int scmp( const void * str1, const void * str2 ) { 9 return -strcmp( *(char **)str1, *(char **)str2 ); // dscending order 10 } // scmp 11 7 12 forall( istype & | istream( istype ), E, V | CfaEnum( E, V ) ) 8 13 istype & ?|?( istype & is, E & e ) { 14 // printf( "here0\n" ); 9 15 if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 10 char val[256]; 11 int args = fmt( is, "%255s", val ); 12 if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 16 17 // Match input enumerator string to enumerator labels. 18 int len = -1; 19 const char * cpy[ 20 /*countof( E )*/ ]; 20 int i = 0; 13 21 for ( s; E ) { 14 if ( strcmp(val, label( s )) == 0 ) { e = s; break; } 22 cpy[i] = label( s ); 23 printf( "%s\n", cpy[i] ); 24 i += 1; 25 } 26 printf( "%d\n", i ); 27 qsort( cpy, i, sizeof(char*), scmp ); 28 i = 0; 29 for ( s; E ) { 30 printf( "%s\n", cpy[i] ); 31 i += 1; 32 } 33 int j = 0; 34 X : for ( s; E ) { 35 len = strlen( cpy[j] ); 36 printf( "%s %d\n", cpy[j], len ); 37 char fmtstr[len + 16]; 38 fmtstr[0] = ' '; // optional leadig whitespace 39 strcpy( &fmtstr[1], cpy[j] ); // copy format and add %n 40 strcpy( &fmtstr[len + 1], "%n" ); 41 printf( "%s\n", fmtstr ); 42 len = -1; 43 // scanf cursor does not move if no match 44 fmt( is, fmtstr, &len ); 45 printf( "%s %d %d\n", fmtstr, len, j ); 46 if ( eof( is ) ) { break; } 47 if ( len != -1 ) { 48 for ( s; E ) { 49 if ( strcmp( label( s ), cpy[j] ) == 0 ) { e = s; break X; } 50 } 51 } 52 j += 1; 15 53 } else { 16 fprintf( stderr, "invalid enumeration constant\n" ); 17 abort(); // cannot use abort stream 54 //ExceptionInst( missing_data ); 18 55 } // for 56 printf( "X %s %d\n", label( e ), len ); 57 if ( ! eof( is ) && len == -1 ) throwResume ExceptionInst( missing_data ); 58 59 // if ( eof( is ) ) throwResume ExceptionInst( missing_data ); 60 // char val[256]; 61 // int args = fmt( is, "%255s", val ); 62 // if ( ! eof( is ) && args != 1 ) throwResume ExceptionInst( missing_data ); 63 // for ( s; E ) { 64 // if ( strcmp(val, label( s )) == 0 ) { e = s; break; } 65 // } else { 66 // fprintf( stderr, "invalid enumeration constant\n" ); 67 // abort(); // cannot use abort stream 68 // } // for 19 69 return is; 20 70 } -
libcfa/src/enum.hfa
rc015e2d rd287f3e 17 17 // Design one 18 18 forall( E, V | Serial( E ) ) trait CfaEnum { 19 c har * label( E e );19 const char * label( E e ); 20 20 unsigned int posn( E e ); 21 21 V value( E e );
Note: See TracChangeset
for help on using the changeset viewer.