1 /** \file pretty_ref.c
2  * split out of exppp.c 9/21/13
3  */
4 
5 #include <express/schema.h>
6 #include <exppp/exppp.h>
7 
8 #include "pp.h"
9 #include "pretty_ref.h"
10 
REFout(Dictionary refdict,Linked_List reflist,char * type,int level)11 void REFout( Dictionary refdict, Linked_List reflist, char * type, int level ) {
12     Dictionary dict;
13     DictionaryEntry de;
14     struct Rename * ren;
15     Linked_List list;
16 
17     LISTdo( reflist, s, Schema )
18     raw( "%s FROM %s;\n", type, s->symbol.name );
19     LISTod
20 
21     if( !refdict ) {
22         return;
23     }
24     dict = DICTcreate( 10 );
25 
26     /* sort each list by schema */
27 
28     /* step 1: for each entry, store it in a schema-specific list */
29     DICTdo_init( refdict, &de );
30     while( 0 != ( ren = ( struct Rename * )DICTdo( &de ) ) ) {
31         Linked_List nameList;
32 
33         nameList = ( Linked_List )DICTlookup( dict, ren->schema->symbol.name );
34         if( !nameList ) {
35             nameList = LISTcreate();
36             DICTdefine( dict, ren->schema->symbol.name, ( Generic ) nameList,
37                         ( Symbol * )0, OBJ_UNKNOWN );
38         }
39         LISTadd_last( nameList, ( Generic ) ren );
40     }
41 
42     /* step 2: for each list, print out the renames */
43     level = 6;  /* no special reason, feels good */
44     indent2 = level + exppp_continuation_indent;
45     DICTdo_init( dict, &de );
46     while( 0 != ( list = ( Linked_List )DICTdo( &de ) ) ) {
47         bool first_time = true;
48         LISTdo( list, r, struct Rename * ) {
49             if( first_time ) {
50                 raw( "%s FROM %s\n", type, r->schema->symbol.name );
51             } else {
52                 /* finish previous line */
53                 raw( ",\n" );
54             }
55 
56             if( first_time ) {
57                 raw( "%*s( ", level, "" );
58                 first_time = false;
59             } else {
60                 raw( "%*s ", level, "" );
61             }
62             raw( r->old->name );
63             if( r->old != r->nnew ) {
64                 wrap( " AS %s", r->nnew->name );
65             }
66         } LISTod
67         raw( " );\n" );
68     }
69     HASHdestroy( dict );
70 }
71