Doxygen Source Code Documentation
niml_private.h File Reference
#include "niml.h"#include <stdarg.h>Go to the source code of this file.
Data Structures | |
| struct | header_stuff |
| struct | int_array |
| struct | intpair |
| struct | MD5_CTX |
Defines | |
| #define | USE_NEW_IOFUN |
| #define | NIML_DEBUG |
| #define | B64_goodchar(c) (dtable[c] != 0x80) |
| #define | B64_EOL1 '\r' |
| #define | B64_EOL2 '\n' |
| #define | B64_encode3(a, b, c, w, x, y, z) |
| #define | B64_encode2(a, b, w, x, y, z) ( B64_encode3(a,b,0,w,x,y,z) , z = '=' ) |
| #define | B64_encode1(a, w, x, y, z) ( B64_encode3(a,0,0,w,x,y,z) , y=z = '=' ) |
| #define | B64_decode4(w, x, y, z, a, b, c) |
| #define | B64_decode_count(w, x, y, z) |
| #define | IS_STRING_CHAR(c) |
| #define | IS_QUOTE_CHAR(c) ( (c) == '"' || (c) == '\'' ) |
| #define | IS_NAME_CHAR(c) (isalnum(c) || (c)=='_' || (c)=='.' || (c)=='-' || (c)==':') |
Typedefs | |
| typedef unsigned char * | POINTER |
| typedef unsigned short int | UINT2 |
| typedef unsigned long int | UINT4 |
Functions | |
| void | NI_dpr (char *,...) |
| void | NI_stream_close_keep (NI_stream_type *,int) |
| int | string_index (char *targ, int nstr, char *str[]) |
| int | unescape_inplace (char *str) |
| char * | quotize_string (char *str) |
| char * | quotize_string_vector (int num, char **str, char sep) |
| char * | quotize_int_vector (int num, int *vec, char sep) |
| char * | quotize_float_vector (int num, float *vec, char sep) |
| int | NI_is_name (char *str) |
| char * | trailname (char *fname, int lev) |
| void | load_encode_table (void) |
| void | load_decode_table (void) |
| void | MD5Init (MD5_CTX *) |
| void | MD5Update (MD5_CTX *, unsigned char *, unsigned int) |
| void | MD5Final (unsigned char[16], MD5_CTX *) |
| void | destroy_header_stuff (header_stuff *hs) |
| intpair | find_string (int nst, int nch, char *ch) |
| header_stuff * | parse_header_stuff (int ndat, char *dat, int *nused) |
| intpair | decode_type_field (char *tf) |
| int_array * | decode_dimen_string (char *ds) |
| int_array * | decode_type_string (char *ts) |
| char | NI_type_char (int typ) |
| void | enhance_header_stuff (header_stuff *hs) |
| char * | get_header_attribute (header_stuff *hs, char *attname) |
| NI_element * | make_empty_data_element (header_stuff *hs) |
| NI_group * | make_empty_group_element (header_stuff *hs) |
| void | NI_fill_vector_row (NI_element *nel, int row, char *buf) |
| int | NI_stream_writestring (NI_stream_type *ns, char *str) |
| int | NI_stream_fillbuf (NI_stream_type *ns, int minread, int msec) |
| void | NI_reset_buffer (NI_stream_type *) |
| int | NI_decode_one_double (NI_stream_type *, double *, int) |
| int | NI_decode_one_string (NI_stream_type *, char **, int) |
Variables | |
| FILE * | dfp |
| int | dtable_mode |
| byte | dtable [256] |
| int | linelen |
| int | ncrlf |
| int | nocrlf |
| int | typedef_nib |
| int | typedef_num |
| char ** | typedef_nam |
| char ** | typedef_typ |
| char ** | typedef_dim |
Define Documentation
|
|
Value: ( a = (dtable[w] << 2) | (dtable[x] >> 4) , \ b = (dtable[x] << 4) | (dtable[y] >> 2) , \ c = (dtable[y] << 6) | dtable[z] ) Definition at line 91 of file niml_private.h. Referenced by B64_to_binary(), and NI_stream_readbuf64(). |
|
|
Value: ( ((w)=='='||(x)=='=') ? 0 \ : ((y)=='=') ? 1 \ : ((z)=='=') ? 2 : 3 ) Definition at line 98 of file niml_private.h. Referenced by B64_to_binary(), and NI_stream_readbuf64(). |
|
|
Encode 1 byte (a) into 4 bytes (w,x,y,z) Definition at line 86 of file niml_private.h. Referenced by B64_to_base64(), and NI_write_columns(). |
|
|
Encode 2 bytes (a,b) into 4 bytes (w,x,y,z) Definition at line 81 of file niml_private.h. Referenced by B64_to_base64(), and NI_write_columns(). |
|
|
Value: ( w = dtable[(a)>>2] , \
x = dtable[((a & 3) << 4) | (b >> 4)] , \
y = dtable[((b & 0xF) << 2) | (c >> 6)] , \
z = dtable[c & 0x3F] )Definition at line 73 of file niml_private.h. Referenced by B64_to_base64(), and NI_write_columns(). |
|
|
Definition at line 68 of file niml_private.h. |
|
|
Definition at line 69 of file niml_private.h. |
|
|
Definition at line 66 of file niml_private.h. Referenced by B64_to_binary(), and NI_stream_readbuf64(). |
|
|
Defines characters allowed inside a "name". Definition at line 146 of file niml_private.h. Referenced by decode_type_field(), and NI_is_name(). |
|
|
Defines what we consider a quoting character. Definition at line 142 of file niml_private.h. Referenced by find_string(), NI_decode_one_string(), and parse_header_stuff(). |
|
|
Value: Characters allowed inside unquoted strings.Definition at line 136 of file niml_private.h. Referenced by find_string(), and NI_write_procins(). |
|
|
Definition at line 18 of file niml_private.h. |
|
|
Definition at line 6 of file niml_private.h. |
Typedef Documentation
|
|
Definition at line 108 of file niml_private.h. |
|
|
Definition at line 109 of file niml_private.h. |
|
|
Definition at line 110 of file niml_private.h. |
Function Documentation
|
|
Decode a ni_dimen string into an array of integers. Returns NULL if the input is bad bad bad. ---------------------------------------------------------------------- Definition at line 391 of file niml_header.c. References int_array::ar, NI_free, NI_malloc, NI_realloc, NI_strlen(), and int_array::num. Referenced by make_empty_data_element().
00392 {
00393 int num , dd,nn,id,jd , lds ;
00394 int_array *iar ;
00395
00396 if( ds == NULL || ds[0] == '\0' ) return NULL ;
00397
00398 iar = NI_malloc(int_array, sizeof(int_array)) ; /* create output */
00399 iar->num = 0 ; iar->ar = NULL ;
00400
00401 /* scan string for integers */
00402
00403 num = id = 0 ;
00404 lds = NI_strlen(ds) ;
00405 do{
00406 /* skip ahead until ds[id] is a digit */
00407
00408 while( id < lds && !isdigit(ds[id]) ) id++ ;
00409 if( id == lds ) break ; /* end of input */
00410
00411 /* decode integer starting here */
00412
00413 nn = jd = 0 ;
00414 sscanf( ds+id , "%d%n" , &jd , &nn ) ; /* get the count */
00415 if( jd < 0 || nn <= 0 ) break ; /* something bad */
00416 id += nn ; /* skip these chars */
00417
00418 /* extend output array, store new dimension in it */
00419
00420 iar->ar = NI_realloc( iar->ar , int, sizeof(int)*(num+1) ) ;
00421 iar->ar[num++] = jd ;
00422 } while(1) ;
00423
00424 if( num == 0 ){ NI_free(iar); return NULL; } /* bad */
00425
00426 iar->num = num ; return iar ;
00427 }
|
|
|
Decode a single type field. Return value is an intpair with the .i component being the type code and the .j component being the number of characters consumed. If the .i component is -1, then no legal type was found (.j is still the number of chars used in the scan). ---------------------------------------------------------------------- Definition at line 249 of file niml_header.c. References intpair::i, IS_NAME_CHAR, intpair::j, NI_BYTE, NI_COMPLEX, NI_DOUBLE, NI_FLOAT, NI_INT, NI_RGB, NI_RGBA, NI_rowtype_name_to_code(), NI_SHORT, and NI_STRING. Referenced by decode_type_string().
00250 {
00251 intpair ans = {-1,1} ; /* default answer */
00252 char tname[256] ;
00253 int jj ;
00254
00255 /* check input for goodness */
00256
00257 if( tf == NULL || !isalpha(*tf) ) return ans ; /* prima facie bad */
00258
00259 #if 1 /*** The new way! Look for full names, not initials - RWCcox ***/
00260
00261 /* copy input into local string,
00262 as long as 'name' characters are found,
00263 then get the integer code for this type name */
00264
00265 for( jj=0 ; jj < 255 && IS_NAME_CHAR(tf[jj]) ; jj++ ) tname[jj] = tf[jj];
00266 tname[jj] = '\0' ;
00267 ans.i = NI_rowtype_name_to_code( tname ) ; /* where the names are stored */
00268 ans.j = jj ;
00269
00270 #else /*** This is the old way! Replaced on 12 Feb 2003 - RWCox ***/
00271
00272 /* check if tf[0] starts a full datum name,
00273 or if it is just an initial */
00274
00275 switch( tf[0] ){
00276
00277 default: break ; /* not a legal datum character */
00278
00279 case 'b':
00280 ans.i = NI_BYTE ;
00281 if( strncmp(tf,"byte" ,4) == 0 ) ans.j = 4 ;
00282 break ;
00283
00284 case 's':
00285 ans.i = NI_SHORT ;
00286 if( strncmp(tf,"short" ,5) == 0 ) ans.j = 5 ;
00287 break ;
00288
00289 case 'i':
00290 ans.i = NI_INT ;
00291 if( strncmp(tf,"int" ,3) == 0 ) ans.j = 3 ;
00292 break ;
00293
00294 case 'f':
00295 ans.i = NI_FLOAT ;
00296 if( strncmp(tf,"float" ,5) == 0 ) ans.j = 5 ;
00297 break ;
00298
00299 case 'c':
00300 ans.i = NI_COMPLEX ;
00301 if( strncmp(tf,"complex",7) == 0 ) ans.j = 7 ;
00302 break ;
00303
00304 case 'd':
00305 ans.i = NI_DOUBLE ;
00306 if( strncmp(tf,"double" ,6) == 0 ) ans.j = 6 ;
00307 break ;
00308
00309 case 'r':
00310 ans.i = NI_RGB ;
00311 if( strncmp(tf,"rgb" ,3) == 0 ) ans.j = 3 ;
00312 break ;
00313
00314 case 'R':
00315 ans.i = NI_RGBA ;
00316 if( strncmp(tf,"RGBA" ,4) == 0 ) ans.j = 4 ;
00317 break ;
00318
00319 case 'S':
00320 ans.i = NI_STRING ;
00321 if( strncmp(tf,"STRING" ,6) == 0 ) ans.j = 6 ;
00322 break ;
00323 }
00324 #endif
00325
00326 return ans ;
00327 }
|
|
|
Decode a data type string into an array of integer codes. Returns NULL if the input is bad bad bad. ---------------------------------------------------------------------- Definition at line 434 of file niml_header.c. References int_array::ar, decode_type_field(), intpair::i, intpair::j, NI_free, NI_malloc, NI_realloc, NI_strlen(), int_array::num, and typ. Referenced by make_empty_data_element(), SUMA_TypeOfColNumb(), and SUMA_TypeOfDsetColNumb().
00435 {
00436 int num, typ, lts, id,jd, nn,kk ;
00437 int_array *iar ;
00438 intpair dc ;
00439
00440 if( ts == NULL || ts[0] == '\0' ) return NULL ;
00441
00442 iar = NI_malloc(int_array, sizeof(int_array)) ; /* create output */
00443 iar->num = 0 ; iar->ar = NULL ;
00444
00445 /* scan type string to find counts/fields and add to output */
00446
00447 lts = NI_strlen(ts) ;
00448 num = 0 ; /* will be count of fields */
00449
00450 for( id=kk=0 ; id < lts ; ){ /* loop over input string */
00451
00452 if( isdigit(ts[id]) ){ /* a count prefix */
00453 jd = nn = 0 ;
00454 sscanf( ts+id , "%d%n" , &jd , &nn ) ; /* get the count */
00455 if( jd <= 0 || nn <= 0 ){ /* shouldn't happen */
00456 NI_free(iar->ar) ; NI_free(iar) ; return NULL ;
00457 }
00458 id += nn ; /* skip count prefix characters */
00459 if( ts[id] == '*' ) id++ ; /* allow for "3*float" */
00460
00461 } else if( isalpha(ts[id]) ){ /* start of a type name */
00462 jd = 1 ; /* default count of 1 */
00463
00464 } else {
00465 id++ ; continue ; /* skip this character */
00466 }
00467
00468 dc = decode_type_field( ts+id ) ;
00469
00470 /* dc.i = type code; dc.j = character count used to get type code */
00471
00472 id += dc.j ; /* skip these characters */
00473 if( dc.i < 0 ) continue ; /* bad type code */
00474
00475 num += jd ; /* this many fields so far */
00476
00477 /* extend output array length */
00478
00479 iar->ar = NI_realloc( iar->ar , int, sizeof(int)*num ) ;
00480
00481 /* put values into output array */
00482
00483 for( nn=0 ; nn < jd ; nn++ ) iar->ar[kk++] = dc.i ;
00484
00485 } /* end of loop over input string */
00486
00487 /* nothing found? */
00488
00489 if( num <= 0 ){
00490 NI_free(iar->ar) ; NI_free(iar) ; return NULL ; /* bad */
00491 }
00492
00493 iar->num = num ; return iar ;
00494 }
|
|
|
Deallocate a header_stuff struct. ---------------------------------------------------------------------------- Definition at line 36 of file niml_header.c. References header_stuff::lhs, header_stuff::name, header_stuff::nattr, NI_free, and header_stuff::rhs. Referenced by NI_read_element(), and parse_header_stuff().
|
|
|
|
|
||||||||||||||||
|
Find an isolated string in the input array of char.
Definition at line 61 of file niml_header.c. References intpair::i, IS_QUOTE_CHAR, IS_STRING_CHAR, intpair::j, and NI_dpr(). Referenced by NI_decode_one_string(), and parse_header_stuff().
00062 {
00063 intpair ans = {-1,-1} ; /* default answer ==> nothing found */
00064 int ii,jj ;
00065 char quot ;
00066
00067 #ifdef NIML_DEBUG
00068 NI_dpr(" ENTER find_string: nst=%d nch=%d\n",nst,nch) ;
00069 #endif
00070
00071 if( nst >= nch || nch < 2 || ch == NULL ) return ans; /* bad input */
00072
00073 for( ii=nst; ii<nch && !IS_STRING_CHAR(ch[ii]); ii++ ) ; /* skip to start */
00074
00075 if( ii >= nch ) return ans ; /* bad input */
00076
00077 if( IS_QUOTE_CHAR(ch[ii]) ){ /* quoted string */
00078 if( ii == nch-1 ) return ans ; /* bad input */
00079 quot = ch[ii] ; ii++ ;
00080 for( jj=ii ; jj<nch && ch[jj] != quot ; jj++ ) ; /* skip to close */
00081 } else {
00082 for( jj=ii+1 ; jj<nch && IS_STRING_CHAR(ch[jj]) ; jj++ ) ; /* to blank */
00083 }
00084
00085 ans.i = ii ; ans.j = jj ; /* answer starts at ch[ii] and goes to ch[jj-1] */
00086 return ans ;
00087 }
|
|
||||||||||||
|
Find an attribute in a header_stuff struct. Returns the RHS or NULL. ---------------------------------------------------------------------------- Definition at line 15 of file niml_header.c. References header_stuff::lhs, header_stuff::nattr, and header_stuff::rhs. Referenced by header_stuff_is_group().
00016 {
00017 int nn ;
00018 static char *zorkon = "\0" ;
00019
00020 if( hs == NULL ) return NULL ;
00021
00022 for( nn=0 ; nn < hs->nattr ; nn++ )
00023 if( strcmp(hs->lhs[nn],attname) == 0 ) break ;
00024
00025 if( nn == hs->nattr ) return NULL ;
00026
00027 if( hs->rhs[nn] == NULL ) return zorkon ;
00028
00029 return hs->rhs[nn] ;
00030 }
|
|
|
Load the base64 decoding table. ------------------------------------------------------------------------ Definition at line 55 of file thd_base64.c. References dtable, dtable_mode, and i. Referenced by B64_to_binary(), and NI_stream_readbuf64().
00056 {
00057 int i;
00058 if( dtable_mode == 2 ) return ;
00059 for (i = 0 ; i < 255 ; i++) dtable[i] = 0x80; /* bad */
00060 for (i = 'A'; i <= 'Z'; i++) dtable[i] = 0 + (i - 'A');
00061 for (i = 'a'; i <= 'z'; i++) dtable[i] = 26 + (i - 'a');
00062 for (i = '0'; i <= '9'; i++) dtable[i] = 52 + (i - '0');
00063 dtable['+'] = 62; dtable['/'] = 63; dtable['='] = 0; dtable_mode = 2 ;
00064 return ;
00065 }
|
|
|
Load the base64 encoding table. ------------------------------------------------------------------------ Definition at line 40 of file thd_base64.c. References dtable, dtable_mode, and i. Referenced by B64_to_base64(), and NI_write_columns().
|
|
|
Construct an empty data element from a header.
Definition at line 15 of file niml_element.c. References int_array::ar, NI_element::attr_lhs, NI_element::attr_num, NI_element::attr_rhs, decode_dimen_string(), decode_type_string(), header_stuff::empty, header_stuff::lhs, NI_element::name, header_stuff::name, header_stuff::nattr, NI_BYTE, NI_decode_string_list(), NI_delete_str_array, NI_dpr(), NI_ELEMENT_TYPE, NI_free, NI_malloc, NI_strdup(), NI_type_size(), NI_str_array::num, int_array::num, NI_element::outmode, header_stuff::rhs, NI_str_array::str, string_index(), NI_element::type, NI_element::vec, NI_element::vec_axis_delta, NI_element::vec_axis_label, NI_element::vec_axis_len, NI_element::vec_axis_origin, NI_element::vec_axis_unit, NI_element::vec_filled, NI_element::vec_len, NI_element::vec_num, NI_element::vec_rank, and NI_element::vec_typ. Referenced by NI_read_element().
00016 {
00017 NI_element *nel ;
00018 int ii , qq ;
00019
00020 if( hs == NULL || hs->name == NULL ) return NULL ;
00021
00022 #ifdef NIML_DEBUG
00023 NI_dpr("ENTER make_empty_data_element\n") ;
00024 #endif
00025
00026 nel = NI_malloc(NI_element, sizeof(NI_element) ) ;
00027
00028 nel->type = NI_ELEMENT_TYPE ;
00029
00030 nel->outmode = -1 ; /* 29 Mar 2005 */
00031
00032 /* move name and attributes from hs to new element */
00033
00034 nel->name = hs->name ; hs->name = NULL ;
00035
00036 nel->attr_num = hs->nattr ;
00037
00038 if( nel->attr_num > 0 ){
00039 nel->attr_lhs = hs->lhs ; hs->lhs = NULL ;
00040 nel->attr_rhs = hs->rhs ; hs->rhs = NULL ;
00041 } else {
00042 nel->attr_lhs = nel->attr_rhs = NULL ;
00043 }
00044
00045 /* set default vector parameters [indicating no data] */
00046
00047 nel->vec_num = 0 ;
00048 nel->vec_len = 0 ;
00049 nel->vec_typ = NULL ;
00050 nel->vec = NULL ;
00051
00052 nel->vec_filled = 0 ; /* no data has been filled into vectors */
00053
00054 nel->vec_rank = 0 ;
00055 nel->vec_axis_len = NULL ;
00056 nel->vec_axis_delta = NULL ;
00057 nel->vec_axis_origin = NULL ;
00058 nel->vec_axis_unit = NULL ;
00059 nel->vec_axis_label = NULL ;
00060
00061 if( !hs->empty ){ /* find and process ni_* attributes about vectors */
00062
00063 /* ni_type attribute: set types of vectors */
00064
00065 ii = string_index( "ni_type" , nel->attr_num , nel->attr_lhs ) ;
00066
00067 if( ii >= 0 && nel->attr_rhs[ii] != NULL ){
00068 int_array *iar = decode_type_string( nel->attr_rhs[ii] ) ;
00069 if( iar != NULL ){
00070 nel->vec_num = iar->num ; /* number of vectors */
00071 nel->vec_typ = iar->ar ; /* vector types */
00072 NI_free(iar) ; /* just the shell of the struct */
00073 }
00074 }
00075
00076 /* ni_dimen attribute: set vector length and rank */
00077
00078 ii = string_index( "ni_dimen" , nel->attr_num , nel->attr_lhs ) ;
00079
00080 if( ii >= 0 && nel->attr_rhs[ii] != NULL ){
00081 int_array *dar = decode_dimen_string( nel->attr_rhs[ii] ) ;
00082 if( dar != NULL && dar->num > 0 ){
00083 int nd=dar->num , qq,pp ;
00084 /* compute product of all dimensions */
00085 for( qq=1,pp=0 ; pp < nd ; pp++ ) qq *= dar->ar[pp] ;
00086 nel->vec_len = qq ; /* length of vectors */
00087 nel->vec_rank = nd ; /* number of dimensions */
00088 nel->vec_axis_len = dar->ar ; /* array of dimension lengths */
00089 NI_free(dar) ; /* just the struct shell */
00090 if( nel->vec_len == 0 ) /* 27 Mar 2003 */
00091 nel->vec_rank = 1 ;
00092 }
00093 }
00094
00095 /* if we had ni_dimen, also try ni_delta */
00096
00097 ii = string_index( "ni_delta" , nel->attr_num , nel->attr_lhs ) ;
00098 if( ii >= 0 && nel->vec_rank > 0 ){
00099 NI_str_array *sar = NI_decode_string_list( nel->attr_rhs[ii] , NULL ) ;
00100 if( sar != NULL && sar->num > 0 ){
00101 int ns=sar->num , nd=nel->vec_rank , pp ;
00102 nel->vec_axis_delta = NI_malloc(float,sizeof(float)*nd) ;
00103 if( nd > ns ) nd = ns ;
00104 for( pp=0 ; pp < nd ; pp++ )
00105 sscanf( sar->str[pp] , "%f" , nel->vec_axis_delta+pp ) ;
00106 NI_delete_str_array(sar) ;
00107 }
00108 }
00109
00110 /* if we had ni_dimen, also try ni_origin */
00111
00112 ii = string_index( "ni_origin" , nel->attr_num , nel->attr_lhs ) ;
00113 if( ii >= 0 && nel->vec_rank > 0 ){
00114 NI_str_array *sar = NI_decode_string_list( nel->attr_rhs[ii] , NULL ) ;
00115 if( sar != NULL && sar->num > 0 ){
00116 int ns=sar->num , nd=nel->vec_rank , pp ;
00117 nel->vec_axis_origin = NI_malloc(float,sizeof(float)*nd) ;
00118 if( nd > ns ) nd = ns ;
00119 for( pp=0 ; pp < nd ; pp++ )
00120 sscanf( sar->str[pp] , "%f" , nel->vec_axis_origin+pp ) ;
00121 NI_delete_str_array(sar) ;
00122 }
00123 }
00124
00125 /* if we had ni_dimen, also try ni_units */
00126
00127 ii = string_index( "ni_units" , nel->attr_num , nel->attr_lhs ) ;
00128 if( ii >= 0 && nel->vec_rank > 0 ){
00129 NI_str_array *sar = NI_decode_string_list( nel->attr_rhs[ii] , NULL ) ;
00130 if( sar != NULL && sar->num > 0 ){
00131 int ns=sar->num , nd=nel->vec_rank , pp ;
00132 nel->vec_axis_unit = NI_malloc(char*,sizeof(char *)*nd) ;
00133 if( nd > ns ) nd = ns ;
00134 for( pp=0 ; pp < nd ; pp++ )
00135 nel->vec_axis_unit[pp] = NI_strdup(sar->str[pp]) ;
00136 NI_delete_str_array(sar) ;
00137 }
00138 }
00139
00140 /* if we had ni_dimen, also try ni_axes */
00141
00142 ii = string_index( "ni_axes" , nel->attr_num , nel->attr_lhs ) ;
00143 if( ii >= 0 && nel->vec_rank > 0 ){
00144 NI_str_array *sar = NI_decode_string_list( nel->attr_rhs[ii] , NULL ) ;
00145 if( sar != NULL && sar->num > 0 ){
00146 int ns=sar->num , nd=nel->vec_rank , pp ;
00147 nel->vec_axis_label = NI_malloc(char*,sizeof(char *)*nd) ;
00148 if( nd > ns ) nd = ns ;
00149 for( pp=0 ; pp < nd ; pp++ )
00150 nel->vec_axis_label[pp] = NI_strdup(sar->str[pp]) ;
00151 NI_delete_str_array(sar) ;
00152 }
00153 }
00154
00155 /* supply vector parameters if none was given */
00156 /* (remember, we DON'T have an empty element) */
00157
00158 if( nel->vec_num == 0 ){ /* default type */
00159 nel->vec_num = 1 ;
00160 nel->vec_typ = NI_malloc(int,sizeof(int)) ;
00161 nel->vec_typ[0] = NI_BYTE ;
00162 }
00163
00164 if( nel->vec_rank == 0 ){ /* default dimensions */
00165 nel->vec_len = 0 ;
00166 nel->vec_rank = 1 ;
00167 nel->vec_axis_len = NI_malloc(int, sizeof(int)) ;
00168 nel->vec_axis_len[0] = 1 ;
00169 }
00170
00171 /* now allocate space for vectors defined above */
00172
00173 nel->vec = NI_malloc(void*, sizeof(void *)*nel->vec_num ) ;
00174
00175 /* 27 Mar 2003: only allocate space if we know how long they are */
00176
00177 if( nel->vec_len > 0 ){
00178 for( ii=0 ; ii < nel->vec_num ; ii++ )
00179 nel->vec[ii] = NI_malloc(void,
00180 NI_type_size(nel->vec_typ[ii])*nel->vec_len) ;
00181 } else {
00182 for( ii=0 ; ii < nel->vec_num ; ii++ )
00183 nel->vec[ii] = NULL ;
00184 }
00185
00186 } /* end of processing non-empty header stuff */
00187
00188 return nel ;
00189 }
|
|
|
Make an empty group element from parsed header info. The attributes in the header are assigned to the group, and the group parts are initialized to nothing. --------------------------------------------------------------------------- Definition at line 197 of file niml_element.c. References NI_group::attr_lhs, NI_group::attr_num, NI_group::attr_rhs, header_stuff::lhs, NI_group::name, header_stuff::name, header_stuff::nattr, NI_GROUP_TYPE, NI_malloc, NI_group::outmode, NI_group::part, NI_group::part_num, NI_group::part_typ, header_stuff::rhs, and NI_group::type. Referenced by NI_read_element().
00198 {
00199 NI_group *ngr ;
00200 int ii , qq ;
00201
00202 if( hs == NULL || hs->name == NULL ) return NULL ;
00203
00204 ngr = NI_malloc(NI_group, sizeof(NI_group) ) ;
00205
00206 ngr->type = NI_GROUP_TYPE ;
00207
00208 ngr->name = hs->name ; hs->name = NULL ; /* 24 Feb 2005 */
00209
00210 ngr->outmode = -1 ; /* 29 Mar 2005 */
00211
00212 /* move attributes from hs to new element */
00213
00214 ngr->attr_num = hs->nattr ;
00215
00216 if( ngr->attr_num > 0 ){
00217 ngr->attr_lhs = hs->lhs ; hs->lhs = NULL ;
00218 ngr->attr_rhs = hs->rhs ; hs->rhs = NULL ;
00219 } else {
00220 ngr->attr_lhs = ngr->attr_rhs = NULL ;
00221 }
00222
00223 /* have no pieces-parts yet */
00224
00225 ngr->part_num = 0 ;
00226 ngr->part_typ = NULL ;
00227 ngr->part = NULL ;
00228
00229 return ngr ;
00230 }
|
|
||||||||||||
|
MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. ------------------------------------------------------------------------ Definition at line 190 of file thd_md5.c.
00191 {
00192 unsigned char bits[8];
00193 unsigned int index, padLen;
00194
00195 /* Save number of bits */
00196
00197 Encode (bits, context->count, 8);
00198
00199 /* Pad out to 56 mod 64. */
00200
00201 index = (unsigned int)((context->count[0] >> 3) & 0x3f);
00202 padLen = (index < 56) ? (56 - index) : (120 - index);
00203 MD5Update (context, PADDING, padLen);
00204
00205 /* Append length (before padding) */
00206
00207 MD5Update (context, bits, 8);
00208
00209 /* Store state in digest */
00210
00211 Encode (digest, context->state, 16);
00212
00213 /* Zeroize sensitive information. */
00214
00215 memset ((POINTER)context, 0, sizeof (*context));
00216 }
|
|
|
MD5 initialization. Begins an MD5 operation, writing a new context. ------------------------------------------------------------------------ Definition at line 127 of file thd_md5.c.
|
|
||||||||||||||||
|
MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. ------------------------------------------------------------------------ Definition at line 145 of file thd_md5.c.
00147 {
00148 unsigned int i, index, partLen;
00149
00150 /* Compute number of bytes mod 64 */
00151
00152 index = (unsigned int)((context->count[0] >> 3) & 0x3F);
00153
00154 /* Update number of bits */
00155
00156 if( (context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3) )
00157 context->count[1]++;
00158
00159 context->count[1] += ((UINT4)inputLen >> 29);
00160
00161 partLen = 64 - index;
00162
00163 /* Transform as many times as possible. */
00164
00165 if (inputLen >= partLen) {
00166
00167 memcpy ((POINTER)&context->buffer[index], (POINTER)input, partLen);
00168
00169 MD5Transform (context->state, context->buffer);
00170
00171 for (i = partLen; i + 63 < inputLen; i += 64)
00172 MD5Transform (context->state, &input[i]);
00173
00174 index = 0;
00175 }
00176 else
00177 i = 0;
00178
00179 /* Buffer remaining input */
00180
00181 memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i],
00182 inputLen-i);
00183 }
|
|
||||||||||||||||
|
From the NI_stream ns, starting at buffer position ns->npos, decode one number into *val.
Definition at line 489 of file niml_elemio.c. References NI_stream_type::bad, NI_stream_type::buf, clear_buffer, IS_CRLF, IS_USELESS, MARKED_FOR_DEATH, NI_stream_type::nbuf, NI_dpr(), NI_reset_buffer(), NI_stream_fillbuf(), NI_stream_type::npos, and NVBUF. Referenced by NI_text_to_val().
00490 {
00491 int epos , num_restart, need_data, nn ;
00492 char vbuf[NVBUF+1] ; /* number string from buffer */
00493
00494 /*-- check inputs for stupidness --*/
00495
00496 if( ns == NULL || ns->bad == MARKED_FOR_DEATH || val == NULL ) return 0 ;
00497
00498 /*--- might loop back here to check if have enough data for a number ---*/
00499
00500 num_restart = 0 ;
00501 Restart:
00502 num_restart++ ; need_data = 0 ;
00503 if( num_restart > 19 ) return 0 ; /*** too much ==> give up ***/
00504
00505 #ifdef NIML_DEBUG
00506 NI_dpr(" {restart: npos=%d nbuf=%d}",ns->npos,ns->nbuf) ;
00507 #endif
00508
00509 /*-- advance over useless characters in the buffer --*/
00510
00511 while( ns->npos < ns->nbuf && IS_USELESS(ns->buf[ns->npos]) ) ns->npos++ ;
00512
00513 /*-- check if we ran into the closing '<' prematurely
00514 (before any useful characters); if we did, then we are done --*/
00515
00516 if( ltend && ns->npos < ns->nbuf && ns->buf[ns->npos] == '<' ) return 0 ;
00517
00518 /*-- 20 Mar 2003: check if we ran into a comment character '#';
00519 if we did, skip to the end of the line (or '<') --*/
00520
00521 if( ltend && ns->npos < ns->nbuf && ns->buf[ns->npos] == '#' ){
00522 int npold = ns->npos ;
00523 while( ns->npos < ns->nbuf && !IS_CRLF(ns->buf[ns->npos]) ){
00524 if( ns->buf[ns->npos] == '<' ) return 0 ; /* STOP HERE! */
00525 ns->npos++ ;
00526 }
00527 if( ns->npos < ns->nbuf ){ /* found end of line, so try again */
00528 num_restart = 0 ; goto Restart ;
00529 }
00530 /* if here, didn't find '<' or end of line in buffer */
00531 /* so reset pointer back to '#', then read more data */
00532 ns->npos = npold ; need_data = 1 ;
00533 }
00534
00535 /*-- if we need some data, try to get some --*/
00536
00537 if( !need_data ) /* need at least 2 unused */
00538 need_data = (ns->nbuf-ns->npos < 2) ; /* bytes to decode a number */
00539
00540 /*-- An input value is decoded from a string of non-useless
00541 characters delimited by a useless character (or by the
00542 element closing '<').
00543 Note that the 1st character we are now at is non-useless.
00544 Scan forward to see if we have a useless character later. --*/
00545
00546 if( !need_data ){ /* so have at least 2 characters */
00547
00548 #ifdef NIML_DEBUG
00549 nn = ns->nbuf-ns->npos ; if( nn > 19 ) nn = 19 ;
00550 NI_dpr(" {buf=%.*s}" , nn , ns->buf+ns->npos ) ;
00551 #endif
00552
00553 for( epos=ns->npos+1 ; epos < ns->nbuf ; epos++ )
00554 if( ns->buf[epos] == '<' || IS_USELESS(ns->buf[epos]) ) break ;
00555
00556 /*- epos is either the delimiter position, or the end of data bytes -*/
00557
00558 need_data = (epos == ns->nbuf) ; /* no delimiter ==> need more data */
00559
00560 #ifdef NIML_DEBUG
00561 if( need_data ) NI_dpr(" {eob}") ;
00562 #endif
00563
00564 /*- If the string of characters we have is not yet
00565 delimited, and it is too long to be a number,
00566 throw out all the data in the buffer and quit. -*/
00567
00568 if( need_data && epos-ns->npos > NVBUF ){ clear_buffer(ns); return 0; }
00569 }
00570
00571 /*-- read more data now if it is needed --*/
00572
00573 if( need_data ){
00574
00575 NI_reset_buffer(ns) ; /* discard used up data in buffer */
00576
00577 /*- read at least 1 byte,
00578 waiting up to 666 ms (unless the data stream goes bad) -*/
00579
00580 #ifdef NIML_DEBUG
00581 NI_dpr(" {fill buf}") ;
00582 #endif
00583 nn = NI_stream_fillbuf( ns , 1 , 666 ) ;
00584
00585 if( nn >= 0 ) goto Restart ; /* check if buffer is adequate now */
00586
00587 /*- if here, the stream went bad. If there are still
00588 data bytes in the stream, we can try to interpret them.
00589 Otherwise, must quit without success. -*/
00590
00591 if( ns->nbuf == 0 ){ ns->npos=0; return 0; } /* quitting */
00592
00593 epos = ns->nbuf ;
00594 }
00595
00596 /*-- if here, try to interpret data bytes ns->npos .. epos-1 --*/
00597
00598 nn = epos-ns->npos ; if( nn > NVBUF ) nn = NVBUF ; /* # bytes to read */
00599 memcpy( vbuf, ns->buf+ns->npos, nn ); vbuf[nn] = '\0'; /* put bytes in vbuf */
00600 *val = 0.0 ; /* initialize val */
00601 sscanf( vbuf , "%lf" , val ) ; /* interpret them */
00602 ns->npos = epos ; return 1 ; /* retire undefeated */
00603 }
|
|
||||||||||||||||
|
From the NI_stream ns, starting at buffer position ns->npos, decode one string into newly NI_malloc()-ed space pointed to by *str.
Definition at line 615 of file niml_elemio.c. References NI_stream_type::bad, NI_stream_type::buf, find_string(), intpair::i, IS_CRLF, IS_QUOTE_CHAR, IS_USELESS, intpair::j, MARKED_FOR_DEATH, NI_stream_type::nbuf, NI_malloc, NI_reset_buffer(), NI_stream_fillbuf(), and NI_stream_type::npos. Referenced by NI_text_to_val().
00616 {
00617 int epos , num_restart, need_data, nn ;
00618 intpair sp ;
00619
00620 /*-- check inputs for stupidness --*/
00621
00622 if( ns == NULL || ns->bad == MARKED_FOR_DEATH || str == NULL ) return 0 ;
00623
00624 /*--- might loop back here to check if have enough data ---*/
00625
00626 num_restart = 0 ;
00627 Restart:
00628 num_restart++ ; need_data = 0 ;
00629 if( num_restart > 19 ) return 0 ; /*** give up ***/
00630
00631 /*-- advance over useless characters in the buffer --*/
00632
00633 while( ns->npos < ns->nbuf && IS_USELESS(ns->buf[ns->npos]) ) ns->npos++ ;
00634
00635 /*-- check if we ran into the closing '<' prematurely
00636 (before any useful characters); if we did, then we are done --*/
00637
00638 if( ltend && ns->npos < ns->nbuf && ns->buf[ns->npos] == '<' ) return 0 ;
00639
00640 /*-- 20 Mar 2003: check if we ran into a comment character '#';
00641 if we did, skip to the end of the line (or '<') --*/
00642
00643 if( ltend && ns->npos < ns->nbuf && ns->buf[ns->npos] == '#' ){
00644 int npold = ns->npos ;
00645 while( ns->npos < ns->nbuf && !IS_CRLF(ns->buf[ns->npos]) ){
00646 if( ns->buf[ns->npos] == '<' ) return 0 ; /* STOP HERE! */
00647 ns->npos++ ;
00648 }
00649 if( ns->npos < ns->nbuf ){ /* found end of line, so try again */
00650 num_restart = 0 ; goto Restart ;
00651 }
00652 /* if here, didn't find '<' or end of line in buffer */
00653 /* so reset pointer back to '#', then read more data */
00654 ns->npos = npold ; need_data = 1 ;
00655 }
00656
00657 /*-- if we need some data, try to get some --*/
00658
00659 if( !need_data ) /* need at least 2 unused */
00660 need_data = (ns->nbuf-ns->npos < 2) ; /* bytes to decode a string */
00661
00662 if( !need_data ){ /* so have at least 2 characters */
00663
00664 /* search for the string from here forward */
00665
00666 sp = find_string( ns->npos , ns->nbuf , ns->buf ) ;
00667
00668 need_data = (sp.i < 0) || /* didn't find a string */
00669 (sp.j <= sp.i) || /* ditto */
00670 (sp.j == ns->nbuf) ; /* hit end of data bytes */
00671 }
00672
00673 /*-- read more data now if it is needed --*/
00674
00675 if( need_data ){
00676
00677 NI_reset_buffer(ns) ; /* discard used up data in buffer */
00678
00679 /*- read at least 1 byte,
00680 waiting up to 666 ms (unless the data stream goes bad) -*/
00681
00682 nn = NI_stream_fillbuf( ns , 1 , 666 ) ;
00683
00684 if( nn >= 0 ) goto Restart ; /* check if buffer is adequate now */
00685
00686 /*- if here, the stream went bad. If there are still
00687 data bytes in the stream, we can try to interpret them.
00688 Otherwise, must quit without success. -*/
00689
00690 if( ns->nbuf == 0 ){ ns->npos=0; return 0; } /* quitting */
00691
00692 sp.i = 0 ; sp.j = ns->nbuf ;
00693 }
00694
00695 /*-- if here, data bytes sp.i .. sp.j-1 are the string --*/
00696
00697 nn = sp.j - sp.i ; /* length of string */
00698 *str = NI_malloc(char, nn+1) ; /* make the string */
00699 memcpy( *str , ns->buf+sp.i , nn ) ; /* copy data to string */
00700 (*str)[nn] = '\0' ; /* terminate string */
00701
00702 /* skip close quote character, if present */
00703
00704 if( sp.j < ns->nbuf && IS_QUOTE_CHAR(ns->buf[sp.j]) ) sp.j++ ;
00705
00706 ns->npos = sp.j ; return 1 ;
00707 }
|
|
||||||||||||
|
Definition at line 9 of file niml_util.c. References dfp, and NI_malloc_status(). Referenced by find_string(), hidden_NI_free(), hidden_NI_malloc(), hidden_NI_realloc(), make_empty_data_element(), NI_decode_one_double(), NI_read_columns(), NI_read_element(), NI_stream_goodcheck(), NI_stream_read(), NI_stream_reopen(), NI_stream_write(), NI_write_columns(), NI_write_element(), parse_header_stuff(), SHM_goodcheck(), and tcp_connect().
00010 {
00011 va_list vararg_ptr ;
00012 char *nms ;
00013 if( dfp == NULL ) return ; /* printing turned off? */
00014 va_start( vararg_ptr , fmt ) ;
00015 vfprintf(dfp,fmt,vararg_ptr) ; /* do printing */
00016
00017 nms = NI_malloc_status() ; /* 18 Nov 2002 */
00018 if( nms != NULL ) fprintf(dfp," NI_malloc status: %s\n",nms) ;
00019
00020 fflush(dfp); va_end(vararg_ptr); /* cleanup */
00021 }
|
|
||||||||||||||||
|
|
|
|
Check a string for 'nameness' - that is, consists only of legal characters for a NIML 'Name' and also starts with an alphabetic character. Returns 1 if it is a Name and 0 if is not. -------------------------------------------------------------------------- Definition at line 437 of file niml_util.c. References IS_NAME_CHAR. Referenced by NI_rowtype_define(), and NI_write_element().
00438 {
00439 int ii , ll ;
00440
00441 if( str == NULL || str[0] == '\0' || !isalpha(str[0]) ) return 0 ;
00442
00443 for( ii=1 ; str[ii] != '\0' ; ii++ )
00444 if( !IS_NAME_CHAR(str[ii]) ) return 0 ; /* this one is bad */
00445
00446 return 1 ; /* all were good ==> success */
00447 }
|
|
|
Reset the unscanned bytes in the buffer to start at position 0 instead of position ns->npos; then set ns->npos to 0. ------------------------------------------------------------------------ Definition at line 714 of file niml_elemio.c. References NI_stream_type::bad, NI_stream_type::buf, MARKED_FOR_DEATH, NI_stream_type::nbuf, and NI_stream_type::npos. Referenced by NI_decode_one_double(), NI_decode_one_string(), NI_read_element(), NI_stream_readbuf(), NI_stream_readbuf64(), and scan_for_angles().
00715 {
00716 if( ns == NULL || ns->npos <= 0 || ns->nbuf <= 0 ) return ;
00717 if( ns->buf == NULL || ns->bad == MARKED_FOR_DEATH ) return ;
00718
00719 if( ns->npos < ns->nbuf ){ /* haven't used up all data yet */
00720 memmove( ns->buf, ns->buf+ns->npos, ns->nbuf-ns->npos ) ;
00721 ns->nbuf -= ns->npos ;
00722 } else {
00723 ns->nbuf = 0 ; /* all data in buffer is used up */
00724 }
00725 ns->npos = 0 ; /* further scanning starts at beginning */
00726 }
|
|
||||||||||||
|
Close a NI_stream, but don't free the insides. If (flag&1 != 0) send a "close_this" message to the other end. If (flag&2 != 0) use TCP OOB data to send a SIGURG to the other end. If (flag&4 != 0) don't remove from open_stream list [only from atexit()] ----------------------------------------------------------------------------- Definition at line 2389 of file niml_stream.c. References NI_stream_type::bad, NI_stream_type::buf, CLOSEDOWN, NI_stream_type::fp, NI_stream_type::io_mode, MARKED_FOR_DEATH, NI_FD_TYPE, NI_FILE_TYPE, NI_free, NI_OUTPUT_MODE, NI_REMOTE_TYPE, NI_SHM_TYPE, NI_sleep(), NI_stream_writecheck(), NI_stream_writestring(), NI_STRING_TYPE, NI_TCP_TYPE, NI_stream_type::orig_name, remove_open_stream(), NI_stream_type::sd, SHM_close(), NI_stream_type::shmioc, tcp_send, and NI_stream_type::type. Referenced by atexit_open_streams(), NI_do(), NI_stream_close(), NI_stream_closenow(), NI_stream_kill(), and NI_stream_reopen().
02390 {
02391 if( ns == NULL || !isgraph(ns->orig_name[0]) ) return ;
02392
02393 if( (flag & 4) == 0 ) /* 22 Apr 2005 */
02394 remove_open_stream( ns ) ; /* 02 Jan 2004 */
02395
02396 if( ns->bad == MARKED_FOR_DEATH ){
02397 if( ns->buf != NULL ){ NI_free(ns->buf); ns->buf = NULL;}
02398 return ;
02399 }
02400
02401 /*-- 20 Dec 2002: write a farewell message to the other end? --*/
02402
02403 if( (flag & 1) != 0 &&
02404 (ns->type == NI_TCP_TYPE || ns->type == NI_SHM_TYPE) &&
02405 NI_stream_writecheck(ns,1) > 0 ){
02406
02407 NI_stream_writestring( ns , "<?ni_do ni_verb='close_this' ?>\n" ) ;
02408 NI_sleep(9) ; /* give it an instant to read the message */
02409 }
02410
02411 /*-- mechanics of closing for different stream types --*/
02412
02413 switch( ns->type ){
02414
02415 #ifndef DONT_USE_SHM
02416 case NI_SHM_TYPE:
02417 NI_sleep(9) ; /* 31 Mar 2005 */
02418 SHM_close( ns->shmioc ) ; /* detach shared memory */
02419 break ;
02420 #endif
02421
02422 case NI_FD_TYPE:
02423 if( ns->fp != NULL && ns->io_mode == NI_OUTPUT_MODE ) fflush(ns->fp) ;
02424 break ;
02425
02426 case NI_REMOTE_TYPE:
02427 case NI_STRING_TYPE: /* nothing to do */
02428 break ;
02429
02430 case NI_FILE_TYPE:
02431 if( ns->fp != NULL ) fclose(ns->fp) ; /* close file */
02432 break ;
02433
02434 case NI_TCP_TYPE:
02435 if( ns->sd >= 0 ){
02436 if( (flag & 2) != 0 ){
02437 tcp_send( ns->sd , "X" , 1 , MSG_OOB ) ; /* 02 Jan 2004 */
02438 NI_sleep(9) ;
02439 }
02440 NI_sleep(2) ; /* 31 Mar 2005 */
02441 CLOSEDOWN(ns->sd) ; /* close socket */
02442 }
02443 break ;
02444 }
02445
02446 ns->bad = MARKED_FOR_DEATH ; /* label this as unclean, not to be touched */
02447 if( (flag & 4) == 0 ){ /* only free buf if program is NOT exiting */
02448 NI_free(ns->buf) ; ns->buf = NULL ;
02449 }
02450 return ;
02451 }
|
|
||||||||||||||||
|
Try to fill up the stream's input buffer. Don't call this function until NI_stream_goodcheck() is 1!
Definition at line 2865 of file niml_stream.c. References NI_stream_type::buf, NI_stream_type::bufsize, NI_stream_type::nbuf, NI_clock_time(), NI_REMOTE_TYPE, NI_stream_goodcheck(), NI_stream_read(), NI_stream_readcheck(), NI_STRING_TYPE, and NI_stream_type::type. Referenced by NI_decode_one_double(), NI_decode_one_string(), NI_stream_readbuf(), NI_stream_readbuf64(), and scan_for_angles().
02866 {
02867 int nn , ii , ntot=0 , ngood=0 , mwait=0 ;
02868 int start_msec = NI_clock_time() ;
02869
02870 if( NI_stream_goodcheck(ns,0) < 0 ) return -1 ; /* bad input */
02871
02872 if( ns->type == NI_STRING_TYPE ) return -1 ; /* goofy input */
02873 if( ns->type == NI_REMOTE_TYPE ) return -1 ; /* goofy input */
02874
02875 if( ns->nbuf >= ns->bufsize ) return 0 ; /* buffer already full */
02876
02877 if( msec < 0 ) msec = 999999999 ; /* a long time (11+ days) */
02878
02879 /* read loop */
02880
02881 while(1){
02882
02883 ngood = NI_stream_readcheck(ns,mwait); /* check if data can be read */
02884
02885 if( ngood < 0 ) break ; /* data stream gone bad, so exit */
02886
02887 if( ngood > 0 ){ /* we can read ==> */
02888 /* try to fill buffer completely */
02889
02890 ii = NI_stream_read( ns, ns->buf+ns->nbuf, ns->bufsize-ns->nbuf ) ;
02891
02892 if( ii > 0 ){ /* we got data! */
02893 ns->nbuf += ii ; /* buffer is now longer */
02894 ntot += ii ; /* total bytes read here so far */
02895
02896 /* if buffer is full,
02897 or we have all the data that was asked for, then exit */
02898
02899 if( ns->nbuf >= ns->bufsize || ntot >= minread ) break ;
02900
02901 } else if( ii < 0 ){ /* stream suddenly died horribly? */
02902 ngood = -1 ; break ;
02903 }
02904 }
02905
02906 /* if we don't require data, then exit no matter what our status is */
02907
02908 if( minread <= 0 ) break ;
02909
02910 /* if the max time has elapsed, then exit */
02911
02912 if( NI_clock_time()-start_msec >= msec ) break ;
02913
02914 /* otherwise, sleep a little bit before trying again */
02915
02916 if( mwait < 9 ) mwait++ ;
02917 }
02918
02919 /* if didn't get any data, and
02920 if the NI_stream was bad, return -1 as a flag of displeasure */
02921
02922 if( ntot == 0 && ngood < 0 ) ntot = -1 ;
02923
02924 return ntot ; /* otherwise, return # of bytes read (may be 0) */
02925 }
|
|
||||||||||||
|
Send a string (without the NUL byte) down the NI_stream. [15 Oct 2002] ------------------------------------------------------------------------------ Definition at line 2648 of file niml_stream.c. References NI_stream_write(). Referenced by NI_stream_close_keep(), NI_write_columns(), NI_write_element(), NI_write_procins(), nifti_set_afni_extension(), and THD_write_nimlatr().
02649 {
02650 if( str == NULL ) return -1 ;
02651 return NI_stream_write( ns , str , strlen(str) ) ;
02652 }
|
|
|
|
|
||||||||||||||||
|
Parse into strings a <header and=its attributes="stuff">.
Definition at line 101 of file niml_header.c. References destroy_header_stuff(), header_stuff::empty, find_string(), intpair::i, IS_QUOTE_CHAR, intpair::j, header_stuff::lhs, header_stuff::name, header_stuff::nattr, NI_dpr(), NI_malloc, NI_realloc, NI_strncpy(), header_stuff::rhs, SKIPABL, and unescape_inplace(). Referenced by NI_read_element().
00102 {
00103 header_stuff *hs ; /* return value */
00104 int id,jd , nn ;
00105 intpair ss ;
00106
00107 if( ndat < 2 || dat == NULL ) return NULL ; /* bad input */
00108
00109 #ifdef NIML_DEBUG
00110 NI_dpr("ENTER parse_header_stuff: %.*s\n",ndat,dat) ;
00111 #endif
00112
00113 for( id=0 ; id < ndat && dat[id] != '<' ; id++ ) ; /* skip to opening '<' */
00114
00115 if( id >= ndat-1 ) return NULL ; /* bad input */
00116
00117 hs = NI_malloc(header_stuff,sizeof(header_stuff)); /* make output struct */
00118 hs->nattr = hs->empty = 0 ;
00119 hs->name = NULL ;
00120 hs->lhs = hs->rhs = NULL ;
00121
00122 /* find and assign name string (immediately after '<') */
00123
00124 ss = find_string( id+1 , ndat , dat ) ;
00125
00126 if( ss.i < 0 || ss.j <= ss.i ){
00127 destroy_header_stuff( hs ) ; return NULL ; /* no name string */
00128 }
00129
00130 nn = ss.j - ss.i ; /* string length */
00131 hs->name = NI_malloc(char, nn+1) ;
00132 NI_strncpy( hs->name , dat+ss.i , nn+1 ) ;
00133
00134 #ifdef NIML_DEBUG
00135 NI_dpr(" parse_header_stuff: name = %s\n",hs->name) ;
00136 #endif
00137
00138 /* start scanning for next string at location id */
00139
00140 id = ss.j ; if( IS_QUOTE_CHAR(dat[id]) ) id++ ;
00141
00142 /* find and assign attribute strings */
00143
00144 while(1){
00145
00146 #ifdef NIML_DEBUG
00147 NI_dpr(" parse_header_stuff: scan start at id=%d\n",id) ;
00148 #endif
00149
00150 for( ; id < ndat && SKIPABL(dat[id]) ; id++ ) ; /* skip blanks */
00151
00152 if( id >= ndat ) break ; /* end of input found */
00153
00154 if( dat[id] == '>' ) break ; /* ">" end found */
00155
00156 if( dat[id] == '/' ){ /* "/>" end found */
00157 if( id < ndat-1 ) id++ ; /* skip the '>' */
00158 hs->empty = 1 ; /* mark header as 'empty' */
00159 break ; /* done with scanning for attributes */
00160 }
00161
00162 if( dat[id] == '?' ){ /* "?>" found */
00163 if( id < ndat-1 ) id++ ; /* ==> this is a procins! */
00164 hs->empty = 1 ;
00165 break ;
00166 }
00167
00168 /* find next string */
00169
00170 ss = find_string( id , ndat , dat ) ;
00171
00172 if( ss.i < 0 || ss.j <= ss.i ) break ; /* didn't find a string */
00173
00174 #ifdef NIML_DEBUG
00175 NI_dpr(" parse_header_stuff: next string = %.*s\n",ss.j-ss.i,dat+ss.i) ;
00176 #endif
00177
00178 /* extend size of attribute arrays */
00179
00180 hs->lhs = NI_realloc( hs->lhs , char*, sizeof(char *)*(hs->nattr+1) ) ;
00181 hs->rhs = NI_realloc( hs->rhs , char*, sizeof(char *)*(hs->nattr+1) ) ;
00182
00183 /* this is the LHS string */
00184
00185 nn = ss.j - ss.i ; /* length of string */
00186 hs->lhs[hs->nattr] = NI_malloc(char, nn+1) ;
00187 NI_strncpy( hs->lhs[hs->nattr] , dat+ss.i , nn+1 ) ;
00188 unescape_inplace( hs->lhs[hs->nattr] ) ;
00189
00190 hs->rhs[hs->nattr] = NULL ; /* in case there is no RHS */
00191
00192 id = ss.j ;
00193 if( id >= ndat ) break ; /* end of input ? */
00194 if( IS_QUOTE_CHAR(dat[id]) ) id++ ; /* skip close quote */
00195 while( id < ndat && SKIPABL(dat[id]) ) id++ ; /* skip blanks */
00196 if( id >= ndat ) break ; /* end of input ? */
00197
00198 if( dat[id] != '=' ){ /* no '=' means no RHS */
00199 (hs->nattr)++ ; /* count the LHS and */
00200 continue ; /* go get next attribute */
00201 }
00202
00203 id++ ; /* skip the '=' */
00204 while( id < ndat && SKIPABL(dat[id]) ) id++ ; /* skip blanks */
00205 if( id >= ndat ) break ; /* end of input ? */
00206
00207 /* find next string (the RHS) */
00208
00209 ss = find_string( id , ndat , dat ) ;
00210
00211 if( ss.i < 0 || ss.j <= ss.i ) break ; /* didn't find a string */
00212
00213 #ifdef NIML_DEBUG
00214 NI_dpr(" parse_header_stuff: next string = %.*s\n",ss.j-ss.i,dat+ss.i) ;
00215 #endif
00216
00217 /* this is the RHS string */
00218
00219 nn = ss.j - ss.i ; /* length of string */
00220 hs->rhs[hs->nattr] = NI_malloc(char, nn+1) ;
00221 NI_strncpy( hs->rhs[hs->nattr] , dat+ss.i , nn+1 ) ;
00222 unescape_inplace( hs->rhs[hs->nattr] ) ;
00223
00224 (hs->nattr)++ ; /* increment attribute count */
00225
00226 /* start scanning for next string at location id */
00227
00228 id = ss.j ;
00229 if( IS_QUOTE_CHAR(dat[id]) ) id++ ; /* skip closing quote */
00230
00231 } /* end of loop over input */
00232
00233 if( nused != NULL ){
00234 if( id >= ndat ) id = ndat-1 ;
00235 *nused = id+1 ; /* number of bytes used from dat */
00236 }
00237
00238 return hs ; /* the goal of all that work */
00239 }
|
|
||||||||||||||||
|
Quotize a bunch of floats into a string like "-2.71828,3.1416,1.111". -------------------------------------------------------------------------- Definition at line 407 of file niml_util.c. References NI_free, NI_malloc, NI_strdup(), quotize_string(), quotize_string_vector(), and vec. Referenced by NI_write_element().
00408 {
00409 int ii , jj , ff ;
00410 char *out , **qstr , fbuf[32] ;
00411
00412 if( num <= 0 || vec == NULL )
00413 return quotize_string(NULL) ;
00414
00415 qstr = NI_malloc(char*, sizeof(char *)*num) ;
00416 for( ii=0 ; ii < num ; ii++ ){
00417 sprintf(fbuf," %12.6g",vec[ii]) ;
00418 for( ff=strlen(fbuf) ; fbuf[ff]==' ' ; ff-- ) /* skip trailing blanks */
00419 fbuf[ff] = '\0' ;
00420 for( ff=0 ; fbuf[ff] == ' ' ; ff++ ) ; /* skip leading blanks */
00421 qstr[ii] = NI_strdup(fbuf+ff) ; /* array of temp strings */
00422 }
00423
00424 out = quotize_string_vector( num , qstr , sep ) ;
00425
00426 for( ii=0 ; ii < num ; ii++ ) NI_free(qstr[ii]) ;
00427
00428 NI_free(qstr) ; return out ;
00429 }
|
|
||||||||||||||||
|
Quotize a bunch of ints int a string like "1,32,-12". -------------------------------------------------------------------------- Definition at line 379 of file niml_util.c. References NI_free, NI_malloc, quotize_string(), quotize_string_vector(), and vec. Referenced by NI_write_element().
00380 {
00381 int ii , jj ;
00382 char *out , **qstr ;
00383
00384 if( num <= 0 || vec == NULL )
00385 return quotize_string(NULL) ;
00386
00387 qstr = NI_malloc(char*, sizeof(char *)*num) ; /* temp array of strings */
00388 for( ii=0 ; ii < num ; ii++ ){
00389 qstr[ii] = NI_malloc(char, 16) ; /* max size of printed int */
00390 sprintf(qstr[ii],"%d",vec[ii]) ; /* print int */
00391 for( jj=strlen(qstr[ii])-1 ; /* clip */
00392 jj > 0 && isspace(qstr[ii][jj]) ; jj-- ) /* trailing */
00393 qstr[ii][jj] = '\0' ; /* blanks */
00394 }
00395
00396 out = quotize_string_vector( num , qstr , sep ) ;
00397
00398 for( ii=0 ; ii < num ; ii++ ) NI_free(qstr[ii]) ;
00399
00400 NI_free(qstr) ; return out ;
00401 }
|
|
|
Quotize (and escapize) one string, returning a new string. Approximately speaking, this is the inverse of unescape_inplace(). -------------------------------------------------------------------------- Definition at line 279 of file niml_util.c. References CR, LF, NI_malloc, and NI_strlen(). Referenced by NI_val_to_text(), NI_write_element(), quotize_float_vector(), quotize_int_vector(), and quotize_string_vector().
00280 {
00281 int ii,jj , lstr,lout ;
00282 char *out ;
00283
00284 lstr = NI_strlen(str) ;
00285 if( lstr == 0 ){ out = NI_malloc(char, 4); strcpy(out,"\"\""); return out; }
00286 lout = 4 ; /* length of output */
00287 for( ii=0 ; ii < lstr ; ii++ ){ /* count characters for output */
00288 switch( str[ii] ){
00289 case '&': lout += 5 ; break ; /* replace '&' with "&" */
00290
00291 case '<':
00292 case '>': lout += 4 ; break ; /* replace '<' with "<" */
00293
00294 case '"' :
00295 case '\'': lout += 6 ; break ; /* replace '"' with """ */
00296
00297 case CR:
00298 case LF: lout += 6 ; break ; /* replace CR with "
"
00299 LF with "
" */
00300
00301 default: lout++ ; break ; /* copy all other chars */
00302 }
00303 }
00304 out = NI_malloc(char, lout) ; /* allocate output string */
00305 out[0] = '"' ; /* opening quote mark */
00306 for( ii=0,jj=1 ; ii < lstr ; ii++ ){
00307 switch( str[ii] ){
00308 default: out[jj++] = str[ii] ; break ; /* normal characters */
00309
00310 case '&': memcpy(out+jj,"&",5) ; jj+=5 ; break ;
00311
00312 case '<': memcpy(out+jj,"<",4) ; jj+=4 ; break ;
00313 case '>': memcpy(out+jj,">",4) ; jj+=4 ; break ;
00314
00315 case '"' : memcpy(out+jj,""",6) ; jj+=6 ; break ;
00316
00317 case '\'': memcpy(out+jj,"'",6) ; jj+=6 ; break ;
00318
00319 case CR: memcpy(out+jj,"
",6) ; jj+=6 ; break ; /* 15 Oct 2002 */
00320 case LF: memcpy(out+jj,"
",6) ; jj+=6 ; break ;
00321 }
00322 }
00323 out[jj++] = '"' ; /* closing quote mark */
00324 out[jj] = '\0' ; /* terminate the string */
00325 return out ;
00326 }
|
|
||||||||||||||||
|
Quotize an array of strings into one string, separating substrings with sep (setting sep=0 means use commas). -------------------------------------------------------------------------- Definition at line 333 of file niml_util.c. References NI_free, NI_malloc, NI_strlen(), and quotize_string(). Referenced by NI_write_element(), quotize_float_vector(), and quotize_int_vector().
00334 {
00335 char *out , **qstr ;
00336 int ii , ntot , ll,nn ;
00337
00338 /* handle special cases */
00339
00340 if( num <= 0 || str == NULL )
00341 return quotize_string(NULL) ; /* will be string of 2 quotes */
00342
00343 if( num == 1 )
00344 return quotize_string( str[0] ) ; /* just quotize the only string */
00345
00346 /* default separator */
00347
00348 if( sep == '\0' ) sep = ',' ;
00349
00350 /* temp array for quotized individual sub-strings */
00351
00352 qstr = NI_malloc(char*, sizeof(char *)*num) ;
00353
00354 for( ntot=ii=0 ; ii < num ; ii++ ){ /* quotize each input string */
00355 qstr[ii] = quotize_string( str[ii] ) ;
00356 ntot += NI_strlen( qstr[ii] ) ; /* length of all quotized strings */
00357 }
00358
00359 /* make output, put 1st sub-string into it */
00360
00361 out = NI_malloc(char, ntot) ;
00362 strcpy( out , qstr[0] ) ; NI_free(qstr[0]) ;
00363 for( ii=1 ; ii < num ; ii++ ){
00364 ll = strlen(out) ; /* put separator at end of output string, */
00365 out[ll-1] = sep ; /* in place of the closing " mark. */
00366
00367 strcat(out,qstr[ii]+1) ; /* catenate with next sub-string, */
00368 /* but skip the opening " mark. */
00369 NI_free(qstr[ii]) ; /* toss the quotized trash */
00370 }
00371
00372 NI_free(qstr) ; return out ;
00373 }
|
|
||||||||||||||||
|
Find a string in an array of strings; return index (-1 if not found). -------------------------------------------------------------------------- Definition at line 152 of file niml_util.c. Referenced by make_empty_data_element(), and NI_read_element().
00153 {
00154 int ii ;
00155
00156 if( nstr < 1 || str == NULL || targ == NULL ) return -1 ;
00157
00158 for( ii=0 ; ii < nstr ; ii++ )
00159 if( str[ii] != NULL && strcmp(str[ii],targ) == 0 ) return ii ;
00160
00161 return -1 ;
00162 }
|
|
||||||||||||
|
Find a trailing name in a pathname. For example, for fname = "/bob/cox/is/the/author/of/NIML",
Definition at line 461 of file niml_util.c. Referenced by NI_read_URL_tmpdir().
00462 {
00463 int fpos , flen , flev ;
00464
00465 if( fname == NULL || (flen=strlen(fname)) <= 1 ) return fname ;
00466
00467 if( lev < 0 ) lev = 0 ;
00468
00469 flev = 0 ;
00470 fpos = flen ;
00471 if( fname[fpos-1] == '/' ) fpos-- ; /* skip trailing slash */
00472
00473 /* fpos = index of latest character I've accepted,
00474 fpos-1 = index of next character to examine,
00475 flev = number of directory levels found so far */
00476
00477 while( fpos > 0 ){
00478
00479 if( fname[fpos-1] == '/' ){
00480 flev++ ; if( flev > lev ) break ; /* reached the lev we like */
00481 }
00482 fpos-- ; /* scan backwards */
00483 }
00484
00485 return (fname+fpos) ;
00486 }
|
|
|
Definition at line 182 of file niml_util.c. References CR, LF, and NI_strlen(). Referenced by NI_text_to_val(), and parse_header_stuff().
00183 {
00184 int ii,jj , nn,ll ;
00185
00186 if( str == NULL ) return 0 ; /* no string? */
00187 ll = NI_strlen(str) ;
00188
00189 /* scan for escapes: &something; */
00190
00191 for( ii=jj=nn=0 ; ii<ll ; ii++,jj++ ){ /* scan at ii; put results in at jj */
00192
00193 if( str[ii] == '&' ){ /* start of escape? */
00194
00195 if( ii+3 < ll && /* < */
00196 str[ii+1] == 'l' &&
00197 str[ii+2] == 't' &&
00198 str[ii+3] == ';' ){ str[jj] = '<' ; ii += 3 ; nn++ ; }
00199
00200 else if( ii+3 < ll && /* > */
00201 str[ii+1] == 'g' &&
00202 str[ii+2] == 't' &&
00203 str[ii+3] == ';' ){ str[jj] = '>' ; ii += 3 ; nn++ ; }
00204
00205 else if( ii+5 < ll && /* " */
00206 str[ii+1] == 'q' &&
00207 str[ii+2] == 'u' &&
00208 str[ii+3] == 'o' &&
00209 str[ii+4] == 't' &&
00210 str[ii+5] == ';' ){ str[jj] = '"' ; ii += 5 ; nn++ ; }
00211
00212 else if( ii+5 < ll && /* ' */
00213 str[ii+1] == 'a' &&
00214 str[ii+2] == 'p' &&
00215 str[ii+3] == 'o' &&
00216 str[ii+4] == 's' &&
00217 str[ii+5] == ';' ){ str[jj] = '\'' ; ii += 5 ; nn++ ; }
00218
00219 else if( ii+4 < ll && /* & */
00220 str[ii+1] == 'a' &&
00221 str[ii+2] == 'm' &&
00222 str[ii+3] == 'p' &&
00223 str[ii+4] == ';' ){ str[jj] = '&' ; ii += 4 ; nn++ ; }
00224
00225 /* although the comments above don't mention it,
00226 we also look for XML style numeric escapes
00227 of the forms   (decimal) and ý (hex) */
00228
00229 else if( ii+3 < ll &&
00230 str[ii+1] == '#' &&
00231 isdigit(str[ii+2]) ){ /* &#dec; */
00232
00233 unsigned int val='?' ; int kk=ii+3 ;
00234 while( kk < ll && str[kk] != ';' ) kk++ ;
00235 sscanf( str+ii+2 , "%u" , &val ) ;
00236 str[jj] = (char) val ; ii = kk ; nn++ ;
00237 }
00238
00239 else if( ii+4 < ll &&
00240 str[ii+1] == '#' &&
00241 str[ii+2] == 'x' &&
00242 isxdigit(str[ii+3]) ){ /* &#hex; */
00243
00244 unsigned int val='?' ; int kk=ii+4 ;
00245 while( kk < ll && str[kk] != ';' ) kk++ ;
00246 sscanf( str+ii+3 , "%x" , &val ) ;
00247 str[jj] = (char) val ; ii = kk ; nn++ ;
00248 }
00249
00250 /* didn't start a recognized escape, so just copy as normal */
00251
00252 else if( jj < ii ){ str[jj] = str[ii] ; }
00253
00254 } else if( str[ii] == CR ) { /* is a carriage return */
00255
00256 if( str[ii+1] == LF ){ str[jj] = LF ; ii++ ; nn++ ; } /* CR LF */
00257 else { str[jj] = LF ; ; nn++ ; } /* CR only */
00258
00259 } else { /* is a normal character, just copy to output */
00260
00261 if( jj < ii ){ str[jj] = str[ii] ; }
00262 }
00263
00264 /* at this point, ii=index of last character used up in scan
00265 jj=index of last character written to (jj <= ii) */
00266
00267 } /* end of loop scanning over input/output string */
00268
00269 if( jj < ll ) str[jj] = '\0' ; /* end string properly */
00270
00271 return nn ;
00272 }
|
Variable Documentation
|
|
Definition at line 22 of file niml_private.h. Referenced by NI_dpr(). |
|
|
Definition at line 61 of file niml_private.h. Referenced by load_decode_table(), and load_encode_table(). |
|
|
[Most are not actually used in NIML, but are here for completeness] * Definition at line 60 of file niml_private.h. |
|
|
Definition at line 62 of file niml_private.h. Referenced by B64_set_linelen(), and B64_to_base64(). |
|
|
Definition at line 63 of file niml_private.h. Referenced by B64_set_crlf(), and B64_to_base64(). |
|
|
Definition at line 64 of file niml_private.h. Referenced by B64_set_crlf(), and B64_to_base64(). |
|
|
Definition at line 132 of file niml_private.h. |
|
|
Definition at line 130 of file niml_private.h. |
|
|
Definition at line 128 of file niml_private.h. |
|
|
Definition at line 129 of file niml_private.h. |
|
|
Definition at line 131 of file niml_private.h. |