00001 /* 00002 * wxChecksums 00003 * Copyright (C) 2003-2004 Julien Couot 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 /** 00021 * \file bytedisp.cpp 00022 * Smart byte displayer. 00023 */ 00024 00025 //--------------------------------------------------------------------------- 00026 // For compilers that support precompilation, includes "wx.h". 00027 #include <wx/wxprec.h> 00028 00029 #ifdef __BORLANDC__ 00030 #pragma hdrstop 00031 #endif 00032 00033 #ifndef WX_PRECOMP 00034 // Include your minimal set of headers here, or wx.h 00035 #include <wx/wx.h> 00036 #endif 00037 00038 #include "bytedisp.hpp" 00039 00040 #include "compat.hpp" 00041 //--------------------------------------------------------------------------- 00042 00043 /// The C++ standard namespace. 00044 using namespace std; 00045 00046 00047 // Static attributes of the BytesDisplayer class 00048 wxString BytesDisplayer::long_units[9]; 00049 wxString BytesDisplayer::short_units[9]; 00050 00051 /* 00052 // Don't work with i18n 00053 00054 const wxString BytesDisplayer::long_units[] = { _("byte"), _("kilobyte"), 00055 _("megabyte"), _("gigabyte"), 00056 _("terabyte"), _("petabyte"), 00057 _("exabyte"), _("zettabyte"), 00058 _("yottabyte") }; 00059 const wxString BytesDisplayer::short_units[] = { _("B"), _("KB"), 00060 _("MB"), _("GB"), 00061 _("TB"), _("PB"), 00062 _("EB"), _("ZB"), 00063 _("YB") };*/ 00064 //--------------------------------------------------------------------------- 00065 00066 00067 /** 00068 * Initializes the static part of the class. 00069 * 00070 * Please call this function before the first use of this class. 00071 */ 00072 void BytesDisplayer::initStatic() 00073 { 00074 long_units[0] = _("byte"); 00075 long_units[1] = _("kilobyte"); 00076 long_units[2] = _("megabyte"); 00077 long_units[3] = _("gigabyte"); 00078 long_units[4] = _("terabyte"); 00079 long_units[5] = _("petabyte"); 00080 long_units[6] = _("exabyte"); 00081 long_units[7] = _("zettabyte"); 00082 long_units[8] = _("yottabyte"); 00083 short_units[0] = _("B"); 00084 short_units[1] = _("KB"); 00085 short_units[2] = _("MB"); 00086 short_units[3] = _("GB"); 00087 short_units[4] = _("TB"); 00088 short_units[5] = _("PB"); 00089 short_units[6] = _("EB"); 00090 short_units[7] = _("ZB"); 00091 short_units[8] = _("YB"); 00092 } 00093 //--------------------------------------------------------------------------- 00094 00095 00096 /** 00097 * Default constructor. 00098 */ 00099 BytesDisplayer::BytesDisplayer() 00100 { 00101 size = 0.0; 00102 unit = byte; 00103 } 00104 //--------------------------------------------------------------------------- 00105 00106 00107 /** 00108 * Clones the source instance in this instance. 00109 * 00110 * @param source Source instance. 00111 */ 00112 void BytesDisplayer::clone(const BytesDisplayer& source) 00113 { 00114 if (this != &source) 00115 { 00116 this->size = source.size; 00117 this->unit = source.unit; 00118 } 00119 } 00120 //--------------------------------------------------------------------------- 00121 00122 00123 /** 00124 * Copy constructor. 00125 * 00126 * @param source Source instance. 00127 */ 00128 BytesDisplayer::BytesDisplayer(const BytesDisplayer& source) 00129 { 00130 clone(source); 00131 } 00132 //--------------------------------------------------------------------------- 00133 00134 00135 /** 00136 * Assignment operator. 00137 * 00138 * @param source Source instance. 00139 * @return A reference on the instance. 00140 */ 00141 BytesDisplayer& BytesDisplayer::operator=(const BytesDisplayer& source) 00142 { 00143 clone(source); 00144 return *this; 00145 } 00146 //--------------------------------------------------------------------------- 00147 00148 00149 /** 00150 * Assignment operator. 00151 * 00152 * @param value The size. 00153 * @return A reference on the instance. 00154 */ 00155 BytesDisplayer& BytesDisplayer::operator=(const double value) 00156 { 00157 size = static_cast<bytesize_type>(value); 00158 unit = byte; 00159 normalize(); 00160 return *this; 00161 } 00162 //--------------------------------------------------------------------------- 00163 00164 00165 /** 00166 * Assignment operator. 00167 * 00168 * @param value The size. 00169 * @return A reference on the instance. 00170 */ 00171 BytesDisplayer& BytesDisplayer::operator=(const long value) 00172 { 00173 size = static_cast<bytesize_type>(value); 00174 unit = byte; 00175 normalize(); 00176 return *this; 00177 } 00178 //--------------------------------------------------------------------------- 00179 00180 00181 /** 00182 * Assignment operator. 00183 * 00184 * @param value The size. 00185 * @return A reference on the instance. 00186 */ 00187 BytesDisplayer& BytesDisplayer::operator=(const unsigned long value) 00188 { 00189 size = static_cast<bytesize_type>(value); 00190 unit = byte; 00191 normalize(); 00192 return *this; 00193 } 00194 //--------------------------------------------------------------------------- 00195 00196 00197 /** 00198 * Constructor with a double type. 00199 * 00200 * @param value The size. 00201 */ 00202 BytesDisplayer::BytesDisplayer(const double value) 00203 { 00204 size = static_cast<bytesize_type>(value); 00205 unit = byte; 00206 normalize(); 00207 } 00208 //--------------------------------------------------------------------------- 00209 00210 00211 /** 00212 * Constructor with a long integer type. 00213 * 00214 * @param value The size. 00215 */ 00216 BytesDisplayer::BytesDisplayer(const long value) 00217 { 00218 size = static_cast<bytesize_type>(value); 00219 unit = byte; 00220 normalize(); 00221 } 00222 //--------------------------------------------------------------------------- 00223 00224 00225 /** 00226 * Constructor with a long integer type. 00227 * 00228 * @param value The size. 00229 */ 00230 BytesDisplayer::BytesDisplayer(const unsigned long value) 00231 { 00232 size = static_cast<bytesize_type>(value); 00233 unit = byte; 00234 normalize(); 00235 } 00236 //--------------------------------------------------------------------------- 00237 00238 00239 /** 00240 * Normalizes the value. 00241 * 00242 * After calling this function: 00243 * - If the size is <CODE>< 0</CODE> then the size equals to <CODE>0</CODE>. 00244 * - If the size is bigger than <CODE>1024</CODE> then the size is bring back 00245 * to the interval [1,1024[. 00246 * - If the size is smaller than <CODE>1</CODE> then the size is bring back 00247 * to the interval [1,1024[. 00248 */ 00249 void BytesDisplayer::normalize() 00250 { 00251 if (size < 0.0) 00252 { 00253 size = 0.0; 00254 unit = byte; 00255 } 00256 else if (size >= 1024.0) 00257 { 00258 while (size >= 1024.0 && unit != yotta) 00259 { 00260 size /= 1024.0; 00261 unit = static_cast<Units>(static_cast<int>(unit) + 1); 00262 } 00263 } 00264 else if (size < 1.0) 00265 { 00266 while (size < 1 && unit != byte) 00267 { 00268 size *= 1024.0; 00269 unit = static_cast<Units>(static_cast<int>(unit) - 1); 00270 } 00271 } 00272 } 00273 //--------------------------------------------------------------------------- 00274 00275 00276 /** 00277 * Operator += 00278 * 00279 * @param a The value to add. 00280 * @return A reference on the instance. 00281 */ 00282 BytesDisplayer& BytesDisplayer::operator+=(const BytesDisplayer& a) 00283 { 00284 bytesize_type val = a.size; 00285 Units uval = a.unit; 00286 int direction = (static_cast<int>(unit) < static_cast<int>(uval)) ? -1 : 1; 00287 00288 while (uval != unit) 00289 { 00290 if (direction == -1) 00291 val *= 1024.0; 00292 else 00293 val /= 1024.0; 00294 uval = static_cast<Units>(static_cast<int>(uval) + direction); 00295 } 00296 00297 size += val; 00298 normalize(); 00299 00300 return *this; 00301 } 00302 //--------------------------------------------------------------------------- 00303 00304 00305 /** 00306 * Gets the size as a double. 00307 * 00308 * @param u Unit in which the size must be returned. 00309 * @return The size as a double. 00310 */ 00311 double BytesDisplayer::toDouble(const Units u) const 00312 { 00313 double val = static_cast<double>(size); 00314 Units uval = unit; 00315 int direction = (static_cast<int>(u) < static_cast<int>(uval)) ? -1 : 1; 00316 00317 while (uval != u) 00318 { 00319 if (direction == -1) 00320 val *= 1024.0; 00321 else 00322 val /= 1024.0; 00323 uval = static_cast<Units>(static_cast<int>(uval) + direction); 00324 } 00325 00326 return val; 00327 } 00328 //--------------------------------------------------------------------------- 00329 00330 00331 /** 00332 * Gets the size as a string. 00333 * 00334 * @param u Unit in which the size must be returned. 00335 * @param uf Format of the unit suffix. 00336 * @return The size as a string. 00337 */ 00338 wxString BytesDisplayer::toString(const Units u, const UnitsFormat uf) const 00339 { 00340 double val = toDouble(u); 00341 wxString res = wxString::Format(wxT("%0.2f"), val); 00342 switch (uf) 00343 { 00344 case None : 00345 break; 00346 case Long : 00347 res += wxT(' ') + long_units[static_cast<int>(u)]; 00348 if (val != 0.0 && val != 1.0) 00349 res += _("s"); 00350 break; 00351 default : 00352 res += wxT(' ') + short_units[static_cast<int>(u)]; 00353 } 00354 00355 return res; 00356 } 00357 //--------------------------------------------------------------------------- 00358 00359 00360 /** 00361 * Gets the size as a string. 00362 * 00363 * @param uf Format of the unit suffix. 00364 * @return The size as a string. 00365 */ 00366 wxString BytesDisplayer::toString(const UnitsFormat uf) const 00367 { 00368 return toString(unit, uf); 00369 } 00370 //---------------------------------------------------------------------------