00001
00002
00003
00004
00005
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
00018
00019
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
00032
00033
00034
00035
00036
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;
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
00066
00067
00068
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
00101
00102
00103
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 }