Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/enum.cfa

    r6d2b3dc rd287f3e  
    11#include "enum.hfa"
    22#include "fstream.hfa"
     3#include <stdlib.h>                                                                             // qsort
    34#include <string.h>
    45
    56#pragma GCC visibility push(default)
    67
     8int scmp( const void * str1, const void * str2 ) {
     9    return -strcmp( *(char **)str1, *(char **)str2 );   // dscending order
     10} // scmp
     11
    712forall( istype & | istream( istype ), E, V | CfaEnum( E, V ) )
    813istype & ?|?( istype & is, E & e ) {
     14//      printf( "here0\n" );
    915        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;
    1321        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;
    1553        } else {
    16                 fprintf( stderr, "invalid enumeration constant\n" );
    17                 abort();                                                                        // cannot use abort stream
     54                //ExceptionInst( missing_data );
    1855        } // 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
    1969        return is;
    2070}
Note: See TracChangeset for help on using the changeset viewer.