pike.git / src / modules / Gz / module.pmod.in

version» Context lines:

pike.git/src/modules/Gz/module.pmod.in:1:   // -*- pike -*-   #pike __REAL_VERSION__ + #require constant(@module@)    - #if constant(@module@) -  +    inherit @module@;    -  + #define DATA_CHUNK_SIZE 64*1024 +    //! Low-level implementation of read/write support for GZip files   class _file {       protected private Stdio.Stream f;    protected private inflate inf;    protected private deflate def; -  protected private int level, strategy; +  protected private int level, strategy, window_size;    protected private string read_buf;    protected private int file_pos, crc, write_mode, at_eof;       final constant SEEK_SET = 0;    final constant SEEK_CUR = 1;    final constant SEEK_END = 2;       protected int check_header(string|void buf)    {    int magic1, magic2, method, flags, len;
pike.git/src/modules/Gz/module.pmod.in:77:    return f->write(sprintf("%1c%1c%1c%1c%4c%1c%1c",    0x1f, 0x8b, 8, 0, 0, 0, 3)) == 10;    }       //! Opens a file for I/O.    //!    //! @param file    //! The filename or an open filedescriptor or Stream for the GZip    //! file to use.    //! @param mode -  //! Mode for the fileoperations. Defaults to read only. +  //! Mode for the file operations. Defaults to read only. The +  //! following mode characters are unique to Gz.File.    //! -  +  //! @string +  //! @value "0" +  //! @value "1" +  //! @value "2" +  //! @value "3" +  //! @value "4" +  //! @value "5" +  //! @value "6" +  //! @value "7" +  //! @value "8" +  //! @value "9" +  //! Values 0 to 9 set the compression level from no +  //! compression to maximum available compression. Defaults to +  //! 6. +  //! @value "f" +  //! Sets the compression strategy to @[FILTERED]. +  //! @value "h" +  //! Sets the compression strategy to @[HUFFMAN_ONLY]. +  //! @endstring +  //!    //! @note    //! If the object already has been opened, it will first be closed.    int open(string|int|Stdio.Stream file, void|string mode)    {    close();    write_mode = 0;    level = 6;    strategy = DEFAULT_STRATEGY; -  +  window_size = 15;    if(mode) {    mode = filter(mode, lambda(int n) {    if(n == 'w' || n == 'a')    write_mode = 1;    if(n >= '0' && n <= '9')    level = n - '0';    else if(n == 'f')    strategy = FILTERED;    else if(n == 'h')    strategy = HUFFMAN_ONLY;
pike.git/src/modules/Gz/module.pmod.in:195:    read_buf = read_buf[len..];    file_pos += sizeof(res);    return res;    }       //! Writes the data to the file.    //! @returns    //! the number of bytes written to the file.    int write(string data)    { -  if(!def) def = deflate(-level, strategy); +  if(!def) def = deflate(-level, strategy, window_size);    string comp = def->deflate(data, NO_FLUSH);    if(f->write(comp) != sizeof(comp))    return 0;    else {    file_pos += sizeof(data);    crc = crc32(data, crc);    return sizeof(data);    }    }   
pike.git/src/modules/Gz/module.pmod.in:276:    //! 1 if EOF has been reached.    int(0..1) eof()    {    if(at_eof) return 1;    if(def || write_mode || sizeof(read_buf)) return 0;    while(!sizeof(read_buf) && fill_read_buffer())    ;    return at_eof;    }    -  //! Sets the encoding level and strategy -  //! @param level -  //! Level of the compression. -  //! 0 is the least compression, 9 is max. 8 is default. -  //! @param strategy -  //! Set strategy for encoding to one of the following: -  //! DEFAULT_STRATEGY -  //! FILTERED -  //! HUFFMAN_ONLY -  int setparams(int level, int strategy) -  { -  if(def) { +  //! Sets the encoding level, strategy and window_size. +  //! +  //! @seealso +  //! @[Gz.deflate] +  int setparams(void|int(0..9) level, void|int strategy, +  void|int(8..15) window_size) { +  if (def) {    string s = def->deflate("", SYNC_FLUSH); -  if(sizeof(s) && f->write(s) != sizeof(s)) +  if (sizeof(s) && f->write(s) != sizeof(s))    return 0;    def = 0;    } -  +  if (!zero_type(level))    _file::level = level; -  +  if (!zero_type(strategy))    _file::strategy = strategy; -  +  if (!zero_type(window_size)) +  _file::window_size = window_size;    return 1;    }      }         //! Allows the user to open a Gzip archive and read and write   //! it's contents in an uncompressed form, emulating the @[Stdio.File]   //! interface.   //! @note
pike.git/src/modules/Gz/module.pmod.in:349:    return UNDEFINED;    }    }       int close()    {    is_open = 0;    return ::close();    }    -  protected void destroy() { +  protected void _destruct() {    close();    }       //! @param file    //! Filename or filedescriptor of the gzip file to open, or an already    //! open Stream.    //! @param mode    //! mode for the file. Defaults to "rb".    //! May be one of the following:    //! @dl
pike.git/src/modules/Gz/module.pmod.in:398:    if (!is_open) {    return 0;    }       if (!zero_type(length))    return ::read(length);       String.Buffer buf = String.Buffer();    string data;    do { -  if (!(data = ::read(1024*64))) break; +  if (!(data = ::read(DATA_CHUNK_SIZE))) break;    buf->add(data);    } while (sizeof(data));    return (string)buf;    } -  +  +  +  function(:string) read_function(int nbytes) +  //! Returns a function that when called will call @[read] with +  //! nbytes as argument. Can be used to get various callback +  //! functions, eg for the fourth argument to +  //! @[String.SplitIterator]. +  { +  return lambda(){ return ::read(nbytes); };    }    - #else - constant this_program_does_not_exist = 1; - #endif // constant(@module@) +  String.SplitIterator|Stdio.LineIterator line_iterator( int|void trim ) +  //! Returns an iterator that will loop over the lines in this file. +  //! If trim is true, all @tt{'\r'@} characters will be removed from +  //! the input. +  { +  if( trim ) +  return String.SplitIterator( "",(<'\n','\r'>),1, +  read_function(DATA_CHUNK_SIZE)); +  // This one is about twice as fast, but it's way less flexible. +  return Stdio.LineIterator( read_function(DATA_CHUNK_SIZE) ); +  } + }