SIMLIB/C++  3.07
print.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 //! \file print.cc Printing - like printf
3 //
4 // Copyright (c) 1991-2004 Petr Peringer
5 //
6 // This library is licensed under GNU Library GPL. See the file COPYING.
7 //
8 
9 //
10 // implementation of direct output to user
11 //
12 
13 #include "simlib.h"
14 #include "internal.h"
15 
16 #include <cstdio>
17 #include <cstdarg>
18 
19 ////////////////////////////////////////////////////////////////////////////
20 // implementation
21 //
22 
23 namespace simlib3 {
24 
26 
27 
28 ////////////////////////////////////////////////////////////////////////////
29 // OutFile
30 //
31 
32 // This singleton solves module initialization order problem
33 class _FileWrap {
34  static FILE *OutFile;
35  static FILE *get() {
36  if(!OutFile)
37  OutFile = stdout;
38  return OutFile;
39  }
40  public:
41  operator FILE *() { return get(); }
42  void operator = (FILE *f) { OutFile=f; }
43 } OutFile;
44 
45 FILE *_FileWrap::OutFile = 0;
46 
47 ////////////////////////////////////////////////////////////////////////////
48 // SetOutput
49 //
50 void SetOutput(const char *name)
51 {
52  if(OutFile!=stdout)
53  fclose(OutFile); // close non-default
54 
55  if (*name != '\0') {
56  OutFile = fopen(name,"wt");
57  // TODO: SIMLIB_error(CantOpenOutFile);
58  if(!OutFile) // can not be open
59  OutFile = stdout; // use default
60  } else {
61  OutFile = stdout; // empty name "" means stdout
62  }
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////
66 // _Print
67 //
68 int _Print(const char *fmt, ...)
69 {
70  va_list argptr;
71  int cnt;
72 
73  va_start(argptr, fmt);
74  cnt = vfprintf(OutFile, fmt, argptr);
75  va_end(argptr);
76 
77  fflush(OutFile);
78 
79  if (OutFile!=stdout) {
80  // copy the same output to stderr
81  va_start(argptr, fmt);
82  cnt = vfprintf(stderr, fmt, argptr);
83  va_end(argptr);
84  }
85 
86  return(cnt);
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////
90 // Print
91 //
92 int Print(const char *fmt, ...)
93 {
94  va_list argptr;
95  int cnt;
96 
97  va_start(argptr, fmt);
98  cnt = vfprintf(OutFile, fmt, argptr);
99  fflush(OutFile);
100  va_end(argptr);
101 
102  return(cnt);
103 }
104 
105 int Print(const double x)
106 {
107  return Print(" %g ", x);
108 }
109 
110 int Print(const double x, const double y)
111 {
112  return Print(" %g %g ", x, y);
113 }
114 
115 int Print(const double x, const double y, const double z)
116 {
117  return Print(" %g %g %g ", x, y, z);
118 }
119 
120 ////////////////////////////////////////////////////////////////////////////
121 // Error - print message & end of program
122 //
123 void Error(const char *fmt, ...)
124 {
125  va_list argptr;
126 
127  va_start(argptr, fmt);
128  vfprintf(OutFile, fmt, argptr);
129  fflush(OutFile);
130  if (OutFile!=stdout)
131  vfprintf(stderr, fmt, argptr);
132  va_end(argptr);
133 
134  _Print("\n");
136 }
137 
138 } // end
139 
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition: error.cc:38
int _Print(const char *fmt,...)
output of messages to stdout, too
Definition: print.cc:68
void operator=(FILE *f)
Definition: print.cc:42
int Print(const char *fmt,...)
for Output methods, can be redirected
Definition: print.cc:92
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
Definition: algloop.cc:32
static FILE * OutFile
Definition: print.cc:34
void Error(const char *fmt,...)
print message and terminate program
Definition: print.cc:123
Internal header file for SIMLIB/C++.
Main SIMLIB/C++ interface.
void SetOutput(const char *name)
redirects Output(), Print() to file
Definition: print.cc:50
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34