ncdf4a13/libsrc/string.c

Go to the documentation of this file.
00001 /*
00002  *      Copyright 1996, University Corporation for Atmospheric Research
00003  *      See netcdf/COPYRIGHT file for copying and redistribution conditions.
00004  */
00005 /* $Id: string.c,v 1.61 2005/07/14 22:52:54 russ Exp $ */
00006 
00007 #include "nc.h"
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include <ctype.h>
00011 #include <assert.h>
00012 #include "ncx.h"
00013 #include "rnd.h"
00014 
00015 
00016 /*
00017  * Free string, and, if needed, its values.
00018  * Formerly
00019 NC_free_string()
00020  */
00021 void
00022 free_NC_string(NC_string *ncstrp)
00023 {
00024         if(ncstrp==NULL)
00025                 return;
00026         free(ncstrp);
00027 }
00028 
00029 
00030 /*
00031  * Verify that a name string is valid
00032  * CDL syntax, eg, all the characters are
00033  * alphanumeric, '-', '_', '+', or '.'.
00034  * Also permit ':', '@', '(', or ')' in names for chemists currently making 
00035  * use of these characters, but don't document until ncgen and ncdump can 
00036  * also handle these characters in names.
00037  */
00038 int
00039 NC_check_name(const char *name)
00040 {
00041         const char *cp = name;
00042         assert(name != NULL);
00043 
00044         if(*name == 0)
00045                 return NC_EBADNAME; /* empty names disallowed */
00046 
00047         for(; *cp != 0; cp++)
00048         {
00049                 int ch = *cp;
00050                 if(!isalnum(ch))
00051                 {
00052                     if(ch != '_' && ch != '-' && ch != '+' && ch != '.' && 
00053                        ch != ':' && ch != '@' && ch != '(' && ch != ')')
00054                                 return NC_EBADNAME;
00055                 }
00056         }
00057         if(cp - name > NC_MAX_NAME)
00058                 return NC_EMAXNAME;
00059 
00060         return NC_NOERR;
00061 }
00062 
00063 
00064 /*
00065  * Allocate a NC_string structure large enough
00066  * to hold slen characters.
00067  * Formerly
00068 NC_new_string(count, str)
00069  */
00070 NC_string *
00071 new_NC_string(size_t slen, const char *str)
00072 {
00073         NC_string *ncstrp;
00074         size_t sz = M_RNDUP(sizeof(NC_string)) + slen + 1;
00075 
00076 #if 0
00077         sz = _RNDUP(sz, X_ALIGN);
00078 #endif
00079                 
00080         ncstrp = (NC_string *)malloc(sz);
00081         if( ncstrp == NULL )
00082                 return NULL;
00083         (void) memset(ncstrp, 0, sz);
00084 
00085         ncstrp->nchars = sz - M_RNDUP(sizeof(NC_string)) - 1;
00086         assert(ncstrp->nchars + 1 > slen);
00087         ncstrp->cp = (char *)ncstrp + M_RNDUP(sizeof(NC_string));
00088 
00089         if(str != NULL && *str != 0)
00090         {
00091                 (void) strncpy(ncstrp->cp, str, ncstrp->nchars +1);
00092                 ncstrp->cp[ncstrp->nchars] = 0;
00093         }
00094         
00095         return(ncstrp);
00096 }
00097 
00098 
00099 /*
00100  * If possible, change the value of an NC_string to 'str'.
00101  *
00102  * Formerly
00103 NC_re_string()
00104  */
00105 int
00106 set_NC_string(NC_string *ncstrp, const char *str)
00107 {
00108         size_t slen;
00109         size_t diff;
00110 
00111         assert(str != NULL && *str != 0);
00112 
00113         slen = strlen(str);
00114 
00115         if(ncstrp->nchars < slen)
00116                 return NC_ENOTINDEFINE;
00117 
00118         (void) memcpy(ncstrp->cp, str, slen);
00119         diff = ncstrp->nchars - slen;
00120         if(diff != 0)
00121                 (void) memset(ncstrp->cp + slen, 0, diff);
00122 
00123         return NC_NOERR;
00124 }

Generated on Thu Mar 16 18:10:10 2006 for nco by  doxygen 1.4.4