made a copy
[strongtalk-kjk.git] / vm / utilities / ostream.cpp
blobe7f888fba9a361d7bdd3c1a55016c52713257535
1 /* Copyright 1994, 1995 LongView Technologies L.L.C. $Revision: 1.16 $ */
2 /* Copyright (c) 2006, Sun Microsystems, Inc.
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6 following conditions are met:
8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
10 disclaimer in the documentation and/or other materials provided with the distribution.
11 * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived
12 from this software without specific prior written permission.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
15 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
16 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
19 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
24 # include "incls/_precompiled.incl"
25 # include "incls/_ostream.cpp.incl"
27 outputStream::outputStream(int width) {
28 _width = width;
29 _position = 0;
30 _indentation = 0;
33 void outputStream::basic_print(char* str) {
34 int len = strlen(str);
35 for (int i = 0; i < len; i++) put(str[i]);
38 #define BUFLEN 2000 /* max size of output of individual print() methods */
40 void outputStream::print(char* format, ...) {
41 char buffer[BUFLEN];
42 va_list ap;
43 va_start(ap, format);
44 if (_vsnprintf(buffer, BUFLEN, format, ap) < 0) {
45 warning("increase BUFLEN in ostream.cpp -- output truncated");
46 buffer[BUFLEN] = 0;
48 va_end(ap);
49 basic_print(buffer);
52 void outputStream::print_cr(char* format, ...) {
53 char buffer[BUFLEN];
54 va_list ap;
55 va_start(ap, format);
56 if (_vsnprintf(buffer, BUFLEN, format, ap) < 0) {
57 warning("increase BUFLEN in ostream.cpp -- output truncated");
58 buffer[BUFLEN] = 0;
60 va_end(ap);
61 basic_print(buffer);
62 cr();
65 void outputStream::vprint(const char *format, va_list argptr) {
66 char buffer[BUFLEN];
67 if (_vsnprintf(buffer, BUFLEN, format, argptr) < 0) {
68 warning("increase BUFLEN in ostream.cpp -- output truncated");
69 buffer[BUFLEN] = 0;
71 basic_print(buffer);
74 void outputStream::vprint_cr(const char* format, va_list argptr) {
75 vprint(format, argptr);
76 cr();
79 void outputStream::fill_to(int col) {
80 while (position() < col) sp();
83 void outputStream::put(char c) {
84 lputc(c);
85 _position++;
88 void outputStream::out_hex(int addr) {
89 Unimplemented();
92 void outputStream::sp() {
93 put(' ');
96 void outputStream::cr() {
97 put('\n');
98 _position = 0;
101 void outputStream::indent() {
102 while (_position < _indentation) sp();
105 stringStream::stringStream(int initial_size) : outputStream() {
106 buffer_length = initial_size;
107 buffer = NEW_RESOURCE_ARRAY(char, buffer_length);
108 buffer_pos = 0;
111 void stringStream::put(char c) {
112 if (buffer_pos >= buffer_length) {
113 // grow string buffer
114 char* oldbuf = buffer;
115 buffer = NEW_RESOURCE_ARRAY(char, buffer_length * 2);
116 strncpy(buffer, oldbuf, buffer_length);
117 buffer_length *= 2;
119 buffer[buffer_pos++] = c;
120 _position++;
123 char* stringStream::as_string() {
124 buffer[buffer_pos] = '\0';
125 char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos + 1);
126 strcpy(copy, buffer);
127 return copy;
130 byteArrayOop stringStream::as_byteArray() {
131 byteArrayOop a = oopFactory::new_byteArray(buffer_pos);
132 for (int i = 0; i < buffer_pos; i++) {
133 a->byte_at_put(i+1, buffer[i]);
135 return a;
138 fileStream::fileStream(char* file_name) {
139 _file = fopen(file_name, "w");
142 void fileStream::put(char c) {
143 fputc(c, _file);
144 _position++;
147 fileStream::~fileStream() {
148 fclose(_file);
149 _file = NULL;
152 outputStream* std;
153 outputStream* err;
155 void ostream_init() {
156 std = new(true) outputStream(); // NB: this stream is allocated on the C heap
157 err = std;