[0b990d] | 1 | //
|
---|
| 2 | // formio.cc
|
---|
| 3 | //
|
---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
| 5 | //
|
---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
| 7 | // Maintainer: LPS
|
---|
| 8 | //
|
---|
| 9 | // This file is part of the SC Toolkit.
|
---|
| 10 | //
|
---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
| 12 | // it under the terms of the GNU Library General Public License as published by
|
---|
| 13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
| 14 | // any later version.
|
---|
| 15 | //
|
---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | // GNU Library General Public License for more details.
|
---|
| 20 | //
|
---|
| 21 | // You should have received a copy of the GNU Library General Public License
|
---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | //
|
---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
| 26 | //
|
---|
| 27 |
|
---|
| 28 | #include <util/misc/formio.h>
|
---|
| 29 | #include <util/misc/exenv.h>
|
---|
| 30 |
|
---|
| 31 | #include <stdio.h> // for vsprintf
|
---|
| 32 | #include <stdlib.h>
|
---|
| 33 | #include <string.h>
|
---|
| 34 | #include <stdarg.h>
|
---|
| 35 |
|
---|
| 36 | using namespace std;
|
---|
| 37 | using namespace sc;
|
---|
| 38 |
|
---|
| 39 | char *SCFormIO::default_basename_ = 0;
|
---|
| 40 | int SCFormIO::ready_ = 0;
|
---|
| 41 | int SCFormIO::xalloc_inited_ = 0;
|
---|
| 42 | long SCFormIO::nindent_ = 0;
|
---|
| 43 | long SCFormIO::indent_size_ = 0;
|
---|
| 44 | long SCFormIO::skip_indent_ = 0;
|
---|
| 45 | long SCFormIO::verbose_ = 0;
|
---|
| 46 | long SCFormIO::initialized_ = 0;
|
---|
| 47 | int SCFormIO::node_to_print_ = 0;
|
---|
| 48 | int SCFormIO::debug_ = 0;
|
---|
| 49 | int SCFormIO::parallel_ = 0;
|
---|
| 50 | int SCFormIO::me_ = 0;
|
---|
| 51 |
|
---|
| 52 | char *
|
---|
| 53 | SCFormIO::fileext_to_filename(const char *ext)
|
---|
| 54 | {
|
---|
| 55 | const char *basename;
|
---|
| 56 |
|
---|
| 57 | if (default_basename_) basename = default_basename_;
|
---|
| 58 | else basename = "SC";
|
---|
| 59 |
|
---|
| 60 | char * res = new char[strlen(basename) + strlen(ext) + 1];
|
---|
| 61 | strcpy(res, basename);
|
---|
| 62 | strcat(res, ext);
|
---|
| 63 |
|
---|
| 64 | return res;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | void
|
---|
| 68 | SCFormIO::set_default_basename(const char *basename)
|
---|
| 69 | {
|
---|
| 70 | if (default_basename_) delete[] default_basename_;
|
---|
| 71 |
|
---|
| 72 | if (basename)
|
---|
| 73 | default_basename_ = strcpy(new char[strlen(basename)+1], basename);
|
---|
| 74 | else
|
---|
| 75 | default_basename_ = 0;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | const char *
|
---|
| 79 | SCFormIO::default_basename()
|
---|
| 80 | {
|
---|
| 81 | return default_basename_;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | int
|
---|
| 85 | SCFormIO::set_printnode(int n)
|
---|
| 86 | {
|
---|
| 87 | int r = node_to_print_;
|
---|
| 88 | node_to_print_ = n;
|
---|
| 89 | return r;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | void
|
---|
| 93 | SCFormIO::set_debug(int n)
|
---|
| 94 | {
|
---|
| 95 | debug_ = n;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void
|
---|
| 99 | SCFormIO::init_mp(int me)
|
---|
| 100 | {
|
---|
| 101 | if (!ready_) init();
|
---|
| 102 | me_ = me;
|
---|
| 103 | parallel_=1;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void
|
---|
| 107 | SCFormIO::init_ostream(ostream &o)
|
---|
| 108 | {
|
---|
| 109 | if (!xalloc_inited_) {
|
---|
| 110 | xalloc_inited_ = 1;
|
---|
| 111 | nindent_ = ios::xalloc();
|
---|
| 112 | indent_size_ = ios::xalloc();
|
---|
| 113 | skip_indent_ = ios::xalloc();
|
---|
| 114 | verbose_ = ios::xalloc();
|
---|
| 115 | initialized_ = ios::xalloc();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | if (o.iword(initialized_)) return;
|
---|
| 119 |
|
---|
| 120 | o.iword(skip_indent_) = 0;
|
---|
| 121 | o.iword(indent_size_) = 0;
|
---|
| 122 | o.iword(nindent_) = 2;
|
---|
| 123 | o.iword(verbose_) = 0;
|
---|
| 124 | o.iword(initialized_) = 1;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void
|
---|
| 128 | SCFormIO::init()
|
---|
| 129 | {
|
---|
| 130 | ready_ = 1;
|
---|
| 131 |
|
---|
| 132 | init_ostream(cout);
|
---|
| 133 | init_ostream(cerr);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | ios&
|
---|
| 137 | SCFormIO::indent(ios&o)
|
---|
| 138 | {
|
---|
| 139 | if (!ready_) init();
|
---|
| 140 | long &skip = o.iword(skip_indent_);
|
---|
| 141 | if (skip) {
|
---|
| 142 | skip--;
|
---|
| 143 | return o;
|
---|
| 144 | }
|
---|
| 145 | if (debug_ && parallel_) {
|
---|
| 146 | char nn[24];
|
---|
| 147 | sprintf(nn,"node %5d:",me_);
|
---|
| 148 | for (size_t i=0; i < strlen(nn); i++) o.rdbuf()->sputc(nn[i]);
|
---|
| 149 | }
|
---|
| 150 | long n = o.iword(nindent_);
|
---|
| 151 | for (int i=0; i<n; i++) o.rdbuf()->sputc(' ');
|
---|
| 152 | return o;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | ios&
|
---|
| 156 | SCFormIO::incindent(ios&o)
|
---|
| 157 | {
|
---|
| 158 | if (!ready_) init();
|
---|
| 159 | long &n = o.iword(nindent_);
|
---|
| 160 | long size = o.iword(indent_size_);
|
---|
| 161 | if (size == 0) size = 2;
|
---|
| 162 | else if (size < 0) size = 0;
|
---|
| 163 | n += size;
|
---|
| 164 | return o;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | ios&
|
---|
| 168 | SCFormIO::decindent(ios&o)
|
---|
| 169 | {
|
---|
| 170 | if (!ready_) init();
|
---|
| 171 | long &n = o.iword(nindent_);
|
---|
| 172 | long size = o.iword(indent_size_);
|
---|
| 173 | if (size == 0) size = 2;
|
---|
| 174 | else if (size < 0) size = 0;
|
---|
| 175 | n -= size;
|
---|
| 176 | if (n<0) n=0;
|
---|
| 177 | return o;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | long
|
---|
| 181 | SCFormIO::getindent(ios&o)
|
---|
| 182 | {
|
---|
| 183 | if (!ready_) init();
|
---|
| 184 | return o.iword(nindent_);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | void
|
---|
| 188 | SCFormIO::setindent(ios&o, long n)
|
---|
| 189 | {
|
---|
| 190 | if (!ready_) init();
|
---|
| 191 | o.iword(nindent_) = n;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | long
|
---|
| 195 | SCFormIO::getverbose(ios&o)
|
---|
| 196 | {
|
---|
| 197 | if (!ready_) init();
|
---|
| 198 | return o.iword(verbose_);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | void
|
---|
| 202 | SCFormIO::setverbose(ios&o, long n)
|
---|
| 203 | {
|
---|
| 204 | if (!ready_) init();
|
---|
| 205 | o.iword(verbose_) = n;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | ios&
|
---|
| 209 | SCFormIO::skipnextindent(ios&o)
|
---|
| 210 | {
|
---|
| 211 | if (!ready_) init();
|
---|
| 212 | o.iword(skip_indent_)++;
|
---|
| 213 | return o;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | ostream&
|
---|
| 217 | SCFormIO::copyright(ostream& o)
|
---|
| 218 | {
|
---|
| 219 | o << indent
|
---|
| 220 | << "Copyright (C) 1997 Limit Point Systems, Inc. and others."
|
---|
| 221 | << endl;
|
---|
| 222 |
|
---|
| 223 | return o;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | ostream&
|
---|
| 227 | SCFormIO::license(ostream& o)
|
---|
| 228 | {
|
---|
| 229 | o << indent
|
---|
| 230 | << "This program is open-source software; you can redistribute it and/or modify"
|
---|
| 231 | << endl << indent
|
---|
| 232 | << "it under the terms of the GNU General Public License as published by"
|
---|
| 233 | << endl << indent
|
---|
| 234 | << "the Free Software Foundation; either version 2 of the License, or"
|
---|
| 235 | << endl << indent
|
---|
| 236 | << "(at your option) any later version."
|
---|
| 237 | << endl;
|
---|
| 238 |
|
---|
| 239 | return o;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | ostream&
|
---|
| 243 | SCFormIO::warranty(ostream& o)
|
---|
| 244 | {
|
---|
| 245 | o << indent
|
---|
| 246 | << "This program is distributed in the hope that it will be useful,"
|
---|
| 247 | << endl << indent
|
---|
| 248 | << "but WITHOUT ANY WARRANTY; without even the implied warranty of"
|
---|
| 249 | << endl << indent
|
---|
| 250 | << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
|
---|
| 251 | << endl << indent
|
---|
| 252 | << "GNU General Public License for more details."
|
---|
| 253 | << endl;
|
---|
| 254 |
|
---|
| 255 | return o;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | ios&
|
---|
| 259 | sc::indent(ios& o)
|
---|
| 260 | {
|
---|
| 261 | return SCFormIO::indent(o);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | ios&
|
---|
| 265 | sc::decindent(ios& o)
|
---|
| 266 | {
|
---|
| 267 | return SCFormIO::decindent(o);
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | ios&
|
---|
| 271 | sc::incindent(ios& o)
|
---|
| 272 | {
|
---|
| 273 | return SCFormIO::incindent(o);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | ios&
|
---|
| 277 | sc::skipnextindent(ios& o)
|
---|
| 278 | {
|
---|
| 279 | return SCFormIO::skipnextindent(o);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 283 |
|
---|
| 284 | scprintf::scprintf(const char *fmt, ...)
|
---|
| 285 | {
|
---|
| 286 | va_list args;
|
---|
| 287 |
|
---|
| 288 | va_start(args, fmt);
|
---|
| 289 |
|
---|
| 290 | str[0] = '\0';
|
---|
| 291 |
|
---|
| 292 | // hopefully this won't overflow
|
---|
| 293 | if (fmt && fmt[0]!='\0') {
|
---|
| 294 | if (vsprintf(str, fmt, args) > 1023) {
|
---|
| 295 | ExEnv::errn() << indent << "scprintf overflow\n";
|
---|
| 296 | abort();
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | va_end(args);
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | ostream&
|
---|
| 304 | sc::operator<<(ostream& o, const scprintf& s)
|
---|
| 305 | {
|
---|
| 306 | o << s.str << flush;
|
---|
| 307 | return o;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 311 |
|
---|
| 312 | // Local Variables:
|
---|
| 313 | // mode: c++
|
---|
| 314 | // c-file-style: "CLJ"
|
---|
| 315 | // End:
|
---|