wrowe 01/07/25 14:32:45
Modified: include apr_strings.h
strings apr_strings.c
Log:
Replace the very limited-use ap_send_size with apr_strfsize(), which
works within a fixed buffer. I'm open to using the old ap_send_size()
text formatting, but this result is one character shorter in size
and equally readable.
Revision Changes Path
1.21 +11 -0 apr/include/apr_strings.h
Index: apr_strings.h
===================================================================
RCS file: /home/cvs/apr/include/apr_strings.h,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- apr_strings.h 2001/07/25 16:09:35 1.20
+++ apr_strings.h 2001/07/25 21:32:45 1.21
@@ -307,6 +307,17 @@
*/
APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n);
+/**
+ * Format a binary size (magnitiudes are 2^10 rather than 10^3) from an
apr_off_t,
+ * as bytes, K, M, T, etc, to a four character compacted human readable
string.
+ * @param size The size to format
+ * @param buf The 5 byte text buffer (counting the trailing null)
+ * @return The buf passed to apr_strfsize()
+ * @deffunc char *apr_strfsize(apr_off_t size, char *buf)
+ * @tip All negative sizes report ' - ', apr_strfsize only formats positive
values.
+ */
+APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf);
+
#ifdef __cplusplus
}
#endif
1.19 +34 -0 apr/strings/apr_strings.c
Index: apr_strings.c
===================================================================
RCS file: /home/cvs/apr/strings/apr_strings.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- apr_strings.c 2001/07/25 19:45:27 1.18
+++ apr_strings.c 2001/07/25 21:32:45 1.19
@@ -235,3 +235,37 @@
}
return start;
}
+
+APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf)
+{
+ const char ord[] = "KMTPE";
+ const char *o = ord;
+ int remain;
+
+ if (size < 0) {
+ return strcpy(buf, " - ");
+ }
+ if (size < 973) {
+ sprintf(buf, "%3d ", (int) size, o);
+ return buf;
+ }
+ do {
+ remain = (int)(size & 1023);
+ size >>= 10;
+ if (size >= 973) {
+ ++o;
+ continue;
+ }
+ if (size < 9 || (size == 9 && remain < 973)) {
+ if ((remain = ((remain * 5) + 256) / 512) >= 10)
+ ++size, remain = 0;
+ sprintf(buf, "%d.%d%c", (int) size, remain, *o);
+ return buf;
+ }
+ if (remain >= 512)
+ ++size;
+ sprintf(buf, "%3d%c", (int) size, *o);
+ return buf;
+ } while (1);
+}
+