diff options
author | Jens Arnold <amiconn@rockbox.org> | 2005-01-31 00:39:20 +0000 |
---|---|---|
committer | Jens Arnold <amiconn@rockbox.org> | 2005-01-31 00:39:20 +0000 |
commit | 2116bba296f12bd94024ec7c39ae03fbfcc5bdef (patch) | |
tree | 29c49672b2dcf2012bb6bb69f8130f024f52bdb9 /apps/misc.c | |
parent | 19afad88f8bb973726af0c6a7850dbd992996a9f (diff) | |
download | rockbox-2116bba296f12bd94024ec7c39ae03fbfcc5bdef.tar.gz rockbox-2116bba296f12bd94024ec7c39ae03fbfcc5bdef.zip |
New function for formatting large-range values for output, both printed and voiced. This replaces num2max5(). It is currently used for the total/free space display in the info menu, for the recorded number of bytes (recorders) and the MMC debug info (Ondios).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@5721 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/misc.c')
-rw-r--r-- | apps/misc.c | 75 |
1 files changed, 55 insertions, 20 deletions
diff --git a/apps/misc.c b/apps/misc.c index b4a21a1dac..1024f9bb80 100644 --- a/apps/misc.c +++ b/apps/misc.c | |||
@@ -28,6 +28,7 @@ | |||
28 | #include "system.h" | 28 | #include "system.h" |
29 | #include "timefuncs.h" | 29 | #include "timefuncs.h" |
30 | #include "screens.h" | 30 | #include "screens.h" |
31 | #include "talk.h" | ||
31 | #include "mpeg.h" | 32 | #include "mpeg.h" |
32 | #include "mp3_playback.h" | 33 | #include "mp3_playback.h" |
33 | #include "settings.h" | 34 | #include "settings.h" |
@@ -42,33 +43,67 @@ | |||
42 | #define ONE_KILOBYTE 1024 | 43 | #define ONE_KILOBYTE 1024 |
43 | #define ONE_MEGABYTE (1024*1024) | 44 | #define ONE_MEGABYTE (1024*1024) |
44 | 45 | ||
45 | /* The point of this function would be to return a string of the input data, | 46 | /* Format a large-range value for output, using the appropriate unit so that |
46 | but never longer than 5 columns. Add suffix k and M when suitable... | 47 | * the displayed value is in the range 1 <= display < 1000 (1024 for "binary" |
47 | Make sure to have space for 6 bytes in the buffer. 5 letters plus the | 48 | * units) if possible, and 3 significant digits are shown. If a buffer is |
48 | terminating zero byte. */ | 49 | * given, the result is snprintf()'d into that buffer, otherwise the result is |
49 | char *num2max5(unsigned int bytes, char *max5) | 50 | * voiced.*/ |
51 | char *output_dyn_value(char *buf, int buf_size, int value, | ||
52 | const unsigned char **units, bool bin_scale) | ||
50 | { | 53 | { |
51 | if(bytes < 100000) { | 54 | int scale = bin_scale ? 1024 : 1000; |
52 | snprintf(max5, 6, "%5d", bytes); | 55 | int fraction = 0; |
53 | return max5; | 56 | int unit_no = 0; |
57 | char tbuf[5]; | ||
58 | |||
59 | while (value >= scale) | ||
60 | { | ||
61 | fraction = value % scale; | ||
62 | value /= scale; | ||
63 | unit_no++; | ||
54 | } | 64 | } |
55 | if(bytes < (9999*ONE_KILOBYTE)) { | 65 | if (bin_scale) |
56 | snprintf(max5, 6, "%4dk", bytes/ONE_KILOBYTE); | 66 | fraction = fraction * 1000 / 1024; |
57 | return max5; | 67 | |
68 | if (buf) | ||
69 | { | ||
70 | if (value >= 100 || !unit_no) | ||
71 | snprintf(tbuf, sizeof(tbuf), "%d", value); | ||
72 | else if (value >= 10) | ||
73 | snprintf(tbuf, sizeof(tbuf), "%d%s%01d", value, str(LANG_POINT), | ||
74 | fraction / 100); | ||
75 | else | ||
76 | snprintf(tbuf, sizeof(tbuf), "%d%s%02d", value, str(LANG_POINT), | ||
77 | fraction / 10); | ||
78 | |||
79 | snprintf(buf, buf_size, "%s %s", tbuf, P2STR(units[unit_no])); | ||
58 | } | 80 | } |
59 | if(bytes < (100*ONE_MEGABYTE)) { | 81 | else |
60 | /* 'XX.XM' is good as long as we're less than 100 megs */ | 82 | { |
61 | snprintf(max5, 6, "%2d.%0dM", | 83 | if (value >= 100 || !unit_no) |
62 | bytes/ONE_MEGABYTE, | 84 | tbuf[0] = '\0'; |
63 | (bytes%ONE_MEGABYTE)/(ONE_MEGABYTE/10) ); | 85 | else if (value >= 10) |
64 | return max5; | 86 | snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100); |
87 | else | ||
88 | snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10); | ||
89 | |||
90 | /* strip trailing zeros from the fraction */ | ||
91 | for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--) | ||
92 | tbuf[i] = '\0'; | ||
93 | |||
94 | talk_number(value, true); | ||
95 | if (strlen(tbuf)) | ||
96 | { | ||
97 | talk_id(LANG_POINT, true); | ||
98 | talk_spell(tbuf, true); | ||
99 | } | ||
100 | talk_id(P2ID(units[unit_no]), true); | ||
65 | } | 101 | } |
66 | snprintf(max5, 6, "%4dM", bytes/ONE_MEGABYTE); | 102 | return buf; |
67 | return max5; | ||
68 | } | 103 | } |
69 | 104 | ||
70 | /* Read (up to) a line of text from fd into buffer and return number of bytes | 105 | /* Read (up to) a line of text from fd into buffer and return number of bytes |
71 | * read (which may be larger than the number of bytes stored in buffer). If | 106 | * read (which may be larger than the number of bytes stored in buffer). If |
72 | * an error occurs, -1 is returned (and buffer contains whatever could be | 107 | * an error occurs, -1 is returned (and buffer contains whatever could be |
73 | * read). A line is terminated by a LF char. Neither LF nor CR chars are | 108 | * read). A line is terminated by a LF char. Neither LF nor CR chars are |
74 | * stored in buffer. | 109 | * stored in buffer. |