File: zed.cpp

package info (click to toggle)
plink 1.07%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: cpp: 72,375; makefile: 123; sh: 12
file content (251 lines) | stat: -rw-r--r-- 4,639 bytes parent folder | download | duplicates (7)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251


//////////////////////////////////////////////////////////////////
//                                                              //
//           PLINK (c) 2005-2009 Shaun Purcell                  //
//                                                              //
// This file is distributed under the GNU General Public        //
// License, Version 2.  Please see the file COPYING for more    //
// details                                                      //
//                                                              //
//////////////////////////////////////////////////////////////////


#include <iostream>
#include <string>
#include <vector>

#include "zed.h"
#include "helper.h"
#include "nlist.h"

extern Plink * PP;

ZInput::ZInput(string f, bool cmode)
{
  open(f,cmode);
}

ZInput::ZInput()
{
  //
}


void ZInput::open(string f, bool cmode)
{
  filename = f;
  compressed = cmode;

#ifndef WITH_ZLIB
  if ( compressed )
    {
      error("ZLIB support is not currently compiled in");
    }
#endif  

  if ( compressed )
    {
      zinf.open( filename.c_str() );
      if ( ! zinf.is_open() )
	error("Problem opening " + filename + "\n");
    }
  else
    {
      inf.open( filename.c_str() );
      if ( ! inf.is_open() )
	error("Problem opening " + filename + "\n");
    }
}

string ZInput::readLine()
{
  
  if ( compressed ) 
    {
      zinf.getline(buf,MAX_LINE_LENGTH,'\n');
    }
  else
    {
      inf.getline(buf,MAX_LINE_LENGTH,'\n');
    }
  
  return buf;

  // std::cerr << buf 
  //           << "\t(" << inf.rdbuf()->in_avail() 
  //           << " chars left in buffer)  ";
}

char ZInput::readChar()
{
  char c;
  if ( compressed ) 
    {
      zinf.get(c);
    }
  else
    {
      inf.get(c);
    }  
  return c;
}

vector<string> ZInput::tokenizeLine()
{  
  string s = readLine();
  vector<string> tok;
  string buf; 
  stringstream ss(s); 
  while (ss >> buf)
    tok.push_back(buf);
  return tok;
}

void ZInput::close()
{
  if ( compressed ) 
    inf.close();
  else
    zinf.close();
}

bool ZInput::endOfFile()
{
  // Check -- eof() doesn't work here -- look up the differences between 
  // these different file states

  if ( compressed )
    return zinf.fail() || ( ! zinf.good() ) ;
  else
    return inf.fail() || ( ! inf.good() ) ;
}

void ZInput::unbuffered()
{
  if ( compressed )
    zinf.rdbuf()->pubsetbuf(0,0);
}

void ZOutput::open(string f, bool cmode)
{
  filename = f;
  compressed = cmode;

#ifndef WITH_ZLIB
  if ( compressed )
    {
      PP->printLOG("Warning: ZLIB support not enabled, so writing uncompressed file\n");
      compressed = false;
    }
#endif  

  if ( compressed )
    {
      zoutf.open( filename.c_str() );
      if ( ! zoutf.is_open() )
	{
	  error("Problem opening " + filename );
	}
    }
  else
    {
      outf.open( filename.c_str() );
      if ( ! outf.is_open() )
	{
	  error("Problem opening " + filename );
	}
    }
}

ZOutput::ZOutput(string f, bool cmode)
{
  open(f,cmode);
}

ZOutput::ZOutput()
{
  //
}

void ZOutput::write(string s)
{
  if ( compressed )
    zoutf << s; 
  else
    outf << s;
}

void ZOutput::writeLine(string s)
{
  if ( compressed )
    zoutf << s << endl;
  else
    outf << s << endl;
}

void ZOutput::close()
{
  if ( compressed )
    zoutf.close();
  else
    outf.close();
}

void ZOutput::unbuffered()
{
  if ( compressed )
    zoutf.rdbuf()->pubsetbuf(0,0);
}

void fileCompress()
{

#ifndef WITH_ZLIB
  error("ZLIB support is not compiled in");
#endif

  PP->printLOG("Compressing [ " + par::compress_filename + " ]...\n");
  ZInput zin( par::compress_filename , false );
  ZOutput zout( par::compress_filename+".gz", true );
  while ( ! zin.endOfFile() )
    {
      string line = zin.readLine();
      if ( zin.endOfFile() )
	break;
      zout.writeLine(line);
    }
  zin.close();
  zout.close();
  PP->printLOG("Wrote compressed file to [ " + par::compress_filename + ".gz ]\n");
}


void fileUncompress()
{

#ifndef WITH_ZLIB
  error("ZLIB support is not compiled in");
#endif
      
  PP->printLOG("Uncompressing [ " + par::compress_filename + " ]...\n");

  int s = par::compress_filename.size();

  if ( s < 3 || par::compress_filename.substr(s-3,3) != ".gz" )
    error("Filename must end if .gz");

  ZInput zin( par::compress_filename , true );
  ZOutput zout( par::compress_filename.substr(0,s-3), false );
  while ( ! zin.endOfFile() )
    {
      string line = zin.readLine();
      if ( zin.endOfFile() )
	break;
      zout.writeLine(line);
    }
  zin.close();
  zout.close();
  PP->printLOG("Wrote uncompressed file to [ " 
	       + par::compress_filename.substr(0,s-3) + " ]\n");
}