-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
72 lines (55 loc) · 1.45 KB
/
test.c
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
#include <stdio.h>
#include <string.h>
#include <CoreFoundation/CoreFoundation.h>
#include "fwrapdata.h"
/*
cc -c fwrapdata.c
cc -o test fwrapdata.o test.c -framework CoreFoundation
*/
void show_data(CFMutableDataRef data)
{
const UInt8 *contents = CFDataGetBytePtr(data);
CFIndex len = CFDataGetLength(data);
fputs("data=```", stdout);
fwrite(contents, len, 1, stdout);
fputs("```\n", stdout);
}
void encode_data(CFMutableDataRef data)
{
const UInt8 *contents = CFDataGetBytePtr(data);
CFIndex len = CFDataGetLength(data);
fputs("data=```", stdout);
for (int i = 0; i < len; ++i)
fprintf(stdout, "(%02x)", contents[i]);
fputs("```\n", stdout);
}
int
main (void)
{
int ch;
FILE *stream;
char buffer[8192];
CFMutableDataRef data = CFDataCreateMutable(NULL, 0);
stream = fwrapdata(data);
fprintf(stream, "Hello %s!\n", "world");
fflush(stream);
show_data(data);
fseek(stream, 0, SEEK_SET);
while ((ch = fgetc(stream)) != EOF)
printf ("Got %c\n", ch);
fseek(stream, 6, SEEK_SET);
fgets(buffer, sizeof(buffer), stream);
printf("Got \"%s\"\n", buffer);
fseek(stream, 6, SEEK_SET);
fputs("universe!\n", stream);
fflush(stream);
show_data(data);
fseek(stream, 0, SEEK_SET);
while ((ch = fgetc(stream)) != EOF)
printf ("Got %c\n", ch);
fseek(stream, 10, SEEK_END);
encode_data(data);
fclose (stream);
CFRelease(data);
return 0;
}