ocarina/tests/src/filter.cpp

99 lines
1.5 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
* Test the filtering code
*/
#include <file.h>
#include <filter.h>
#include <print.h>
#include <string>
#include <stdlib.h>
#include <unistd.h>
enum action_t { ADD, LOWERCASE, SEARCH };
void add_text(const std::string &text)
{
std::string lc = filter :: add(text, 0);
print("%s\n", lc.c_str());
}
void to_lowercase(const std::string &text)
{
std::string lc = filter :: lowercase(text);
print("%s\n", lc.c_str());
}
void read_file(unsigned int n)
{
File f("filter.txt");
if (f.open(OPEN_READ)) {
for (unsigned int i = 0; i < n; i++) {
std::string text = f.getline();
filter :: add(text, i);
}
f.close();
}
}
void do_search(const std::string &text)
{
std::set<unsigned int> res;
std::set<unsigned int>::iterator it;
filter :: search(text, res);
it = res.begin();
if (it == res.end())
return;
print("%u", *it);
for (it++; it != res.end(); it++)
print(" %u", *it);
print("\n");
}
int main(int argc, char **argv)
{
char c;
unsigned int n;
action_t action = ADD;
while ((c = getopt(argc, argv, "als:")) != -1) {
switch (c) {
case 'a':
action = ADD;
break;
case 'l':
action = LOWERCASE;
break;
case 's':
action = SEARCH;
n = atoi(optarg);
break;
}
}
std::string text;
for (int i = optind; i < argc; i++) {
text += " ";
text += argv[i];
}
switch (action) {
case ADD:
add_text(text);
break;
case LOWERCASE:
to_lowercase(text);
break;
case SEARCH:
read_file(n);
do_search(text);
break;
}
return 0;
}