#include <cctype>
#include <fcntl.h> #include <iostream> #include <iterator> #include <sys/mman.h> #include <unistd.h> #include "MemObject.hpp" int main() { int fd = open("/usr/dict/words", O_RDONLY); if (fd < 0) { return 1; } off_t res = lseek(fd, 0, SEEK_END); if (res < 0) { return 1; } size_t len = res; const MemObject words{fd, /* offset = */ 0, /* size = */ len, PROT_READ, MAP_SHARED}; const char* words_begin = &words.access<char>(0); const char* begin = words_begin; bool print_it = false; try { for (const char* cp = words_begin; cp < words_begin + len; ++cp) { if (*cp == '\n') { if (print_it) { std::copy(begin, cp+1, std::ostream_iterator<char>(std::cout)); print_it = false; } begin = cp + 1; } else { if (!isalpha(*cp)) { print_it = true; } } } } catch (const MemObject::Exception& exc) { std::cout << "got " << exc.what() << std::endl; } } |