YAZ  5.23.1
nmemsdup.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14 
15 #include <string.h>
16 #include <yaz/nmem_xml.h>
17 
18 char *nmem_strdup(NMEM mem, const char *src)
19 {
20  char *dst = (char *) nmem_malloc(mem, strlen(src)+1);
21  strcpy(dst, src);
22  return dst;
23 }
24 
25 char *nmem_strdup_null(NMEM mem, const char *src)
26 {
27  if (!src)
28  return 0;
29  else
30  return nmem_strdup(mem, src);
31 }
32 
33 char *nmem_strdupn(NMEM mem, const char *src, size_t n)
34 {
35  char *dst = (char *) nmem_malloc(mem, n+1);
36  memcpy(dst, src, n);
37  dst[n] = '\0';
38  return dst;
39 }
40 
42 {
43  nmem_int_t *dst = (nmem_int_t*) nmem_malloc(mem, sizeof(*dst));
44  *dst = v;
45  return dst;
46 }
47 
49 {
50  nmem_bool_t *dst = (nmem_bool_t*) nmem_malloc(mem, sizeof(*dst));
51  *dst = v;
52  return dst;
53 }
54 
55 void nmem_strsplit_blank(NMEM nmem, const char *dstr, char ***darray, int *num)
56 {
57  nmem_strsplit(nmem, " ", dstr, darray, num);
58 }
59 
60 
61 void nmem_strsplit(NMEM nmem, const char *delim, const char *dstr,
62  char ***darray, int *num)
63 {
64  nmem_strsplitx(nmem, delim, dstr, darray, num, 1);
65 }
66 
67 void nmem_strsplitx(NMEM nmem, const char *delim, const char *dstr,
68  char ***darray, int *num, int collapse)
69 {
70  nmem_strsplit_escape(nmem, delim, dstr, darray, num, collapse, 0);
71 }
72 
73 void nmem_strsplit_escape(NMEM nmem, const char *delim, const char *dstr,
74  char ***darray, int *num, int collapse,
75  int escape_char)
76 {
77  nmem_strsplit_escape2(nmem, delim, dstr, darray, num, collapse,
78  escape_char, 1);
79 }
80 
81 void nmem_strsplit_escape2(NMEM nmem, const char *delim, const char *dstr,
82  char ***darray, int *num, int collapse,
83  int escape_char, int subst_escape)
84 {
85  *darray = 0;
86  /* two passes over the input string.. */
87  while (1)
88  {
89  size_t i = 0;
90  const char *cp = dstr;
91  while (1)
92  {
93  const char *cp0;
94  if (collapse)
95  {
96  if (!*cp)
97  break;
98  while (*cp && strchr(delim, *cp) && *cp != escape_char)
99  cp++;
100  if (!*cp)
101  break;
102  }
103 
104  cp0 = cp;
105  while (*cp && !strchr(delim, *cp))
106  {
107  if (*cp == escape_char)
108  cp++;
109  cp++;
110  }
111  if (*darray)
112  {
113  (*darray)[i] = nmem_strdupn(nmem, cp0, cp - cp0);
114  if (subst_escape)
115  {
116  char *dst = (*darray)[i];
117  const char *src = dst;
118  while (*src != '\0')
119  {
120  if (*src == escape_char && src[1])
121  src++;
122  *dst++ = *src++;
123  }
124  *dst = '\0';
125  }
126  }
127  i++;
128  if (!collapse)
129  {
130  if (!*cp)
131  break;
132  cp++;
133  }
134  }
135  *num = i;
136  if (!*num)
137  break; /* no items, so stop, *darray=0 already */
138  else if (*darray)
139  break; /* second pass, stop */
140  *darray = (char **) nmem_malloc(nmem, *num * sizeof(**darray));
141  }
142 }
143 
144 #if YAZ_HAVE_XML2
145 char *nmem_text_node_cdata(const xmlNode *ptr_cdata, NMEM nmem)
146 {
147  char *cdata;
148  int len = 0;
149  const xmlNode *ptr;
150 
151  for (ptr = ptr_cdata; ptr; ptr = ptr->next)
152  if (ptr->type == XML_TEXT_NODE)
153  len += xmlStrlen(ptr->content);
154  cdata = (char *) nmem_malloc(nmem, len+1);
155  *cdata = '\0';
156  for (ptr = ptr_cdata; ptr; ptr = ptr->next)
157  if (ptr->type == XML_TEXT_NODE)
158  strcat(cdata, (const char *) ptr->content);
159  return cdata;
160 }
161 #endif
162 
163 /*
164  * Local variables:
165  * c-basic-offset: 4
166  * c-file-style: "Stroustrup"
167  * indent-tabs-mode: nil
168  * End:
169  * vim: shiftwidth=4 tabstop=8 expandtab
170  */
171 
void nmem_strsplit(NMEM nmem, const char *delim, const char *dstr, char ***darray, int *num)
allocates sub strings out of string using certain delimitors
Definition: nmemsdup.c:61
Header for Nibble Memory functions + Libxml2 specific stuff.
void nmem_strsplit_blank(NMEM nmem, const char *dstr, char ***darray, int *num)
splits string into sub strings delimited by blanks
Definition: nmemsdup.c:55
void nmem_strsplit_escape2(NMEM nmem, const char *delim, const char *dstr, char ***darray, int *num, int collapse, int escape_char, int subst_escape)
allocates sub strings out of string using certain delimitors
Definition: nmemsdup.c:81
void * nmem_malloc(NMEM n, size_t size)
allocates memory block on NMEM handle
Definition: nmem.c:145
long long int nmem_int_t
Definition: nmem.h:59
char * nmem_strdup_null(NMEM mem, const char *src)
allocates string on NMEM handle - allows NULL ptr buffer
Definition: nmemsdup.c:25
char * nmem_strdup(NMEM mem, const char *src)
allocates string on NMEM handle (similar strdup)
Definition: nmemsdup.c:18
nmem_int_t * nmem_intdup(NMEM mem, nmem_int_t v)
allocates and sets integer for NMEM
Definition: nmemsdup.c:41
int nmem_bool_t
BER/utility boolean.
Definition: nmem.h:71
char * nmem_text_node_cdata(const xmlNode *ptr_cdata, NMEM nmem)
copies TEXT Libxml2 node data to NMEM
Definition: nmemsdup.c:145
nmem_bool_t * nmem_booldup(NMEM mem, nmem_bool_t v)
allocates and sets boolean for NMEM
Definition: nmemsdup.c:48
void nmem_strsplitx(NMEM nmem, const char *delim, const char *dstr, char ***darray, int *num, int collapse)
allocates sub strings out of string using certain delimitors
Definition: nmemsdup.c:67
void nmem_strsplit_escape(NMEM nmem, const char *delim, const char *dstr, char ***darray, int *num, int collapse, int escape_char)
allocates sub strings out of string using certain delimitors
Definition: nmemsdup.c:73
char * nmem_strdupn(NMEM mem, const char *src, size_t n)
allocates string of certain size on NMEM handle
Definition: nmemsdup.c:33