Exemple #1
0
/* ----------------------------- MNI Header -----------------------------------
@NAME       : add_vff_attributes_to_file
@INPUT      : hvol volume handle
	      vattrs vff attributes to be added to minc2.0
@OUTPUT     : (nothing)
@RETURNS    :  
@DESCRIPTION: adds various attributes to minc file
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : Jul 2006 (Leila Baghdadi)
@MODIFIED   : 
---------------------------------------------------------------------------- */
int
add_vff_attribute_to_file(mihandle_t hvol,const struct vff_attrs *vattrs)
{
  int ival;
  double dval;
  int r;
  ival = vattrs->y_bin;
  r = miset_attr_values(hvol, MI_TYPE_INT, "acquisition",
			"ybin",1 , &ival);
  if (r < 0) {
    TESTRPT("failed to add y_bin attribute", r);
  }
  ival = vattrs->z_bin;
  r = miset_attr_values(hvol, MI_TYPE_INT, "acquisition",
			"z_bin",1 , &ival);
  if (r < 0) {
    TESTRPT("failed to add z_bin attribute", r);
  }
  ival = vattrs->bands;
  r = miset_attr_values(hvol, MI_TYPE_INT, "acquisition",
			"bands",1 , &ival);
  if (r < 0) {
   TESTRPT("failed to add bands attribute", r);
  }
  dval = vattrs->center_of_rotation;
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "acquisition",
			"center_of_rotation",1 , &dval);
  if (r < 0) {
    TESTRPT("failed to add center_of_rotation attribute", r);
  }
  dval = vattrs->central_slice;
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "acquisition",
			"central_slice",1 , &dval);
  if (r < 0) {
    TESTRPT("failed to add central_slice attribute", r);
  }
  dval = vattrs->rfan_y;
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "acquisition",
			"rfan_y",1 , &dval);
  if (r < 0) {
    TESTRPT("failed to add rfan_y attribute", r);
  }
  dval = vattrs->rfan_z;
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "acquisition",
			"rfan_z",1 , &dval);
  if (r < 0) {
    TESTRPT("failed to add rfan_z attribute", r);
  }
  dval = vattrs->angle_increment;
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "acquisition",
			"angle_increment",1 , &dval);
  if (r < 0) {
    TESTRPT("failed to add angle_increment attribute", r);
  }
  ival = vattrs->reverse_order;
  r = miset_attr_values(hvol, MI_TYPE_INT, "acquisition",
			"reverse_order",1 , &ival);
  if (r < 0) {
    TESTRPT("failed to add reverse_order attribute", r);
  }
  ival =  vattrs->year;
  r = miset_attr_values(hvol, MI_TYPE_INT, "study",
			MIstart_year,1 , &ival);
  if (r < 0) {
    TESTRPT("failed to add date:year attribute", r);
  }
  ival = vattrs->month;
  r = miset_attr_values(hvol, MI_TYPE_INT, "study",
			MIstart_month,1 , &ival);
  if (r < 0) {
    TESTRPT("failed to add date:month attribute", r);
  }
  ival = vattrs->day;
  r = miset_attr_values(hvol, MI_TYPE_INT, "study",
			MIstart_day,1 , &ival);
  if (r < 0) {
    TESTRPT("failed to add date:day attribute", r);
  }

  return(0);
}
Exemple #2
0
int 
main(int argc, char *argv[])
{   
    int r;
    
    const char **file_list = NULL;     /* List of file names */
    struct vff_attrs vffattrs;
    struct mnc_vars mnc2;
    char out_str[1024];         /* Big string for filename */
    midimhandle_t hdim[MAX_VFF_DIMS];
    mihandle_t hvol;
   
    double mnc_vrange[2];       /* MINC valid min/max */
    int num_file_args;          /* Number of files on command line */
    string_t out_dir;           /* Output directory */
    int length;
    struct stat st;
    int ifile;
    int num_files;              /* Total number of files */
    int is_file=0;
    int is_list=0;
    int ival;
    char *extension;

    mnc2.mnc_srange[0]= -1;
    mnc2.mnc_srange[1]= -1;

    G.pname = argv[0];          /* get program name */
    G.dirname = NULL;
    G.little_endian = 1; /*default is little endian unless otherwise*/
    G.minc_history = time_stamp(argc, argv); /* Create minc history string */

    if (ParseArgv(&argc, argv, argTable, 0) || argc < 2) {
        usage();
	exit(EXIT_FAILURE);
    }
     
    if (G.dirname != NULL) {
#if HAVE_DIRENT_H
      
      if (stat(G.dirname, &st) != 0 || !S_ISDIR(st.st_mode)) {
        fprintf(stderr,"Option -addattrs requires directory as argument!!!\n");
        exit(EXIT_FAILURE);
      }
#endif 
    }

    if (G.List) 
      {
      num_file_args = argc - 1; 
      }
    else 
      {
	strcpy(out_str, argv[1]);
        extension = strrchr(out_str, '.');
        if (extension != NULL )
	  {
	    extension++;
            if (strcmp(extension, "mnc") !=0)
	      {
		usage();
		exit(EXIT_FAILURE);
	      }
	  }
	if (argc == 3)
	  {
	  /* check if last argument is dir */
	  num_file_args = argc - 2; 
	  strcpy(out_dir, argv[argc - 1]); 
	  
	  /* make sure path ends with slash 
	   */
	  length = strlen(out_dir);
	  if (out_dir[length - 1] != '/') 
	    {
	    out_dir[length++] = '/';
	    out_dir[length++] = '\0';
	    }

	  if (stat(out_dir, &st) != 0 || !S_ISDIR(st.st_mode)) 
	    {/* assume filename */
	    is_file =1;
	    }
	  }
	else
	  { //list of 2d files must check!
	  num_file_args = argc - 2;
	  is_list = 1;
	  }
      }
	    
    if (!is_file || G.List)
      {
	
      /* Get space for file lists */
      /* Allocate the array of pointers used to implement the
       * list of filenames.
       */
      file_list = malloc(1 * sizeof(char *));
      CHKMEM(file_list);

      /* Go through the list of files, expanding directories where they
       * are encountered...
       */
      num_files = 0;
      for (ifile = 1 ; ifile <= num_file_args; ifile++) 
	{
#if HAVE_DIRENT_H    
	
	if (stat(argv[ifile + 1], &st) == 0 && S_ISDIR(st.st_mode)) 
	  {
	  DIR *dp;
	  struct dirent *np;
	  char *tmp_str;

	  length = strlen(argv[ifile + 1]);

	  dp = opendir(argv[ifile + 1]);
	  if (dp != NULL) 
	    {
	    while ((np = readdir(dp)) != NULL) 
	      {
	      /* Generate the full path to the file.
	       */
	      tmp_str = malloc(length + strlen(np->d_name) + 2);
	      strcpy(tmp_str, argv[ifile + 1]);
	      if (tmp_str[length-1] != '/') 
	       {
	       tmp_str[length] = '/';
	       tmp_str[length+1] = '\0';
	       }
	      strcat(&tmp_str[length], np->d_name);
	      if (stat(tmp_str, &st) == 0 && S_ISREG(st.st_mode)) 
		{
		file_list = realloc(file_list,
				    (num_files + 1) * sizeof(char *));
		file_list[num_files++] = tmp_str;
		}
	      else 
		{
		free(tmp_str);
		}
	      }
	    closedir(dp);
	  }
	  else 
	    {
	      fprintf(stderr, "Error opening directory '%s'\n", 
		      argv[ifile + 1]);
	    }
	  }
	else 
	  {
	  file_list = realloc(file_list, (num_files + 1) * sizeof(char *));
	  file_list[num_files++] = strdup(argv[ifile + 1]);
	  }
#else
	file_list = realloc(file_list, (num_files + 1) * sizeof(char *));
        file_list[num_files++] = strdup(argv[ifile + 1]);
#endif
	}
      }
 
    if (G.List)
      {
      exit(EXIT_SUCCESS);
      }

    if (is_file)
      {
      read_3Dvff_file_header(argv[2],&mnc2,&vffattrs);
      }
    else
      {
      read_2Dvff_files_header(file_list,num_files,&mnc2,&vffattrs);
      }

    /* ok starting to create minc2.0 file assuming 3D must take care of 2D*/

    r = micreate_dimension("zspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, mnc2.mnc_count[2], &hdim[0]);
    if (r != 0) {
      TESTRPT("failed create_dimension zspace", r);
      return (1);
    }
    r = micreate_dimension("yspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, mnc2.mnc_count[1], &hdim[1]);
    if (r != 0) {
     TESTRPT("failed create_dimension yspace", r);
      return (1);
    }
    r = micreate_dimension("xspace", MI_DIMCLASS_SPATIAL, 
                           MI_DIMATTR_REGULARLY_SAMPLED, mnc2.mnc_count[0], &hdim[2]);
    if (r != 0) {
      TESTRPT("failed create_dimension xspace", r);
      return (1);
    }
    
    r = miset_dimension_start(hdim[0], mnc2.mnc_starts[2]);
    if (r < 0) {
      TESTRPT("failed dimension start xspace", r);
      return (1);
    }
    r = miset_dimension_start(hdim[1], mnc2.mnc_starts[1]);
    if (r < 0) {
      TESTRPT("failed dimension start yspace", r);
      return (1);
    }
    /* create negative start for xspace to correct orientation */
    r = miset_dimension_start(hdim[2], mnc2.mnc_starts[0] * -1);
    if (r < 0) {
      TESTRPT("failed dimension start zspace", r);
      return (1);
    }
    /* create negative spacing for zspace to correct orientation */
    r = miset_dimension_separation(hdim[0], mnc2.mnc_steps[2] * -1);
    if (r < 0) {
      TESTRPT("failed dimension separation xspace", r);
      return (1);
    }
    /* create negative spacing for yspace to correct orientation */
    r = miset_dimension_separation(hdim[1], mnc2.mnc_steps[1] * -1);
    if (r < 0) {
      TESTRPT("failed dimension separation yspace", r);
      return (1);
    }
    
    r = miset_dimension_separation(hdim[2], mnc2.mnc_steps[0]);
    if (r < 0) {
      TESTRPT("failed dimension separation zspace", r);
      return (1);
    }
    
    r = micreate_volume(out_str,MAX_VFF_DIMS, hdim, mnc2.mnc_type, 
                        MI_CLASS_REAL, NULL, &hvol);
    if (r != 0) {
      TESTRPT("error creating volume", r);
      return (1);
    }

    r = micreate_volume_image(hvol);
    if (r != 0) {
      TESTRPT("error creating volume", r);
      return (1);
    }
    
    // read image slice by slice
    if (is_file)
      {
	read_3Dvff_file_image(hvol, argv[2], mnc2, vffattrs, mnc_vrange);
      }
    else 
      {
	read_2Dvff_files_image(hvol,file_list,num_files, mnc2, vffattrs, mnc_vrange);
      }
    miset_volume_valid_range(hvol,mnc_vrange[1], mnc_vrange[0]);

    if (mnc2.mnc_srange[0] == -1 || mnc2.mnc_srange[1] == -1) {
      /* min and max are not specified in the file
         voxel range is set same as real range */
      mnc2.mnc_srange[0] = mnc_vrange[0];
      mnc2.mnc_srange[1] = mnc_vrange[1];
    }
    miset_volume_range(hvol,mnc2.mnc_srange[1], mnc2.mnc_srange[0]);
    
    if (is_file) {
      /* create minc history 3D */
      strcat(vffattrs.cmd_line,G.minc_history);
      G.minc_history = vffattrs.cmd_line;
      /* attributes from vff file itself 3D case*/
      add_vff_attribute_to_file(hvol,&vffattrs);
    }

    if (G.dirname != NULL) {
      /* attributes from external files 3D case*/
      add_attributes_from_files(hvol);
    }
    else if (!is_file) {
      /* just afew attributes from 2D case */
      ival =  vffattrs.year;
      r = miset_attr_values(hvol, MI_TYPE_INT, "study",
			    MIstart_year,1 , &ival);
      if (r < 0) {
	TESTRPT("failed to add date:year attribute", r);
      }
      ival = vffattrs.month;
      r = miset_attr_values(hvol, MI_TYPE_INT, "study",
			    MIstart_month,1 , &ival);
      if (r < 0) {
	TESTRPT("failed to add date:month attribute", r);
      }
      ival = vffattrs.day;
      r = miset_attr_values(hvol, MI_TYPE_INT, "study",
			    MIstart_day,1 , &ival);
      if (r < 0) {
	TESTRPT("failed to add date:day attribute", r);
      }
    }

    /* add history attribute */
    r = miadd_history_attr(hvol,strlen(G.minc_history), G.minc_history);
    if (r < 0) {
	TESTRPT("error creating history", r);
	return (1);
      }
    
    if (file_list != NULL) {
      free_list(num_files, file_list);
      free(file_list);
    }
    miclose_volume(hvol);

    exit(EXIT_SUCCESS);
}
Exemple #3
0
/* ----------------------------- MNI Header -----------------------------------
@NAME       : add_attributes_from_files
@INPUT      : hvol volume handle
	      
@OUTPUT     : (nothing)
@RETURNS    :  
@DESCRIPTION: adds a few files generated with vff file to minc file
@METHOD     : This method assumes the txt files are in the top directory
@GLOBALS    : 
@CALLS      : 
@CREATED    : Jul 2006 (Leila Baghdadi)
@MODIFIED   : This code is a bit ugly thanks for the wodnerful work of windows.
---------------------------------------------------------------------------- */
int
add_attributes_from_files(mihandle_t hvol)
{
  FILE *inf;
  char *strbuf;
  char *buffer;
  string_t fullpath_pro;
  string_t fullpath_des;
  string_t fullpath_par;
  char *p;
  int r,i;
  char **str;
  int found_protocol=0;
  int found_description=0;
  int found_parameters=0;

  
  /* first find files names --> different platforms */
  find_filenames_first(fullpath_pro,fullpath_des,fullpath_par,
		       &found_protocol,&found_description, &found_parameters); 

  if (!found_protocol) {
    printf("could not find file with extension protocol\n");
  }
  else {
    /* add *.protocol to "acquisition" protocol attribute */
    inf = fopen(fullpath_pro,"r");
    if (inf == NULL) {
      printf("Could not open file %s \n", fullpath_pro);
      exit(EXIT_FAILURE);
    }
    else {
      strbuf = malloc(MAX_BUF_LINE + 1);
      CHKMEM(strbuf);
      strbuf[0]='\0';
      buffer = malloc(MAX_BUF_TEXT + 1);
      CHKMEM(buffer);
      buffer[0]='\0';
      while (fgets(strbuf, MAX_BUF_LINE, inf) != NULL) {
	strcat(buffer,strbuf);
      }
    }
    fclose(inf);
    
    r = miset_attr_values(hvol, MI_TYPE_STRING, "acquisition",
			  "protocol", strlen(buffer) ,buffer);
    free(buffer);
    free(strbuf);
    if (r < 0) {
      TESTRPT("failed to add protocol attribute", r);
    }
  }


  if (!found_description) {
    printf("Could not find file Description.txt\n");
  }
  else {
    /* add Description.txt to patient name and study id */
    inf = fopen(fullpath_des,"r");		       
    if (inf == NULL) {
      printf("Could not open file %s \n", fullpath_des);
      exit(EXIT_FAILURE);
    }
    else {
      /* just need the first two lines of this file */
      str = malloc(MAX_DESCRIPTION);
      CHKMEM(str);
      for (i=0; i < MAX_DESCRIPTION; i++) {
	str[i] = malloc(MAX_BUF_LINE + 1);
	CHKMEM(str[i]);
	if(fgets(str[i], MAX_BUF_LINE, inf) == NULL){
      fprintf(stderr, "Error with fgets on line %d of %s\n", 
         __LINE__, __FILE__);
      }
	for (p = str[i]; *p != '\0'; p++) {
	  if (*p == '\r') {
	    *p = '\0';
	  }
	}
      }
      r = miset_attr_values(hvol, MI_TYPE_STRING, "patient",
			    "full_name",strlen(str[0]) , str[0]);
      if (r < 0) {
	TESTRPT("failed to add full_name attribute", r);
      }
      
      r = miset_attr_values(hvol, MI_TYPE_STRING, "study",
			    "study_id",strlen(str[1]) , str[1]);
      if (r < 0) {
	TESTRPT("failed to add study_id attribute", r);
      }
      fclose(inf);
      for (i=0; i < MAX_DESCRIPTION; i++) {
	free(str[i]);
      }
      free(str);
    }
  }

  if (!found_parameters) {
    printf("Could not find file Parameters.txt\n");
  }
  else {
    /* add Parameters.txt to "acquisition" scan_parameters */
    inf = fopen(fullpath_par,"r");
    if (inf == NULL) {
      printf("Could not open file %s \n", fullpath_par);
      exit(EXIT_FAILURE);
    }
   else {
     strbuf = malloc(MAX_BUF_LINE + 1);
     CHKMEM(strbuf);
     strbuf[0]='\0';
     buffer = malloc(MAX_BUF_TEXT + 1);
     CHKMEM(buffer);
     buffer[0]='\0';
     while (fgets(strbuf, MAX_BUF_LINE, inf) != NULL) {
       strcat(buffer,strbuf);
     }
   }
    fclose(inf);
    
    r = miset_attr_values(hvol, MI_TYPE_STRING, "acquisition",
			  "scan_parameters",strlen(buffer) ,buffer);
    free(buffer);
    free(strbuf);
    if (r < 0) {
      TESTRPT("failed to add scan_parameters attribute", r);
    }
  }

  return(0);
}
Exemple #4
0
int main(int argc, char **argv)
{
    mihandle_t hvol;
    int r;
    mitype_t data_type;
    int length;
    static double tstarr[TESTARRAYSIZE] = { 
	1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10, 11.11
    };
    double dblarr[TESTARRAYSIZE];
    float fltarr[TESTARRAYSIZE];
    int intarr[TESTARRAYSIZE];
    char valstr[128];
    milisthandle_t hlist, h1list;
    char pathbuf[256];
    char namebuf[256];
    char pathbuf1[256];
    int count=0;

    r = micreate_volume("tst-grpa.mnc", 0, NULL, MI_TYPE_UINT, 
                        MI_CLASS_INT, NULL, &hvol);
    if (r < 0) {
	TESTRPT("Unable to create test file", r);
	return (-1);
    }

    r = micreate_group(hvol, "/", "test1");
    if (r < 0) {
        TESTRPT("micreate_group failed", r);
    }

    r = micreate_group(hvol, "/", "test2");
    if (r < 0) {
        TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/", "test3");
    if (r < 0) {
        TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/", "test4");
    if (r < 0) {
        TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test2", "stuff2");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1", "stuff");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1", "otherstuff");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1", "theotherstuff");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1/theotherstuff", "thisstuff");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1/stuff", "hello");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    r = micreate_group(hvol, "/test1/stuff", "helloleila");
    if (r < 0) {
	TESTRPT("micreate_group failed", r);
    }
    
    r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff/hello", 
			  "animal", 8, "fruitbat");
    if (r < 0) {
	TESTRPT("miset_attr_values failed", r);
    }

    r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff", 
			  "objtype", 10, "automobile");
    if (r < 0) {
	TESTRPT("miset_attr_values failed", r);
    }

    r = miset_attr_values(hvol, MI_TYPE_STRING, "/test3", 
			  "objtype", 10, "automobile");
    if (r < 0) {
	TESTRPT("miset_attr_values failed", r);
    }

    r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff", 
			  "objname", 10, "automobile");
    if (r < 0) {
	TESTRPT("miset_attr_values failed", r);
    }
    r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "/test2", 
			  "maxvals", TESTARRAYSIZE, tstarr);
    if (r < 0) {
	TESTRPT("miset_attr_values failed", r);
    }

    r = miget_attr_type(hvol, "/test1/stuff/hello", "animal", 
			&data_type);
    if (r < 0) {
	TESTRPT("miget_attr_type failed", r);
    }

    r = miget_attr_length(hvol, "/test1/stuff/hello", "animal", 
			  &length);
    if (r < 0) {
	TESTRPT("miget_attr_length failed", r);
    }

    if (data_type != MI_TYPE_STRING) {
	TESTRPT("miget_attr_type failed", data_type);
    }
    if (length != 8) {
	TESTRPT("miget_attr_length failed", length);
    }
    
    r = midelete_group(hvol, "/test1/stuff", "goodbye");
    if (r >= 0) {
	TESTRPT("midelete_group failed", r);
    }

    r = midelete_group(hvol, "/test1/stuff", "hello");
    /* This should succeed.
     */
    if (r < 0) {
	TESTRPT("midelete_group failed", r);
    }

    r = miget_attr_length(hvol, "/test1/stuff/hello", "animal", 
			  &length);
    /* This should fail since we deleted the group.
     */
    if (r >= 0) {
	TESTRPT("miget_attr_length failed", r);
    }

    r = miget_attr_values(hvol, MI_TYPE_DOUBLE, "/test2", "maxvals", 
			  TESTARRAYSIZE, dblarr);
    if (r < 0) {
	TESTRPT("miget_attr_values failed", r);
    }

    for (r = 0; r < TESTARRAYSIZE; r++) {
	if (dblarr[r] != tstarr[r]) {
	    TESTRPT("miget_attr_values mismatch", r);
	}
    }

    /* Get the values again in float rather than double format.
     */
    r = miget_attr_values(hvol, MI_TYPE_FLOAT, "/test2", "maxvals", 
			  TESTARRAYSIZE, fltarr);
    if (r < 0) {
	TESTRPT("miget_attr_values failed", r);
    }

    for (r = 0; r < TESTARRAYSIZE; r++) {
	if (fltarr[r] != (float) tstarr[r]) {
	    TESTRPT("miget_attr_values mismatch", r);
	    fprintf(stderr, "fltarr[%d] = %f, tstarr[%d] = %f\n",
		    r, fltarr[r], r, tstarr[r]);
	}
    }

    /* Get the values again in int rather than double format.
     */
    r = miget_attr_values(hvol, MI_TYPE_INT, "/test2", "maxvals", 
			  TESTARRAYSIZE, intarr);
    if (r < 0) {
	TESTRPT("miget_attr_values failed", r);
    }

    for (r = 0; r < TESTARRAYSIZE; r++) {
	if (intarr[r] != (int) tstarr[r]) {
	    TESTRPT("miget_attr_values mismatch", r);
	    fprintf(stderr, "intarr[%d] = %d, tstarr[%d] = %d\n",
		    r, intarr[r], r, (int) tstarr[r]);
	}
    }

    r = miget_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff", 
			  "objtype", 128, valstr);
    if (r < 0) {
	TESTRPT("miget_attr_values failed", r);
    }

    if (strcmp(valstr, "automobile") != 0) {
	TESTRPT("miget_attr_values failed", 0);
    }

    r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
			  "objtype", 8, "bicycle");

    if (r < 0) {
	TESTRPT("miset_attr_values failed on rewrite", r);
    }

    r = miget_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff", 
			  "objtype", 128, valstr);
    if (r < 0) {
	TESTRPT("miget_attr_values failed", r);
    }

    if (strcmp(valstr, "bicycle") != 0) {
	TESTRPT("miget_attr_values failed", 0);
    }

    
    r = milist_start(hvol, "/", 1, &hlist);
    if (r == MI_NOERROR) {
      count++;
      while (milist_attr_next(hvol, hlist, pathbuf, sizeof(pathbuf),
			      namebuf, sizeof(namebuf)) == MI_NOERROR) {
	printf(" %s %s\n", pathbuf, namebuf);
      }
    }
    milist_finish(hlist);
    
    printf("***************** \n");
    
    r = milist_start(hvol, "/", 1, &h1list);
    if (r == MI_NOERROR) {
      while( milist_grp_next(h1list, pathbuf1, sizeof(pathbuf1)) == MI_NOERROR) {
	printf("%s \n", pathbuf1);
      }
      
    }
    
    milist_finish(h1list);
	
    miclose_volume(hvol);

    if (error_cnt != 0) {
	fprintf(stderr, "%d error%s reported\n", 
		error_cnt, (error_cnt == 1) ? "" : "s");
    }
    else {
	fprintf(stderr, "No errors\n");
    }
    return (error_cnt);
}
Exemple #5
0
/**
 * Set minc space type
 */
int miset_space_name ( mihandle_t volume, const char *name )
{
  return miset_attr_values ( volume, MI_TYPE_STRING, MI_ROOT_PATH "/" MI_INFO_NAME,
                             "spacetype", strlen ( name ), name );

}
Exemple #6
0
int main(void)
{
  mihandle_t hvol;
  mihandle_t hvol1;
  int r;
  mitype_t data_type;
  size_t length;
  static double tstarr[TESTARRAYSIZE] = {
    1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10, 11.11
  };
  double dblarr[TESTARRAYSIZE];
  float fltarr[TESTARRAYSIZE];
  int intarr[TESTARRAYSIZE];
  char valstr[128]="";
  float val1=12.5;
  float val2=34.5;
  milisthandle_t hlist, h1list;
  char pathbuf[256]="";
  char namebuf[256]="";
  char pathbuf1[1024]="";
  int count=0;
  
  r = micreate_volume("tst-grpa.mnc", 0, NULL, MI_TYPE_UINT,
                      MI_CLASS_REAL, NULL, &hvol);
  if (r < 0) {
    TESTRPT("Unable to create test file", r);
    return (-1);
  }
  r = micreate_volume("tst-grpb.mnc", 0, NULL, MI_TYPE_UINT,
                      MI_CLASS_REAL, NULL, &hvol1);
  if (r < 0) {
    TESTRPT("Unable to create test file", r);
    return (-1);
  }
  
  r = micreate_group(hvol, "/", "test1");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  
  r = micreate_group(hvol, "/", "test2");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  r = micreate_group(hvol, "/", "test3");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  r = micreate_group(hvol, "/", "test4");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  r = micreate_group(hvol, "/test2", "stuff2");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  r = micreate_group(hvol, "/test1", "stuff");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  r = micreate_group(hvol, "/test1", "otherstuff");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  r = micreate_group(hvol, "/test1", "theotherstuff");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  r = micreate_group(hvol, "/test1/theotherstuff", "thisstuff");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  r = micreate_group(hvol, "/test1/stuff", "hello");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  r = micreate_group(hvol, "/test1/stuff", "helloleila");
  if (r < 0) {
    TESTRPT("micreate_group failed", r);
  }
  
  r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff/hello",
                        "animal", 8, "fruitbat");
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }
  
  r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
                        "objtype", 10, "automobile");
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }
  
  r = miset_attr_values(hvol, MI_TYPE_STRING, "/test3",
                        "objtype", 10, "automobile");
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }
  
  r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
                        "objname", 10, "automobile");
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }
  r = miset_attr_values(hvol, MI_TYPE_DOUBLE, "/test2",
                        "maxvals", TESTARRAYSIZE, tstarr);
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }
  
  r = miget_attr_type(hvol, "/test1/stuff/hello", "animal",
                      &data_type);
  if (r < 0) {
    TESTRPT("miget_attr_type failed", r);
  }
  if (data_type != MI_TYPE_STRING) {
    TESTRPT("miget_attr_type failed", data_type);
  }
  
  r = miget_attr_length(hvol, "/test1/stuff/hello", "animal",
                        &length);
  if (r < 0) {
    TESTRPT("miget_attr_length failed", r);
  }
  
  if (length != 8) {
    TESTRPT("miget_attr_length failed", (int)length);
  }
  
  r = midelete_group(hvol, "/test1/stuff", "goodbye");
  if (r >= 0) {
    TESTRPT("midelete_group failed", r);
  }
  
  r = midelete_group(hvol, "/test1/stuff", "hello");
  /* This should succeed.
   */
  if (r < 0) {
    TESTRPT("midelete_group failed", r);
  }
  
  r = miget_attr_length(hvol, "/test1/stuff/hello", "animal",
                        &length);
  /* This should fail since we deleted the group.
   */
  if (r >= 0) {
    TESTRPT("miget_attr_length not failed", r);
  }
  
  r = miget_attr_values(hvol, MI_TYPE_DOUBLE, "/test2", "maxvals",
                        TESTARRAYSIZE, dblarr);
  if (r < 0) {
    TESTRPT("miget_attr_values failed", r);
  }
  
  for (r = 0; r < TESTARRAYSIZE; r++) {
    if (dblarr[r] != tstarr[r]) {
	    TESTRPT("miget_attr_values mismatch", r);
    }
  }
  
  /* Get the values again in float rather than double format.
   */
  r = miget_attr_values(hvol, MI_TYPE_FLOAT, "/test2", "maxvals",
                        TESTARRAYSIZE, fltarr);
  if (r < 0) {
    TESTRPT("miget_attr_values failed", r);
  }
  
  for (r = 0; r < TESTARRAYSIZE; r++) {
    if (fltarr[r] != (float) tstarr[r]) {
	    TESTRPT("miget_attr_values mismatch", r);
	    fprintf(stderr, "fltarr[%d] = %f, tstarr[%d] = %f\n",
              r, fltarr[r], r, tstarr[r]);
    }
  }
  
  /* Get the values again in int rather than double format.
   */
  r = miget_attr_values(hvol, MI_TYPE_INT, "/test2", "maxvals",
                        TESTARRAYSIZE, intarr);
  if (r < 0) {
    TESTRPT("miget_attr_values failed", r);
  }
  
  for (r = 0; r < TESTARRAYSIZE; r++) {
    if (intarr[r] != (int) tstarr[r]) {
	    TESTRPT("miget_attr_values mismatch", r);
	    fprintf(stderr, "intarr[%d] = %d, tstarr[%d] = %d\n",
              r, intarr[r], r, (int) tstarr[r]);
    }
  }
  
  r = miget_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
                        "objtype", 128, valstr);
  if (r < 0) {
    TESTRPT("miget_attr_values failed", r);
  }
  
  if (strcmp(valstr, "automobile") != 0) {
    TESTRPT("miget_attr_values failed", 0);
    fprintf(stderr,"Expected :\"%s\" read \"%s\"\n","automobile",valstr);
  }

  /* Get the values again but this time with only enough space
     for the result with null termination.
   */
  memset(valstr, 0x55, sizeof(valstr));
  r = miget_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
                        "objtype", 11, valstr);
  if (r < 0) {
    TESTRPT("miget_attr_values failed", r);
  }
  if (strcmp(valstr, "automobile") != 0) {
    TESTRPT("miget_attr_values failed", 0);
  }

  /* Get the values again but this time with only enough space
     for the result and without null termination.
   */
  memset(valstr, 0x55, sizeof(valstr));
  r = miget_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
                        "objtype", 10, valstr);
  if (r < 0) {
    TESTRPT("miget_attr_values failed", r);
  }
  if (valstr[9] != 'e') {
    TESTRPT("miget_attr_values failed", 0);
  }
  if (valstr[10] != 0x55) {
    TESTRPT("miget_attr_values failed", 0);
  }

  r = miset_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
                        "objtype", 8, "bicycle");
  
  if (r < 0) {
    TESTRPT("miset_attr_values failed on rewrite", r);
  }
  
  r = miget_attr_values(hvol, MI_TYPE_STRING, "/test1/stuff",
                        "objtype", 128, valstr);
  if (r < 0) {
    TESTRPT("miget_attr_values failed", r);
  }
  
  if (strcmp(valstr, "bicycle") != 0) {
    TESTRPT("miget_attr_values failed", 0);
  }
  r = miset_attr_values(hvol, MI_TYPE_FLOAT, "/OPT",
                        "zoom",1, &val1);
  
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }
  
  r = miset_attr_values(hvol, MI_TYPE_FLOAT, "/OPT",
                        "binning",1, &val2);
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }
  
  r = miset_attr_values(hvol, MI_TYPE_FLOAT, "/OPT",
                        "gain",1, &val1);
  if (r < 0) {
    TESTRPT("miset_attr_values failed", r);
  }

  r = milist_start(hvol, "/", 0, &hlist);
  if (r == MI_NOERROR) {
    count++;
    while (milist_attr_next(hvol, hlist, pathbuf, sizeof(pathbuf),
                            namebuf, sizeof(namebuf)) == MI_NOERROR) {
      printf(" %s %s\n", pathbuf, namebuf);
    }
  } else {
    TESTRPT("milist_start failed", r);
  }
  milist_finish(hlist);
  
  printf("copy all attributes in the provided path in the new volume\n");
  if((r = micopy_attr(hvol,"/OPT",hvol1))<0) 
    TESTRPT("micopy_attr failed", r);
  printf("***************** \n");

  r = milist_start(hvol1, "/", 1, &h1list);
  if (r == MI_NOERROR) {
    while( milist_grp_next(h1list, pathbuf1, sizeof(pathbuf1)-1) == MI_NOERROR) {
      printf("%s \n", pathbuf1);
    }
  } else {
    TESTRPT("milist_start failed", r);  
  }
  
  r = milist_finish(h1list);
  if(r<0)
  {
    TESTRPT("milist_finish failed", r);  
  }

  r = miclose_volume(hvol1);
  if(r<0)
  {
    TESTRPT("miclose_volume failed", r);  
  }
  
  r = miclose_volume(hvol);
  if(r<0)
  {
    TESTRPT("miclose_volume failed", r);  
  }
  
  if (error_cnt != 0) {
    fprintf(stderr, "%d error%s reported\n",
            error_cnt, (error_cnt == 1) ? "" : "s");
  } else {
    fprintf(stderr, "No errors\n");
  }
  
  return (error_cnt);
}