libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/FSize.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:       FSize.cc
00020 
00021   Author:     Michael Andres <ma@suse.de>
00022   Maintainer: Michael Andres <ma@suse.de>
00023 
00024   Purpose:
00025 
00026 /-*/
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <iostream>
00031 
00032 #include "FSize.h"
00033 
00034 FSize::FSize( const std::string &sizeStr, const Unit unit_r )
00035   : _size( atoll( sizeStr.c_str() ) * factor( unit_r ) )
00036 {
00037 }
00038 
00039 //
00040 //
00041 //      METHOD NAME : FSize::fillBlock
00042 //      METHOD TYPE : FSize &
00043 //
00044 //      DESCRIPTION :
00045 //
00046 FSize & FSize::fillBlock( FSize blocksize_r )
00047 {
00048   if ( _size && blocksize_r ) {
00049     long long diff = _size % blocksize_r;
00050     if ( diff ) {
00051       if ( _size > 0 )
00052         _size += blocksize_r;
00053       _size -= diff;
00054     }
00055   }
00056   return *this;
00057 }
00058 
00059 //
00060 //
00061 //      METHOD NAME : FSize::bestUnit
00062 //      METHOD TYPE : FSize::Unit
00063 //
00064 //      DESCRIPTION :
00065 //
00066 FSize::Unit FSize::bestUnit() const
00067 {
00068   long long usize( _size < 0 ? -_size : _size );
00069   if ( usize < KB )
00070     return B;
00071   if ( usize < MB )
00072     return K;
00073   if ( usize < GB )
00074     return M;
00075   if ( usize < TB )
00076     return G;
00077   return T;
00078 }
00079 
00080 //
00081 //
00082 //      METHOD NAME : FSize::form
00083 //      METHOD TYPE : std::string
00084 //
00085 //      DESCRIPTION :
00086 //
00087 std::string FSize::form( const Unit unit_r, unsigned fw, unsigned prec, const bool showunit ) const
00088 {
00089   if ( prec == bestPrec ) {
00090     switch ( unit_r )
00091     {
00092       case T:  prec = 3; break;
00093       case G:  prec = 2; break;
00094       case M:  prec = 1; break;
00095       case K:  prec = 1; break;
00096       case B:  prec = 0; break;
00097     }
00098   } else if ( unit_r == B )
00099     prec = 0; // doesn't make sense for Byte
00100 
00101   char buffer[80]; // should be long enough for any numeric sprintf()
00102   snprintf( buffer, sizeof( buffer ),
00103             "%*.*f",
00104             fw, prec, ( double( _size ) / factor( unit_r ) ) );
00105 
00106   std::string ret( buffer );
00107 
00108   if ( showunit )
00109     ret += std::string(" ") + unit( unit_r );
00110 
00111   return ret;
00112 }
00113 
00114 
00115 //
00116 //
00117 //      METHOD NAME : FSize::asString
00118 //      METHOD TYPE : std::string
00119 //
00120 //      DESCRIPTION :
00121 //
00122 std::string FSize::asString() const
00123 {
00124   return form();
00125 }
 All Classes Functions Variables Enumerations Friends