Update info.c to use size_trun_len() & Co.
David Martin
dmartina at excite.com
Sat Sep 1 16:57:50 UTC 2001
May I propose some rearrangements for inclusion/discussion?
Basically I like the patch. We agree it's not easy to understand the code,
so we must rewrite the comments. Moreover, it's a useful function, likely to
be ported and documentation would help a lot.
On the other hand, I try to avoid using "break" (and relatives) when
possible. I feel we had too many here. I think there's little or no overhead
when removing them and I find this more readable.
A couple of things my students could point:
- Why do we call the function size_trunc_len() when it's a general procedure
we could use for many other (binary) things?
- Why do we use signed integers for "size" and "units"?
Time to go back to work.
*8-) David
------------
--- ChangeLog.v0 Thu Aug 30 18:59:12 2001
+++ ChangeLog Sat Sep 1 17:39:56 2001
@@ -1,3 +1,8 @@
+2001-09-01 David Martin <dmartina at excite.es>
+
+ * util.c (size_trunc_len): Rearrange comments and code for
+ readability and structure.
+
2001-08-30 David Martin <dmartina at excite.es>
* info.c (info_show_info): Use size_trunc_len() instead of
--- util.c.v0 Thu Aug 30 18:41:08 2001
+++ util.c Sat Sep 1 12:16:48 2001
@@ -333,25 +333,33 @@
for (j = units; suffix [j] != NULL; j++) {
if (size == 0) {
if (j == units) {
- /* Empty files will print "0" even with minimal width. */
+ /* First iteration => True empty files: print "0". */
g_snprintf (buffer, len + 1, "0");
- break;
- }
- /* Use "~K" or just "K" if len is 1. Use "B" for bytes. */
- g_snprintf (buffer, len + 1, (len > 1) ? "~%s" : "%s",
- (j > 1) ? suffix[j - 1] : "B");
- break;
- }
+ } else {
+ /* Ficticious zero from previous scaling+rounding:
+ - Print no number but "~" (meaning "some"), if possible.
+ - Use previous suffix, or "B" for bytes. */
+ g_snprintf (buffer, len + 1, (len > 1) ? "~%s" : "%s",
+ (j > 1) ? suffix[j - 1] : "B");
+ }
- if (size < power10 [len - (j > 0)]) {
+ } else if (size < power10 [len - (j > 0)]) {
+ /* Length of size is valid: Print its value and suffix */
g_snprintf (buffer, len + 1, "%lu%s", (unsigned long) size,
suffix[j]);
- break;
- }
- /* Powers of 1024, with rounding. */
- size = (size + 512) >> 10;
+ } else {
+ /* Scaling: Divide/rotate with powers of 1024 + rounding. */
+ size = (size + 512) >> 10;
+ continue;
+ }
+
+ /* Size was printed: Go away*/
+ return;
}
+
+ /* Shortage of suffixes! Should be unreachable. Alert programmers. */
+ g_snprintf (buffer, len + 1, "#");
}
int is_exe (mode_t mode)
_______________________________________________________
http://inbox.excite.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: util_strunc2.diff.gz
Type: application/x-gzip-compressed
Size: 929 bytes
Desc: not available
URL: <http://lists.midnight-commander.org/pipermail/mc-devel/attachments/20010901/5247f7e8/attachment.bin>
More information about the mc-devel
mailing list