-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
114 lines (104 loc) · 3.43 KB
/
example.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
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
/*
Build
#Windows MSVC
cl -D_CRT_SECURE_NO_WARNINGS -W4 User32.Lib crosslz.c example.c /Feexample.exe
#GCC(Linux, MinGW, Cygwin, MSYS2)
gcc -Wall crosslz.c example.c -o example
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crosslz.h"
int main (int argc, char **argv)
{
clz_FILE *pFile;
if (argc < 3) {
printf ("Write regular text file: %s wt txtfile.txt\n", argv[0]);
printf ("Write compressed text file: %s wt txtfile.clz\n", argv[0]);
printf ("Append regular text file: %s at txtfile.txt\n", argv[0]);
printf ("Append compressed text file: %s at txtfile.clz\n", argv[0]);
printf ("Read regular text file: %s rt txtfile.txt\n", argv[0]);
printf ("Read compressed text file: %s rt txtfile.clz\n", argv[0]);
printf ("Write regular bin file: %s wb binfile.bin\n", argv[0]);
printf ("Write compressed bin file: %s wb binfile.clz\n", argv[0]);
printf ("Append regular bin file: %s ab binfile.bin\n", argv[0]);
printf ("Append compressed bin file: %s ab binfile.clz\n", argv[0]);
printf ("Read regular bin file: %s rb binfile.bin\n", argv[0]);
printf ("Read compressed bin file: %s rb binfile.clz\n", argv[0]);
return 0;
}
char *file = argv[2];
char *mode = argv[1];
char buf[1024];
int len;
if (strstr(mode, "wt") || strstr(mode, "at")) {
if (strstr(file, ".clz")) {
printf ("Write/Append compressed text file: %s\n", file);
pFile = clz_fopen (file, mode, CLZ_ALG_DFT, CLZ_BLK_DFT);
} else {
printf ("Write/Append regular text file: %s\n", file);
pFile = clz_fopen (file, mode, 0, 0);
}
if (NULL == pFile) {
printf ("Can't open file %s\n", file);
return -1;
}
for (int i=0; i<100000; ++i) {
len = clz_fprintf (pFile, "count number: %06d\n", i);
if (i == 0) {
printf ("Each fprintf: %d\n", len);
}
}
clz_fclose (pFile);
} else if (strstr(mode, "rt")) {
printf ("Read regular/compressed text file: %s\n", file);
pFile = clz_fopen (file, "rt", 0, 0);
if (NULL == pFile) {
printf ("Can't open file %s\n", file);
return -1;
}
len = clz_fgets (buf, sizeof(buf), pFile);
printf ("First row(%d): %s", len, buf);
while ((len = clz_fgets (buf, sizeof(buf), pFile)) > 0) {
}
printf ("Last row: %s", buf);
clz_fclose (pFile);
} else if (strstr(mode, "wb") || strstr(mode, "ab")) {
if (strstr(file, ".clz")) {
printf ("Write/Append compressed bin file: %s\n", file);
pFile = clz_fopen (file, mode, CLZ_ALG_DFT, CLZ_BLK_DFT);
} else {
printf ("Write regular bin file: %s\n", file);
pFile = clz_fopen (file, mode, 0, 0);
}
if (NULL == pFile) {
printf ("Can't open file %s\n", file);
return -1;
}
for (int i=0; i<100000; ++i) {
len = sprintf (buf, "count number: %06d\n", i);
len = clz_fwrite (buf, 1, len, pFile);
if (i == 0) {
printf ("Each clz_fwrite: %d\n", len);
}
}
clz_fclose (pFile);
} else if (strstr(mode, "rb")) {
printf ("Read compressed/regular bin file: %s\n", file);
pFile = clz_fopen (file, "rb", 0, 0);
if (NULL == pFile) {
printf ("Can't open file %s\n", file);
return -1;
}
buf[21] = '\0';
len = clz_fread (buf, 1, 21, pFile);
printf ("First row(%d): %s", len, buf);
while ((len = clz_fread (buf, 1, 21, pFile)) > 0) {
}
printf ("Last row: %s", buf);
clz_fclose (pFile);
} else {
printf ("Unkown mode: %s\n", mode);
}
return 0;
}