1 #ifndef _NIML_HEADER_FILE_
2 #define _NIML_HEADER_FILE_
3 
4 #include <stddef.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <ctype.h>
9 #include <errno.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #ifndef TCP_NODELAY
17 # include <netinet/tcp.h>
18 #endif
19 #include <netdb.h>
20 #include <arpa/inet.h>
21 #include <sys/time.h>
22 #include <fcntl.h>
23 #include <sys/times.h>
24 #include <limits.h>
25 #include <math.h>     /* 14 Sep 2018 */
26 
27 #ifdef  __cplusplus
28 extern "C" {                    /* care of Greg Balls    7 Aug 2006 [rickr] */
29 #endif
30 
31 /* preferentially include f2c header from local directory, otherwise use system
32  header */
33 #include "f2c.h"
34 /* The following was added to harmonize with system f2c header. Subsequent
35 typedef for complex is now ignored */
36 #define TYPEDEF_complex
37 
38 
39 /*****---------------------------------------------------*****/
40 
41 /* This is suppose to be defined in stddef.h, but
42    apparently it isn't on all systems for some reason. */
43 
44 #ifndef offsetof
45 # define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
46 #endif
47 
48 /*****---------------------------------------------------*****/
49 
50 #ifndef TYPEDEF_byte
51 #define TYPEDEF_byte
52 typedef unsigned char byte ;
53 #endif
54 
55 #ifndef TYPEDEF_rgb
56 #define TYPEDEF_rgb
57 typedef struct { byte r,g,b ; } rgb ;
58 #endif
59 
60 #ifndef TYPEDEF_rgba
61 #define TYPEDEF_rgba
62 typedef struct { byte r,g,b,a ; } rgba ;
63 #endif
64 
65 #ifdef _SUNPERF_COMPLEX
66 # define TYPEDEF_complex
67 #endif
68 
69 #ifndef TYPEDEF_complex
70 #define TYPEDEF_complex
71 typedef struct { float r,i ; } complex ;
72 #endif
73 
74 /*****---------------------------------------------------*****/
75 
76 /***** Macros for data type codes. *****/
77 /*---- The first 8 match mrilib.h. ----*/
78 
79 #define NI_BYTE        0               /* == MRI_byte    */
80 #define NI_SHORT       1               /* == MRI_short   */
81 #define NI_INT         2               /* == MRI_int     */
82 #define NI_FLOAT32     3               /* == MRI_float   */
83 #define NI_FLOAT       NI_FLOAT32
84 #define NI_FLOAT64     4               /* == MRI_double  */
85 #define NI_DOUBLE      NI_FLOAT64
86 #define NI_COMPLEX64   5               /* == MRI_complex */
87 #define NI_COMPLEX     NI_COMPLEX64
88 #define NI_RGB         6               /* == MRI_rgb     */
89 #define NI_RGBA        7               /* == MRI_rgba    */
90 
91 #define NI_IS_NUMERIC_TYPE(t) ( (t) >= 0 && (t) <= NI_RGBA )
92 
93 #define NI_STRING      8               /* after "basic" types */
94 
95 /*! One more than the last NI_ data type code defined above. */
96 
97 #define NI_NUM_TYPES        9
98 
99 /*! Number of types of fixed size ("basic" types).
100     Note that if this changes,
101     the NI_rowtype stuff must be altered accordingly. */
102 
103 #define NI_NUM_BASIC_TYPES  8
104 
105 /*! Valid data type character codes. */
106 
107 #define IS_DATUM_CHAR(c) ( (c) == 'b' || (c) == 's' || (c) == 'i' ||  \
108                            (c) == 'f' || (c) == 'd' || (c) == 'c' ||  \
109                            (c) == 'r' || (c) == 'S' || (c) == 'L' ||  \
110                            (c) == 'R'                               )
111 
112 #define NI_is_builtin_type(t)                                         \
113   ( (t) >= 0 && (t) < NI_NUM_TYPES )
114 
115 #define NI_is_basic_type(t)                                           \
116   ( (t) >= 0 && (t) < NI_NUM_BASIC_TYPES )
117 
118 /*--------------------------------------------------------------------------*/
119 /*! This type stores the information about user-defined types. 09 Dec 2002. */
120 
121 #ifndef TYPEDEF_NI_rowtype
122 #define TYPEDEF_NI_rowtype
123 struct NI_rowtype ;  /* incomplete definition */
124 
125 typedef struct NI_rowtype {
126   int   code ;         /*!< unique integer code for this type */
127   int   size ;         /*!< number of bytes for this type (w/padding) */
128   int   psiz ;         /*!< sum of sizes of the parts (no padding)
129                             - will be zero if has variable type arrays */
130   int   algn ;         /*!< byte alignment for this type */
131   int   flag ;         /*!< various bit flags */
132   char *name ;         /*!< unique string name for this type */
133   char *userdef ;      /*!< definition user gave for this type */
134   int   comp_num ;     /*!< number of components (components may be rowtypes) */
135   int  *comp_typ ;     /*!< integer codes of the components */
136   int  *comp_dim ;     /*!< if >=0, index of dimension of this component */
137   int   part_num ;     /*!< number of parts (parts are usually basic types) */
138   int  *part_typ ;     /*!< integer codes of the parts */
139   int  *part_off ;     /*!< byte offsets of the parts */
140   int  *part_siz ;     /*!< byte sizes of the parts */
141   int  *part_dim ;     /*!< if >=0, index of dimension of this part */
142 
143   struct NI_rowtype **part_rtp; /*!< rowtype ptr for each part;
144                                      N.B.: builtin types point to themselves! */
145 } NI_rowtype ;
146 #endif
147 
148 /*! NI_rowtype bit flag for variable size data. */
149 
150 #define ROWTYPE_VARSIZE_MASK (1<<0)
151 
152 /*! Check if a NI_rowtype struct is marked as having variable size data */
153 
154 #define ROWTYPE_is_varsize(rr) (((rr)->flag & ROWTYPE_VARSIZE_MASK) != 0)
155 
156 /*! Get the dimension of the qq-th part of
157     the struct stored at pointer pt, of type rt.
158     This macro should only be used if rt->part_dim[qq] >= 0. */
159 
160 #define ROWTYPE_part_dimen(rt,pt,qq)                           \
161  ( *((int *)( (pt) + (rt)->part_off[ (rt)->part_dim[qq] ] )) )
162 
163 /*! Macro to delete a NI_rowtype struct.  Only used when an
164     error happens when creating one, since new types last forever. */
165 
166 #define delete_rowtype(rr)                 \
167  do{ NI_free((rr)->name)     ;             \
168      NI_free((rr)->userdef)  ;             \
169      NI_free((rr)->comp_typ) ;             \
170      NI_free((rr)->part_typ) ;             \
171      NI_free((rr)->part_off) ;             \
172      NI_free(rr)             ; } while(0)
173 
174 extern int          NI_rowtype_define       ( char *, char * ) ;
175 extern NI_rowtype * NI_rowtype_find_name    ( char * ) ;
176 extern NI_rowtype * NI_rowtype_find_code    ( int ) ;
177 extern int          NI_rowtype_name_to_code ( char * ) ;
178 extern char *       NI_rowtype_code_to_name ( int ) ;
179 extern char *       NI_rowtype_code_to_alias( int ) ;    /* 19 Feb 2003 */
180 extern int          NI_rowtype_name_to_size ( char * ) ;
181 extern int          NI_rowtype_code_to_size ( int ) ;
182 
183 extern int          NI_rowtype_vsize     ( NI_rowtype *, void * ) ;
184 extern void         NI_val_to_text       ( NI_rowtype *, char *, char * ) ;
185 extern void         NI_set_raw_val_to_text( int ) ;
186 extern int          NI_val_to_binary     ( NI_rowtype *, char *, char * ) ;
187 extern void         NI_multival_to_text  ( NI_rowtype *, int, char *, char * );
188 extern int          NI_multival_to_binary( NI_rowtype *, int, char *, char * );
189 extern int          NI_has_String        ( NI_rowtype * ) ;
190 extern void         NI_swap_column       ( NI_rowtype * , int , char * ) ;
191 
192 extern void         NI_rowtype_debug( int ) ;
193 
194 /*! Used to test if a rowtype code is a basic type. */
195 
196 #define ROWTYPE_is_basic_code  NI_is_basic_type
197 
198 /*! Integer type code to name string. */
199 
200 extern char * NI_type_name( int ) ;
201 
202 /*****------------------------------------------------------------------*****/
203 
204 #define NI_ELEMENT_TYPE  17
205 #define NI_GROUP_TYPE    18
206 #define NI_PROCINS_TYPE  19
207 
208 /*! A data element. */
209 
210 #ifndef TYPEDEF_NI_element
211 #define TYPEDEF_NI_element
212 typedef struct {
213    int    type ;       /*!< What type of struct is this? */
214    int    outmode ;    /*!< If >=0, output mode. */
215    char  *name ;       /*!< Name of element. */
216    int    attr_num ;   /*!< Number of attributes. */
217    char **attr_lhs ;   /*!< Left-hand-sides of attributes. */
218    char **attr_rhs ;   /*!< Right-hand-sides of attributes. */
219    int    vec_num ;    /*!< Number of vectors (may be 0). */
220    int    vec_len ;    /*!< Length of each vector. */
221    int    vec_filled ; /*!< Length that each one was filled up. */
222    int   *vec_typ ;    /*!< Type code for each vector. */
223    void **vec ;        /*!< Pointer to each vector. */
224    char **vec_lab ;    /*!< Ptr to label string for each vector [optional] */
225 
226    int    vec_rank ;        /*!< Number of dimensions, from ni_dimen. */
227    int   *vec_axis_len ;    /*!< Array of dimensions, from ni_dimen. */
228    float *vec_axis_delta ;  /*!< Array of step sizes, from ni_delta. */
229    float *vec_axis_origin ; /*!< Array of origins, from ni_origin. */
230    char **vec_axis_unit ;   /*!< Array of units, from ni_units. */
231    char **vec_axis_label ;  /*!< Array of labels, from ni_axes. */
232 
233    char  *filename ;        /*!< filename it came from, if any. [16 Jun 2020] */
234 } NI_element ;
235 #endif
236 
237 /*! A bunch of elements. */
238 
239 #ifndef TYPEDEF_NI_group
240 #define TYPEDEF_NI_group
241 typedef struct {
242    int    type ;       /*!< What type of struct is this? */
243    int    outmode ;    /*!< If >=0, output mode. */
244    int    attr_num ;   /*!< Number of attributes. */
245    char **attr_lhs ;   /*!< Left-hand-sides of attributes. */
246    char **attr_rhs ;   /*!< Right-hand-sides of attributes. */
247 
248    int    part_num ;   /*!< Number of parts within this group. */
249    int   *part_typ ;   /*!< Type of each part (element or group). */
250    void **part ;       /*!< Pointer to each part. */
251 
252    char  *name ;       /*!< Name (default="ni_group") - 03 Jun 2002 */
253 
254    char  *filename ;   /*!< filename it came from, if any. [16 Jun 2020] */
255 } NI_group ;
256 #endif
257 
258 /*! A processing instruction. */
259 
260 #ifndef TYPEDEF_NI_procins
261 #define TYPEDEF_NI_procins
262 typedef struct {
263    int    type ;       /*!< What type of struct is this? */
264    int    attr_num ;   /*!< Number of attributes. */
265    char **attr_lhs ;   /*!< Left-hand-sides of attributes. */
266    char **attr_rhs ;   /*!< Right-hand-sides of attributes. */
267    char  *name ;       /*!< The 'PItarget', as in '<?name ...?>' */
268 } NI_procins ;
269 #endif
270 
271 extern NI_procins * NI_rowtype_procins( NI_rowtype * ) ; /* 19 Apr 2005 */
272 
273 #ifdef  __cplusplus
274 }
275 #endif
276 
277 /*-----------------------------------------------------------------
278   Stuff for shared memory transport between processes
279 -------------------------------------------------------------------*/
280 
281 #ifdef CYGWIN
282 # define DONT_USE_SHM
283 #endif
284 
285 #ifndef DONT_USE_SHM
286 # include <sys/ipc.h>
287 # include <sys/shm.h>
288 
289 # define SHM_WAIT_CREATE   9
290 # define SHM_WAIT_ACCEPT  10
291 # define SHM_IS_DEAD      99
292 
293 # define SHM_CREATOR      33
294 # define SHM_ACCEPTOR     44
295 
296 # define SHM_DEFAULT_SIZE 196689
297 
298 # define SHM_HSIZE        128  /* header size in bytes    */
299 # define SHM_SIZE1        0    /* size1   = shmhead[this] */
300 # define SHM_BSTART1      1    /* bstart1 = shmhead[this] */
301 # define SHM_BEND1        2    /* bend1   = shmhead[this] */
302 # define SHM_SIZE2        3
303 # define SHM_BSTART2      4
304 # define SHM_BEND2        5
305 
306  /**
307    The shm segment is split into a 128 byte header and 2 buffers:
308      buf1 is written into by the "w" creator and read by the "r" acceptor;
309      buf2 is written into by the "r" acceptor and read by the "w" creator.
310    Each of these is a circular buffer, as described below.
311    The header currently contains 6 ints.  For buf1:
312      size1   = size of buf1 in bytes (fixed by creator)
313      bstart1 = offset into buf1 where good data starts (changed by acceptor)
314      bend1   = offset into buf1 where good data ends (changed by creator)
315    For buf2, a similar triple is set (mutatis mutandum).
316  **/
317 
318  typedef struct {
319    int id ;          /* shmid */
320    int bad ;         /* tells whether I/O is OK for this yet */
321    int whoami ;      /* SHM_CREATOR or SHM_ACCEPTOR? */
322 
323    char name[128] ;  /* keystring */
324 
325    char * shmbuf ;   /* actual shm buffer */
326    int  * shmhead ;  /* buffer as ints */
327 
328    int bufsize1 ;    /* size of 1st internal buffer */
329    char * buf1 ;     /* 1st internal buffer [after header] */
330    int  * bstart1 ;
331    int  * bend1 ;
332 
333    int bufsize2 ;    /* size of 2nd internal buffer */
334    char * buf2 ;     /* 2nd internal buffer [after buf1] */
335    int  * bstart2 ;
336    int  * bend2 ;
337 
338    int goodcheck_time ;   /*!< NI_clock_time() of last SHM_goodcheck() */
339  } SHMioc ;
340 
341 #else  /* DONT_USE_SHM */
342 
343 # define SHMioc void  /* dummy definition */
344 
345 #endif /* DONT_USE_SHM */
346 /*-----------------------------------------------------------------*/
347 
348 #ifdef  __cplusplus
349 extern "C" {                    /* care of Greg Balls    7 Aug 2006 [rickr] */
350 #endif
351 
352 /*! Size of NI_stream buffer. */
353 
354 #define NI_BUFSIZE (255*1024)
355 
356 /*! Data needed to process input stream. */
357 
358 #ifndef TYPEDEF_NI_stream_type
359 #define TYPEDEF_NI_stream_type
360 typedef struct {
361    int type ;        /*!< NI_TCP_TYPE, NI_FILE_TYPE, etc. */
362    int bad ;         /*!< Tells whether I/O is OK for this yet */
363 
364    int port ;        /*!< TCP only: port number */
365    int sd ;          /*!< TCP only: socket descriptor */
366 
367    FILE *fp ;        /*!< FILE only: pointer to open file */
368    int64_t fsize ;   /*!< FILE only: length of file for input */
369 
370    char name[256] ;  /*!< Hostname or filename */
371 
372    int io_mode ;     /*!< Input or output? */
373    int data_mode ;   /*!< Text, binary, or base64? */
374 
375    int bin_thresh ;  /*!< Threshold size for binary write. */
376 
377    int nbuf ;             /*!< Number of bytes left in buf. */
378    int npos ;             /*!< Index of next unscanned byte in buf. */
379    int bufsize ;          /*!< Length of buf array. */
380    char *buf ;            /*!< I/O buffer (may be NULL). */
381 
382    SHMioc *shmioc ;       /*!< for NI_SHM_TYPE only */
383 
384    char orig_name[256] ;  /*!< original (input) name when opened */
385 
386    int goodcheck_time ;   /*!< NI_clock_time() of last NI_stream_goodcheck() */
387 
388    int b64_numleft ;      /*!< For use in NI_stream_readbuf64() */
389    byte b64_left[4] ;     /*!< Leftover decoded bytes from NI_stream_readbuf64() */
390 } NI_stream_type ;
391 #endif
392 
393 /*! Opaque type for the C API. */
394 
395 #ifndef TYPEDEF_NI_stream
396 #define TYPEDEF_NI_stream
397 typedef NI_stream_type *NI_stream ;
398 #endif
399 
400 #define NI_TCP_TYPE    1  /* tcp: */
401 #define NI_FILE_TYPE   2  /* file: */
402 #define NI_STRING_TYPE 3  /* str: */
403 #define NI_REMOTE_TYPE 4  /* http: or ftp: */
404 #define NI_FD_TYPE     5  /* fd: */
405 #define NI_SHM_TYPE    6  /* shm: */
406 
407 #define TCP_WAIT_ACCEPT   7
408 #define TCP_WAIT_CONNECT  8
409 
410 #define MARKED_FOR_DEATH  6666
411 
412 /* I/O Modes for a NI_stream_type: input or output. */
413 
414 #define NI_INPUT_MODE  0
415 #define NI_OUTPUT_MODE 1
416 
417 /* Data modes for a NI_stream_type: text, binary, base64. */
418 
419 #define NI_TEXT_MODE    0
420 #define NI_BINARY_MODE  1
421 #define NI_BASE64_MODE  2
422 
423 #define NI_HEADERONLY_FLAG  (1<<8)  /* 20 Feb 2003 */
424 #define NI_HEADERSHARP_FLAG (1<<9)  /* 20 Mar 2003 */
425 
426 #define NI_LSB_FIRST    1
427 #define NI_MSB_FIRST    2
428 
429 /* Attribute writing modes [15 Oct 2002] */
430 
431 #define NI_ATTMODE_NORMAL 0
432 #define NI_ATTMODE_SPACED 1
433 #define NI_ATTMODE_LAST   1
434 
435 extern void NI_set_attribute_mode( int ) ;
436 
437 /* Type name writing modes [19 Feb 2003] */
438 
439 #define NI_NAMEMODE_NORMAL 0
440 #define NI_NAMEMODE_ALIAS  1
441 #define NI_NAMEMODE_LAST   1
442 
443 extern void NI_set_typename_mode ( int ) ;
444 
445 /*****---------- Hash table stuff [26 Aug 2002] ----------*****/
446 
447 #ifndef TYPEDEF_Htable
448 #define TYPEDEF_Htable
449 typedef struct {
450   int     len , ntot ;
451   void ***vtab ;             /* pointers */
452   char ***ctab ;             /* digests */
453   int    *ntab ;             /* counts */
454 } Htable ;
455 #endif
456 
457 extern Htable * new_Htable( int ) ;
458 extern void     destroy_Htable( Htable * ) ;
459 extern void     addto_Htable( char *, void *, Htable * ) ;
460 extern void *   findin_Htable( char *, Htable * ) ;
461 extern void     removefrom_Htable( char *, Htable * ) ;
462 extern void     profile_Htable( char *, Htable * ) ;
463 extern void     subsume_Htable( Htable *, Htable * ) ;
464 extern void     Htable_set_vtkill( int ) ;
465 extern void     resize_Htable( int , Htable * ) ;
466 
467 #define         sizeof_Htable(ht) ((ht)->ntot)
468 
469 /*--- double Htable (string-string pairs) [15 Oct 2003] ---*/
470 
471 #ifndef TYPEDEF_Dtable
472 #define TYPEDEF_Dtable
473 typedef struct { Htable *hta, *htb ; } Dtable ;
474 #endif
475 
476 extern Dtable * new_Dtable( int ) ;
477 extern void     destroy_Dtable( Dtable * ) ;
478 extern void     addto_Dtable( char *, char *, Dtable * ) ;
479 extern char *   findin_Dtable_a( char *, Dtable * ) ;
480 extern char *   findin_Dtable_b( char *, Dtable * ) ;
481 extern void     removefrom_Dtable_a( char *, Dtable * ) ;
482 extern void     removefrom_Dtable_b( char *, Dtable * ) ;
483 extern int      listize_Dtable( Dtable *, char ***, char *** ) ;
484 extern char *   Dtable_to_nimlstring( Dtable * , char * ) ;
485 extern Dtable * Dtable_from_nimlstring( char * ) ;
486 
487 /*****------------------- DIME stuff [04 Nov 2002] ------------------*****/
488 
489 #ifndef TYPEDEF_DIME_part
490 #define TYPEDEF_DIME_part
491 typedef struct {
492    int          type ;
493    int          flags ;
494    char        *id_string ;
495    char        *type_string ;
496    unsigned int data_len ;
497    byte        *data ;
498 } DIME_part ;
499 #endif
500 
501 #ifndef TYPEDEF_DIME_message
502 #define TYPEDEF_DIME_message
503 typedef struct {
504    int         num_part ;
505    DIME_part **part ;
506 } DIME_message ;
507 #endif
508 
509 #define DIME_MB_MASK      (1<<0)
510 #define DIME_ME_MASK      (1<<1)
511 #define DIME_CF_MASK      (1<<2)
512 
513 #define DIME_VERSION_MASK (0xf8)
514 
515 DIME_message * DIME_read_message( NI_stream_type * , int ) ;
516 DIME_part    * DIME_read_part   ( NI_stream_type * , int ) ;
517 
518 void           DIME_destroy_message( DIME_message * ) ;
519 
520 /*****--------------------------------------------------------------*****/
521 
522 #if 0
523 typedef struct { int nar ; float  *ar ; } NI_floatvec ;
524 
525 #define KILL_NI_floatvec(fv)                 \
526   do{ if( (fv) != NULL ){                     \
527         if( (fv)->ar != NULL ) free((fv)->ar); \
528         free(fv); (fv) = NULL;                  \
529   }} while(0)
530 
531 #define MAKE_NI_floatvec(fv,n)                           \
532   do{ (fv) = (NI_floatvec *)malloc(sizeof(NI_floatvec)) ; \
533       (fv)->nar = (n) ;                                    \
534       (fv)->ar  = (float *)calloc(sizeof(float),(n)) ;      \
535   } while(0)
536 
537 #define RESIZE_NI_floatvec(fv,m)                               \
538   do{ if( (fv)->nar != (m) ){                                   \
539         (fv)->nar = (m) ;                                        \
540         (fv)->ar  = (float *)realloc((fv)->ar,sizeof(float)*(m)); \
541   }} while(0)
542 #endif
543 
544 /*****------------------------- prototypes -------------------------*****/
545 
546 /** 18 Nov 2002: replace old malloc functions with new ones **/
547 
548 #undef NIML_OLD_MALLOC
549 #if (defined(NIML_OLD_MALLOC) || !defined(ALLOW_MCW_MALLOC)) && !defined(__cplusplus) && !defined(c_plusplus)
550 #define NI_malloc(typ,a) (typ*) old_NI_malloc((a))
551 #define NI_calloc(a,b) old_NI_malloc((a)*(b))
552 #define NI_realloc(a,typ,b)  (typ*) old_NI_realloc((a),(b))
553 
554   extern void * old_NI_malloc( size_t ) ;
555   extern void   NI_free( void * ) ;
556   extern void * old_NI_realloc( void *, size_t ) ;
557 #else
558 #  define NI_malloc(typ,a)   (typ*) hidden_NI_malloc((a),__FILE__,__LINE__)
559 #  define NI_calloc(a,b)   hidden_NI_malloc((a)*(b),__FILE__,__LINE__)
560 #  define NI_realloc(a,typ,b) (typ*) hidden_NI_realloc((a),(b),__FILE__,__LINE__)
561 #  define NI_free(a)       hidden_NI_free((a),__FILE__,__LINE__)
562 
563   extern void * hidden_NI_malloc( size_t , char * , int ) ;
564   extern void * hidden_NI_realloc( void * , size_t , char * , int ) ;
565   extern void   hidden_NI_free( void * , char * , int ) ;
566 #endif
567 
568 extern char * NI_malloc_status(void) ;
569 extern void NI_malloc_dump(void) ;
570 extern void NI_malloc_enable_tracking(void) ;
571 extern int NI_malloc_tracking_enabled(void) ;
572 
573 extern int NI_malloc_replace( void *(*um)(size_t)        ,
574                               void *(*ur)(void *,size_t) ,
575                               void  (*uf)(void *)         ) ;
576 
577 /*! Free and set pointer to NULL. */
578 #define NI_FREE(p) ( NI_free(p), (p)=NULL )
579 
580 /*! Make a new block of a given type. */
581 
582 /* #define NI_new(typ) ( (typ *)NI_malloc(sizeof(typ)) )   09 Dec 2002 */
583 #define NI_new(typ) ( NI_malloc(typ, sizeof(typ)) )   /* 15 Dec 2003 */
584 
585 extern char * NI_strncpy( char *, const char *, size_t ) ;
586 extern char * NI_strdup( char * ) ;
587 extern char * NI_strdup_len( char *, int ) ;
588 extern int    NI_strlen( char * ) ;
589 extern int64_t NI_filesize( char * ) ;  /* changed to int64_t 20 Jul 2021 */
590 extern int    NI_is_fifo( char * ) ;            /* 27 Aug 2019 */
591 extern int    NI_clock_time(void) ;
592 extern int    NI_byteorder(void) ;
593 extern void   NI_swap2( int, void * ) ;
594 extern void   NI_swap4( int, void * ) ;
595 extern void   NI_swap8( int, void * ) ;
596 
597 #define NI_is_file(pn) (NI_filesize(pn) >= 0)   /* 10 Dec 2002 */
598 
599 extern char * NI_mktemp( char * ) ;  /* 21 Aug 2002 */
600 
601 extern int    NI_type_size( int ) ;
602 
603 extern int NI_element_rowsize( NI_element * ) ;
604 extern int NI_element_allsize( NI_element * ) ;
605 
606 extern void NI_free_element( void * ) ;
607 extern void NI_free_element_data( void * ) ;  /* 17 Jul 2006 */
608 extern int  NI_element_type( void * ) ;
609 extern char * NI_element_name( void * ) ;  /* 18 Apr 2005 */
610 
611 extern NI_element * NI_new_data_element( char *, int ) ;
612 extern void NI_add_column( NI_element *, int, void * ) ;
613 extern void NI_set_column_label( NI_element *nel, int cc, char *lab ) ; /* 11 Sep 2018 */
614 extern void NI_move_column(NI_element *nel, int ibefore, int iafter);
615 extern void NI_insert_column( NI_element *nel , int typ , void *arr, int icol );
616 extern float NI_extract_float_value( NI_element *nel , int row , int col ) ; /* 14 Sep 2018 */
617 extern char * NI_extract_text_value( NI_element *nel , int row , int col ) ; /* 19 Jun 2020 */
618 extern void NI_remove_column(NI_element *nel, int irm);
619 extern void NI_copy_all_attributes( void *nisrc , void *nitrg );
620 void *NI_duplicate(void *vel, byte with_data);
621 void *NI_duplicate_element (void *vel, byte with_data);
622 void *NI_duplicate_group (void *vel, byte with_data);
623 extern void   NI_kill_attribute( void *, char * ) ;
624 extern void   NI_set_attribute( void *, char *, char * ) ;
625 extern char * NI_get_attribute( void *, char * ) ;
626 extern char * NI_get_attribute_nocase( void *, char * ) ;           /* 20 Aug 2019 */
627 extern void NI_set_dimen( NI_element *nel , int rank , int *nd );
628 extern void NI_set_axes( NI_element *nel , char **ax );
629 extern void NI_insert_value( NI_element *, int,int, void * );       /* 03 Apr 2003 */
630 extern void NI_insert_column_stride( NI_element *nel, int typ, void *arr, int stride, int icol );
631 extern void NI_add_column_stride( NI_element *, int, void *, int ); /* 29 May 2003 */
632 extern void NI_fill_column_stride( NI_element *,int,void *,int,int);/* 23 Mar 2004 */
633 extern void NI_insert_string( NI_element *, int,int, char *);       /* 19 Apr 2005 */
634 extern void NI_alter_veclen( NI_element * , int ) ;                 /* 19 Apr 2005 */
635 extern void NI_set_ni_type_atr( NI_element * ) ;       /* 14 Jul 2006 [rickr] */
636 
637 extern NI_element * NI_extract_columns( NI_element *nel, int nc, int *cc ) ; /* 13 Sep 2018 */
638 
639 
640 extern NI_group * NI_new_group_element(void) ;
641 extern void NI_add_to_group( NI_group *, void * ) ;
642 extern void NI_rename_group( NI_group *, char * ) ;                 /* 03 Jun 2002 */
643 extern void NI_remove_from_group( NI_group *, void * ) ;            /* 16 Apr 2005 */
644 
645 extern int NI_search_group_shallow( NI_group *, char *, void *** ); /* 18 Apr 2005 */
646 extern int NI_search_group_deep   ( NI_group *, char *, void *** ); /* 18 Apr 2005 */
647 
648 extern NI_procins * NI_new_processing_instruction( char * ) ;       /* 16 Mar 2005 */
649 
650 extern void NI_swap_vector( int, int, void * ) ;
651 
652 #undef  NI_set_attribute_int
653 #define NI_set_attribute_int(el,nm,vv)  \
654  do{ char ib[16]; sprintf(ib,"%d",(vv)); NI_set_attribute((el),(nm),ib); } while(0)
655 
656 /* port assignment functions from afni_ports.c */
657 extern int init_ports_list();
658 extern void set_ports_list_reinit(void);
659 extern int get_port_named(char *name);
660 extern void show_ports_list(void);
661 extern int get_available_npb(void);
662 extern int set_user_np(int v);
663 extern int get_user_np(void);
664 extern char *get_port_numbered(int port);
665 extern char *get_np_help();
666 extern int set_user_pif(char *s);
667 extern char * get_user_pif(void);
668 extern int set_user_np_bloc(int v);
669 extern int get_max_port_bloc(void);
670 extern int get_num_ports(void);
671 extern int get_user_np_bloc(void);
672 
673 /** I/O functions **/
674 
675 extern NI_stream NI_stream_open( char *, char * ) ;
676 extern int NI_stream_goodcheck( NI_stream_type *, int ) ;
677 extern void NI_stream_close( NI_stream_type * ) ;
678 extern void NI_stream_kill ( NI_stream_type * ) ;      /* 02 Jan 2004 */
679 extern void NI_stream_closenow( NI_stream_type * ) ;   /* 02 Jan 2004 */
680 extern int NI_stream_readcheck( NI_stream_type *, int  ) ;
681 extern int NI_stream_writecheck( NI_stream_type *, int  ) ;
682 extern int NI_stream_write( NI_stream_type *, char *, int ) ;
683 extern int NI_stream_read( NI_stream_type *, char *, int ) ;
684 extern void NI_binary_threshold( NI_stream_type *, int ) ;
685 extern void NI_sleep( int ) ;
686 extern char * NI_stream_getbuf( NI_stream_type * ) ;
687 extern void   NI_stream_clearbuf( NI_stream_type * ) ;
688 extern void   NI_stream_setbuf( NI_stream_type *, char * ) ;
689 extern char * NI_stream_name( NI_stream_type * ) ;
690 extern int NI_stream_readable( NI_stream_type * ) ;
691 extern int NI_stream_writeable( NI_stream_type * ) ;
692 extern int NI_stream_hasinput( NI_stream_type * , int ) ;
693 extern void NI_stream_seek( NI_stream_type * , int64_t , int ) ; /* 24 Mar 2003 */
694 extern int NI_stream_writestring( NI_stream_type * , char * ) ;
695 
696 extern int NI_stream_setbufsize( NI_stream_type *, int ) ; /* 03 Jan 2003 */
697 extern int NI_stream_getbufsize( NI_stream_type * ) ;
698 extern int NI_stream_readbuf( NI_stream_type *, char *, int ) ;
699 extern int NI_stream_readbuf64( NI_stream_type *, char *, int ) ;  /* 20 Apr 2005 */
700 extern int NI_text_to_val  ( NI_stream_type *, NI_rowtype *, void *, int );
701 extern int NI_binary_to_val( NI_stream_type *, NI_rowtype *, void *, int );
702 extern int NI_base64_to_val( NI_stream_type *, NI_rowtype *, void *, int );
703 
704 extern int NI_stream_setb64( NI_stream_type * , int ) ;   /* 20 Apr 2005 */
705 
706 extern int NI_stream_reopen( NI_stream_type *, char * ) ; /* 23 Aug 2002 */
707 
708 extern char * NI_suck_file( char * ) ;                    /* 27 Aug 2019 */
709 extern void * NI_read_element ( NI_stream_type *, int ) ;
710 extern int64_t  NI_write_element( NI_stream_type *, void *, int ) ;
711 extern int    NI_write_procins( NI_stream_type *, char * ) ; /* 17 Mar 2005 */
712 extern int64_t NI_write_columns( NI_stream_type * ,
713                                 int , int * , int , void ** , int ) ;
714 extern int64_t NI_write_rowtype( NI_stream_type * ,
715                                 NI_rowtype * , int , void * , int ) ;
716 extern int    NI_read_columns ( NI_stream_type *,
717                                 int, int *, int, void **, int,int ) ;
718 extern void   NI_free_column  ( NI_rowtype * , int , void * );
719 extern void * NI_copy_column  ( NI_rowtype * , int , void * );
720 extern int    NI_size_column  ( NI_rowtype * , int , void * ); /* 26 Mar 2003 */
721 
722 extern void   NI_set_read_header_only   ( int ) ;              /* 21 Mar 2003 */
723 extern int    NI_get_read_header_only( void );                 /* 24 Feb. 2012 */
724 extern void * NI_read_element_header( NI_stream_type *, int ); /* 26 Mar 2003 */
725 extern void   NI_skip_procins( int ) ;                         /* 03 Jun 2005 */
726 
727 extern int64_t NI_write_element_tofile( char *, void *, int ) ;    /* 07 Mar 2007 */
728 extern void * NI_read_element_fromfile( char * ) ;             /* 12 Mar 2007 */
729 extern void * NI_read_element_fromstring( char *nstr );     /* 26 Feb 2010 ZSS*/
730 extern char * NI_write_element_tostring( void *nel ); /* Oct 2011 ZSS */
731 
732 extern char * NI_preview_string( NI_element *, int , char *) ; /* 22 Jun 2020 */
733 
734 #define NI_SWAP_MASK  (1<<0)
735 #define NI_LTEND_MASK (1<<1)
736 
737 /* prototypes for Web data fetchers */
738 
739 extern int  NI_read_URL_tmpdir( char *url, char **tname ) ;
740 extern int  NI_read_URL       ( char *url, char **data  ) ;
741 extern void NI_set_URL_ftp_ident( char *name, char *pwd ) ;
742 
743 /* prototypes for Base64 and MD5 functions */
744 
745 extern void   B64_set_crlf( int nn ) ;
746 extern void   B64_set_linelen( int ll ) ;
747 extern void   B64_to_binary( int nb64, byte * b64, int * nbin, byte ** bin ) ;
748 extern void   B64_to_base64( int nbin, byte * bin, int * nb64, byte ** b64 ) ;
749 
750 extern char * MD5_static_array( int n, char * bytes ) ;
751 extern char * MD5_malloc_array( int n, char * bytes ) ;
752 extern char * MD5_static_string( char * string ) ;
753 extern char * MD5_malloc_string( char * string ) ;
754 extern char * MD5_static_file(char * filename) ;
755 extern char * MD5_malloc_file(char * filename) ;
756 
757 extern char * MD5_B64_array( int n, char * bytes ) ;
758 extern char * MD5_B64_string( char * string ) ;
759 extern char * MD5_B64_file(char * filename) ;
760 extern void   MD5_set_xor_use(int) ;  /* 09 Apr 2015 */
761 
762 extern char * UNIQ_idcode(void) ;
763 extern char * UNIQ_idcode_11(void) ;             /* 10 Feb 2016 */
764 extern void   UNIQ_idcode_fill( char * ) ;
765 extern char * UNIQ_hashcode( char * ) ;
766 extern void UNIQ_hashprefix_fill( char *idc ) ;  /* 30 Apr 2013 */
767 extern char * UNIQ_hashprefix( void ) ;
768 
769 extern char * UUID_hashcode( char * ) ;   /* 20 Aug 2002 */
770 extern char * UUID_idcode(void) ;
771 
772 /* trusted host manipulation */
773 
774 extern char * NI_hostname_to_inet( char *host ) ;
775 extern void   NI_add_trusted_host( char *hostname ) ;
776 extern int    NI_trust_host( char *hostid ) ;
777 
778 /*! Close a NI_stream, and set the pointer to NULL. */
779 
780 #define NI_STREAM_CLOSE(nn) do{ NI_stream_close(nn); (nn)=NULL; } while(0)
781 
782 /****************************************************************************
783   This stuff defines various types, macros, and function prototypes
784   for generic datasets and domains for neuroimaging applications.
785 *****************************************************************************/
786 
787 #ifndef TYPEDEF_NI_INDEX_T
788 #define TYPEDEF_NI_INDEX_T
789 typedef int NI_index_t ;      /* used to store indexes, vector lengths */
790 #endif
791 
792 /*---------------------------------------------------------------------------*/
793 /*! Stuff that goes at the top of every NI struct:
794      - type is a code that lets us tell what kind of struct it is
795      - nref is a reference count
796      - idcode is a globally unique string (max 63 characters)
797      - name is an arbitrary string for fun, profit, and elucidation
798      - either or both of these strings may be NULL
799 -----------------------------------------------------------------------------*/
800 
801 #define NI_BASIC_PARTS  \
802   int type ;            \
803   int nref ;            \
804   char *idcode ;        \
805   char *name
806 
807 /*---------------------------------------------------------------------------*/
808 /*! The minimal NI struct, with only the basic elements. */
809 
810 typedef struct {
811   NI_BASIC_PARTS ;
812 } NI_struct ;
813 
814 extern void   NI_free_struct      ( void * ) ;
815 extern void * NI_copy_struct      ( void * ) ;
816 extern void * NI_pointto_struct   ( void * ) ;
817 
818 extern void   NI_register_struct  ( void * ) ;
819 extern void * NI_find_struct      ( char * ) ;
820 extern void   NI_unregister_struct( void * ) ;
821 
822 /*---------------------------------------------------------------------------*/
823 /*! NI struct to hold one float. */
824 
825 typedef struct {
826   NI_BASIC_PARTS ;
827   float val ;
828 } NI_float_one ;
829 
830 #define NI_float_val(nd) ((nd)->val)
831 
832 /*---------------------------------------------------------------------------*/
833 /*! NI struct to hold the definition of a statistical distribution for
834     a NI_vector (call it v):
835       - statcode  = one of the NI_STAT_* codes
836       - param_num = number of parameters for this distribution
837       - param[i]  = parameter #i, for i=0..param_num-1:
838              - this will either be a NI_float_one, which means it
839                 is constant for all elements of the vector
840              - OR
841              - this will be a NI_vector itself, of float type,
842                 which means that the #i parameter for v[j] is stored
843                 in param[i][j]
844              - for example, you can have an F-statistic with the
845                first DOF param being different for every node and
846                the second DOF param being fixed
847 -----------------------------------------------------------------------------*/
848 
849 typedef struct {
850   NI_BASIC_PARTS ;
851   int statcode ;
852   NI_index_t param_num ;
853   NI_struct  **param ;
854 } NI_statistic ;
855 
856 #define NI_stat_code(nd)      ((nd)->statcode)
857 #define NI_stat_param_num(nd) ((nd)->param_num)
858 #define NI_stat_param(nd,i,j)                             \
859   ( ((nd)->param[i]->type == NI_FLOAT_ONE_TYPE)           \
860      ? ( ((NI_float_one *)(nd)->param[i])->val )          \
861      : ( ((NI_float_vector *)(nd)->param[i])->vec[j] ) )
862 
863 /*--- Statistical type codes:
864        2..10 match AFNI's 3ddata.h
865        the rest match those in NIfTI-1.1 (nifti1.h) ---*/
866 
867                                  /** Parameters **/
868 #define NI_STAT_CORREL      2   /* Samples, fits, orts   */
869 #define NI_STAT_TTEST       3   /* DOF                   */
870 #define NI_STAT_FTEST       4   /* 2 DOF                 */
871 #define NI_STAT_ZSCORE      5   /* no params             */
872 #define NI_STAT_CHISQ       6   /* DOF                   */
873 #define NI_STAT_BETA        7   /* a and b params        */
874 #define NI_STAT_BINOM       8   /* # trials, p per trial */
875 #define NI_STAT_GAMMA       9   /* shape, scale params   */
876 #define NI_STAT_POISSON    10   /* mean                  */
877 
878 #define NI_STAT_NORMAL     11   /* mean, variance        */
879 #define NI_STAT_FTEST_NONC 12   /* 2 DOF, noncentrality  */
880 #define NI_STAT_CHISQ_NONC 13   /* DOF, noncentrality    */
881 #define NI_STAT_LOGISTIC   14   /* location, scale       */
882 #define NI_STAT_LAPLACE    15   /* location, scale       */
883 #define NI_STAT_UNIFORM    16   /* start, end            */
884 #define NI_STAT_TTEST_NONC 17   /* DOF, noncentrality    */
885 #define NI_STAT_WEIBULL    18   /* location, scale, power*/
886 #define NI_STAT_CHI        19   /* DOF                   */
887 #define NI_STAT_INVGAUSS   20   /* mu, lambda            */
888 #define NI_STAT_EXTVAL     21   /* location, scale       */
889 
890 #define NI_STAT_PVAL       22
891 #define NI_STAT_LOGPVAL    23
892 #define NI_STAT_LOG10PVAL  24
893 
894 #define NI_STAT_FIRSTCODE   2
895 #define NI_STAT_LASTCODE   24
896 
897 extern int    NI_stat_numparam( int ) ;
898 extern char * NI_stat_distname( int ) ;
899 extern void   NI_stat_decode( char *, int *, float *, float *, float * ) ;
900 extern char * NI_stat_encode( int , float,float,float ) ;
901 
902 /*---------------------------------------------------------------------------*/
903 /*! NI struct to hold a vector of values:
904      - vec_len   = number of values
905      - vec_typ   = type of values (e.g., NI_FLOAT, etc.)
906      - vec       = pointer to array of data of length vec_len
907      - vec_range = pointer to array of length 2 (if not NULL):
908                    - vec_range[0] = smallest value in vec
909                    - vec_range[1] = largest value in vec
910      - statistic = defines statistical distribution for these values
911                    (if not NULL)
912      - the size in bytes of each element of vec can be determined
913        by NI_datatype_size(vec_typ)
914 -----------------------------------------------------------------------------*/
915 
916 typedef struct {
917   NI_BASIC_PARTS ;
918   NI_index_t vec_len ;
919   int vec_typ ;
920   void *vec ;
921   void *vec_range ;
922   NI_statistic *statistic ;
923 } NI_vector ;
924 
925 extern void * NI_new_vector( int , NI_index_t ) ;
926 extern void   NI_set_vector_range( void * ) ;
927 
928 /*********************************************************
929    The special vector types below are mostly convenient
930    for having vectors of the basic types pre-defined.
931    Field for field, they match the NI_vector above,
932    except that the "void *" components are pre-declared
933    to be the correct basic type (don't have to cast).
934    Therefore, you can do casts like this:
935      NI_vector *vv ;
936      if( vv->vec_typ == NI_FLOAT ){
937        NI_float_vector *ff = (NI_float_vector *) vv ;
938        ff->vec[0] = 7.3 ;
939      }
940 **********************************************************/
941 
942 /*---------------------------------------------------------------------------*/
943 /*! NI struct to hold a vector of byte values:
944      - vec_len   = number of values
945      - vec_typ   = type of values (must be NI_BYTE)
946      - vec       = pointer to array of data of length vec_len
947      - vec_range = pointer to array of length 2 (if not NULL):
948                    - vec_range[0] = smallest value in vec
949                    - vec_range[1] = largest value in vec
950      - statistic = defines statistical distribution for these values
951                    (if not NULL)
952 -----------------------------------------------------------------------------*/
953 
954 typedef struct {
955   NI_BASIC_PARTS ;
956   NI_index_t vec_len ;
957   int vec_typ ;
958   byte *vec ;
959   byte *vec_range ;
960   NI_statistic *statistic ;
961 } NI_byte_vector ;
962 
963 /*---------------------------------------------------------------------------*/
964 /*! NI struct to hold a vector of short values:
965      - vec_len   = number of values
966      - vec_typ   = type of values (must be NI_SHORT)
967      - vec       = pointer to array of data of length vec_len
968      - vec_range = pointer to array of length 2 (if not NULL):
969                    - vec_range[0] = smallest value in vec
970                    - vec_range[1] = largest value in vec
971      - statistic = defines statistical distribution for these values
972                    (if not NULL)
973 -----------------------------------------------------------------------------*/
974 
975 typedef struct {
976   NI_BASIC_PARTS ;
977   NI_index_t vec_len ;
978   int vec_typ ;
979   short *vec ;
980   short *vec_range ;
981   NI_statistic *statistic ;
982 } NI_short_vector ;
983 
984 /*---------------------------------------------------------------------------*/
985 /*! NI struct to hold a vector of int values:
986      - vec_len   = number of values
987      - vec_typ   = type of values (must be NI_INT)
988      - vec       = pointer to array of data of length vec_len
989      - vec_range = pointer to array of length 2 (if not NULL):
990                    - vec_range[0] = smallest value in vec
991                    - vec_range[1] = largest value in vec
992      - statistic = defines statistical distribution for these values
993                    (if not NULL)
994 -----------------------------------------------------------------------------*/
995 
996 typedef struct {
997   NI_BASIC_PARTS ;
998   NI_index_t vec_len ;
999   int vec_typ ;
1000   int *vec ;
1001   int *vec_range ;
1002   NI_statistic *statistic ;
1003 } NI_int_vector ;
1004 
1005 /*---------------------------------------------------------------------------*/
1006 /*! NI struct to hold a vector of float values:
1007      - vec_len   = number of values
1008      - vec_typ   = type of values (must be NI_FLOAT)
1009      - vec       = pointer to array of data of length vec_len
1010      - vec_range = pointer to array of length 2 (if not NULL):
1011                    - vec_range[0] = smallest value in vec
1012                    - vec_range[1] = largest value in vec
1013      - statistic = defines statistical distribution for these values
1014                    (if not NULL)
1015 -----------------------------------------------------------------------------*/
1016 
1017 typedef struct {
1018   NI_BASIC_PARTS ;
1019   NI_index_t vec_len ;
1020   int vec_typ ;
1021   float *vec ;
1022   float *vec_range ;
1023   NI_statistic *statistic ;
1024 } NI_float_vector ;
1025 
1026 /*---------------------------------------------------------------------------*/
1027 /*! NI struct to hold a vector of double values:
1028      - vec_len   = number of values
1029      - vec_typ   = type of values (must be NI_DOUBLE)
1030      - vec       = pointer to array of data of length vec_len
1031      - vec_range = pointer to array of length 2 (if not NULL):
1032                    - vec_range[0] = smallest value in vec
1033                    - vec_range[1] = largest value in vec
1034      - statistic = defines statistical distribution for these values
1035                    (if not NULL)
1036 -----------------------------------------------------------------------------*/
1037 
1038 typedef struct {
1039   NI_BASIC_PARTS ;
1040   NI_index_t vec_len ;
1041   int vec_typ ;
1042   double *vec ;
1043   double *vec_range ;
1044   NI_statistic *statistic ;
1045 } NI_double_vector ;
1046 
1047 /*---------------------------------------------------------------------------*/
1048 /*! NI struct to hold a vector of complex values:
1049      - vec_len   = number of values
1050      - vec_typ   = type of values (must be NI_COMPLEX)
1051      - vec       = pointer to array of data of length vec_len
1052      - vec_range = pointer to array of length 2 (if not NULL):
1053                    - vec_range[0] = smallest value in vec
1054                    - vec_range[1] = largest value in vec
1055      - statistic = defines statistical distribution for these values
1056                    (if not NULL)
1057 -----------------------------------------------------------------------------*/
1058 
1059 typedef struct {
1060   NI_BASIC_PARTS ;
1061   NI_index_t vec_len ;
1062   int vec_typ ;
1063   complex *vec ;
1064   complex *vec_range ;
1065   NI_statistic *statistic ;
1066 } NI_complex_vector ;
1067 
1068 /*---------------------------------------------------------------------------*/
1069 /*! NI struct to hold a vector of rgb values:
1070      - vec_len   = number of values
1071      - vec_typ   = type of values (must be NI_RGB)
1072      - vec       = pointer to array of data of length vec_len
1073      - vec_range = pointer to array of length 2 (if not NULL):
1074                    - vec_range[0] = smallest value in vec
1075                    - vec_range[1] = largest value in vec
1076      - statistic = defines statistical distribution for these values
1077                    (if not NULL)
1078 -----------------------------------------------------------------------------*/
1079 
1080 typedef struct {
1081   NI_BASIC_PARTS ;
1082   NI_index_t vec_len ;
1083   int vec_typ ;
1084   rgb *vec ;
1085   rgb *vec_range ;
1086   NI_statistic *statistic ;
1087 } NI_rgb_vector ;
1088 
1089 /*---------------------------------------------------------------------------*/
1090 /*! NI struct to hold a vector of rgba values:
1091      - vec_len   = number of values
1092      - vec_typ   = type of values (must be NI_RGBA)
1093      - vec       = pointer to array of data of length vec_len
1094      - vec_range = pointer to array of length 2 (if not NULL):
1095                    - vec_range[0] = smallest value in vec
1096                    - vec_range[1] = largest value in vec
1097      - statistic = defines statistical distribution for these values
1098                    (if not NULL)
1099 -----------------------------------------------------------------------------*/
1100 
1101 typedef struct {
1102   NI_BASIC_PARTS ;
1103   NI_index_t vec_len ;
1104   int vec_typ ;
1105   rgba *vec ;
1106   rgba *vec_range ;
1107   NI_statistic *statistic ;
1108 } NI_rgba_vector ;
1109 
1110 /*---------------------------------------------------------------------------*/
1111 /*! NI struct to hold a vector of string values:
1112      - vec_len   = number of values
1113      - vec_typ   = type of values (must be NI_STRING)
1114      - vec       = pointer to array of data of length vec_len
1115      - vec_range = pointer to array of length 2 (if not NULL):
1116                    - vec_range[0] = smallest value in vec
1117                    - vec_range[1] = largest value in vec
1118      - statistic = defines statistical distribution for these values
1119                    (if not NULL)
1120 -----------------------------------------------------------------------------*/
1121 
1122 typedef struct {
1123   NI_BASIC_PARTS ;
1124   NI_index_t vec_len ;
1125   int vec_typ ;
1126   char **vec ;
1127   char **vec_range ;
1128   NI_statistic *statistic ;
1129 } NI_string_vector ;
1130 
1131 /*---------------------------------------------------------------------------*/
1132 /*! NI struct to define a coordinate mapping between one 3D domain
1133     and another.
1134 -----------------------------------------------------------------------------*/
1135 
1136 typedef struct {
1137   NI_BASIC_PARTS ;
1138   float mat[4][4] ;
1139 } NI_affine_3dmap ;
1140 
1141 /*---------------------------------------------------------------------------*/
1142 /*! NI struct to define a 1..4 dimensional rectangular domain:
1143      - nx,ny,nz,nt = number of voxels along each axis
1144      - nvox        = total number of voxels
1145      - dx,dy,dz,dt = grid spacing along each axis
1146      - xo,yo,zo,to = origin of each axis
1147 -----------------------------------------------------------------------------*/
1148 
1149 typedef struct {
1150   NI_BASIC_PARTS ;
1151   NI_index_t nx,ny,nz,nt , nvox ;
1152   float dx,dy,dz,dt ;
1153   float xo,yo,zo,to ;
1154 } NI_rect_domain ;
1155 
1156 /*---------------------------------------------------------------------------*/
1157 /*! NI struct to define a domain of scattered points:
1158      - num_node = number of nodes (points)
1159      - id       = list of integer node identifiers
1160      - x,y,z    = list of spatial coordinates
1161      - seq      = If 1, node id's are sequential
1162      - seqbase  = If id's are sequential, is smallest id
1163      - sorted   = If 1, id's are sorted into increasing order
1164 -----------------------------------------------------------------------------*/
1165 
1166 typedef struct {
1167   NI_BASIC_PARTS ;
1168   NI_index_t  num_node ;
1169   NI_index_t *id ;
1170   float        *x , *y , *z ;
1171   int           seq ;
1172   int           seqbase ;
1173   int           sorted ;
1174 } NI_points_domain ;
1175 
1176 /*---------------------------------------------------------------------------*/
1177 /*! NI struct to hold a generic dataset, which is a collection of value
1178     vectors defined over a common domain.
1179       - num_node = number of nodes in the domain
1180       - num_val  = number of values at each node
1181       - order    = code indicated whether the value vectors are
1182                    along the node direction or value index direction
1183       - vec[i]   = i-th value vector
1184       - domain   = definition of domain the nodes occupy (if not NULL)
1185 -----------------------------------------------------------------------------*/
1186 
1187 typedef struct {
1188   NI_BASIC_PARTS ;
1189   NI_index_t num_node , num_val ;
1190   int order ;
1191   NI_vector **vec ;
1192   NI_struct  *domain ;
1193 } NI_dataset ;
1194 
1195 #define NI_NODE_DIRECTION  55   /* for the order element */
1196 #define NI_INDEX_DIRECTION 56
1197 
1198 #define NI_dataset_vecnum(nd)  \
1199   ( ((nd)->order == NI_NODE_DIRECTION) ? (nd)->num_val : (nd)->num_node )
1200 
1201 #define NI_dataset_veclen(nd)  \
1202   ( ((nd)->order == NI_NODE_DIRECTION) ? (nd)->num_node: (nd)->num_val  )
1203 
1204 #define NI_opposite_order(oo)  \
1205   ( ((oo) == NI_NODE_DIRECTION) ? NI_INDEX_DIRECTION : NI_NODE_DIRECTION )
1206 
1207 extern void * NI_dataset_transpose( void * ) ;
1208 
1209 /*---------------------------------------------------------------------------*/
1210 /* Codes for the "type" element of a NI struct. */
1211 
1212 #define NI_STRUCT_TYPE          6660000
1213 #define NI_FLOAT_ONE_TYPE       6660002
1214 #define NI_STATISTIC_TYPE       6660003
1215 #define NI_DATASET_TYPE         6660004
1216 
1217 #define NI_VECTOR_TYPE          6660100
1218 #define NI_BYTE_VECTOR_TYPE     6660101
1219 #define NI_SHORT_VECTOR_TYPE    6660102
1220 #define NI_INT_VECTOR_TYPE      6660103
1221 #define NI_FLOAT_VECTOR_TYPE    6660104
1222 #define NI_DOUBLE_VECTOR_TYPE   6660105
1223 #define NI_COMPLEX_VECTOR_TYPE  6660106
1224 #define NI_RGB_VECTOR_TYPE      6660107
1225 #define NI_RGBA_VECTOR_TYPE     6660108
1226 #define NI_STRING_VECTOR_TYPE   6660109
1227 
1228 #define NI_is_vector_type(tt)                                 \
1229  ( (tt) >= NI_VECTOR_TYPE && (tt) <= NI_STRING_VECTOR_TYPE )
1230 
1231 #define NI_patch_vector_type(nn)                              \
1232  do{ if( NI_is_vector_type((nn)->type) &&                     \
1233          NI_is_builtin_type((nn)->vec_typ) )                  \
1234        (nn)->type = NI_VECTOR_TYPE + (nn)->vec_typ + 1 ;      \
1235  } while(0)
1236 
1237 #define NI_RECT_DOMAIN_TYPE     6660201
1238 #define NI_POINTS_DOMAIN_TYPE   6660202
1239 
1240 #define NI_is_domain_type(tt)                                     \
1241  ( (tt) >= NI_RECT_DOMAIN_TYPE && (tt) <= NI_POINTS_DOMAIN_TYPE )
1242 
1243 #define NI_AFFINE_3DMAP_TYPE    6660301
1244 
1245 #define NI_is_3dmap_type(tt)                                      \
1246  ( (tt) >= NI_AFFINE_3DMAP_TYPE && (tt) <= NI_AFFINE_3DMAP_TYPE )
1247 
1248 #define NI_datatype_size(n) NI_rowtype_code_to_size(n)
1249 
1250 /*-------------------------------------------------------------------------*/
1251 
1252 typedef void NI_voidfunc() ;
1253 
1254 extern int NI_do( NI_stream_type * , NI_element * ) ;
1255 extern void NI_register_doer( char *, NI_voidfunc * ) ;
1256 /*-------------------------------------------------------------------------*/
1257 /*! An array of strings, each allocated with NI_malloc(). */
1258 
1259 typedef struct { int num; char **str; } NI_str_array ;
1260 
1261 #define NI_delete_str_array(sar)             \
1262   do{ int pp ;                               \
1263       for( pp=0 ; pp < (sar)->num ; pp++ )   \
1264         NI_free( (sar)->str[pp] );           \
1265       NI_free((sar)->str) ; NI_free(sar) ;   \
1266   } while(0)
1267 
1268 extern NI_str_array * NI_decode_string_list( char *ss , char *sep ) ;
1269 extern NI_str_array * NI_strict_decode_string_list( char *ss , char *sep );
1270 #define NI_decode_str_array NI_decode_string_list
1271 
1272 extern int NI_str_array_find( char *, NI_str_array *) ; /* 20 May 2010 */
1273 
1274 extern int NI_count_numbers( int nstr , char **str ) ;  /* 11 Sep 2018 */
1275 
1276 /*-------------------------------------------------------------------------*/
1277 /*! An array of floats. */
1278 
1279 typedef struct { int num; float *ar; } NI_float_array ;
1280 
1281 #define NI_delete_float_array(far) \
1282   do{ if( (far)->ar != NULL ) NI_free((far)->ar); NI_free(far); } while(0)
1283 
1284 extern NI_float_array * NI_decode_float_list( char *ss , char *sep ) ;
1285 extern char *           NI_encode_float_list( NI_float_array *, char * ) ;
1286 #define NI_decode_float_array NI_decode_float_list
1287 
1288 /*-------------------------------------------------------------------------*/
1289 /*! An array of ints. */
1290 
1291 typedef struct { int num; int *ar; } NI_int_array ;
1292 
1293 #define NI_delete_int_array(iar) \
1294   do{ if( (iar)->ar != NULL ) NI_free((iar)->ar); NI_free(iar); } while(0)
1295 
1296 extern NI_int_array * NI_decode_int_list( char *ss , char* sep ) ;
1297 extern char *         NI_encode_int_list( NI_int_array * , char * ) ;
1298 #define NI_decode_int_array NI_decode_int_list
1299 
1300 /*-------------------------------------------------------------------------*/
1301 /* Registry stuff -- niml_registry.c [25 Feb 2005] */
1302 
1303 extern void * NI_registry_malloc          ( char *, char *, size_t ) ;
1304 extern void * NI_registry_realloc         ( void *, size_t ) ;
1305 extern void   NI_registry_free            ( void * ) ;
1306 extern void * NI_registry_idcode_to_ptr   ( char * ) ;
1307 extern char * NI_registry_idcode_to_name  ( char * ) ;
1308 extern char * NI_registry_ptr_to_idcode   ( void * ) ;
1309 extern char * NI_registry_ptr_to_name     ( void * ) ;
1310 extern void   NI_registry_idcode_altername( char *, char * ) ;
1311 extern void   NI_registry_ptr_altername   ( void *, char * ) ;
1312 extern size_t NI_registry_idcode_to_len   ( char * ) ;
1313 extern size_t NI_registry_ptr_to_len      ( void * ) ;
1314 extern void * NI_registry_add             ( char *, char *, void * ) ;
1315 extern void * NI_registry_replace         ( void *, void * ) ;
1316 
1317 /*-------------------------------------------------------------------------*/
1318 
1319 #define IDCODE_LEN 32
1320 #define LEN_IDCODE IDCODE_LEN
1321 
1322 #ifndef TYPEDEF_NI_datacontainer
1323 #define TYPEDEF_NI_datacontainer
1324 typedef struct {
1325   char type_name  [IDCODE_LEN] ; /* e.g., "NI_ELEMENT"   */
1326   char self_name  [IDCODE_LEN] ; /* e.g., "AFNI_dataset" */
1327   char self_idcode[IDCODE_LEN] ;
1328   int  ival , jval ;
1329   void *self_data ;              /* the actual data */
1330 } NI_objcontainer ;
1331 
1332 typedef int (*NI_objconverter_func)( NI_objcontainer * ) ;
1333 #endif
1334 
1335 extern char * NI_self_idcode( void * ) ;
1336 extern void   NI_suck_stream( char *, int, int *, NI_objcontainer *** ) ;
1337 extern void   NI_convert_elm_to_obj( NI_objcontainer * ) ;
1338 extern void   NI_convert_obj_to_elm( NI_objcontainer * ) ;
1339 extern void   NI_register_objconverters( char * ,
1340                                          NI_objconverter_func ,
1341                                          NI_objconverter_func  ) ;
1342 
1343 /**-------------------------------------------------------------------------**/
1344 /** Type: array of NI_element pointers [adapted from MRI_IMARR 16 Jun 2020] **/
1345 
1346 /*! Array of NI_element pointers */
1347 
1348 typedef struct NI_ELARR {
1349       int num ;             /*!< Number of actual NI_element here */
1350       int nall ;            /*!< Size of elarr array currently allocated */
1351       NI_element **elarr ;  /*!< Array of NI_element pointers */
1352 } NI_ELARR ;
1353 
1354 /*! Get the nn-th element from the NIML array "name" */
1355 
1356 #define ELEMENT_IN_ELARR(name,nn) ((name)->elarr[(nn)])
1357 #define ELARR_SUBELEMENT          ELEMENT_IN_ELARR
1358 #define ELARR_SUBEL               ELEMENT_IN_ELARR
1359 
1360 /*! Get the number of elements in the array "name" */
1361 
1362 #define ELARR_COUNT(name)         ((name)->num)
1363 
1364 #define ELARR_LASTEL(name)        ((name)->elarr[(name)->num-1])
1365 #define ELARR_FIRSTEL(name)       ((name)->elarr[0])
1366 
1367 #define INC_ELARR 32
1368 
1369 /*! Initialize an NI_ELARR struct */
1370 
1371 #define INIT_ELARR(name)                                                           \
1372    do{ int iq ; (name) = (NI_ELARR *) malloc(sizeof(NI_ELARR)) ;                   \
1373        (name)->num = 0 ; (name)->nall = INC_ELARR ;                                \
1374        (name)->elarr = (NI_element **)malloc(sizeof(NI_element *)*INC_ELARR) ;     \
1375        for( iq=(name)->num ; iq < (name)->nall ; iq++ ) (name)->elarr[iq] = NULL ; \
1376        break ; } while(0)
1377 
1378 /*! Add one NI_element to the NI_ELARR struct */
1379 
1380 #define ADDTO_ELARR(name,emm)                                                              \
1381    do{ int nn , iq ;                                                                       \
1382        if( (name)->num == (name)->nall ){                                                  \
1383           nn = (name)->nall = 1.1*(name)->nall + INC_ELARR ;                               \
1384           (name)->elarr = (NI_element **)realloc( (name)->elarr,sizeof(NI_element *)*nn ); \
1385           for( iq=(name)->num ; iq < (name)->nall ; iq++ ) (name)->elarr[iq] = NULL ; }    \
1386        nn = (name)->num ; ((name)->num)++ ;                                                \
1387        (name)->elarr[nn] = (emm) ; break ; } while(0)
1388 
1389 /*! Free the NI_ELARR struct (but not the data within) */
1390 
1391 #define FREE_ELARR(name)                                                        \
1392    do{ if( (name) != NULL ){                                                    \
1393           free((name)->elarr); free((name)); (name) = NULL; } break; } while(0)
1394 
1395 /*! Free the NI_ELARR struct, including the data within */
1396 
1397 #define DESTROY_ELARR(name)                                                          \
1398    do{ int nn ;                                                                      \
1399        if( (name) != NULL ){                                                         \
1400           for( nn=0 ; nn < (name)->num ; nn++ ) NI_free_element((name)->elarr[nn]) ; \
1401           free((name)->elarr); free((name)); (name) = NULL; } break; } while(0)
1402 
1403 /*! Free all data elements at-and-after [qq] in the NI_ELARR struct. */
1404 
1405 #define TRUNCATE_ELARR(name,qq)                                                      \
1406    do{ int nn ;                                                                      \
1407        if( (name) != NULL && qq < (name)->num ){                                     \
1408           for( nn=qq ; nn < (name)->num ; nn++ ) NI_free_element((name)->elarr[nn]); \
1409           (name)->num = qq ;                                                         \
1410        } } while(0)
1411 
1412 /**-------------------------------------------------------------------------**/
1413 
1414 #ifdef  __cplusplus
1415 }
1416 #endif
1417 
1418 #endif /* _NIML_HEADER_FILE */
1419