Commit a24c2e7d authored by Felix Paul Kühne's avatar 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
parent 09758763
......@@ -243,3 +243,25 @@
@interface NSView (EnableSubviews)
- (void)enableSubviews:(BOOL)b_enable;
@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
@interface VLCByteCountFormatter : NSFormatter {
}
+ (NSString *)stringFromByteCount:(long long)byteCount countStyle:(NSByteCountFormatterCountStyle)countStyle;
@end
......@@ -984,3 +984,76 @@ void _drawFrameInRect(NSRect frameRect)
}
@end
/*****************************************************************************
* VLCByteCountFormatter addition
*****************************************************************************/
#ifndef MAC_OS_X_VERSION_10_8
@interface NSByteCountFormatter (IntroducedInMountainLion)
+ (NSString *)stringFromByteCount:(long long)byteCount countStyle:(NSByteCountFormatterCountStyle)countStyle;
@end
#endif
@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;
}
@end
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment