"Fossies" - the Fresh Open Source Software Archive

Member "srg-1.3.6/include/getline.hh" (5 Aug 2009, 983 Bytes) of package /linux/privat/old/srg-1.3.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #ifndef GETLINE_HH
    2 #define GETLINE_HH
    3 #include <unistd.h>
    4 #include <sys/mman.h>
    5 #include <string.h>
    6 #include <stdio.h>
    7 #include <fcntl.h>
    8 
    9 class Line {
   10     private:
   11         int fd;
   12         char *buffer;
   13         char *ptr;
   14         int len;
   15         int error;
   16     public:
   17         Line(const char *filename) {
   18             fd=open(filename,O_RDONLY);
   19             error=0;
   20             if (fd<0) {
   21                 error=errno;
   22                 return;
   23             }
   24             len = lseek(fd,0,SEEK_END);
   25             if (len==-1) {
   26                 error=errno;
   27                 return;
   28             }
   29             ptr=buffer=(char*)mmap(0,len,PROT_READ,MAP_SHARED,fd,0);
   30             madvise(buffer,len,MADV_SEQUENTIAL);
   31             return;
   32         }
   33         int eof() { return !buffer || ptr>=(buffer+len); }
   34         int getError() { return error; }
   35 
   36         char *getline() {
   37             char *end;
   38             if (!buffer)
   39                 return NULL;
   40             end=ptr;
   41             while (end<=(buffer+len) && *end!='\x0a') {
   42                 end++;
   43             }
   44             char *ret;
   45             ret=(char *)malloc(end-ptr+2);
   46             memcpy(ret,ptr,end-ptr);
   47             ret[end-ptr]='\0';
   48             ptr=end+1;
   49             return ret;
   50         }
   51 
   52         ~Line() {
   53             munmap(buffer,len);
   54             close(fd);
   55         }
   56 
   57 };
   58 
   59 #endif