forked from brown-cs0330/lab06-alloc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_fns.c
More file actions
44 lines (38 loc) · 1.05 KB
/
string_fns.c
File metadata and controls
44 lines (38 loc) · 1.05 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "string_fns.h"
/* The following function signatures will be used for
* sets containing strings, where all void * pointers
* are really char *s.
*/
/* Test two strings for equality */
int string_equals(void *arg1, void *arg2) {
if (!arg1 || !arg2)
return !arg1 && !arg2;
else
return !strcmp((char *)arg1, (char *)arg2);
}
/* Create a dynamically-allocated copy of the
* given string, and return a pointer to it */
void *string_copy(void *arg) {
char *str = (char *)arg;
if (str) {
size_t len = strlen(str);
char *kopje = (char *)malloc(len + 1);
strncpy(kopje, str, len + 1);
return kopje;
} else {
return NULL;
}
}
/* Free the given string */
void string_delete(void *arg) { free(arg); }
/* Print a representation of the given string
* to the given stream */
void string_print(FILE *stream, void *arg) {
if (arg == NULL)
fprintf(stream, "null ");
else
fprintf(stream, "\"%s\" ", (char *)arg);
}