00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "Inform.h"
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <stdio.h>
00028
00029 #include "config.h"
00030 #if defined(VMDTKCON)
00031 #include "vmdconsole.h"
00032 #endif
00033
00034
00035 Inform msgInfo("Info) ");
00036 Inform msgWarn("Warning) ");
00037 Inform msgErr("ERROR) ");
00038
00039 Inform& sendmsg(Inform& inform) {
00040 Inform& rc = inform.send();
00041
00042 #if defined(VMDTKCON)
00043 vmdcon_purge();
00044 #else
00045 fflush(stdout);
00046
00047
00048 #endif
00049 return rc;
00050 }
00051
00052 Inform& ends(Inform& inform) { return inform; }
00053
00054 Inform::Inform(const char *myname) {
00055 name = strdup(myname);
00056 reset();
00057 }
00058
00059 Inform::~Inform() {
00060 free(name);
00061 }
00062
00063 Inform& Inform::send() {
00064 char *nlptr, *bufptr;
00065 bufptr = buf;
00066 if (!strchr(buf, '\n'))
00067 strcat(buf, "\n");
00068
00069 while ((nlptr = strchr(bufptr, '\n'))) {
00070 *nlptr = '\0';
00071 #if defined(VMDTKCON)
00072 vmdcon_append(name, -1);
00073 vmdcon_append(bufptr, -1);
00074 vmdcon_append("\n", 1);
00075 #else
00076 printf("%s%s\n", name, bufptr);
00077 #endif
00078 bufptr = nlptr + 1;
00079 }
00080 buf[0] = '\0';
00081 return *this;
00082 }
00083
00084 Inform& Inform::reset() {
00085 buf[0] = '\0';
00086 return *this;
00087 }
00088
00089 Inform& Inform::operator<<(const char *s) {
00090 strncat(buf, s, MAX_MSG_SIZE - strlen(buf));
00091 return *this;
00092 }
00093
00094 Inform& Inform::operator<<(char c) {
00095 char tmpbuf[2];
00096 tmpbuf[0] = c;
00097 tmpbuf[1] = '\0';
00098 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf));
00099 return *this;
00100 }
00101
00102 Inform& Inform::operator<<(int i) {
00103 char tmpbuf[128];
00104 sprintf(tmpbuf, "%d", i);
00105 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf));
00106 return *this;
00107 }
00108
00109 Inform& Inform::operator<<(long i) {
00110 char tmpbuf[128];
00111 sprintf(tmpbuf, "%ld", i);
00112 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf));
00113 return *this;
00114 }
00115
00116 Inform& Inform::operator<<(unsigned long u) {
00117 char tmpbuf[128];
00118 sprintf(tmpbuf, "%ld", u);
00119 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf));
00120 return *this;
00121 }
00122
00123 Inform& Inform::operator<<(double d) {
00124 char tmpbuf[128];
00125 sprintf(tmpbuf, "%f", d);
00126 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf));
00127 return *this;
00128 }
00129
00130 Inform& Inform::operator<<(Inform& (*f)(Inform &)) {
00131 return f(*this);
00132 }
00133
00134 #ifdef TEST_INFORM
00135
00136 int main() {
00137 msgInfo << "1\n";
00138 msgInfo << "12\n";
00139 msgInfo << "123\n";
00140 msgInfo << sendmsg;
00141 msgInfo << "6789";
00142 msgInfo << sendmsg;
00143 return 0;
00144 }
00145
00146 #endif
00147