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
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
// $Id: module.pmod.in,v 1.3 2002/11/24 22:14:39 agehall Exp $ 
#pike __REAL_VERSION__ 
 
inherit @module@; 
 
 
 
//! 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 
//! An important limitation on this class is that it may only be used 
//! for reading @b{or@} writing, not both at the same time. 
//! Please also note that if you want to reopen a file for reading 
//! after a write, you must close the file before calling open or 
//! strange effects might be the result. 
class File { 
  inherit @module@._file; 
 
  private int is_open = 0; 
 
  //! @decl void create(void|string|int file, void|string mode) 
  //! @param file 
  //! Filename or filedescriptor of the gzip file to open. 
  //! @param mode 
  //! mode for the file. Defaults to "rb". 
  //! @seealso 
  //!   @[open] @[Stdio.File] 
  void create(mixed ... args) { 
    ::create(); 
 
    if(sizeof(args)) { 
      open(@args); 
    } 
  } 
 
  string _sprintf(int t) 
  { 
    switch(t) { 
    case 'O': 
      return sprintf("Gz.File(/*%s open */)", is_open ? "" : " not"); 
    case 't': 
      return "Gz.File"; 
    default: 
      return ""; 
    } 
  } 
 
  int close() 
  { 
    is_open = 0; 
    return ::close(); 
  } 
 
  void destroy() { 
    close(); 
  } 
 
  //! @param file 
  //! Filename or filedescriptor of the gzip file to open. 
  //! @param mode 
  //! mode for the file. Defaults to "rb". 
  //! May be one of the following: 
  //! @dl 
  //! @item rb 
  //! read mode 
  //! @item wb 
  //! write mode 
  //! @item ab 
  //! append mode 
  //! @enddl 
  //! For the wb and ab mode, additional parameters may 
  //! be specified. Please se zlib manual for more info. 
  //! @returns  
  //!  non-zero if successful. 
  int open(string|int file, void|string mode) { 
    string open_mode="rb"; 
    if (is_open) { 
      ::close(); 
    } 
 
    if (stringp(mode)) { 
      open_mode = lower_case(mode); 
    } 
 
    is_open = ::open(file, open_mode); 
    return is_open; 
  } 
 
  //! Reads data from the file. 
  //! If no argument is given, the whole file is read. 
  int|string read(void|int length) { 
    string ret = ""; 
 
    if (!is_open) { 
      return 0; 
    } 
 
    if (!zero_type(length)) 
      return ::read(length); 
    else 
      while(!eof()) { 
        ret += ::read(1024*64); 
      } 
    return ret; 
  } 
}