NMD
Defines | Functions
nmdcat.c File Reference
#include <stdlib.h>
#include "string.h"
#include "nmd.h"
#include "nmd_impl.h"
Include dependency graph for nmdcat.c:

Go to the source code of this file.

Defines

#define CMPCHUNK   30

Functions

NMDErrorCode NMDObjectTryGetCategory (NMD_metadata obj, const char *cat, NMD_metadata_category *rctg, NMDTruth *f)
NMDErrorCode NMDObjectGetCategory (NMD_metadata obj, const char *cat, NMD_metadata_category *ctg)
static NMDErrorCode NMDAllocateCategory (NMD_metadata_category *rcat)
NMDErrorCode NMDObjectAllocateNewCategory (NMD_metadata obj, const char *cat, NMD_metadata_category *rctg)
NMDErrorCode NMDObjectGetOrCreateCategory (NMD_metadata obj, const char *cat, NMD_metadata_category *ctg)
NMDErrorCode NMDGetCategories (NMD_metadata obj, int *ncat, char ***cats)
NMDErrorCode NMDRemoveCategory (NMD_metadata obj, const char *cat)
NMDErrorCode NMDCopyCategory (NMD_metadata_category incat, NMD_metadata_category outcat)

Define Documentation

#define CMPCHUNK   30

Definition at line 6 of file nmdcat.c.

Referenced by NMDAllocateCategory().


Function Documentation

static NMDErrorCode NMDAllocateCategory ( NMD_metadata_category rcat) [static]

This is an internal routine that merely allocates the data structure for storing a category.

Definition at line 63 of file nmdcat.c.

References NMD_metadata_category_::alloc, CHKMEMQ, CMPCHUNK, NMD_metadata_category_::cmps, NMD_metadata_category_::cookie, NMD_metadata_category_::ncmp, NMD_MALLOC, and NMDCOOKIE.

Referenced by NMDObjectAllocateNewCategory().

{
  NMD_metadata_category cat;
  NMD_MALLOC(cat,1,struct NMD_metadata_category_,"category");
  cat->cookie = NMDCOOKIE;
  cat->alloc = CMPCHUNK; cat->ncmp = 0;
  NMD_MALLOC(cat->cmps,cat->alloc,NMD_metadata_item,"items array");
  *rcat = cat;  
  CHKMEMQ
  return 0;
}
NMDErrorCode NMDCopyCategory ( NMD_metadata_category  incat,
NMD_metadata_category  outcat 
)

Copy category data from one metadata structure into another. This assumes that the category already exists in the target; see for instance NMDHasCategory(), NMDCloneObject(), NMDCloneObjectStructure().

Definition at line 180 of file nmdcat.c.

References CHECKHASNMDCOOKIE, CHKMEMQ, NMD_metadata_category_::cmps, NMD_metadata_item_::name, NMD_metadata_category_::ncmp, NMDCategoryGetOrCreateComponent(), NMDCopyItemValues(), and NMD_metadata_item_::t.

{
  int icmp; NMDErrorCode ierr;

  CHECKHASNMDCOOKIE(incat);
  CHECKHASNMDCOOKIE(outcat);

  for (icmp=0; icmp<incat->ncmp; icmp++) {
    NMD_metadata_item src = incat->cmps[icmp],tar;
    ierr = NMDCategoryGetOrCreateComponent
      (outcat,src->name,src->t,&tar); NMD_ERR_RETURN(ierr);
    ierr = NMDCopyItemValues(src,tar); NMD_ERR_RETURN(ierr);
  }
  CHKMEMQ

  return 0;
}

Here is the call graph for this function:

NMDErrorCode NMDGetCategories ( NMD_metadata  obj,
int *  ncat,
char ***  cats 
)

Get the number of categories and their names. Both arguments can be NULL. The names array is allocated; the user needs to free it. The names themselves are pointers to the strings in the metadata object, so they do not need to be freed.

Definition at line 137 of file nmdcat.c.

References NMD_metadata_::cats, CHECKHASNMDCOOKIE, CHKMEMQ, NMD_metadata_category_::name, NMD_metadata_::ncat, and NMD_MALLOC.

Referenced by main(), and NMDTabReportData().

{
  CHECKHASNMDCOOKIE(obj);
  if (ncat) *ncat = obj->ncat;
  if (obj->ncat && cats) {
    char **names; int icat;
    NMD_MALLOC(names,obj->ncat,char*,"name array");
    for (icat=0; icat<obj->ncat; icat++)
      names[icat] = obj->cats[icat]->name;
    *cats = names;
  }
  CHKMEMQ
  return 0;
}
NMDErrorCode NMDObjectAllocateNewCategory ( NMD_metadata  obj,
const char *  cat,
NMD_metadata_category rctg 
)

Allocate a category in a metadata object. There is no testing whether the category name is already in use.

If a category pointer is supplied, the category is returned, but this pointer is allowed to be null.

Definition at line 84 of file nmdcat.c.

References NMD_metadata_::alloc, NMD_metadata_::cats, CHKMEMQ, NMD_metadata_category_::name, NMD_metadata_::ncat, NMD_STRDUP, and NMDAllocateCategory().

Referenced by main(), NMDObjectEnsureCategoryComponent(), and NMDObjectGetOrCreateCategory().

{
  NMD_metadata_category ctg; int idx; NMDErrorCode ierr;
  /*
   * if we are about to overflow, reallocate 
   */
  if (obj->ncat >= obj->alloc) {
    NMD_metadata_category* newcats; int newalloc;
    newalloc = 2*obj->alloc;
    ierr = PetscMalloc
      (newalloc*sizeof(struct NMD_metadata_category_),&newcats); CHKERRQ(ierr);
    for (idx=0; idx<obj->ncat; idx++)
      newcats[idx] = obj->cats[idx];
    ierr = PetscFree(obj->cats); CHKERRQ(ierr);
    obj->cats = newcats; obj->alloc = newalloc;
  }
  /*
   * with space guaranteed, create the category
   */
  idx = obj->ncat++;
  ierr = NMDAllocateCategory(&ctg); NMD_ERR_RETURN(ierr);
  NMD_STRDUP(cat,ctg->name);
  obj->cats[idx] = ctg;
  if (rctg) *rctg = ctg;
  CHKMEMQ
  return 0;
}

Here is the call graph for this function:

NMDErrorCode NMDObjectGetCategory ( NMD_metadata  obj,
const char *  cat,
NMD_metadata_category ctg 
)

Retrieve a category from a metadata object. The category has to exist.

Definition at line 49 of file nmdcat.c.

References CHECKHASNMDCOOKIE, and NMDObjectTryGetCategory().

Referenced by NMDCategoryGetComponents(), NMDGetArrayValue(), NMDGetDataType(), and NMDRemoveCategory().

{
  NMDTruth flg; NMDErrorCode ierr;
  CHECKHASNMDCOOKIE(obj);
  ierr = NMDObjectTryGetCategory(obj,cat,ctg,&flg); NMD_ERR_RETURN(ierr);
  if (!flg) NMD_ERR_REPORTs("Category not found",cat);
  return 0;
}

Here is the call graph for this function:

NMDErrorCode NMDObjectGetOrCreateCategory ( NMD_metadata  obj,
const char *  cat,
NMD_metadata_category ctg 
)

Retrieve a category from a metadata object, or create it if it doesn't exist yet.

Definition at line 118 of file nmdcat.c.

References CHECKHASNMDCOOKIE, CHKMEMQ, NMDObjectAllocateNewCategory(), and NMDObjectTryGetCategory().

Referenced by NMDCloneObject(), NMDCopyArrayValue(), NMDSetArrayValue(), and NMDSetValue().

{
  NMDTruth flg; NMDErrorCode ierr;
  CHECKHASNMDCOOKIE(obj);
  ierr = NMDObjectTryGetCategory(obj,cat,ctg,&flg); NMD_ERR_RETURN(ierr);
  if (!flg) {
    ierr = NMDObjectAllocateNewCategory(obj,cat,ctg); NMD_ERR_RETURN(ierr);
  }
  CHKMEMQ
  return 0;
}

Here is the call graph for this function:

NMDErrorCode NMDObjectTryGetCategory ( NMD_metadata  obj,
const char *  cat,
NMD_metadata_category rctg,
NMDTruth f 
)

Test whether a metadata object has a certain category, if so yield up its pointer.

The category pointer parameter can be null, in which case only existence is tested.

Definition at line 29 of file nmdcat.c.

References NMD_metadata_::cats, CHECKHASNMDCOOKIE, NMD_metadata_category_::name, NMD_metadata_::ncat, NMDFalse, and NMDTrue.

Referenced by main(), NMDGetValue(), NMDObjectEnsureCategoryComponent(), NMDObjectGetCategory(), NMDObjectGetOrCreateCategory(), NMDObjectHasCategoryComponent(), and NMDUnsetValue().

{
  int icat;
  CHECKHASNMDCOOKIE(obj);
  *f = NMDFalse;
  for (icat=0; icat<obj->ncat; icat++) {
    NMD_metadata_category ctg = obj->cats[icat];
    CHECKHASNMDCOOKIE(ctg);
    if (strcmp(cat,ctg->name)==0) {
      if (rctg) *rctg = ctg; *f = NMDTrue; break;
    }
  }
  return 0;
}
NMDErrorCode NMDRemoveCategory ( NMD_metadata  obj,
const char *  cat 
)

Definition at line 160 of file nmdcat.c.

References CHECKHASNMDCOOKIE, CHKMEMQ, NMD_metadata_category_::name, NMD_FREE, NMD_STRDUP, and NMDObjectGetCategory().

Referenced by main().

{
  NMD_metadata_category ctg; NMDErrorCode ierr;

  CHECKHASNMDCOOKIE(obj);
  ierr = NMDObjectGetCategory(obj,(char*)cat,&ctg); NMD_ERR_RETURN(ierr);
  NMD_FREE(ctg->name);
  NMD_STRDUP("invalid",ctg->name);
  CHKMEMQ

  return 0;
}

Here is the call graph for this function: