sc_int64_io.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003   The following code is derived, directly or indirectly, from the SystemC
00004   source code Copyright (c) 1996-2006 by all Contributors.
00005   All Rights reserved.
00006 
00007   The contents of this file are subject to the restrictions and limitations
00008   set forth in the SystemC Open Source License Version 2.4 (the "License");
00009   You may not use this file except in compliance with such restrictions and
00010   limitations. You may obtain instructions on how to receive a copy of the
00011   License at http://www.systemc.org/. Software distributed by Contributors
00012   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00013   ANY KIND, either express or implied. See the License for the specific
00014   language governing rights and limitations under the License.
00015 
00016  *****************************************************************************/
00017 
00018 /*****************************************************************************
00019 
00020   sc_int64_io.cpp --
00021 
00022   Original Author: Amit Rao, Synopsys, Inc.
00023 
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027 
00028   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00029   changes you are making here.
00030 
00031       Name, Affiliation, Date:
00032   Description of Modification:
00033 
00034  *****************************************************************************/
00035 
00036 
00037 // $Log: sc_int64_io.cpp,v $
00038 // Revision 1.1.1.1  2006/12/15 20:31:36  acg
00039 // SystemC 2.2
00040 //
00041 // Revision 1.3  2006/01/13 18:49:31  acg
00042 // Added $Log command so that CVS check in comments are reproduced in the
00043 // source.
00044 //
00045 
00046 #include "sysc/utils/sc_iostream.h"
00047 #include "sysc/datatypes/int/sc_unsigned.h"
00048 #include "sysc/datatypes/int/sc_signed.h"
00049 #include "sysc/datatypes/int/sc_int_base.h"
00050 #include "sysc/datatypes/int/sc_uint_base.h"
00051 
00052 
00053 #if defined( _MSC_VER )
00054 
00055 namespace sc_dt
00056 {
00057 
00058 static void
00059 write_uint64(::std::ostream& os, uint64 val, int sign)
00060 {
00061     const int WRITE_BUF_SIZE = 10 + sizeof(uint64)*3;
00062     char buf[WRITE_BUF_SIZE];
00063     char* buf_ptr = buf + WRITE_BUF_SIZE;
00064     const char* show_base = "";
00065     int show_base_len = 0;
00066     int show_pos = 0;
00067     fmtflags flags = os.flags();
00068 
00069     if ((flags & ::std::ios::basefield) == ::std::ios::oct) {
00070         do {
00071             *--buf_ptr = (char)((val & 7) + '0');
00072             val = val >> 3;
00073         } while (val != 0);
00074         if ((flags & ::std::ios::showbase) && (*buf_ptr != '0'))
00075             *--buf_ptr = '0';
00076     } else if ((flags & ::std::ios::basefield) == ::std::ios::hex) {
00077         const char* xdigs = (flags & ::std::ios::uppercase) ? 
00078             "0123456789ABCDEF0X" : 
00079             "0123456789abcdef0x";
00080         do {
00081             *--buf_ptr = xdigs[val & 15];
00082             val = val >> 4;
00083         } while (val != 0);
00084         if ((flags & ::std::ios::showbase)) {
00085             show_base = xdigs + 16;
00086             show_base_len = 2;
00087         }
00088     } else {
00089         while (val > UINT_MAX) {
00090             *--buf_ptr = (char)((val % 10) + '0');
00091             val /= 10;
00092         }
00093         unsigned ival = (unsigned) val;
00094         do {
00095             *--buf_ptr = (ival % 10) + '0';
00096             ival /= 10;
00097         } while (ival != 0);
00098         if (sign > 0 && (flags & ::std::ios::showpos))
00099             show_pos = 1;
00100     }
00101 
00102     int buf_len = buf + WRITE_BUF_SIZE - buf_ptr;
00103     int w = os.width(0);
00104 
00105     int len = buf_len + show_pos;
00106     if (sign < 0) len++;
00107     len += show_base_len;
00108 
00109     int padding = len > w ? 0 : w - len;
00110     fmtflags pad_kind = flags & ::std::ios::adjustfield;
00111     char fill_char = os.fill();
00112 
00113     if (padding > 0 &&
00114         ::std::ios::left != pad_kind &&
00115         ::std::ios::internal != pad_kind) {
00116         for (int i = padding - 1; i >= 0; --i) {
00117             if (! os.put(fill_char))
00118                 goto fail;
00119         }
00120     }
00121     if (sign < 0 || show_pos) {
00122         if (! os.put(sign < 0 ? '-' : '+'))
00123             goto fail;
00124     }
00125     if (show_base_len) {
00126         if (! os.write(show_base, show_base_len))
00127             goto fail;
00128     }
00129     if ((fmtflags)::std::ios::internal == pad_kind && padding > 0) {
00130         for (int i = padding - 1; i >= 0; --i) {
00131             if (! os.put(fill_char))
00132                 goto fail;
00133         }
00134     }
00135     if (! os.write(buf_ptr, buf_len))
00136         goto fail;
00137     if ((fmtflags)::std::ios::left == pad_kind && padding > 0) {
00138         for (int i = padding - 1; i >= 0; --i) {
00139             if (! os.put(fill_char))
00140                 goto fail;
00141         }
00142     }
00143     os.osfx();
00144     return;
00145 fail:
00146     //os.set(::std::ios::badbit);
00147     os.osfx();
00148 }
00149 
00150 ::std::ostream&
00151 operator << ( ::std::ostream& os, int64 n )
00152 {
00153     if (os.opfx()) {
00154         int sign = 1;
00155         uint64 abs_n = (uint64) n;
00156         if (n < 0 && (os.flags() & (::std::ios::oct|::std::ios::hex)) == 0) {
00157             abs_n = -1*((uint64) n);
00158             sign = -1;
00159         }
00160         sc_dt::write_uint64(os, abs_n, sign);
00161     }
00162     return os;
00163 }
00164 
00165 ::std::ostream&
00166 operator << ( ::std::ostream& os, uint64 n )
00167 {
00168     if (os.opfx()) {
00169         sc_dt::write_uint64(os, n, 0);
00170     }
00171     return os;
00172 }
00173 
00174 } // namespace sc_dt
00175 
00176 
00177 #endif

Generated on Wed Jan 21 15:32:08 2009 for SystemC by  doxygen 1.5.5