#include "readlines.h" // C includes #include #include #include #include #include std::vector readlines(const char *filename){ int fd = open(filename, O_RDONLY); if(fd < 0){ perror(filename); return std::vector(); } struct stat sb; fstat(fd, &sb); char *mem = (char*)mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); std::vector lines; size_t line_beginning = 0; for(size_t i = 0; i < sb.st_size; i++){ if(mem[i] == '\n'){ // add a line that begins at line_beginning and ends at i lines.push_back(std::string(mem + line_beginning, i - line_beginning)); line_beginning = i + 1; } } munmap(mem, sb.st_size); close(fd); return lines; }