| 150 | } |
| 151 | |
| 152 | static FILE* fopenGzipped(const char* filename, const char* mode) |
| 153 | { |
| 154 | // check mode |
| 155 | if (mode[0] == 'r') |
| 156 | { |
| 157 | // open input file |
| 158 | FILE* gzipInput = LASfopen(filename, mode); |
| 159 | if (!gzipInput) return NULL; |
| 160 | |
| 161 | // create the pipe |
| 162 | int hPipe[2]; |
| 163 | if (_pipe(hPipe, 2048, ((mode[1] =='b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1) |
| 164 | { |
| 165 | laserror("could not create pipe"); |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | // duplicate stdin handle |
| 170 | int hStdIn = _dup(_fileno(stdin)); |
| 171 | // redirect stdin to input file |
| 172 | if (_dup2(_fileno(gzipInput), _fileno(stdin)) != 0) |
| 173 | { |
| 174 | laserror("could not redirect stdin"); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | // duplicate stdout handle |
| 179 | int hStdOut = _dup(_fileno(stdout)); |
| 180 | // redirect stdout to write end of pipe |
| 181 | if (_dup2(hPipe[WRITE_HANDLE], _fileno(stdout)) != 0) |
| 182 | { |
| 183 | laserror("could not set pipe output"); |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | // close original write end of pipe |
| 188 | _close(hPipe[WRITE_HANDLE]); |
| 189 | |
| 190 | // redirect read end of pipe to input file |
| 191 | if (_dup2(hPipe[READ_HANDLE], _fileno(gzipInput)) != 0) |
| 192 | { |
| 193 | laserror("could not redirect input file"); |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | // close original read end of pipe |
| 198 | _close(hPipe[READ_HANDLE]); |
| 199 | |
| 200 | // Spawn process |
| 201 | HANDLE hProcess = (HANDLE)_spawnlp(P_NOWAIT, "gzip", "gzip", "-d", NULL); |
| 202 | |
| 203 | // redirect stdin back into stdin |
| 204 | if (_dup2(hStdIn, _fileno(stdin)) != 0) |
| 205 | { |
| 206 | laserror("could not reconstruct stdin"); |
| 207 | return NULL; |
| 208 | } |
| 209 |
no test coverage detected