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