Jpp  15.0.1-rc.1-highQE
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFileStream.cc
Go to the documentation of this file.
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 
6 #include <string>
7 #include <iostream>
8 #include <iomanip>
9 #include <vector>
10 #include <iterator>
11 
12 #include "JLang/JFileStream.hh"
13 
14 #include "Jeep/JParser.hh"
15 #include "Jeep/JMessage.hh"
16 
17 
18 /**
19  * \file
20  *
21  * Example program to test JLANG::JFileStream class.
22  * \author mdejong
23  */
24 int main(int argc, char **argv)
25 {
26  using namespace std;
27  using namespace JPP;
28 
29  string file_name = "test_file.txt";
30  int debug;
31 
32  try {
33 
34  JParser<> zap("Example program to test file streaming.");
35 
36  zap['f'] = make_field(file_name) = "test_file.txt";
37  zap['d'] = make_field(debug) = 3;
38 
39  zap(argc, argv);
40  }
41  catch(const exception &error) {
42  FATAL(error.what() << endl);
43  }
44 
45 
46  vector<string> data_in;
47 
48  data_in.push_back("hello");
49  data_in.push_back("world");
50 
51  {
52  int fd = open(file_name.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
53 
54  ASSERT(fd != -1);
55 
56  JFileOutputStream os(fd);
57 
58  for (vector<string>::const_iterator i = data_in.begin(); i != data_in.end(); ++i) {
59  os << *i << endl;
60  }
61 
62  close(fd);
63  }
64 
65  vector<string> data_out;
66 
67  {
68  int fd = open(file_name.c_str(), O_RDWR);
69 
70  ASSERT(fd != -1);
71 
72  JFileInputStream is(fd);
73 
74  for (string buffer; getline(is,buffer); ) {
75  data_out.push_back(buffer);
76  }
77 
78  close(fd);
79  }
80 
81  if (debug >= debug_t) {
82  cout << "data_in "; copy(data_in .begin(), data_in .end(), ostream_iterator<string>(cout, " ")); cout << endl;
83  cout << "data_out "; copy(data_out.begin(), data_out.end(), ostream_iterator<string>(cout, " ")); cout << endl;
84  }
85 
86  ASSERT(data_in == data_out);
87 
88  return 0;
89 }
Utility class to parse command line options.
Definition: JParser.hh:1500
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
is
Definition: JDAQCHSM.chsm:167
T * open(const std::string &file_name)
Open file.
Definition: JeepToolkit.hh:306
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
void close(std::istream *pf)
Close file.
Definition: JeepToolkit.hh:346
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:139