Jpp 21.0.1
the software that should make you happy
Loading...
Searching...
No Matches
JFileStream.cc
Go to the documentation of this file.
1#include <fcntl.h>
2
3#include <string>
4#include <iostream>
5#include <iomanip>
6#include <vector>
7#include <iterator>
8
10
11#include "Jeep/JParser.hh"
12#include "Jeep/JMessage.hh"
13
14
15/**
16 * \file
17 *
18 * Example program to test JLANG::JFileStream class.
19 * \author mdejong
20 */
21int main(int argc, char **argv)
22{
23 using namespace std;
24 using namespace JPP;
25
26 string file_name = "test_file.txt";
27 int debug;
28
29 try {
30
31 JParser<> zap("Example program to test file streaming.");
32
33 zap['f'] = make_field(file_name) = "test_file.txt";
34 zap['d'] = make_field(debug) = 3;
35
36 zap(argc, argv);
37 }
38 catch(const exception &error) {
39 FATAL(error.what() << endl);
40 }
41
42
43 vector<string> data_in;
44
45 data_in.push_back("hello");
46 data_in.push_back("world");
47
48 {
49 int fd = open(file_name.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
50
51 ASSERT(fd != -1);
52
53 JFileOutputStream os(fd);
54
55 for (vector<string>::const_iterator i = data_in.begin(); i != data_in.end(); ++i) {
56 os << *i << endl;
57 }
58
59 close(fd);
60 }
61
62 vector<string> data_out;
63
64 {
65 int fd = open(file_name.c_str(), O_RDWR);
66
67 ASSERT(fd != -1);
68
69 JFileInputStream is(fd);
70
71 for (string buffer; getline(is,buffer); ) {
72 data_out.push_back(buffer);
73 }
74
75 close(fd);
76 }
77
78 if (debug >= debug_t) {
79 cout << "data_in "; copy(data_in .begin(), data_in .end(), ostream_iterator<string>(cout, " ")); cout << endl;
80 cout << "data_out "; copy(data_out.begin(), data_out.end(), ostream_iterator<string>(cout, " ")); cout << endl;
81 }
82
83 ASSERT(data_in == data_out);
84
85 return 0;
86}
General purpose messaging.
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:74
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2107
Streaming of input.
Streaming of output.
Utility class to parse command line options.
Definition JParser.hh:1664
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).