00001 #include "niml.h"
00002
00003
00004
00005
00006
00007
00008 int main( int argc , char *argv[] )
00009 {
00010 NI_stream ns , nsout , nsf=NULL ;
00011 int nn , tt , nopt=1 , bmode , tflag=0 ;
00012 void *nini ;
00013 char *ccc ;
00014
00015 if( argc < 2 ){
00016 printf("Usage: nimltest [-bB fname] [-w[r]] [-#] streamspec\n");exit(0);
00017 }
00018
00019 #if 0
00020 for( nn=0 ; nn < 5 ; nn++ ){
00021 ccc = UNIQ_idcode() ;
00022 fprintf(stderr,"%d: %s\n",nn,ccc) ; free(ccc) ;
00023 }
00024 ccc = UNIQ_hashcode("Elvis") ;
00025 fprintf(stderr," %s\n",ccc) ; free(ccc) ;
00026 #endif
00027
00028
00029
00030 if( strncmp(argv[1],"-w",2) == 0 ){
00031 char lbuf[1024] , *bbb ;
00032 int raw=(argv[1][2]=='r') ;
00033 if( argc < 3 ) exit(1) ;
00034 nopt = 2 ;
00035 ns = NI_stream_open( argv[nopt] , "w" ) ;
00036 if( ns == NULL ){
00037 fprintf(stderr,"NI_stream_open fails\n") ; exit(1) ;
00038 }
00039 while(1){
00040 nn = NI_stream_writecheck( ns , 400 ) ;
00041 if( nn == 1 ){ fprintf(stderr,"\n") ; break ; }
00042 if( nn < 0 ){ fprintf(stderr,"BAD writecheck\n"); exit(1) ; }
00043 fprintf(stderr,".") ;
00044 }
00045 if( !raw ){
00046 while(1){
00047 fprintf(stderr,"READY> ") ;
00048 bbb = fgets( lbuf , 1024 , stdin ) ; if( bbb == NULL ) exit(0) ;
00049 nn = NI_stream_write( ns , lbuf , strlen(lbuf) ) ;
00050 if( nn < 0 ){
00051 fprintf(stderr,"NI_stream_write fails\n"); exit(1);
00052 }
00053 }
00054 } else {
00055 int nbyt ;
00056 while(1){
00057 nbyt = fread( lbuf , 1,1024 , stdin ) ;
00058 if( nbyt <= 0 ){ NI_sleep(10) ; NI_stream_close(ns) ; exit(0) ; }
00059 nn = NI_stream_write( ns , lbuf , nbyt ) ;
00060 if( nn < 0 ){
00061 fprintf(stderr,"NI_stream_write fails\n"); exit(1);
00062 }
00063 }
00064 }
00065 }
00066
00067 if( strcmp(argv[1],"-b") == 0 || strcmp(argv[1],"-B") == 0 ){
00068 char fname[256] ;
00069 nopt = 3 ;
00070 if( argc < 4 ){ fprintf(stderr,"Too few args\n"); exit(1); }
00071
00072 sprintf(fname,"file:%s",argv[2]) ;
00073 nsf = NI_stream_open( fname, "w" ) ;
00074 if( nsf == NULL ) fprintf(stderr,"Can't open %s\n",fname) ;
00075
00076 bmode = (strcmp(argv[1],"-b") == 0) ? NI_BINARY_MODE
00077 : NI_BASE64_MODE ;
00078 }
00079
00080 if( strcmp(argv[nopt],"-#") == 0 ){
00081 tflag = NI_HEADERSHARP_FLAG ; nopt++ ;
00082 }
00083
00084
00085
00086 NI_add_trusted_host(NULL) ;
00087
00088 ns = NI_stream_open( argv[nopt] , "r" ) ;
00089 if( ns == NULL ){
00090 fprintf(stderr,"NI_stream_open fails\n") ; exit(1) ;
00091 }
00092 while(1){
00093 nn = NI_stream_goodcheck( ns , 400 ) ;
00094 if( nn == 1 ){ fprintf(stderr,"\n") ; break ; }
00095 if( nn < 0 ){ fprintf(stderr,"BAD goodcheck\n"); exit(1) ; }
00096 fprintf(stderr,".") ;
00097 }
00098
00099 GetElement:
00100 nini = NI_read_element( ns , -1 ) ;
00101 if( nini == NULL ){
00102 if( NI_stream_goodcheck(ns,0) < 0 ){
00103 fprintf(stderr,"NI_read_element fails\n") ; exit(1) ;
00104 }
00105 NI_sleep(999) ; goto GetElement ;
00106 }
00107
00108 tt = NI_element_type( nini ) ;
00109
00110 if( tt == NI_ELEMENT_TYPE ){
00111 NI_element *nel = (NI_element *) nini ;
00112 fprintf(stderr,"Data element:\n"
00113 " name = %s\n"
00114 " vec_num = %d\n"
00115 " vec_len = %d\n"
00116 " vec_filled = %d\n"
00117 " vec_rank = %d\n"
00118 " attr_num = %d\n" ,
00119 nel->name,nel->vec_num,nel->vec_len,nel->vec_filled,
00120 nel->vec_rank,nel->attr_num );
00121 for( nn=0 ; nn < nel->attr_num ; nn++ )
00122 fprintf(stderr," %2d: lhs=%s rhs=%s\n",
00123 nn , nel->attr_lhs[nn] , nel->attr_rhs[nn] ) ;
00124
00125 #if 0
00126 for( nn=0 ; nn < nel->vec_rank ; nn++ ){
00127 fprintf(stderr," axis[%d]: len=%d delta=%f origin=%f unit=%s label=%s\n",
00128 nn , nel->vec_axis_len[nn] ,
00129 (nel->vec_axis_delta) ? nel->vec_axis_delta[nn] : -666.0 ,
00130 (nel->vec_axis_origin) ? nel->vec_axis_origin[nn] : -666.0 ,
00131 (nel->vec_axis_unit) ? nel->vec_axis_unit[nn] : "NULL" ,
00132 (nel->vec_axis_label) ? nel->vec_axis_label[nn] : "NULL" ) ;
00133 }
00134 #endif
00135 } else if( tt == NI_GROUP_TYPE ){
00136 NI_group *ngr = (NI_group *) nini ;
00137 fprintf(stderr,"Group element:\n"
00138 " part_num = %d\n"
00139 " attr_num = %d\n" ,
00140 ngr->part_num , ngr->attr_num ) ;
00141 for( nn=0 ; nn < ngr->attr_num ; nn++ )
00142 fprintf(stderr," %2d: lhs=%s rhs=%s\n",
00143 nn , ngr->attr_lhs[nn] , ngr->attr_rhs[nn] ) ;
00144
00145 } else if( tt == NI_PROCINS_TYPE ){
00146 NI_procins *npi = (NI_procins *)nini ;
00147 fprintf(stderr,"Processing instruction:\n"
00148 " target = %s\n" , npi->name ) ;
00149 for( nn=0 ; nn < npi->attr_num ; nn++ )
00150 fprintf(stderr," %2d: lhs=%s rhs=%s\n",
00151 nn , npi->attr_lhs[nn] , npi->attr_rhs[nn] ) ;
00152 }
00153
00154 #if 1
00155 nsout = NI_stream_open( "str:" , "w" ) ;
00156 if( nsout == NULL ){
00157 fprintf(stderr,"NI_stream_open fails for output\n"); exit(1);
00158 }
00159
00160 nn = NI_write_element( nsout , nini , NI_TEXT_MODE | tflag ) ;
00161
00162 fprintf(stderr,"\n------ NI_write_element = %d ------\n%s\n==========================\n" ,
00163 nn, NI_stream_getbuf(nsout) ) ;
00164 NI_stream_close(nsout) ;
00165 #endif
00166
00167 if( nsf != NULL ){
00168 nn = NI_write_element( nsf , nini , bmode ) ;
00169 fprintf(stderr,"NI_write_element to file = %d\n",nn) ;
00170 }
00171
00172 goto GetElement ;
00173 }