| 1 | // |
|---|
| 2 | // TxWin, Textmode Windowing Library |
|---|
| 3 | // |
|---|
| 4 | // Original code Copyright (c) 1995-2005 Fsys Software and Jan van Wijk |
|---|
| 5 | // |
|---|
| 6 | // ========================================================================== |
|---|
| 7 | // |
|---|
| 8 | // This file contains Original Code and/or Modifications of Original Code as |
|---|
| 9 | // defined in and that are subject to the GNU Lesser General Public License. |
|---|
| 10 | // You may not use this file except in compliance with the License. |
|---|
| 11 | // BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE LICENSE. |
|---|
| 12 | // A copy of the License is provided with the Original Code and Modifications, |
|---|
| 13 | // and is also available at http://www.dfsee.com/txwin/lgpl.htm |
|---|
| 14 | // |
|---|
| 15 | // This library is free software; you can redistribute it and/or modify |
|---|
| 16 | // it under the terms of the GNU Lesser General Public License as published |
|---|
| 17 | // by the Free Software Foundation; either version 2.1 of the License, |
|---|
| 18 | // or (at your option) any later version. |
|---|
| 19 | // |
|---|
| 20 | // This library is distributed in the hope that it will be useful, |
|---|
| 21 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 22 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 23 | // See the GNU Lesser General Public License for more details. |
|---|
| 24 | // |
|---|
| 25 | // You should have received a copy of the GNU Lesser General Public License |
|---|
| 26 | // along with this library; (lgpl.htm) if not, write to the Free Software |
|---|
| 27 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 28 | // |
|---|
| 29 | // Questions on TxWin licensing can be directed to: txwin@fsys.nl |
|---|
| 30 | // |
|---|
| 31 | // ========================================================================== |
|---|
| 32 | // |
|---|
| 33 | // TX size value to string or TxPrint |
|---|
| 34 | // |
|---|
| 35 | // Author: J. van Wijk |
|---|
| 36 | // |
|---|
| 37 | // JvW 24-07-2005 Initial version, split off from TXUTIL |
|---|
| 38 | |
|---|
| 39 | #include <txlib.h> |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /*****************************************************************************/ |
|---|
| 43 | // Format (file) size in 7.1 value + Byte/KiB/MiB/GiB (11); to TxPrint output |
|---|
| 44 | /*****************************************************************************/ |
|---|
| 45 | void txPrtSize64 |
|---|
| 46 | ( |
|---|
| 47 | char *text, // IN leading string |
|---|
| 48 | LLONG data, // IN size data |
|---|
| 49 | char *trail // IN trailing text |
|---|
| 50 | ) |
|---|
| 51 | { |
|---|
| 52 | TXLN string; |
|---|
| 53 | |
|---|
| 54 | strcpy( string, ""); |
|---|
| 55 | TxPrint("%s", txStrSize64( text, string, data, trail)); |
|---|
| 56 | } // end 'txPrtSize64' |
|---|
| 57 | /*---------------------------------------------------------------------------*/ |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | /*****************************************************************************/ |
|---|
| 61 | // Format (file) size in 7.1 value + Byte/KiB/MiB/GiB (11); append to string |
|---|
| 62 | /*****************************************************************************/ |
|---|
| 63 | char *txStrSize64 // RET resulting string |
|---|
| 64 | ( |
|---|
| 65 | char *str, // OUT resulting string |
|---|
| 66 | char *text, // IN leading string |
|---|
| 67 | LLONG data, // IN size data |
|---|
| 68 | char *trail // IN trailing text |
|---|
| 69 | ) |
|---|
| 70 | { |
|---|
| 71 | TXTS form; |
|---|
| 72 | |
|---|
| 73 | strcat( str, text); |
|---|
| 74 | if (data <= 99999) // express as a byte-value |
|---|
| 75 | { |
|---|
| 76 | sprintf( form, "%5lld bytes", data); |
|---|
| 77 | } |
|---|
| 78 | else // use KiB/MiB/GiB/TiB |
|---|
| 79 | { |
|---|
| 80 | double value = ((double) data) / 1024.0; // start with KiB |
|---|
| 81 | |
|---|
| 82 | if (value <= 4999.9) // arbitrary, but 7.8 GiB |
|---|
| 83 | { // works out OK then :-) |
|---|
| 84 | sprintf( form, "%7.1lf KiB", value); // consistent with DFSee |
|---|
| 85 | } |
|---|
| 86 | else |
|---|
| 87 | { |
|---|
| 88 | value /= 1024.0; // convert to MiB |
|---|
| 89 | if (value <= 49999.9) |
|---|
| 90 | { |
|---|
| 91 | sprintf(form, "%7.1lf MiB", value); |
|---|
| 92 | } |
|---|
| 93 | else |
|---|
| 94 | { |
|---|
| 95 | value /= 1024.0; // convert to GiB |
|---|
| 96 | if (value <= 49999.9) |
|---|
| 97 | { |
|---|
| 98 | sprintf(form, "%7.1lf GiB", value); |
|---|
| 99 | } |
|---|
| 100 | else |
|---|
| 101 | { |
|---|
| 102 | value /= 1024.0; // convert to TiB |
|---|
| 103 | sprintf(form, "%7.1lf TiB", value); |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | strcat( str, form); |
|---|
| 109 | strcat( str, trail); |
|---|
| 110 | return( str); |
|---|
| 111 | } // end 'txStrSize64' |
|---|
| 112 | /*---------------------------------------------------------------------------*/ |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | /*****************************************************************************/ |
|---|
| 116 | // Format time in seconds in an HHH:MM:SS string (Elapsed/ETA) |
|---|
| 117 | /*****************************************************************************/ |
|---|
| 118 | char *txStrSec2hms // RET resulting string |
|---|
| 119 | ( |
|---|
| 120 | char *str, // OUT resulting string |
|---|
| 121 | char *text, // IN leading string |
|---|
| 122 | ULONG sec, // IN seconds |
|---|
| 123 | char *trail // IN trailing text |
|---|
| 124 | ) |
|---|
| 125 | { |
|---|
| 126 | TXTS form = {0}; |
|---|
| 127 | ULONG hours = (sec / 3600); |
|---|
| 128 | ULONG minutes = (sec % 3600) / 60; |
|---|
| 129 | ULONG seconds = (sec % 60); |
|---|
| 130 | |
|---|
| 131 | strcat( str, text); |
|---|
| 132 | |
|---|
| 133 | if (hours) |
|---|
| 134 | { |
|---|
| 135 | sprintf( form, "%lu:", hours); |
|---|
| 136 | } |
|---|
| 137 | strcat( str, form); |
|---|
| 138 | |
|---|
| 139 | sprintf( form, "%lu:%02lu", minutes, seconds); |
|---|
| 140 | if (minutes < 10) |
|---|
| 141 | { |
|---|
| 142 | strcat( str, (hours) ? "0" : " "); |
|---|
| 143 | } |
|---|
| 144 | strcat( str, form); |
|---|
| 145 | strcat( str, trail); |
|---|
| 146 | return( str); |
|---|
| 147 | } // end 'txStrSec2hms' |
|---|
| 148 | /*---------------------------------------------------------------------------*/ |
|---|