on master help sign in
Add strict and lazy string support to C fileread

--HG-- extra : convert_revision : 8a114f72ea67b3f0e9bc23ba2fbda9b6be6a8dda

commitf8aa5302ed6a258ebcbd12bd6c4e4587e8d8e5ed authorBryan O'Sullivan <bos@serpentine.com> dateTue 17 Aug 2010, 03:21 (16 years ago) parent8b56910
1 file changed+39−1
tests/benchmarks/fileread.c+39−1
tests/benchmarks/fileread.c+39−1
@@ -3,6 +3,22 @@
33#include <stdlib.h>
44#include <string.h>
55
6+void lazystring(const char *name)
7+{
8+ FILE *ufp = fopen(name, "r");
9+ const size_t bufsize = sizeof(char) * 32 * 1024;
10+ char *str = malloc(bufsize);
11+ long len = 0;
12+ int32_t n;
13+
14+ do {
15+ n = fread(str, sizeof(char), bufsize, ufp);
16+ len += n;
17+ } while (n > 0);
18+
19+ printf("%ld\n", len);
20+}
21+
622void lazytext(const char *name)
723{
824 UFILE *ufp = u_fopen(name, "r", NULL, "UTF-8");
@@ -38,6 +54,24 @@ void text(const char *name)
3854 printf("%d\n", n);
3955}
4056
57+void string(const char *name)
58+{
59+ FILE *fp = fopen(name, "r");
60+ char *str;
61+ long fsize;
62+ int32_t n;
63+
64+ fseek(fp, 0, SEEK_END);
65+ fsize = ftell(fp);
66+ fseek(fp, 0, SEEK_SET);
67+
68+ str = malloc(sizeof(*str) * fsize);
69+
70+ n = fread(str, sizeof(char), fsize, fp);
71+
72+ printf("%d\n", n);
73+}
74+
4175int main(int argc, char **argv)
4276{
4377 if (argc != 3) {
@@ -45,8 +79,12 @@ int main(int argc, char **argv)
4579 exit(1);
4680 }
4781
48− if (strcmp(argv[1], "lazytext") == 0)
82+ if (strcmp(argv[1], "lazystring") == 0)
83+ lazystring(argv[2]);
84+ else if (strcmp(argv[1], "lazytext") == 0)
4985 lazytext(argv[2]);
86+ else if (strcmp(argv[1], "string") == 0)
87+ string(argv[2]);
5088 else if (strcmp(argv[1], "text") == 0)
5189 text(argv[2]);
5290 else {