-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
116 lines (77 loc) · 2.44 KB
/
example.c
File metadata and controls
116 lines (77 loc) · 2.44 KB
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
/**
** example.c
**
** String library with dynamic memory handling
** https://github.com/heinyx/dynamem
**
** Author: Dipl.-Ing. Thomas Heiny, Wolfsburg, Germany
** Version: 07.01.2018
**
**/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include "dynamem.h"
int main(int argc, char **argv)
{
/* Examples mstrdup and mstrdup_replace */
char *s1, *s2;
s1 = mstrdup("key=%s value=%d", "version", 1);
s2 = mstrdup_replace(s1, "key", "k");
printf("strlen(s1)=%ld s1=>%s<\n", strlen(s1), s1);
printf("strlen(s2)=%ld s2=>%s<\n", strlen(s2), s2);
if (s1) free(s1);
if (s2) free(s2);
/* Examples mstrcpy and mreplace */
char *s3 = NULL;
size_t i;
i = mstrcpy(&s3, "dynamem library");
printf("p=%p i=%ld s3=>%s<\n", s3, i, s3);
i = mstrcpy(&s3, "dynamem library %s %d %s", "allocates memory", 4, "strings");
printf("p=%p i=%ld s3=>%s<\n", s3, i, s3);
i = mreplace(&s3, "4", "for");
printf("p=%p i=%ld s3=>%s<\n", s3, i, s3);
i = mreplace(&s3, "strings", "my strings");
printf("p=%p i=%ld s3=>%s<\n", s3, i, s3);
/* Example mstrcat */
mstrcat(&s3, " automatically!");
printf("p=%p s3=>%s<\n", s3, s3);
/* Example moccurence */
printf("number of 'string' in s3=%ld\n", moccurence(s3, "string"));
/* Example msubstring */
i = msubstring(&s3, 8, 31);
printf("p=%p i=%ld s3=>%s<\n", s3, i, s3);
/* Example mstrlen */
i = mstrlen("1234%d", 5678);
printf("strlen('12345678')=%ld\n", i);
/* Example msplitdup */
char *buff = NULL;
char *word1 = msplitdup(s3, " ", &buff);
char *word2 = msplitdup(NULL, " ", &buff);
char *word3 = msplitdup(NULL, " ", &buff);
printf("p=%p word1=>%s<\n", word1, word1);
printf("p=%p word2=>%s<\n", word2, word2);
printf("p=%p word3=>%s<\n", word3, word3);
printf("p=%p s3=>%s<\n", s3, s3);
if (word1) free(word1);
if (word2) free(word2);
if (word3) free(word3);
if (s3) free(s3);
/* Example msplitarray */
carray split;
split = msplitarray("splitted by the msplitarray function", " ");
for (i = 0; i < split.len; i++) printf("p=%p split=>%s<\n", split.arr[i], split.arr[i]);
free_carray(&split);
/* Example carray */
carray carr = create_carray();
insert_carray(&carr, "first entry");
insert_carray(&carr, "second entry");
char *entry;
i = 0;
while ((entry = select_carray(&carr, &i)) != NULL) printf("carr entry=>%s<\n", entry);
printf("length(carr) = %ld\n", len_carray(&carr));
close_carray(&carr);
return(0);
}