Exemple #1
0
int
NC3_rename_dim( int ncid, int dimid, const char *unewname)
{
	int status = NC_NOERR;
	NC *nc;
	NC3_INFO* ncp;
	int existid;
	NC_dim *dimp;
	char *newname = NULL; /* normalized */
	NC_string *old = NULL;
 	uintptr_t intdata;


	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		goto done;
	ncp = NC3_DATA(nc);

	if(NC_readonly(ncp))
		{status = NC_EPERM; goto done;}

	status = NC_check_name(unewname);
	if(status != NC_NOERR)
		goto done;

	existid = NC_finddim(&ncp->dims, unewname, &dimp);
	if(existid != -1)
		{status = NC_ENAMEINUSE; goto done;}

	dimp = elem_NC_dimarray(&ncp->dims, (size_t)dimid);
	if(dimp == NULL)
		{status = NC_EBADDIM; goto done;}

    old = dimp->name;
    status = nc_utf8_normalize((const unsigned char *)unewname,(unsigned char **)&newname);
    if(status != NC_NOERR)
	goto done;
    if(NC_indef(ncp))
	{
		NC_string *newStr = new_NC_string(strlen(newname), newname);
		if(newStr == NULL)
			{status = NC_ENOMEM; goto done;}

		/* Remove old name from hashmap; add new... */
	        NC_hashmapremove(ncp->dims.hashmap, old->cp, strlen(old->cp), NULL);
		dimp->name = newStr;

		intdata = dimid;
		NC_hashmapadd(ncp->dims.hashmap, intdata, newStr->cp, strlen(newStr->cp));
		free_NC_string(old);
		goto done;
	}

	/* else, not in define mode */

	/* If new name is longer than old, then complain,
           but otherwise, no change (test is same as set_NC_string)*/
	if(dimp->name->nchars < strlen(newname)) {
	    {status = NC_ENOTINDEFINE; goto done;}
	}

	/* Remove old name from hashmap; add new... */
	/* WARNING: strlen(NC_string.cp) may be less than NC_string.nchars */
	NC_hashmapremove(ncp->dims.hashmap, old->cp, strlen(old->cp), NULL);

	/* WARNING: strlen(NC_string.cp) may be less than NC_string.nchars */
	status = set_NC_string(dimp->name, newname);
	if(status != NC_NOERR)
		goto done;

        intdata = (uintptr_t)dimid;
	NC_hashmapadd(ncp->dims.hashmap, intdata, dimp->name->cp, strlen(dimp->name->cp));

	set_NC_hdirty(ncp);

	if(NC_doHsync(ncp))
	{
		status = NC_sync(ncp);
		if(status != NC_NOERR)
			goto done;
	}

done:
	if(newname) free(newname);
	return status;
}
int
nc_put_att_text(int ncid, int varid, const char *name,
  size_t nelems, const char *value)
{
  int status;
  NC *ncp;
  NC_attrarray *ncap;
  NC_attr **attrpp;
  NC_attr *old = NULL;
  NC_attr *attrp;

  status = NC_check_id(ncid, &ncp);
  if(status != NC_NOERR)
    return status;

  if(NC_readonly(ncp))
    return NC_EPERM;

  ncap = NC_attrarray0(ncp, varid);
  if(ncap == NULL)
    return NC_ENOTVAR;

  status = NC_check_name(name);
  if(status != NC_NOERR)
    return status;

    /* cast needed for braindead systems with signed size_t */
  if((unsigned long) nelems > X_INT_MAX) /* backward compat */
    return NC_EINVAL; /* Invalid nelems */

  if(nelems != 0 && value == NULL)
    return NC_EINVAL; /* Null arg */

  attrpp = NC_findattr(ncap, name);
  if(attrpp != NULL) /* name in use */
  {
    if(!NC_indef(ncp) )
    {
      const size_t xsz = ncx_len_NC_attrV(NC_CHAR, nelems);
      attrp = *attrpp; /* convenience */
  
      if(xsz > attrp->xsz)
        return NC_ENOTINDEFINE;
      /* else, we can reuse existing without redef */
      
      attrp->xsz = xsz;
      attrp->type = NC_CHAR;
      attrp->nelems = nelems;

      if(nelems != 0)
      {
        void *xp = attrp->xvalue;
        status = ncx_pad_putn_text(&xp, nelems, value);
        if(status != NC_NOERR)
          return status;
      }
      
      set_NC_hdirty(ncp);

      if(NC_doHsync(ncp))
      {
        status = NC_sync(ncp);
        if(status != NC_NOERR)
          return status;
      }

      return NC_NOERR;
    }
    /* else, redefine using existing array slot */
    old = *attrpp;
  } 
  else
  {
    if(!NC_indef(ncp))
      return NC_ENOTINDEFINE;

    if(ncap->nelems >= NC_MAX_ATTRS)
      return NC_EMAXATTS;
  }

  attrp = new_NC_attr(name, NC_CHAR, nelems);
  if(attrp == NULL)
    return NC_ENOMEM;

  if(nelems != 0)
  {
    void *xp = attrp->xvalue;
    status = ncx_pad_putn_text(&xp, nelems, value);
    if(status != NC_NOERR)
      return status;
  }

  if(attrpp != NULL)
  {
    assert(old != NULL);
    *attrpp = attrp;
    free_NC_attr(old);
  }
  else
  {
    status = incr_NC_attrarray(ncap, attrp);
    if(status != NC_NOERR)
    {
      free_NC_attr(attrp);
      return status;
    }
  }

  return NC_NOERR;
}
Exemple #3
0
int
nc_rename_var(int ncid, int varid, const char *newname)
{
  int status;
  NC *ncp;
  NC_var *varp;
  NC_string *old, *newStr;
  int other;

  status = NC_check_id(ncid, &ncp); 
  if(status != NC_NOERR)
    return status;

  if(NC_readonly(ncp))
  {
    return NC_EPERM;
  }

  status = NC_check_name(newname);
  if(status != NC_NOERR)
    return status;

  /* check for name in use */
  other = NC_findvar(&ncp->vars, newname, &varp);
  if(other != -1)
  {
    return NC_ENAMEINUSE;
  }
  
  varp = NC_lookupvar(ncp, varid);
  if(varp == NULL)
  {
    /* invalid varid */
    return NC_ENOTVAR; /* TODO: is this the right error code? */
  }

  old = varp->name;
  if(NC_indef(ncp))
  {
    newStr = new_NC_string(strlen(newname),newname);
    if(newStr == NULL)
      return(-1);
    varp->name = newStr;
    free_NC_string(old);
    return NC_NOERR;
  }

  /* else, not in define mode */
  status = set_NC_string(varp->name, newname);
  if(status != NC_NOERR)
    return status;

  set_NC_hdirty(ncp);

  if(NC_doHsync(ncp))
  {
    status = NC_sync(ncp);
    if(status != NC_NOERR)
      return status;
  }

  return NC_NOERR;
}
Exemple #4
0
int
NC3_rename_dim( int ncid, int dimid, const char *unewname)
{
	int status;
	NC *ncp;
	int existid;
	NC_dim *dimp;
	char *newname;		/* normalized */

	status = NC_check_id(ncid, &ncp); 
	if(status != NC_NOERR)
		return status;

	if(NC_readonly(ncp))
		return NC_EPERM;

	status = NC_check_name(unewname);
	if(status != NC_NOERR)
		return status;

	existid = NC_finddim(&ncp->dims, unewname, &dimp);
	if(existid != -1)
		return NC_ENAMEINUSE;

	dimp = elem_NC_dimarray(&ncp->dims, (size_t)dimid);
	if(dimp == NULL)
		return NC_EBADDIM;

	newname = (char *)utf8proc_NFC((const unsigned char *)unewname);
	if(newname == NULL)
	    return NC_ENOMEM;
	if(NC_indef(ncp))
	{
		NC_string *old = dimp->name;
		NC_string *newStr = new_NC_string(strlen(newname), newname);
		free(newname);
		if(newStr == NULL)
			return NC_ENOMEM;
		dimp->name = newStr;
		dimp->hash = hash_fast(newStr->cp, strlen(newStr->cp));
		free_NC_string(old);
		return NC_NOERR;
	}

	/* else, not in define mode */

	status = set_NC_string(dimp->name, newname);
	dimp->hash = hash_fast(newname, strlen(newname));
	free(newname);
	if(status != NC_NOERR)
		return status;

	set_NC_hdirty(ncp);

	if(NC_doHsync(ncp))
	{
		status = NC_sync(ncp);
		if(status != NC_NOERR)
			return status;
	}

	return NC_NOERR;
}
Exemple #5
0
int
NC3_rename_att( int ncid, int varid, const char *name, const char *unewname)
{
        int status;
        NC *ncp;
        NC_attrarray *ncap;
        NC_attr **tmp;
        NC_attr *attrp;
        NC_string *newStr, *old;
        char *newname;  /* normalized version */

                        /* sortof inline clone of NC_lookupattr() */
        status = NC_check_id(ncid, &ncp);
        if(status != NC_NOERR)
                return status;

        if(NC_readonly(ncp))
                return NC_EPERM;

        ncap = NC_attrarray0(ncp, varid);
        if(ncap == NULL)
                return NC_ENOTVAR;

        status = NC_check_name(unewname);
        if(status != NC_NOERR)
                return status;

        tmp = NC_findattr(ncap, name);
        if(tmp == NULL)
                return NC_ENOTATT;
        attrp = *tmp;
                        /* end inline clone NC_lookupattr() */

        if(NC_findattr(ncap, unewname) != NULL)
        {
                /* name in use */
                return NC_ENAMEINUSE;
        }

        old = attrp->name;
        newname = (char *)utf8proc_NFC((const unsigned char *)unewname);
        if(newname == NULL)
            return NC_EBADNAME;
        if(NC_indef(ncp))
        {
                newStr = new_NC_string(strlen(newname), newname);
                free(newname);
                if( newStr == NULL)
                        return NC_ENOMEM;
                attrp->name = newStr;
                free_NC_string(old);
                return NC_NOERR;
        }
        /* else */
        status = set_NC_string(old, newname);
        free(newname);
        if( status != NC_NOERR)
                return status;

        set_NC_hdirty(ncp);

        if(NC_doHsync(ncp))
        {
                status = NC_sync(ncp);
                if(status != NC_NOERR)
                        return status;
        }

        return NC_NOERR;
}
Exemple #6
0
int
NC3_put_att(
        int ncid,
        int varid,
        const char *name,
        nc_type type,
        size_t nelems,
        const void *value,
        nc_type memtype)
{
    int status;
    NC *ncp;
    NC_attrarray *ncap;
    NC_attr **attrpp;
    NC_attr *old = NULL;
    NC_attr *attrp;

    status = NC_check_id(ncid, &ncp);
    if(status != NC_NOERR)
        return status;

    if(NC_readonly(ncp))
        return NC_EPERM;

    ncap = NC_attrarray0(ncp, varid);
    if(ncap == NULL)
        return NC_ENOTVAR;

    status = nc_cktype(type);
    if(status != NC_NOERR)
        return status;

    if(memtype == NC_NAT) memtype = type;

    if(memtype != NC_CHAR && type == NC_CHAR)
        return NC_ECHAR;
    if(memtype == NC_CHAR && type != NC_CHAR)
        return NC_ECHAR;

    /* cast needed for braindead systems with signed size_t */
    if((unsigned long) nelems > X_INT_MAX) /* backward compat */
        return NC_EINVAL; /* Invalid nelems */

    if(nelems != 0 && value == NULL)
        return NC_EINVAL; /* Null arg */

    attrpp = NC_findattr(ncap, name);

    /* 4 cases: exists X indef */

    if(attrpp != NULL) { /* name in use */
        if(!NC_indef(ncp)) {
            const size_t xsz = ncx_len_NC_attrV(type, nelems);
            attrp = *attrpp; /* convenience */

            if(xsz > attrp->xsz) return NC_ENOTINDEFINE;
            /* else, we can reuse existing without redef */

            attrp->xsz = xsz;
            attrp->type = type;
            attrp->nelems = nelems;

            if(nelems != 0) {
                void *xp = attrp->xvalue;
                status = dispatchput(&xp, nelems, (const void*)value, type, memtype);
            }

            set_NC_hdirty(ncp);

            if(NC_doHsync(ncp)) {
                const int lstatus = NC_sync(ncp);
                /*
                 * N.B.: potentially overrides NC_ERANGE
                 * set by ncx_pad_putn_I$1
                 */
                if(lstatus != ENOERR) return lstatus;
            }

            return status;
        }
        /* else, redefine using existing array slot */
        old = *attrpp;
    } else {
        if(!NC_indef(ncp)) return NC_ENOTINDEFINE;

        if(ncap->nelems >= NC_MAX_ATTRS) return NC_EMAXATTS;
    }

    status = NC_check_name(name);
    if(status != NC_NOERR) return status;

    attrp = new_NC_attr(name, type, nelems);
    if(attrp == NULL) return NC_ENOMEM;

    if(nelems != 0) {
        void *xp = attrp->xvalue;
        status = dispatchput(&xp, nelems, (const void*)value, type, memtype);
    }

    if(attrpp != NULL) {
        assert(old != NULL);
        *attrpp = attrp;
        free_NC_attr(old);
    } else {
        const int lstatus = incr_NC_attrarray(ncap, attrp);
        /*
         * N.B.: potentially overrides NC_ERANGE
         * set by ncx_pad_putn_I$1
         */
        if(lstatus != NC_NOERR) {
           free_NC_attr(attrp);
           return lstatus;
        }
    }
    return status;
}
Exemple #7
0
int
NC3_rename_var(int ncid, int varid, const char *unewname)
{
	int status;
	NC *nc;
	NC3_INFO* ncp;
	NC_var *varp;
	NC_string *old, *newStr;
	int other;
	char *newname;		/* normalized */

	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		return status;
	ncp = NC3_DATA(nc);

	if(NC_readonly(ncp))
	{
		return NC_EPERM;
	}

	status = NC_check_name(unewname);
	if(status != NC_NOERR)
		return status;

	/* check for name in use */
	other = NC_findvar(&ncp->vars, unewname, &varp);
	if(other != -1)
	{
		return NC_ENAMEINUSE;
	}

	status = NC_lookupvar(ncp, varid, &varp);
	if(status != NC_NOERR)
	{
		/* invalid varid */
		return status;
	}


	old = varp->name;
	newname = (char *)utf8proc_NFC((const unsigned char *)unewname);
	if(newname == NULL)
	    return NC_ENOMEM;
	if(NC_indef(ncp))
	{
		newStr = new_NC_string(strlen(newname),newname);
		free(newname);
		if(newStr == NULL)
			return(-1);
		varp->name = newStr;

		/* Remove old name from hashmap; add new... */
		NC_hashmapRemoveVar(&ncp->vars, old->cp);
		NC_hashmapAddVar(&ncp->vars, varid, newStr->cp);
		free_NC_string(old);

		return NC_NOERR;
	}

	/* else, not in define mode */
	status = set_NC_string(varp->name, newname);
	free(newname);
	if(status != NC_NOERR)
		return status;

	/* Remove old name from hashmap; add new... */
	NC_hashmapRemoveVar(&ncp->vars, old->cp);
	NC_hashmapAddVar(&ncp->vars, varid, varp->name->cp);

	set_NC_hdirty(ncp);

	if(NC_doHsync(ncp))
	{
		status = NC_sync(ncp);
		if(status != NC_NOERR)
			return status;
	}

	return NC_NOERR;
}
Exemple #8
0
int
nc_copy_att(int ncid_in, int varid_in, const char *name, int ncid_out, int ovarid)
{
  int status;
  NC_attr *iattrp;
  NC *ncp;
  NC_attrarray *ncap;
  NC_attr **attrpp;
  NC_attr *old = NULL;
  NC_attr *attrp;

  status = NC_lookupattr(ncid_in, varid_in, name, &iattrp);
  if(status != NC_NOERR)
    return status;

  status = NC_check_id(ncid_out, &ncp);
  if(status != NC_NOERR)
    return status;

  if(NC_readonly(ncp))
    return NC_EPERM;

  ncap = NC_attrarray0(ncp, ovarid);
  if(ncap == NULL)
    return NC_ENOTVAR;

  attrpp = NC_findattr(ncap, name);
  if(attrpp != NULL) /* name in use */
  {
    if(!NC_indef(ncp) )
    {
      attrp = *attrpp; /* convenience */
  
      if(iattrp->xsz > attrp->xsz)
        return NC_ENOTINDEFINE;
      /* else, we can reuse existing without redef */
      
      attrp->xsz = iattrp->xsz;
      attrp->type = iattrp->type;
      attrp->nelems = iattrp->nelems;

      (void) memcpy(attrp->xvalue, iattrp->xvalue,
        iattrp->xsz);
      
      set_NC_hdirty(ncp);

      if(NC_doHsync(ncp))
      {
        status = NC_sync(ncp);
        if(status != NC_NOERR)
          return status;
      }

      return NC_NOERR;
    }
    /* else, redefine using existing array slot */
    old = *attrpp;
  } 
  else
  {
    if(!NC_indef(ncp))
      return NC_ENOTINDEFINE;

    if(ncap->nelems >= NC_MAX_ATTRS)
      return NC_EMAXATTS;
  }

  attrp = new_NC_attr(name, iattrp->type, iattrp->nelems);
  if(attrp == NULL)
    return NC_ENOMEM;

  (void) memcpy(attrp->xvalue, iattrp->xvalue,
    iattrp->xsz);

  if(attrpp != NULL)
  {
    assert(old != NULL);
    *attrpp = attrp;
    free_NC_attr(old);
  }
  else
  {
    status = incr_NC_attrarray(ncap, attrp);
    if(status != NC_NOERR)
    {
      free_NC_attr(attrp);
      return status;
    }
  }

  return NC_NOERR;
}
Exemple #9
0
int
NC3_rename_att( int ncid, int varid, const char *name, const char *unewname)
{
	int status;
	NC *nc;
	NC3_INFO* ncp;
	NC_attrarray *ncap;
	NC_attr **tmp;
	NC_attr *attrp;
	NC_string *newStr, *old;
	char *newname;  /* normalized version */

			/* sortof inline clone of NC_lookupattr() */
	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		return status;
	ncp = NC3_DATA(nc);

	if(NC_readonly(ncp))
		return NC_EPERM;

	ncap = NC_attrarray0(ncp, varid);
	if(ncap == NULL)
		return NC_ENOTVAR;

	status = NC_check_name(unewname);
	if(status != NC_NOERR)
		return status;

	tmp = NC_findattr(ncap, name);
	if(tmp == NULL)
		return NC_ENOTATT;
	attrp = *tmp;
			/* end inline clone NC_lookupattr() */

	if(NC_findattr(ncap, unewname) != NULL)
	{
		/* name in use */
		return NC_ENAMEINUSE;
	}

	old = attrp->name;
	status = nc_utf8_normalize((const unsigned char *)unewname,(unsigned char**)&newname);
	if(status != NC_NOERR)
	    return status;
	if(NC_indef(ncp))
	{
		newStr = new_NC_string(strlen(newname), newname);
		free(newname);
		if( newStr == NULL)
			return NC_ENOMEM;
		attrp->name = newStr;
		free_NC_string(old);
		return NC_NOERR;
	}
	/* else not in define mode */

	/* If new name is longer than old, then complain,
           but otherwise, no change (test is same as set_NC_string)*/
	if(old->nchars < strlen(newname)) {
	    free(newname);
	    return NC_ENOTINDEFINE;
	}

	status = set_NC_string(old, newname);
	free(newname);
	if( status != NC_NOERR)
		return status;

	set_NC_hdirty(ncp);

	if(NC_doHsync(ncp))
	{
		status = NC_sync(ncp);
		if(status != NC_NOERR)
			return status;
	}

	return NC_NOERR;
}
Exemple #10
0
int
NC3_put_att(
	int ncid,
	int varid,
	const char *name,
	nc_type type,
	size_t nelems,
	const void *value,
	nc_type memtype)
{
    int status;
    NC *nc;
    NC3_INFO* ncp;
    NC_attrarray *ncap;
    NC_attr **attrpp;
    NC_attr *old = NULL;
    NC_attr *attrp;
    unsigned char fill[8]; /* fill value in internal representation */

    status = NC_check_id(ncid, &nc);
    if(status != NC_NOERR)
	return status;
    ncp = NC3_DATA(nc);

    if(NC_readonly(ncp))
	return NC_EPERM;

    ncap = NC_attrarray0(ncp, varid);
    if(ncap == NULL)
	return NC_ENOTVAR;

    if (name == NULL)
        return NC_EBADNAME;

    /* check NC_EBADTYPE */
    status = nc3_cktype(nc->mode, type);
    if(status != NC_NOERR)
	return status;

    if(memtype == NC_NAT) memtype = type;

    if(memtype != NC_CHAR && type == NC_CHAR)
	return NC_ECHAR;
    if(memtype == NC_CHAR && type != NC_CHAR)
	return NC_ECHAR;

    /* cast needed for braindead systems with signed size_t */
    if((unsigned long) nelems > X_INT_MAX) /* backward compat */
	return NC_EINVAL; /* Invalid nelems */

    if(nelems != 0 && value == NULL)
	return NC_EINVAL; /* Null arg */

    /* Temporarily removed to preserve extant
       workflows (NCO based and others). See

       https://github.com/Unidata/netcdf-c/issues/843

       for more information. */

//    if (varid != NC_GLOBAL && !strcmp(name, _FillValue)) {
//        /* Fill value must be of the same data type */
//        if (type != ncp->vars.value[varid]->type) return NC_EBADTYPE;
//
//        /* Fill value must have exactly one value */
//        if (nelems != 1) return NC_EINVAL;
//
//        /* Only allow for variables defined in initial define mode */
//        if (ncp->old != NULL && varid < ncp->old->vars.nelems)
//            return NC_ELATEFILL; /* try put attribute for an old variable */
//    }

    attrpp = NC_findattr(ncap, name);

    /* 4 cases: exists X indef */

    status = NC3_inq_default_fill_value(type, &fill);
    if (status != NC_NOERR) return status;

    if(attrpp != NULL) { /* name in use */
        if(!NC_indef(ncp)) {
	    const size_t xsz = ncx_len_NC_attrV(type, nelems);
            attrp = *attrpp; /* convenience */

	    if(xsz > attrp->xsz) return NC_ENOTINDEFINE;
	    /* else, we can reuse existing without redef */

	    attrp->xsz = xsz;
            attrp->type = type;
            attrp->nelems = nelems;

            if(nelems != 0) {
                void *xp = attrp->xvalue;
                /* for CDF-1 and CDF-2, NC_BYTE is treated the same type as uchar memtype */
                if (!fIsSet(ncp->flags,NC_64BIT_DATA) && type == NC_BYTE && memtype == NC_UBYTE) {
                    status = NC3_inq_default_fill_value(NC_UBYTE, &fill);
                    if (status != NC_NOERR) return status;
                    status = dispatchput(&xp, nelems, value, memtype, memtype, &fill);
                } else
                    status = dispatchput(&xp, nelems, value, type, memtype, &fill);
            }

            set_NC_hdirty(ncp);

            if(NC_doHsync(ncp)) {
	        const int lstatus = NC_sync(ncp);
                /*
                 * N.B.: potentially overrides NC_ERANGE
                 * set by ncx_pad_putn_I$1
                 */
                if(lstatus != NC_NOERR) return lstatus;
            }

            return status;
        }
        /* else, redefine using existing array slot */
        old = *attrpp;
    } else {
        if(!NC_indef(ncp)) return NC_ENOTINDEFINE;
    }

    status = NC_check_name(name);
    if(status != NC_NOERR) return status;

    attrp = new_NC_attr(name, type, nelems);
    if(attrp == NULL) return NC_ENOMEM;

    if(nelems != 0) {
        void *xp = attrp->xvalue;
        /* for CDF-1 and CDF-2, NC_BYTE is treated the same type as uchar memtype */
        if (!fIsSet(ncp->flags,NC_64BIT_DATA) && type == NC_BYTE && memtype == NC_UBYTE) {
            status = NC3_inq_default_fill_value(NC_UBYTE, &fill);
            if (status != NC_NOERR) return status;
            status = dispatchput(&xp, nelems, (const void*)value, memtype, memtype, &fill);
        } else
            status = dispatchput(&xp, nelems, (const void*)value, type, memtype, &fill);
    }

    if(attrpp != NULL) {
        *attrpp = attrp;
	if(old != NULL)
	        free_NC_attr(old);
    } else {
        const int lstatus = incr_NC_attrarray(ncap, attrp);
        /*
         * N.B.: potentially overrides NC_ERANGE
         * set by ncx_pad_putn_I$1
         */
        if(lstatus != NC_NOERR) {
           free_NC_attr(attrp);
           return lstatus;
        }
    }
    return status;
}
Exemple #11
0
int
NC3_rename_var(int ncid, int varid, const char *unewname)
{
	int status;
	NC *nc;
	NC3_INFO* ncp;
	NC_var *varp;
	NC_string *old, *newStr;
	int other;
	char *newname;		/* normalized */

	status = NC_check_id(ncid, &nc);
	if(status != NC_NOERR)
		return status;
	ncp = NC3_DATA(nc);

	if(NC_readonly(ncp))
	{
		return NC_EPERM;
	}

	status = NC_check_name(unewname);
	if(status != NC_NOERR)
		return status;

	/* check for name in use */
	other = NC_findvar(&ncp->vars, unewname, &varp);
	if(other != -1)
	{
		return NC_ENAMEINUSE;
	}

	varp = NC_lookupvar(ncp, varid);
	if(varp == NULL)
	{
		/* invalid varid */
		return NC_ENOTVAR; /* TODO: is this the right error code? */
	}

	old = varp->name;
	newname = (char *)utf8proc_NFC((const unsigned char *)unewname);
	if(newname == NULL)
	    return NC_ENOMEM;
	if(NC_indef(ncp))
	{
		newStr = new_NC_string(strlen(newname),newname);
		free(newname);
		if(newStr == NULL)
			return(-1);
		varp->name = newStr;
		varp->hash = hash_fast(newStr->cp, strlen(newStr->cp));
		free_NC_string(old);
		return NC_NOERR;
	}

	/* else, not in define mode */
	status = set_NC_string(varp->name, newname);
	varp->hash = hash_fast(newname, strlen(newname));
	free(newname);
	if(status != NC_NOERR)
		return status;

	set_NC_hdirty(ncp);

	if(NC_doHsync(ncp))
	{
		status = NC_sync(ncp);
		if(status != NC_NOERR)
			return status;
	}

	return NC_NOERR;
}