This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2gis_test.cpp
181 lines (166 loc) · 6.22 KB
/
2gis_test.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Test -- 2gis test utility
//
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <algorithm>
//#include <cstdint>
#include <stdint.h>
typedef uint32_t checksum_t ;
void usage()
{
std::cout << "Test -- 2gis test utility" << std::endl;
std::cout << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << " test -f <filename> -m words -v <word>" << std::endl;
std::cout << " test -f <filename> -m checksum" << std::endl;
std::cout << " test -h" << std::endl;
std::cout << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " -f <filename> filename for processing" << std::endl;
std::cout << " -m (words|checksum) mode (word occurence count or checksum generation)" << std::endl;
std::cout << " -v <word> word to count its occurences (case-sensitive)" << std::endl;
std::cout << " -h show this screen" << std::endl;
}
checksum_t checksum(std::ifstream &ifile)
{
checksum_t checksum = 0;
const std::streamsize BUF_SIZE = sizeof(checksum_t);
while(ifile)
{
checksum_t buf = 0;
ifile.read(reinterpret_cast<char*>(&buf), BUF_SIZE);
checksum += buf;
}
return checksum;
}
int_fast64_t wordcount(std::ifstream &ifile, const char* s)
{
size_t len = std::strlen(s);
assert(len > 0);
int_fast64_t cnt = 0;
size_t buf_size = (len < 4096)?4096:len;
char *buf = new char[buf_size+len];
char *match = buf - 1;
ifile.read(buf, len-1);
buf[ifile.gcount()] = 0;
while(ifile) {
ifile.read(buf+len-1, buf_size);
buf[len-1 + ifile.gcount()] = 0;
do {
match = std::strstr(match + 1, s);
cnt++;
} while (match != NULL);
cnt--;
}
return cnt;
}
int main(int argc, char* argv[])
{
std::ifstream ifile;
char *ifname = NULL;
char *search_str = NULL;
char *mode = NULL;
if (argc < 2){
std::cerr << "No input arguments specified, use -h for help." << std::endl;
return 1;
}
for (int loop = 1 ; loop < argc ; loop++) {
if (0 == std::strcmp(argv[loop], "-h")) {
usage();
return 0;
}
else if (0 == std::strcmp(argv[loop], "-f")) {
if (argc <= loop + 1) {
std::cerr << "No argument specified for "<< argv[loop] << std::endl;
return 1;
}
else {
if (ifname) {
std::cerr << "Duplicate "<< argv[loop] << " options are not allowed." << std::endl;
return 1;
}
else
ifname = argv[++loop];
if (!*ifname) {
std::cerr << "Empty filename is not allowed." << std::endl;
return 1;
}
}
}
else if (0 == std::strcmp(argv[loop], "-m")) {
if (argc <= loop + 1) {
std::cerr << "No argument specified for "<< argv[loop] << std::endl;
return 1;
}
else {
if (mode) {
std::cerr << "Duplicate "<< argv[loop] << " options are not allowed." << std::endl;
return 1;
}
else
mode = argv[++loop];
if (!*mode) {
std::cerr << "Empty mode is not allowed." << std::endl;
return 1;
}
if ( (0 != std::strcmp(mode, "checksum")) && (0 != std::strcmp(mode, "words")) ) {
std::cerr << "Incorrect mode specified, use -h for help." << std::endl;
return 1;
}
}
}
else if (0 == std::strcmp(argv[loop], "-v")) {
if (argc <= loop + 1) {
std::cerr << "No argument specified for "<< argv[loop] << std::endl;
return 1;
}
else {
if (search_str) {
std::cerr << "Duplicate "<< argv[loop] << " options are not allowed." << std::endl;
return 1;
}
else {
search_str = argv[++loop];
if (!*search_str) {
std::cerr << "Empty word is not allowed." << std::endl;
return 1;
}
}
}
}
else {
std::cerr << "Unknown option " << argv[loop] << ", use -h for help." << std::endl;
return 1;
}
}
if (!mode) {
std::cerr << "No mode specified, use -h for help." << std::endl;
return 1;
}
if (!ifname) {
std::cerr << "No input file specified, use -h for help." << std::endl;
return 1;
}
if (!search_str && (0 == std::strcmp(mode, "words"))) {
std::cerr << "Option -v is required in 'words' mode, use -h for help." << std::endl;
return 1;
}
if (search_str && (0 == std::strcmp(mode, "checksum"))) {
std::cerr << "Option -v is not allowed in 'checksum' mode, use -h for help." << std::endl;
return 1;
}
ifile.open(ifname, std::ios::binary);
if (!ifile.is_open()) {
std::cerr << "Couldn't open input file." << std::endl;
return 1;
}
if (0 == std::strcmp(mode, "checksum"))
std::cout << std::hex << std::setw(sizeof(checksum_t)*2) << std::setfill('0') << checksum(ifile) << std::endl;
else if (0 == std::strcmp(mode, "words"))
std::cout << wordcount(ifile, search_str) << std::endl;
ifile.close();
return 0;
}