-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathio.nw
More file actions
137 lines (116 loc) · 3.24 KB
/
io.nw
File metadata and controls
137 lines (116 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
% -*- mode: Noweb; noweb-code-mode: c++-mode; c-basic-offset: 8; -*-
\section{IO Facilities}
\begin{comment}
Silent printing is a useful trick I learned from \cite{knuth86tex}.
\end{comment}
<<io.h>>=
#ifndef _IO_H_
#define _IO_H_
#include <string>
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
#define STDOUT 1
#define STDERR 2
#define SILENT 3
#define EXFILE 4
#define PIPE 5
void initPipe();
void closePipe();
void setSelector(FILE * in);
void setSelector(int x);
int getSelector();
void ioprint(const string & x);
void ioprint(int x);
void ioprint(long long int x);
void ioprint(double x);
void ioprint(char x);
void ioprintln(const string & x);
void ioprintln(int x);
void ioprintln(long long int x);
void ioprintln(double x);
void ioprintln(char x);
void ioprintln();
#endif
@ %def ioprint ioprintln setSelector getSelector
<<io.cc>>=
#include "io.h"
ofstream bcpipe;
FILE * iofile;
static int selector;
ofstream logfile("log.complete");
void initPipe() { bcpipe.open("pipe1"); bcpipe.setf(ios::fixed); }
void closePipe() { bcpipe.close(); }
void setSelector(FILE * in) { iofile = in; selector = EXFILE; }
void setSelector(int x) { selector = x; }
int getSelector() { return selector; }
void ioprint(const string & x) {
<<io::common print command>> <<io::file>> }
void ioprint(int x) {
<<io::common print command>> <<io::file 2>> }
void ioprint(long long int x) {
<<io::common print command>> <<io::file 2a>> }
void ioprint(double x) {
// cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
<<io::common print command>> <<io::file 3>> }
void ioprint(char x) {
<<io::common print command>> <<io::file 4>> }
void ioprintln(const string & x) {
<<io::common print command ln>> <<io::file>> }
void ioprintln(int x) {
<<io::common print command ln>> <<io::file 2>> }
void ioprintln(long long int x) {
<<io::common print command ln>> <<io::file 2a>> }
void ioprintln(double x) {
//cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
<<io::common print command ln>> <<io::file 3>> }
void ioprintln(char x) {
<<io::common print command ln>> <<io::file 4>> }
void ioprintln() {
#ifdef DEBUG
logfile << endl;
#endif
if (selector == SILENT) return;
else if (selector == STDOUT) cout << endl;
else if (selector == EXFILE) fprintf(iofile, "\n");
else if (selector == PIPE) bcpipe << endl;
else cerr << endl;
}
@ %def ioprint ioprintln setSelector getSelector
<<io::common print command>>=
// #define DEBUG
#ifdef DEBUG
logfile << x;
#endif
if (selector == SILENT) return;
if (selector == STDOUT) cout << x;
if (selector == STDERR) cerr << x;
else if (selector == PIPE) bcpipe << x;
@
<<io::common print command ln>>=
#ifdef DEBUG
logfile << x << endl;
#endif
if (selector == SILENT) return;
if (selector == STDOUT) cout << x << endl;
if (selector == STDERR) cerr << x << endl;
else if (selector == PIPE) bcpipe << x << endl;
@
<<io::file>>=
if (selector == EXFILE) fprintf(iofile, x.c_str());
@
<<io::file 2>>=
if (selector == EXFILE) fprintf(iofile, "%d", x);
@
<<io::file 2a>>=
if (selector == EXFILE) fprintf(iofile, "%lld", x);
@
<<io::file 3>>=
if (selector == EXFILE) fprintf(iofile, "%f", x);
@
<<io::file 4>>=
if (selector == EXFILE) fprintf(iofile, "%c", x);
@