Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hddmcat.cpp
Go to the documentation of this file.
1 /*
2  * hddmcat : tool that reads in a sequence of HDDM files
3  * and catenates them into a single HDDM stream
4  *
5  * Version 1.2 - Richard Jones, December 2005.
6  * - Updated code to use STL strings and vectors instead of old c-style
7  * pre-allocated arrays and strXXX functions.
8  * - Moved functions into classes grouped by function for better clarity.
9  *
10  * Original version - Richard Jones, February 24 2004.
11  *
12  */
13 
14 
15 #include <iostream>
16 #include <fstream>
17 #include <string>
18 #include <list>
19 using namespace std;
20 
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <rpc/rpc.h>
26 #include <unistd.h>
27 
28 
29 void usage()
30 {
31  cerr << "\nUsage:\n"
32  << " hddmcat file1.hddm [file2.hddm] ...\n\n"
33  << endl;
34 }
35 
36 int main(int argC, char* argV[])
37 {
38  string xFilename;
39 
40  int argInd;
41  for (argInd = 1; argInd < argC; argInd++)
42  {
43  if (argV[argInd][0] != '-')
44  {
45  break;
46  }
47  else
48  {
49  usage();
50  return 1;
51  }
52  }
53 
54  string hddmFile;
55  istream* ifs;
56  if (argInd == argC)
57  {
58  ifs = &cin;
59  }
60  else if (argInd < argC)
61  {
62  hddmFile = string(argV[argInd++]);
63  ifs = new ifstream(hddmFile.c_str());
64  }
65  else
66  {
67  usage();
68  return 1;
69  }
70  if (!ifs->good())
71  {
72  cerr << "hddmcat: Error opening input stream " << hddmFile << endl;
73  exit(1);
74  }
75 
76  list<std::string*> stringList;
77  stringList.push_back(new string);
78  list<std::string*>::iterator h;
79  h = stringList.begin();
80  if (std::getline(*ifs,**h))
81  {
82  if ((*h)->substr(0,5) == "<?xml")
83  {
84  cerr << "hddmcat: Error reading input stream " << hddmFile << endl;
85  cerr << "Input file appears to be an xml document!" << endl;
86  exit(1);
87  }
88  else if ((*h)->substr(0,5) == "<HDDM")
89  {
90  cout << **h << endl;
91  }
92  else
93  {
94  cerr << "hddmcat: Input stream contains invalid hddm header"
95  << endl;
96  exit(1);
97  }
98  }
99  else
100  {
101  cerr << "hddmcat: Error reading from input stream " << hddmFile << endl;
102  exit(1);
103  }
104  stringList.push_back(new string);
105  while (getline(*ifs,**(++h)))
106  {
107  cout << **h << endl;
108  if (**h == "</HDDM>")
109  {
110  break;
111  }
112  stringList.push_back(new string);
113  }
114 
115  const int bufferSize = 65536;
116  char buffer[bufferSize];
117  int count;
118  while ((count = (ifs->read(buffer,bufferSize), ifs->gcount())))
119  {
120  cout.write(buffer,count);
121  }
122  if (ifs != &cin)
123  {
124  ((ifstream*)ifs)->close();
125  }
126 
127  while (argInd < argC)
128  {
129  ifstream* ifs;
130  hddmFile = argV[argInd++];
131  ifs = new ifstream(hddmFile.c_str());
132  if (!ifs->good())
133  {
134  cerr << "hddmcat: Error opening input stream " << hddmFile << endl;
135  exit(1);
136  }
137  h = stringList.begin();
138  string line;
139  if (getline(*ifs,line))
140  {
141  if (line.substr(0,5) == "<?xml")
142  {
143  cerr << "hddmcat: Error reading input stream " << hddmFile << endl;
144  cerr << "Input file appears to be an xml document!" << endl;
145  exit(1);
146  }
147  else if (**h == line)
148  {
149  ++h;
150  }
151  else
152  {
153  cerr << "hddmcat: Input stream contains invalid hddm header"
154  << endl;
155  exit(1);
156  }
157  }
158  else
159  {
160  cerr << "hddmcat: Error reading from input stream " << hddmFile
161  << endl;
162  exit(1);
163  }
164  while (getline(*ifs,line))
165  {
166  if (h == stringList.end() || **h != line)
167  {
168  cerr << "hddmcat: Input stream contains invalid hddm header"
169  << endl;
170  exit(1);
171  }
172  else if (++h == stringList.end() && line == "</HDDM>")
173  {
174  break;
175  }
176  }
177 
178  while ((count = (ifs->read(buffer,bufferSize), ifs->gcount())))
179  {
180  cout.write(buffer,count);
181  }
182  delete ifs;
183  }
184 }
char string[256]
void usage()
Definition: t_rest.cxx:114
int main(int argc, char *argv[])
Definition: gendoc.cc:6