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, 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, 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().

00064 {
00065   NMD_metadata_category cat;
00066   NMD_MALLOC(cat,1,struct NMD_metadata_category_,"category");
00067   cat->cookie = NMDCOOKIE;
00068   cat->alloc = CMPCHUNK; cat->ncmp = 0;
00069   NMD_MALLOC(cat->cmps,cat->alloc,NMD_metadata_item,"items array");
00070   *rcat = cat;  
00071   CHKMEMQ
00072   return 0;
00073 }

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.

00181 {
00182   int icmp; NMDErrorCode ierr;
00183 
00184   CHECKHASNMDCOOKIE(incat);
00185   CHECKHASNMDCOOKIE(outcat);
00186 
00187   for (icmp=0; icmp<incat->ncmp; icmp++) {
00188     NMD_metadata_item src = incat->cmps[icmp],tar;
00189     ierr = NMDCategoryGetOrCreateComponent
00190       (outcat,src->name,src->t,&tar); NMD_ERR_RETURN(ierr);
00191     ierr = NMDCopyItemValues(src,tar); NMD_ERR_RETURN(ierr);
00192   }
00193   CHKMEMQ
00194 
00195   return 0;
00196 }

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().

00138 {
00139   CHECKHASNMDCOOKIE(obj);
00140   if (ncat) *ncat = obj->ncat;
00141   if (obj->ncat && cats) {
00142     char **names; int icat;
00143     NMD_MALLOC(names,obj->ncat,char*,"name array");
00144     for (icat=0; icat<obj->ncat; icat++)
00145       names[icat] = obj->cats[icat]->name;
00146     *cats = names;
00147   }
00148   CHKMEMQ
00149   return 0;
00150 }

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().

00085 {
00086   NMD_metadata_category ctg; int idx; NMDErrorCode ierr;
00087   /*
00088    * if we are about to overflow, reallocate 
00089    */
00090   if (obj->ncat >= obj->alloc) {
00091     NMD_metadata_category* newcats; int newalloc;
00092     newalloc = 2*obj->alloc;
00093     ierr = PetscMalloc
00094       (newalloc*sizeof(struct NMD_metadata_category_),&newcats); CHKERRQ(ierr);
00095     for (idx=0; idx<obj->ncat; idx++)
00096       newcats[idx] = obj->cats[idx];
00097     ierr = PetscFree(obj->cats); CHKERRQ(ierr);
00098     obj->cats = newcats; obj->alloc = newalloc;
00099   }
00100   /*
00101    * with space guaranteed, create the category
00102    */
00103   idx = obj->ncat++;
00104   ierr = NMDAllocateCategory(&ctg); NMD_ERR_RETURN(ierr);
00105   NMD_STRDUP(cat,ctg->name);
00106   obj->cats[idx] = ctg;
00107   if (rctg) *rctg = ctg;
00108   CHKMEMQ
00109   return 0;
00110 }

Here is the call graph for this function:

NMDErrorCode NMDObjectGetCategory ( NMD_metadata  obj,
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().

00050 {
00051   NMDTruth flg; NMDErrorCode ierr;
00052   CHECKHASNMDCOOKIE(obj);
00053   ierr = NMDObjectTryGetCategory(obj,cat,ctg,&flg); NMD_ERR_RETURN(ierr);
00054   if (!flg) NMD_ERR_REPORTs("Category not found",cat);
00055   return 0;
00056 }

Here is the call graph for this function:

NMDErrorCode NMDObjectGetOrCreateCategory ( NMD_metadata  obj,
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().

00119 {
00120   NMDTruth flg; NMDErrorCode ierr;
00121   CHECKHASNMDCOOKIE(obj);
00122   ierr = NMDObjectTryGetCategory(obj,cat,ctg,&flg); NMD_ERR_RETURN(ierr);
00123   if (!flg) {
00124     ierr = NMDObjectAllocateNewCategory(obj,cat,ctg); NMD_ERR_RETURN(ierr);
00125   }
00126   CHKMEMQ
00127   return 0;
00128 }

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().

00030 {
00031   int icat;
00032   CHECKHASNMDCOOKIE(obj);
00033   *f = NMDFalse;
00034   for (icat=0; icat<obj->ncat; icat++) {
00035     NMD_metadata_category ctg = obj->cats[icat];
00036     CHECKHASNMDCOOKIE(ctg);
00037     if (strcmp(cat,ctg->name)==0) {
00038       if (rctg) *rctg = ctg; *f = NMDTrue; break;
00039     }
00040   }
00041   return 0;
00042 }

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().

00161 {
00162   NMD_metadata_category ctg; NMDErrorCode ierr;
00163 
00164   CHECKHASNMDCOOKIE(obj);
00165   ierr = NMDObjectGetCategory(obj,(char*)cat,&ctg); NMD_ERR_RETURN(ierr);
00166   NMD_FREE(ctg->name);
00167   NMD_STRDUP("invalid",ctg->name);
00168   CHKMEMQ
00169 
00170   return 0;
00171 }

Here is the call graph for this function:


Generated on Sun Oct 4 03:59:29 2009 for NMD by  doxygen 1.5.9