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

Last change on this file since 1583 was 1466, checked in by Malte Marquarding, 16 years ago

make gcc-4.3 compliant; Mark C. still needs to fix char* cast deprecation warnings

File size: 4.8 KB
Line 
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#include <cstring>
39
40//------------------------------------------------------------- PKSmsg::PKSmsg
41
42// Default constructor.
43
44PKSmsg::PKSmsg()
45{
46 initMsg();
47}
48
49void PKSmsg::initMsg()
50{
51 cMsgFD = stderr;
52 cMsgLen = 0;
53 cMsgBuff = 0x0;
54 cMsgIdx = cMsgBuff;
55 cNMsg = 0;
56}
57
58//------------------------------------------------------------ PKSmsg::~PKSmsg
59
60// Destructor.
61
62PKSmsg::~PKSmsg()
63{
64 delete [] cMsgBuff;
65 cMsgBuff = 0x0;
66 cMsgIdx = cMsgBuff;
67}
68
69//------------------------------------------------------------- PKSmsg::setMsg
70
71// Set message disposition. If fd is non-zero messages will be written
72// to that file descriptor, else stored for retrieval by getMsg().
73
74int PKSmsg::setMsg(FILE *fd)
75{
76 cMsgFD = fd;
77
78 if (cMsgFD == 0x0) {
79 clearMsg();
80
81 } else {
82 delete [] cMsgBuff;
83 cMsgLen = 0;
84 cMsgBuff = 0x0;
85 cMsgIdx = cMsgBuff;
86 }
87
88 return 0;
89}
90
91//------------------------------------------------------------- PKSmsg::logMsg
92
93// Log a message.
94
95void PKSmsg::logMsg(const char *msg)
96{
97 if (msg) {
98 if (cMsgFD) {
99 fprintf(cMsgFD, "%s\n", msg);
100
101 } else {
102 // Expand the message buffer if necessary.
103 if (!cMsgBuff) {
104 cMsgLen = 128;
105 cMsgBuff = new char[cMsgLen];
106 cMsgIdx = cMsgBuff;
107 clearMsg();
108 }
109
110 int used = strlen(cMsgBuff);
111 int free = cMsgLen - (used + 1);
112 int xtra = 1 + strlen(msg);
113 if (free < xtra) {
114 if (xtra < 128) xtra = 128;
115 cMsgLen += xtra;
116 char *newBuff = new char[cMsgLen];
117 strcpy(newBuff, cMsgBuff);
118 delete [] cMsgBuff;
119 cMsgBuff = newBuff;
120 cMsgIdx = cMsgBuff;
121 }
122
123 sprintf(cMsgBuff+used, "%s%s", ((cNMsg++)?"\n":""), msg);
124 }
125 }
126}
127
128//------------------------------------------------------------- PKSmsg::getMsg
129
130// Get a message string, or 0x0 if there is none.
131
132const char *PKSmsg::getMsg()
133{
134 if (cMsgBuff && *cMsgBuff) {
135 cMsgIdx = cMsgBuff;
136 return cMsgBuff;
137 }
138
139 return 0x0;
140}
141
142
143// Get the next group of messages by type: ERROR, WARNING, or otherwise.
144
145const char *PKSmsg::getMsg(msgType &type)
146{
147 if (cMsgIdx && *cMsgIdx) {
148 if (strncmp(cMsgIdx, "ERROR", 5) == 0) {
149 type = ERROR;
150 } else if (strncmp(cMsgIdx, "WARNING", 7) == 0) {
151 type = WARNING;
152 } else {
153 type = NORMAL;
154 }
155
156 // Gather multi-line messages.
157 char *cp = cMsgIdx;
158 while ((cMsgIdx = strchr(cMsgIdx, '\n'))) {
159 cMsgIdx++;
160
161 if (type == ERROR) {
162 if (strncmp(cMsgIdx, "ERROR", 5) == 0 ||
163 strncmp(cMsgIdx, " ", 5) == 0) {
164 continue;
165 }
166
167 } else if (type == WARNING) {
168 if (strncmp(cMsgIdx, "WARNING", 7) == 0 ||
169 strncmp(cMsgIdx, " ", 7) == 0) {
170 continue;
171 }
172
173 } else {
174 if (strncmp(cMsgIdx, "ERROR", 5) ||
175 strncmp(cMsgIdx, "WARNING", 7)) {
176 continue;
177 }
178 }
179
180 *(cMsgIdx-1) = '\0';
181 break;
182 }
183
184 return cp;
185 }
186
187 return 0x0;
188}
189
190//----------------------------------------------------------- PKSmsg::clearMsg
191
192// Clear the message buffer.
193
194void PKSmsg::clearMsg(void)
195{
196 if (cMsgBuff) *cMsgBuff = '\0';
197 cMsgIdx = cMsgBuff;
198 cNMsg = 0;
199}
Note: See TracBrowser for help on using the repository browser.