[vlc-commits] macosx: reimplement NSByteCountFormatter for 10.7 and earlier

Felix Paul Kühne git at videolan.org
Sun Feb 2 22:50:00 CET 2014


vlc | branch: master | Felix Paul Kühne <fkuehne at videolan.org> | Sun Feb  2 22:46:08 2014 +0100| [a24c2e7d38b888915ed1ec2c3198ef616c719959] | committer: Felix Paul Kühne

macosx: reimplement NSByteCountFormatter for 10.7 and earlier

Note that this implementation only includes the features we care about and is incomplete

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=a24c2e7d38b888915ed1ec2c3198ef616c719959
---

 modules/gui/macosx/misc.h     |   24 +++++++++++++-
 modules/gui/macosx/misc.m     |   73 +++++++++++++++++++++++++++++++++++++++++
 modules/gui/macosx/playlist.m |    2 +-
 3 files changed, 97 insertions(+), 2 deletions(-)

diff --git a/modules/gui/macosx/misc.h b/modules/gui/macosx/misc.h
index b759483..4f38bf0 100644
--- a/modules/gui/macosx/misc.h
+++ b/modules/gui/macosx/misc.h
@@ -242,4 +242,26 @@
 
 @interface NSView (EnableSubviews)
 - (void)enableSubviews:(BOOL)b_enable;
- at end
\ No newline at end of file
+ at end
+
+/*****************************************************************************
+ * VLCByteCountFormatter addition
+ *****************************************************************************/
+
+#ifndef MAC_OS_X_VERSION_10_8
+typedef NS_ENUM(NSInteger, NSByteCountFormatterCountStyle) {
+    // Specifies display of file or storage byte counts. The actual behavior for this is platform-specific; on OS X 10.7 and less, this uses the binary style, but decimal style on 10.8 and above
+    NSByteCountFormatterCountStyleFile   = 0,
+    // Specifies display of memory byte counts. The actual behavior for this is platform-specific; on OS X 10.7 and less, this uses the binary style, but that may change over time.
+    NSByteCountFormatterCountStyleMemory = 1,
+    // The following two allow specifying the number of bytes for KB explicitly. It's better to use one of the above values in most cases.
+    NSByteCountFormatterCountStyleDecimal = 2,    // 1000 bytes are shown as 1 KB
+    NSByteCountFormatterCountStyleBinary  = 3     // 1024 bytes are shown as 1 KB
+};
+#endif
+
+ at interface VLCByteCountFormatter : NSFormatter {
+}
+
++ (NSString *)stringFromByteCount:(long long)byteCount countStyle:(NSByteCountFormatterCountStyle)countStyle;
+ at end
diff --git a/modules/gui/macosx/misc.m b/modules/gui/macosx/misc.m
index 9be3e73..06b2a5f 100644
--- a/modules/gui/macosx/misc.m
+++ b/modules/gui/macosx/misc.m
@@ -984,3 +984,76 @@ void _drawFrameInRect(NSRect frameRect)
 }
 
 @end
+
+/*****************************************************************************
+ * VLCByteCountFormatter addition
+ *****************************************************************************/
+
+#ifndef MAC_OS_X_VERSION_10_8
+ at interface NSByteCountFormatter (IntroducedInMountainLion)
++ (NSString *)stringFromByteCount:(long long)byteCount countStyle:(NSByteCountFormatterCountStyle)countStyle;
+ at end
+#endif
+
+
+ at implementation VLCByteCountFormatter
+
++ (NSString *)stringFromByteCount:(long long)byteCount countStyle:(NSByteCountFormatterCountStyle)countStyle
+{
+    if (OSX_MAVERICKS || OSX_MOUNTAIN_LION)
+        return [NSByteCountFormatter stringFromByteCount:byteCount countStyle:NSByteCountFormatterCountStyleFile];
+
+    float devider = 0.;
+    float returnValue = 0.;
+    NSString *suffix;
+
+    NSNumberFormatter *theFormatter = [[NSNumberFormatter alloc] init];
+    [theFormatter setLocale:[NSLocale currentLocale]];
+    [theFormatter setAllowsFloats:YES];
+
+    NSString *returnString = @"";
+
+    if (countStyle != NSByteCountFormatterCountStyleDecimal)
+        devider = 1024.;
+    else
+        devider = 1000.;
+
+    if (byteCount < 1000) {
+        returnValue = byteCount;
+        suffix = _NS("B");
+        [theFormatter setMaximumFractionDigits:0];
+        goto end;
+    }
+
+    if (byteCount < 1000000) {
+        returnValue = byteCount / devider;
+        suffix = _NS("KB");
+        [theFormatter setMaximumFractionDigits:0];
+        goto end;
+    }
+
+    if (byteCount < 1000000000) {
+        returnValue = byteCount / devider / devider;
+        suffix = _NS("MB");
+        [theFormatter setMaximumFractionDigits:1];
+        goto end;
+    }
+
+    [theFormatter setMaximumFractionDigits:2];
+    if (byteCount < 1000000000000) {
+        returnValue = byteCount / devider / devider / devider;
+        suffix = _NS("GB");
+        goto end;
+    }
+
+    returnValue = byteCount / devider / devider / devider / devider;
+    suffix = _NS("TB");
+
+end:
+    returnString = [NSString stringWithFormat:@"%@ %@", [theFormatter stringFromNumber:[NSNumber numberWithFloat:returnValue]], suffix];
+    [theFormatter release];
+
+    return returnString;
+}
+
+ at end
diff --git a/modules/gui/macosx/playlist.m b/modules/gui/macosx/playlist.m
index f2c8052..ada48d1 100644
--- a/modules/gui/macosx/playlist.m
+++ b/modules/gui/macosx/playlist.m
@@ -375,7 +375,7 @@
                 if ([fileManager fileExistsAtPath:[url path]]) {
                     NSError *error;
                     NSDictionary *attributes = [fileManager attributesOfItemAtPath:[url path] error:&error];
-                    o_value = [NSByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleDecimal];
+                    o_value = [VLCByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleDecimal];
                 }
             }
             free(psz_value);



More information about the vlc-commits mailing list