LAL  7.5.0.1-bede9b2
GzipTest.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2013 Matthew Pitkin
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 
21 #include <lal/LALStdlib.h>
22 #include <lal/LALgetopt.h>
23 #include <lal/XLALError.h>
24 #include <lal/LALString.h>
25 #include <lal/FileIO.h>
26 
27 #define USAGE \
28 "Usage: %s [options]\n\n"\
29 " --help (-h) display this message\n"\
30 " --file (-f) name of ascii text file to gzip/unzip\n"\
31 " --gzip (-g) gzip (compress) the text file\n"\
32 " --gunzip (-u) gunzip (decompress) the gzipped text file\n"\
33 "\n"
34 
35 int main(int argc, char **argv){
36  CHAR *filename = NULL;
37  INT4 gzip = 0, gunzip = 0;
38 
39  struct LALoption long_options[] =
40  {
41  { "help", no_argument, 0, 'h' },
42  { "file", required_argument, 0, 'f' },
43  { "gzip", no_argument, NULL, 'g' },
44  { "guzip", no_argument, NULL, 'u' },
45  { 0, 0, 0, 0 }
46  };
47 
48  CHAR args[] = "hf:gu";
49  CHAR *program = argv[0];
50 
51  /* get input arguments */
52  while(1){
53  int option_index = 0;
54  int c;
55 
56  c = LALgetopt_long( argc, argv, args, long_options, &option_index );
57  if ( c == -1 ) /* end of options */
58  break;
59 
60  switch(c){
61  case 0: /* if option set a flag, nothing else to do */
62  if ( long_options[option_index].flag )
63  break;
64  else
65  fprintf(stderr, "Error parsing option %s with argument %s\n", long_options[option_index].name, LALoptarg );
66  /* fallthrough */
67  case 'h': /* help message */
68  fprintf(stderr, USAGE, program);
69  exit(0);
70  case 'f': /* input file */
72  break;
73  case 'g': /* gzip the file */
74  gzip = 1;
75  break;
76  case 'u': /* gunzip the file */
77  gunzip = 1;
78  break;
79  case '?':
80  fprintf(stderr, "Unknown error while parsing options\n" );
81  exit(0);
82  default:
83  fprintf(stderr, "Unknown error while parsing options\n" );
84  exit(0);
85  }
86  }
87 
88  if ( filename == NULL ){
89  fprintf(stderr, "Must specify an input file\n");
90  fprintf(stderr, USAGE, program);
91  exit(0);
92  }
93 
94  if ( ( !gzip && !gunzip ) || ( gzip && gunzip ) ){
95  fprintf(stderr, "Must specify whether you want to either gzip (-g) or gunzip (-u) the input file.\n");
96  fprintf(stderr, USAGE, program);
97  exit(0);
98  }
99 
100  if ( gzip ){
101  /* zip the file */
103  fprintf(stderr, "Gzip of %s has failed\n", filename);
104  exit(1);
105  }
106  }
107  if ( gunzip ){
108  /* unzip the file */
110  fprintf(stderr, "Guzip of %s has failed\n", filename);
111  exit(1);
112  }
113  }
114 
115  return 0;
116 }
const char * program
int main(int argc, char **argv)
Definition: GzipTest.c:35
#define USAGE
Definition: GzipTest.c:27
int LALgetopt_long(int argc, char *const *argv, const char *options, const struct LALoption *long_options, int *opt_index)
Definition: LALgetopt.c:178
char * LALoptarg
Definition: LALgetopt.c:64
#define no_argument
Definition: LALgetopt.h:100
#define required_argument
Definition: LALgetopt.h:101
#define fprintf
const char *const name
type name
Definition: UserInput.c:193
int XLALGunzipTextFile(const char *filename)
Use gzip to uncompress a compressed text file.
Definition: FileIO.c:790
int XLALGzipTextFile(const char *filename)
Use gzip to compress a text file This function will use the gzip compression routines in zlib to comp...
Definition: FileIO.c:707
char CHAR
One-byte signed integer, see Headers LAL(Atomic)Datatypes.h for more details.
int32_t INT4
Four-byte signed integer.
char * XLALStringDuplicate(const char *s)
Like strdup but uses LAL allocation routines (free with LALFree).
Definition: LALString.c:89
@ XLAL_SUCCESS
Success return value (not an error number)
Definition: XLALError.h:401
int * flag
Definition: LALgetopt.h:90