#include "niml.h" /*************************************************************************/ /*** Compile with gcc -O2 -o nini nini.c niml.c -I. ***/ /*************************************************************************/ typedef struct { int a; short b; float c; complex d; char *e; } zork ; int main( int argc , char *argv[] ) { NI_stream ns ; NI_element *nel ; int ii ; static zork sss={0,0,0,{0,0},NULL} ; char buf[128] ; if( argc > 1 && strcmp(argv[1],"-help") == 0 ){ printf("Usage: nini [streamspec]\n");exit(0); } /* writing to the screen? */ if( argc < 2 ){ nel = NI_new_data_element( "junk" , -1 ) ; NI_define_rowmap_VA( nel , NI_INT , offsetof(zork,a) , NI_SHORT , offsetof(zork,b) , NI_FLOAT , offsetof(zork,c) , NI_COMPLEX , offsetof(zork,d) , NI_STRING , offsetof(zork,e) , -1 ) ; for( ii=0 ; ii < 19 ; ii++ ){ sss.a = 100*ii+1 ; sss.b = 100*ii+2 ; sss.c = 100*ii+3.3 ; sss.d.r = 100*ii+4.4 ; sss.d.i = 100*ii+5.5 ; sprintf(buf,"Hi #%d",ii) ; sss.e = strdup(buf) ; NI_add_row( nel , &sss ) ; } fprintf(stderr,"nel: vec_len=%d vec_num=%d\n",nel->vec_len,nel->vec_num) ; ns = NI_stream_open( "str:" , "w" ) ; NI_write_element( ns , nel , NI_TEXT_MODE ) ; fprintf(stderr,"%s\n", NI_stream_getbuf(ns) ) ; exit(0) ; } /* reading from a stream */ ns = NI_stream_open( argv[1] , "r" ) ; if( ns == NULL ){fprintf(stderr,"NI_stream_open fails\n");exit(1);} nel = NI_read_element( ns , -1 ) ; if( nel == NULL ){fprintf(stderr,"NI_read_element fails\n");exit(1);} NI_stream_close(ns) ; fprintf(stderr,"nel: vec_len=%d vec_num=%d\n",nel->vec_len,nel->vec_num) ; ns = NI_stream_open( "str:" , "w" ) ; NI_write_element( ns , nel , NI_TEXT_MODE ) ; fprintf(stderr,"%s\n", NI_stream_getbuf(ns) ) ; NI_stream_close(ns) ; NI_define_rowmap_VA( nel , NI_INT , offsetof(zork,a) , NI_SHORT , offsetof(zork,b) , NI_FLOAT , offsetof(zork,c) , NI_COMPLEX , offsetof(zork,d) , NI_STRING , offsetof(zork,e) , -1 ) ; for( ii=0 ; ii < nel->vec_len ; ii++ ){ NI_get_row( nel , ii , &sss ) ; fprintf(stderr,"row %d: a=%d b=%d c=%g d=(%g,%g) e=%s\n", ii , sss.a , sss.b , sss.c , sss.d.r,sss.d.i , sss.e ) ; } NI_free_element(nel) ; exit(0) ; }