summaryrefslogtreecommitdiff
path: root/src/mt/mstream.h
blob: 4f5aeafb3a995a2900549fa22f124a691437741e (plain)
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
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef MSTREAMH
#define MSTREAMH

#include <mt/stringref.h>
#include <unistd.h>
#include <stdio.h>

///////////////////////////////////////////////////////////

const char cvt_form_i[]="%d";

///////////////////////////////////////////////////////////

inline sref itoa(sref_t& buf,int i) { return sref(buf,sprintf(buf,cvt_form_i,i)); }
sref ftoa(sref_t& buf,double f);

///////////////////////////////////////////////////////////

struct mstream {
  virtual int appc(char c);
  virtual int appi(int i);
  virtual int appf(double f);
  virtual int apps(const sref& s);
  
  mstream& put(char c) { return appc(c),*this; }
  int write(const char* s,int n) { return apps(sref(s,n)); }
};

inline mstream& operator<<(mstream& out,int i) { return out.appi(i),out; }
inline mstream& operator<<(mstream& out,long l) { return out.appi(l),out; }
inline mstream& operator<<(mstream& out,unsigned u) { return out.appi(u),out; }
inline mstream& operator<<(mstream& out,unsigned long ul) { return out.appi(ul),out; }
inline mstream& operator<<(mstream& out,double f) { return out.appf(f),out; }

inline mstream& operator<<(mstream& out,const sref& s) { return out.apps(s),out; }
inline mstream& operator<<(mstream& out,const char* s) { return out.apps(sref(s)),out; }

///////////////////////////////////////////////////////////

struct cvt_stream : public mstream {
  int appi(int i);
  int appf(double f);
};

///////////////////////////////////////////////////////////

struct fp_stream : public cvt_stream {
  FILE* fp;

  int appc(char c);
  int apps(const sref& s);

  fp_stream(FILE* f) : fp(f) {}

  int flush() { return fflush(fp); }
};

///////////////////////////////////////////////////////////

struct fd_stream : public cvt_stream {
  int fd;

  int appc(char c);
  int apps(const sref& s);

  fd_stream(int f) : fd(f) {}
};

///////////////////////////////////////////////////////////

struct pipe_stream : public cvt_stream {
  int fd,hup;
  
  int appc(char c);
  int apps(const sref& s);
  
  pipe_stream(int f) : fd(f),hup(0) {}

  int mwrite(const char* s,int n);
};

///////////////////////////////////////////////////////////

#endif