ostream.cpp

Go to the documentation of this file.
00001 /* Copyright 1994, 1995 LongView Technologies L.L.C. $Revision: 1.16 $ */
00002 /* Copyright (c) 2006, Sun Microsystems, Inc.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
00006 following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
00010           disclaimer in the documentation and/or other materials provided with the distribution.
00011     * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 
00012           from this software without specific prior written permission.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00020 
00021 
00022 */
00023 
00024 # include "incls/_precompiled.incl"
00025 # include "incls/_ostream.cpp.incl"
00026 
00027 outputStream::outputStream(int width) {
00028   _width       = width;
00029   _position    = 0;
00030   _indentation = 0;
00031 }
00032 
00033 void outputStream::basic_print(char* str) {
00034   int len = strlen(str);
00035   for (int i = 0; i < len; i++) put(str[i]);
00036 }
00037 
00038 #define BUFLEN 2000   /* max size of output of individual print() methods */
00039 
00040 void outputStream::print(char* format, ...) {
00041   char buffer[BUFLEN];
00042   va_list ap;
00043   va_start(ap, format);
00044   if (_vsnprintf(buffer, BUFLEN, format, ap) < 0) {
00045     warning("increase BUFLEN in ostream.cpp -- output truncated");
00046     buffer[BUFLEN] = 0;
00047   }
00048   va_end(ap);
00049   basic_print(buffer);
00050 }
00051 
00052 void outputStream::print_cr(char* format, ...) {
00053   char buffer[BUFLEN];
00054   va_list ap;
00055   va_start(ap, format);
00056   if (_vsnprintf(buffer, BUFLEN, format, ap) < 0) {
00057     warning("increase BUFLEN in ostream.cpp -- output truncated");
00058     buffer[BUFLEN] = 0;
00059   }
00060   va_end(ap);
00061   basic_print(buffer);
00062   cr();
00063 }
00064 
00065 void outputStream::vprint(const char *format, va_list argptr) {
00066   char buffer[BUFLEN];
00067   if (_vsnprintf(buffer, BUFLEN, format, argptr) < 0) {
00068     warning("increase BUFLEN in ostream.cpp -- output truncated");
00069     buffer[BUFLEN] = 0;
00070   }
00071   basic_print(buffer);
00072 }
00073 
00074 void outputStream::vprint_cr(const char* format, va_list argptr) {
00075   vprint(format, argptr);
00076   cr();
00077 }
00078 
00079 void outputStream::fill_to(int col) {
00080   while (position() < col) sp();
00081 }
00082 
00083 void outputStream::put(char c) {
00084   lputc(c);
00085   _position++;
00086 }
00087 
00088 void outputStream::out_hex(int addr) {
00089   Unimplemented();
00090 }
00091 
00092 void outputStream::sp() {
00093   put(' ');
00094 }
00095 
00096 void outputStream::cr() {
00097   put('\n');
00098   _position = 0;
00099 }
00100 
00101 void outputStream::indent() {
00102  while (_position < _indentation) sp();
00103 }
00104 
00105 stringStream::stringStream(int initial_size) : outputStream() {
00106   buffer_length = initial_size;
00107   buffer        = NEW_RESOURCE_ARRAY(char, buffer_length);
00108   buffer_pos    = 0;  
00109 }
00110 
00111 void stringStream::put(char c) {
00112   if (buffer_pos >= buffer_length) {
00113     // grow string buffer
00114     char* oldbuf = buffer;
00115     buffer = NEW_RESOURCE_ARRAY(char, buffer_length * 2);
00116     strncpy(buffer, oldbuf, buffer_length);
00117     buffer_length *= 2;
00118   }
00119   buffer[buffer_pos++] = c;
00120   _position++;
00121 }
00122 
00123 char* stringStream::as_string() {
00124   buffer[buffer_pos] = '\0';
00125   char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos + 1);
00126   strcpy(copy, buffer);
00127   return copy;
00128 }
00129 
00130 byteArrayOop stringStream::as_byteArray() {
00131   byteArrayOop a = oopFactory::new_byteArray(buffer_pos);
00132   for (int i = 0; i < buffer_pos; i++) {
00133     a->byte_at_put(i+1, buffer[i]);
00134   }
00135   return a;
00136 }
00137 
00138 fileStream::fileStream(char* file_name) {
00139   _file = fopen(file_name, "w");
00140 }
00141 
00142 void fileStream::put(char c) {
00143   fputc(c, _file);
00144   _position++;
00145 }
00146 
00147 fileStream::~fileStream() {
00148   fclose(_file);
00149   _file = NULL;
00150 }
00151 
00152 outputStream* std;
00153 outputStream* err;
00154 
00155 void ostream_init() {
00156   std = new(true) outputStream();   // NB: this stream is allocated on the C heap
00157   err = std;
00158 }
00159 
00160 

Generated on Mon Oct 9 13:37:21 2006 for Strongtalk VM by  doxygen 1.4.7