Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

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 
00006 #include "nc.h"
00007 #include <stdlib.h>
00008 #include <string.h>
00009 #include <ctype.h>
00010 #include <assert.h>
00011 #include "ncx.h"
00012 #include "rnd.h"
00013 
00014 
00015 /*
00016  * Free string, and, if needed, its values.
00017  * Formerly
00018 NC_free_string()
00019  */
00020 void
00021 free_NC_string(NC_string *ncstrp)
00022 {
00023         if(ncstrp==NULL)
00024                 return;
00025         free(ncstrp);
00026 }
00027 
00028 
00029 /*
00030  * Verify that a name string is valid
00031  * CDL syntax, eg, all the characters are
00032  * alphanumeric, '-', '_', or '.'.
00033  */
00034 int
00035 NC_check_name(const char *name)
00036 {
00037         const char *cp = name;
00038         assert(name != NULL);
00039 
00040         if(*name == 0)
00041                 return NC_EBADNAME; /* empty names disallowed */
00042 
00043         for(; *cp != 0; cp++)
00044         {
00045                 int ch = *cp;
00046                 if(!isalnum(ch))
00047                 {
00048                         if(ch != '_' && ch != '-' && ch != '.')
00049                                 return NC_EBADNAME;
00050                 }
00051         }
00052         if(cp - name > NC_MAX_NAME)
00053                 return NC_EMAXNAME;
00054 
00055         return NC_NOERR;
00056 }
00057 
00058 
00059 /*
00060  * Allocate a NC_string structure large enough
00061  * to hold slen characters.
00062  * Formerly
00063 NC_new_string(count, str)
00064  */
00065 NC_string *
00066 new_NC_string(size_t slen, const char *str)
00067 {
00068         NC_string *ncstrp;
00069         size_t sz = M_RNDUP(sizeof(NC_string)) + slen + 1;
00070 
00071 #if 0
00072         sz = _RNDUP(sz, X_ALIGN);
00073 #endif
00074                 
00075         ncstrp = (NC_string *)malloc(sz);
00076         if( ncstrp == NULL )
00077                 return NULL;
00078         (void) memset(ncstrp, 0, sz);
00079 
00080         ncstrp->nchars = sz - M_RNDUP(sizeof(NC_string)) - 1;
00081         assert(ncstrp->nchars + 1 > slen);
00082         ncstrp->cp = (char *)ncstrp + M_RNDUP(sizeof(NC_string));
00083 
00084         if(str != NULL && *str != 0)
00085         {
00086                 (void) strncpy(ncstrp->cp, str, ncstrp->nchars +1);
00087                 ncstrp->cp[ncstrp->nchars] = 0;
00088         }
00089         
00090         return(ncstrp);
00091 }
00092 
00093 
00094 /*
00095  * If possible, change the value of an NC_string to 'str'.
00096  *
00097  * Formerly
00098 NC_re_string()
00099  */
00100 int
00101 set_NC_string(NC_string *ncstrp, const char *str)
00102 {
00103         size_t slen;
00104         size_t diff;
00105 
00106         assert(str != NULL && *str != 0);
00107 
00108         slen = strlen(str);
00109 
00110         if(ncstrp->nchars < slen)
00111                 return NC_ENOTINDEFINE;
00112 
00113         (void) memcpy(ncstrp->cp, str, slen);
00114         diff = ncstrp->nchars - slen;
00115         if(diff != 0)
00116                 (void) memset(ncstrp->cp + slen, 0, diff);
00117 
00118         return NC_NOERR;
00119 }
 

Powered by Plone

This site conforms to the following standards: