1/* Functions.m 2 * 3 * Copyright (C) 2004-2016 Free Software Foundation, Inc. 4 * 5 * Author: Enrico Sersale <enrico@imago.ro> 6 * Date: January 2004 7 * 8 * This file is part of the GNUstep Inspector application 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA. 23 */ 24 25#include <limits.h> 26 27#import <Foundation/Foundation.h> 28#import <AppKit/AppKit.h> 29#import "Functions.h" 30 31#define ONE_KB 1024 32#define ONE_MB (ONE_KB * ONE_KB) 33#define ONE_GB (ONE_KB * ONE_MB) 34 35 36static NSString *fix_path(NSString *s, const char *c) 37{ 38 static NSFileManager *mgr = nil; 39 const char *ptr = c; 40 unsigned len; 41 42 if (mgr == nil) { 43 mgr = [NSFileManager defaultManager]; 44 RETAIN (mgr); 45 } 46 47 if (ptr == 0) { 48 if (s == nil) { 49 return nil; 50 } 51 ptr = [s cString]; 52 } 53 54 len = strlen(ptr); 55 56 return [mgr stringWithFileSystemRepresentation: ptr length: len]; 57} 58 59NSString *fixpath(NSString *s, const char *c) 60{ 61 return fix_path(s, c); 62} 63 64NSString *relativePathFit(id container, NSString *fullPath) 65{ 66 NSArray *pathcomps; 67 float cntwidth; 68 NSFont *font; 69 NSString *path; 70 NSString *relpath = nil; 71 NSUInteger i; 72 NSString *prefix; 73 74 cntwidth = [container bounds].size.width; 75 font = [container font]; 76 77 prefix = @"/(..)"; 78 if (![fullPath isAbsolutePath]) 79 prefix = @".."; 80 81 if([font widthOfString: fullPath] < cntwidth) 82 return fullPath; 83 84 cntwidth = cntwidth - [font widthOfString: prefix]; 85 86 pathcomps = [fullPath pathComponents]; 87 i = [pathcomps count] - 1; 88 path = [NSString stringWithString: [pathcomps objectAtIndex: i]]; 89 relpath = path; 90 while(i > 0) 91 { 92 i--; 93 if([font widthOfString: path] < cntwidth) 94 relpath = [NSString stringWithString: path]; 95 else 96 break; 97 path = [[pathcomps objectAtIndex: i] stringByAppendingPathComponent:path];; 98 } 99 relpath = [prefix stringByAppendingPathComponent:relpath]; 100 101 return relpath; 102} 103 104NSString *fsDescription(unsigned long long size) 105{ 106 NSString *sizeStr; 107 char *sign = ""; 108 109 if (size == 1) 110 sizeStr = @"1 byte"; 111 else if (size == 0) 112 sizeStr = @"0 bytes"; 113 else if (size < (10 * ONE_KB)) 114 sizeStr = [NSString stringWithFormat:@"%s %ld bytes", sign, (long)size]; 115 else if (size < (100 * ONE_KB)) 116 sizeStr = [NSString stringWithFormat:@"%s %3.2fKB", sign, 117 ((double)size / (double)(ONE_KB))]; 118 else if(size < (100 * ONE_MB)) 119 sizeStr = [NSString stringWithFormat:@"%s %3.2fMB", sign, 120 ((double)size / (double)(ONE_MB))]; 121 else 122 sizeStr = [NSString stringWithFormat:@"%s %3.2fGB", sign, 123 ((double)size / (double)(ONE_GB))]; 124 125 return sizeStr; 126} 127 128