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
#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 (fd0) {
        return 1;
    }

    off_t res = lseek(fd0SEEK_END);
    if (res0) {
        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_itfalse;
                }
                begin = cp1;
            } else {
                if (!isalpha(*cp)) {
                    print_ittrue;
                }
            }
        }
    }
    catch (const MemObject::Exception& exc) {
        std::cout << "got " << exc.what() << std::endl;
    }
}