Browse Source

Add string module

Ilja Kartašov 11 months ago
parent
commit
35c8b96967
2 changed files with 165 additions and 0 deletions
  1. 104
    0
      string.c
  2. 61
    0
      string.h

+ 104
- 0
string.c View File

@@ -0,0 +1,104 @@
1
+/******************************************************************************
2
+ *
3
+ *                Copyright (c) 2017-2019 by Löwenware Ltd
4
+ *             Please, refer LICENSE file for legal information
5
+ *
6
+ ******************************************************************************/
7
+
8
+/**
9
+ * @file string.c
10
+ * @author Ilja Kartašov <ik@lowenware.com>
11
+ * @brief cStuff string module implementation
12
+ *
13
+ * @see https://lowenware.com/
14
+ */
15
+
16
+#include <stdio.h>
17
+#include <stdarg.h>
18
+#include <stdlib.h>
19
+#include <string.h>
20
+
21
+#include "string.h"
22
+
23
+int
24
+cstuff_strcpy(char **out, const char *in)
25
+{
26
+	return cstuff_strncpy(out, in, strlen(in));
27
+}
28
+
29
+
30
+int
31
+cstuff_strncpy(char **out, const char *in, int len)
32
+{
33
+	char *s;
34
+ 
35
+	if (!(s = malloc( len + 1 )))
36
+		return -1;
37
+
38
+	strncpy(s, in, len);
39
+	s[len]=0;
40
+
41
+	*out = s;
42
+
43
+	return len;
44
+}
45
+
46
+
47
+int
48
+cstuff_strset(char **out, const char *in)
49
+{
50
+	return cstuff_strnset(out, in, strlen(in));
51
+}
52
+
53
+
54
+int
55
+cstuff_strnset(char **out, const char *in, int len)
56
+{
57
+	char *s;
58
+
59
+	if (!(s = realloc(*out, len+1)))
60
+		return -1;
61
+
62
+	strncpy(s, in, len);
63
+	s[len] = 0;
64
+
65
+	*out = s;
66
+
67
+	return len;
68
+}
69
+
70
+
71
+int
72
+cstuff_sprintf(char **out, const char *format, ...)
73
+{
74
+	int result;
75
+	va_list vl;
76
+
77
+	va_start(vl, format);
78
+	result = cstuff_vsprintf(out, format, vl);
79
+	va_end(vl);
80
+
81
+	return result;
82
+}
83
+
84
+
85
+int
86
+cstuff_vsprintf(char **out, const char *format, va_list args)
87
+{
88
+	int result;
89
+	va_list vc;
90
+	char tmp, *s;
91
+
92
+	va_copy(vc, args);
93
+	result = vsnprintf(&tmp, 1, format, vc);
94
+	va_end(vc);
95
+
96
+	if (!(s = malloc(result + 1)))
97
+		return -1;
98
+
99
+	*out = s;
100
+	result = vsnprintf(s, result+1, format, args); 
101
+
102
+	return result;
103
+}
104
+

+ 61
- 0
string.h View File

@@ -0,0 +1,61 @@
1
+/******************************************************************************
2
+ *
3
+ *                Copyright (c) 2017-2019 by Löwenware Ltd
4
+ *             Please, refer LICENSE file for legal information
5
+ *
6
+ ******************************************************************************/
7
+
8
+/**
9
+ * @file string.h
10
+ * @author Ilja Kartašov <ik@lowenware.com>
11
+ * @brief String module declarations
12
+ *
13
+ * @see https://lowenware.com/
14
+ */
15
+
16
+#ifndef CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87
17
+#define CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87
18
+
19
+#include <stdarg.h>
20
+#include "retcode.h"
21
+
22
+
23
+/** @brief Copies in string to newly allocated out buffer
24
+ *  @param out a pointer to an address where new pointer must be stored
25
+ *  @param in an input string
26
+ *  @return length of string or -1 if out of memory
27
+ */
28
+int
29
+cstuff_strcpy(char **out, const char *in);
30
+
31
+
32
+int
33
+cstuff_strncpy(char **out, const char *in, int len);
34
+
35
+
36
+/** @brief Overwrites content of out buffer with in string using realloc
37
+ *  @param out a pointer to an address where new string must be stored
38
+ *  @param in an input string
39
+ *  @return length of string or -1 if out of memory
40
+ */
41
+int
42
+cstuff_strset(char **out, const char *in);
43
+
44
+
45
+int
46
+cstuff_strnset(char **out, const char *in, int len);
47
+
48
+
49
+/** @brief Allocates memory and prints a formatted string into it
50
+ *  @param out a pointer to an address where new string must be stored
51
+ *  @param format a format for string (see manual for stdlib sprintf)
52
+ *  @return length of string or -1 if out of memory
53
+ */
54
+int
55
+cstuff_sprintf(char **out, const char *format, ...);
56
+
57
+
58
+int
59
+cstuff_vsprintf(char **out, const char *format, va_list args);
60
+
61
+#endif /* !CSTUFF_STRING_H */

Loading…
Cancel
Save