source: trunk/external/atnf/PKSIO/PKSmsg.cc@ 1712

Last change on this file since 1712 was 1635, checked in by Malte Marquarding, 15 years ago

Update from livedata CVS

File size: 4.8 KB
RevLine 
[1452]1//#---------------------------------------------------------------------------
2//# PKSmsg.cc: Message handling for the PKSIO classes.
3//#---------------------------------------------------------------------------
4//# Copyright (C) 2008
5//# Associated Universities, Inc. Washington DC, USA.
6//#
7//# This library is free software; you can redistribute it and/or modify it
8//# under the terms of the GNU Library General Public License as published by
9//# the Free Software Foundation; either version 2 of the License, or (at your
10//# option) any later version.
11//#
12//# This library is distributed in the hope that it will be useful, but WITHOUT
13//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15//# License for more details.
16//#
17//# You should have received a copy of the GNU Library General Public License
18//# along with this library; if not, write to the Free Software Foundation,
19//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20//#
21//# Correspondence concerning AIPS++ should be addressed as follows:
22//# Internet email: aips2-request@nrao.edu.
23//# Postal address: AIPS++ Project Office
24//# National Radio Astronomy Observatory
25//# 520 Edgemont Road
26//# Charlottesville, VA 22903-2475 USA
27//#
28//# $Id: PKSmsg.cc,v 1.2 2008-11-17 06:57:44 cal103 Exp $
29//#---------------------------------------------------------------------------
30//# Original: 2008/09/18, Mark Calabretta, ATNF
31//#---------------------------------------------------------------------------
32
33#include <atnf/PKSIO/PKSmsg.h>
34
35#include <casa/stdio.h>
36
37#include <string>
38
39//------------------------------------------------------------- PKSmsg::PKSmsg
40
41// Default constructor.
42
43PKSmsg::PKSmsg()
44{
45 initMsg();
46}
47
48void PKSmsg::initMsg()
49{
50 cMsgFD = stderr;
51 cMsgLen = 0;
52 cMsgBuff = 0x0;
53 cMsgIdx = cMsgBuff;
54 cNMsg = 0;
55}
56
57//------------------------------------------------------------ PKSmsg::~PKSmsg
58
59// Destructor.
60
61PKSmsg::~PKSmsg()
62{
63 delete [] cMsgBuff;
64 cMsgBuff = 0x0;
65 cMsgIdx = cMsgBuff;
66}
67
68//------------------------------------------------------------- PKSmsg::setMsg
69
70// Set message disposition. If fd is non-zero messages will be written
71// to that file descriptor, else stored for retrieval by getMsg().
72
73int PKSmsg::setMsg(FILE *fd)
74{
75 cMsgFD = fd;
76
77 if (cMsgFD == 0x0) {
78 clearMsg();
79
80 } else {
81 delete [] cMsgBuff;
82 cMsgLen = 0;
83 cMsgBuff = 0x0;
84 cMsgIdx = cMsgBuff;
85 }
86
87 return 0;
88}
89
90//------------------------------------------------------------- PKSmsg::logMsg
91
92// Log a message.
93
94void PKSmsg::logMsg(const char *msg)
95{
96 if (msg) {
97 if (cMsgFD) {
98 fprintf(cMsgFD, "%s\n", msg);
99
100 } else {
101 // Expand the message buffer if necessary.
102 if (!cMsgBuff) {
103 cMsgLen = 128;
104 cMsgBuff = new char[cMsgLen];
105 cMsgIdx = cMsgBuff;
106 clearMsg();
107 }
108
109 int used = strlen(cMsgBuff);
110 int free = cMsgLen - (used + 1);
111 int xtra = 1 + strlen(msg);
112 if (free < xtra) {
113 if (xtra < 128) xtra = 128;
114 cMsgLen += xtra;
115 char *newBuff = new char[cMsgLen];
116 strcpy(newBuff, cMsgBuff);
117 delete [] cMsgBuff;
118 cMsgBuff = newBuff;
119 cMsgIdx = cMsgBuff;
120 }
121
122 sprintf(cMsgBuff+used, "%s%s", ((cNMsg++)?"\n":""), msg);
123 }
124 }
125}
126
127//------------------------------------------------------------- PKSmsg::getMsg
128
129// Get a message string, or 0x0 if there is none.
130
131const char *PKSmsg::getMsg()
132{
133 if (cMsgBuff && *cMsgBuff) {
134 cMsgIdx = cMsgBuff;
135 return cMsgBuff;
136 }
137
138 return 0x0;
139}
140
141
142// Get the next group of messages by type: ERROR, WARNING, or otherwise.
143
144const char *PKSmsg::getMsg(msgType &type)
145{
146 if (cMsgIdx && *cMsgIdx) {
147 if (strncmp(cMsgIdx, "ERROR", 5) == 0) {
148 type = ERROR;
149 } else if (strncmp(cMsgIdx, "WARNING", 7) == 0) {
150 type = WARNING;
151 } else {
152 type = NORMAL;
153 }
154
155 // Gather multi-line messages.
156 char *cp = cMsgIdx;
157 while ((cMsgIdx = strchr(cMsgIdx, '\n'))) {
158 cMsgIdx++;
159
160 if (type == ERROR) {
161 if (strncmp(cMsgIdx, "ERROR", 5) == 0 ||
162 strncmp(cMsgIdx, " ", 5) == 0) {
163 continue;
164 }
165
166 } else if (type == WARNING) {
167 if (strncmp(cMsgIdx, "WARNING", 7) == 0 ||
168 strncmp(cMsgIdx, " ", 7) == 0) {
169 continue;
170 }
171
172 } else {
173 if (strncmp(cMsgIdx, "ERROR", 5) ||
174 strncmp(cMsgIdx, "WARNING", 7)) {
175 continue;
176 }
177 }
178
179 *(cMsgIdx-1) = '\0';
180 break;
181 }
182
183 return cp;
184 }
185
186 return 0x0;
187}
188
189//----------------------------------------------------------- PKSmsg::clearMsg
190
191// Clear the message buffer.
192
193void PKSmsg::clearMsg(void)
194{
195 if (cMsgBuff) *cMsgBuff = '\0';
196 cMsgIdx = cMsgBuff;
197 cNMsg = 0;
198}
Note: See TracBrowser for help on using the repository browser.