[c333ed2] | 1 | #include "enum.hfa"
|
---|
[85855b0] | 2 | #include "fstream.hfa"
|
---|
[d287f3e] | 3 | #include <stdlib.h> // qsort
|
---|
[6d2b3dc] | 4 | #include <string.h>
|
---|
[c333ed2] | 5 |
|
---|
[03ac869] | 6 | #pragma GCC visibility push(default)
|
---|
| 7 |
|
---|
[d287f3e] | 8 | int scmp( const void * str1, const void * str2 ) {
|
---|
| 9 | return -strcmp( *(char **)str1, *(char **)str2 ); // dscending order
|
---|
| 10 | } // scmp
|
---|
| 11 |
|
---|
[64eeb06] | 12 | forall( istype & | istream( istype ), E, V | CfaEnum( E, V ) )
|
---|
| 13 | istype & ?|?( istype & is, E & e ) {
|
---|
[d287f3e] | 14 | // printf( "here0\n" );
|
---|
[64eeb06] | 15 | if ( eof( is ) ) throwResume ExceptionInst( missing_data );
|
---|
[d287f3e] | 16 |
|
---|
| 17 | // Match input enumerator string to enumerator labels.
|
---|
| 18 | int len = -1;
|
---|
| 19 | const char * cpy[ 20 /*countof( E )*/ ];
|
---|
| 20 | int i = 0;
|
---|
| 21 | for ( s; E ) {
|
---|
| 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;
|
---|
[6d2b3dc] | 29 | for ( s; E ) {
|
---|
[d287f3e] | 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;
|
---|
[6d2b3dc] | 53 | } else {
|
---|
[d287f3e] | 54 | //ExceptionInst( missing_data );
|
---|
[6d2b3dc] | 55 | } // for
|
---|
[d287f3e] | 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
|
---|
[64eeb06] | 69 | return is;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | forall( ostype & | ostream( ostype ), E, V | CfaEnum( E, V ) ) {
|
---|
| 73 | ostype & ?|?( ostype & os, E e ) {
|
---|
[d5efcb7] | 74 | return os | label( e );
|
---|
| 75 | }
|
---|
| 76 | OSTYPE_VOID_IMPL( E )
|
---|
[85855b0] | 77 | }
|
---|
[c333ed2] | 78 |
|
---|
[64eeb06] | 79 | forall( ostype & | ostream( ostype ), E | CfaEnum( E, quasi_void ) ) {
|
---|
| 80 | ostype & ?|?( ostype & os, E e ) {
|
---|
| 81 | return os | label( e );
|
---|
| 82 | }
|
---|
| 83 | OSTYPE_VOID_IMPL( E )
|
---|
[259012e] | 84 | }
|
---|