source: libcfa/src/enum.cfa @ dbff8ec

Last change on this file since dbff8ec was d287f3e, checked in by Peter A. Buhr <pabuhr@…>, 11 days ago

fix selecting wrong input operator for enumeration, first attempt at reading enumerators

  • Property mode set to 100644
File size: 2.2 KB
Line 
1#include "enum.hfa"
2#include "fstream.hfa"
3#include <stdlib.h>                                                                             // qsort
4#include <string.h>
5
6#pragma GCC visibility push(default)
7
8int scmp( const void * str1, const void * str2 ) {
9    return -strcmp( *(char **)str1, *(char **)str2 );   // dscending order
10} // scmp
11
12forall( istype & | istream( istype ), E, V | CfaEnum( E, V ) )
13istype & ?|?( istype & is, E & e ) {
14//      printf( "here0\n" );
15        if ( eof( is ) ) 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;
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;
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;
53        } else {
54                //ExceptionInst( missing_data );
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
69        return is;
70}
71
72forall( ostype & | ostream( ostype ), E, V | CfaEnum( E, V ) ) {
73        ostype & ?|?( ostype & os, E e ) {
74                return os | label( e );
75        }
76        OSTYPE_VOID_IMPL( E )
77}
78
79forall( 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 )
84}
Note: See TracBrowser for help on using the repository browser.