Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALApps 10.1.0.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
join.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2007 Duncan Brown, Stephen Fairhurst
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 <config.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25#include <FrameL.h>
26
27#include <lal/LALgetopt.h>
28
29
30struct options {
31 char *outfile;
32 char **infiles;
35};
36
37
38static int print_usage(const char *prog)
39{
40 return fprintf(stderr,
41"Usage: %s [option ...] --output filename inputfilename ...\n" \
42"\n" \
43"The following options are recognized:\n" \
44" --help\n" \
45" --output (required)\n" \
46" --verbose\n", prog);
47}
48
49
50static struct options parse_command_line(int argc, char *argv[])
51{
52 int c;
53 int option_index;
54 struct options options = {
55 NULL,
56 NULL,
57 0,
58 0
59 };
60 struct LALoption long_options[] = {
61 {"help", no_argument, NULL, 'h'},
62 {"output", required_argument, NULL, 'o'},
63 {"verbose", no_argument, NULL, 'v'},
64 {NULL, 0, NULL, 0}
65 };
66
67 do switch(c = LALgetopt_long(argc, argv, "", long_options, &option_index)) {
68 /* --output */
69 case 'o':
71 break;
72
73 /* --help */
74 case 'h':
75 print_usage(argv[0]);
76 exit(0);
77
78 /* --verbose */
79 case 'v':
80 options.verbose = 1;
81 break;
82
83 /* option sets a flag */
84 case 0:
85 break;
86
87 /* end of arguments */
88 case -1:
89 if(LALoptind >= argc) {
90 /* nothing on command line after options */
91 print_usage(argv[0]);
92 exit(1);
93 }
94 options.infiles = &argv[LALoptind];
96 break;
97
98 /* unrecognized option */
99 case '?':
100 print_usage(argv[0]);
101 exit(1);
102
103 /* missing argument for an option */
104 case ':':
105 print_usage(argv[0]);
106 exit(1);
107 } while(c != -1);
108
109 if(!options.outfile) {
110 /* --output not among command line options, use first
111 * filename on command line as output (ugh). emulates
112 * legacy behaviour */
113 if(LALoptind + 1 >= argc) {
114 /* not enough file names on command line */
115 print_usage(argv[0]);
116 exit(1);
117 }
121 }
122
123 return options;
124}
125
126
127int main(int argc, char *argv[])
128{
129 char history[] = "Created by " PACKAGE "-" VERSION ".";
130 FILE *devnull;
131 struct options options;
132 int i;
133 struct FrFile *frfileout;
134
135 options = parse_command_line(argc, argv);
136
137 /* note the hack to silence libframe if --verbose is not given */
138 devnull = fopen("/dev/null", "w");
139 FrLibIni(NULL, options.verbose || !devnull ? stderr : devnull, 0);
140 if(devnull)
141 fclose(devnull);
142
143 if(options.verbose)
144 fprintf(stderr, "%s: writing to \"%s\"\n", argv[0], options.outfile);
145
146 frfileout = FrFileONewH(options.outfile, 0, history);
147 if(!frfileout) {
148 fprintf(stderr, "%s: error: could not open output file \"%s\"\n", argv[0], options.outfile);
149 exit(1);
150 }
151
152 for(i = 0; i < options.n_infiles; i++) {
153 struct FrFile *frfilein;
154 struct FrameH *frame;
155
156 if(options.verbose)
157 fprintf(stderr, "%s: reading \"%s\"\n", argv[0], options.infiles[i]);
158
159 frfilein = FrFileINew(options.infiles[i]);
160 if(!frfilein) {
161 FrFileOEnd(frfileout);
162 fprintf(stderr, "%s: error: input file \"%s\" not found\n", argv[0], options.infiles[i]);
163 exit(1);
164 }
165
166 while((frame = FrameRead(frfilein))) {
167 FrameWrite(frame, frfileout);
168 FrameFree(frame);
169 }
170 FrFileIEnd(frfilein);
171 }
172
173 FrFileOEnd(frfileout);
174
175 exit(0);
176}
#define char
int LALgetopt_long(int argc, char *const *argv, const char *options, const struct LALoption *long_options, int *opt_index)
int LALoptind
char * LALoptarg
#define no_argument
#define required_argument
#define fprintf
int i
Definition: inspinj.c:596
int main(int argc, char *argv[])
Definition: join.c:127
static int print_usage(const char *prog)
Definition: join.c:38
static struct options parse_command_line(int argc, char *argv[])
Definition: join.c:50
c
int
string PACKAGE
Definition: join.c:30
int n_infiles
Definition: join.c:33
char ** infiles
Definition: join.c:32
int verbose
Definition: join.c:34
char * outfile
Definition: join.c:31