Browse code

core: add str_append helper function

Victor Seva authored on 28/04/2014 14:20:57
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,45 @@
0
+/**
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2014 Victor Seva <vseva@sipwise.com>
4
+ *
5
+ * This file is part of kamailio, a free SIP server.
6
+ *
7
+ * Permission to use, copy, modify, and distribute this software for any
8
+ * purpose with or without fee is hereby granted, provided that the above
9
+ * copyright notice and this permission notice appear in all copies.
10
+ *
11
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
+ */
19
+
20
+#include <string.h>
21
+#include "str.h"
22
+#include "mem/mem.h"
23
+
24
+int str_append(str *orig, str *suffix, str *dest)
25
+{
26
+	if(orig == NULL || suffix == NULL || suffix->len == 0 || dest == NULL)
27
+	{
28
+		LM_ERR("wrong parameters\n");
29
+		return -1;
30
+	}
31
+	dest->len = orig->len + suffix->len;
32
+	dest->s = pkg_malloc(sizeof(char)*dest->len);
33
+	if(dest->s==NULL)
34
+	{
35
+		LOG(L_ERR, "memory allocation failure\n");
36
+		return -1;
37
+	}
38
+	if(orig->len>0)
39
+	{
40
+		memcpy(dest->s, orig->s, orig->len);
41
+	}
42
+	memcpy(dest->s+orig->len, suffix->s, suffix->len);
43
+	return 0;
44
+}
... ...
@@ -121,4 +121,13 @@ typedef struct _str str;
121 121
 
122 122
 /** @} */
123 123
 
124
+/** Appends a sufffix
125
+ * @param orig is the original string
126
+ * @param suffix is the suffix string
127
+ * @param dest is the result ::str of appending suffix to orig
128
+ * @return 0 if ok -1 if error
129
+ * remember to free the dest->s private memory
130
+ */
131
+int str_append(str *orig, str *suffix, str *dest);
132
+
124 133
 #endif