File: string.c

package info (click to toggle)
vtk 5.0.4-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 51,084 kB
  • ctags: 70,426
  • sloc: cpp: 524,166; ansic: 220,276; tcl: 43,377; python: 14,037; perl: 3,102; java: 1,436; yacc: 1,033; sh: 339; lex: 248; makefile: 197; asm: 154
file content (120 lines) | stat: -rw-r--r-- 2,204 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 *  Copyright 1996, University Corporation for Atmospheric Research
 *      See netcdf/COPYRIGHT file for copying and redistribution conditions.
 */
/* $Id: string.c,v 1.1 2005/07/15 21:56:39 andy Exp $ */

#include "nc.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "ncx.h"
#include "rnd.h"


/*
 * Free string, and, if needed, its values.
 * Formerly
NC_free_string()
 */
void
free_NC_string(NC_string *ncstrp)
{
  if(ncstrp==NULL)
    return;
  free(ncstrp);
}


/*
 * Verify that a name string is valid
 * CDL syntax, eg, all the characters are
 * alphanumeric, '-', '_', or '.'.
 */
int
NC_check_name(const char *name)
{
  const char *cp = name;
  assert(name != NULL);

  if(*name == 0)
    return NC_EBADNAME; /* empty names disallowed */

  for(; *cp != 0; cp++)
  {
    int ch = *cp;
    if(!isalnum(ch))
    {
      if(ch != '_' && ch != '-' && ch != '.')
        return NC_EBADNAME;
    }
  }
  if(cp - name > NC_MAX_NAME)
    return NC_EMAXNAME;

  return NC_NOERR;
}


/*
 * Allocate a NC_string structure large enough
 * to hold slen characters.
 * Formerly
NC_new_string(count, str)
 */
NC_string *
new_NC_string(size_t slen, const char *str)
{
  NC_string *ncstrp;
  size_t sz = M_RNDUP(sizeof(NC_string)) + slen + 1;

#if 0
  sz = _RNDUP(sz, X_ALIGN);
#endif
    
  ncstrp = (NC_string *)malloc(sz);
  if( ncstrp == NULL )
    return NULL;
  (void) memset(ncstrp, 0, sz);

  ncstrp->nchars = sz - M_RNDUP(sizeof(NC_string)) - 1;
  assert(ncstrp->nchars + 1 > slen);
  ncstrp->cp = (char *)ncstrp + M_RNDUP(sizeof(NC_string));

  if(str != NULL && *str != 0)
  {
    (void) strncpy(ncstrp->cp, str, ncstrp->nchars +1);
    ncstrp->cp[ncstrp->nchars] = 0;
  }
  
  return(ncstrp);
}


/*
 * If possible, change the value of an NC_string to 'str'.
 *
 * Formerly
NC_re_string()
 */
int
set_NC_string(NC_string *ncstrp, const char *str)
{
  size_t slen;
  size_t diff;

  assert(str != NULL && *str != 0);

  slen = strlen(str);

  if(ncstrp->nchars < slen)
    return NC_ENOTINDEFINE;

  (void) memcpy(ncstrp->cp, str, slen);
  diff = ncstrp->nchars - slen;
  if(diff != 0)
    (void) memset(ncstrp->cp + slen, 0, diff);

  return NC_NOERR;
}