Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 86 lines (66 sloc) 2.777 kB
dc5be1c Initial check-in.
uli authored
1 /* =============================================================================
2 PROJECT: Filie
3 FILE: NSFileManager+NameForTempFile.m
4
5 PURPOSE: Assorted unique-filename-generation methods.
6
7 COPYRIGHT: (c) 2004 by M. Uli Kusterer, all rights reserved.
8
9 AUTHORS: M. Uli Kusterer - UK
10
11 LICENSES: GNU GPL, Modified BSD
12
13 REVISIONS:
14 2004-02-08 UK Created.
15 ========================================================================== */
16
17 // -----------------------------------------------------------------------------
18 // Headers:
19 // -----------------------------------------------------------------------------
20
21 #import "NSFileManager+NameForTempFile.h"
22
23
24 @implementation NSFileManager (UKNameForTempFile)
25
26 // -----------------------------------------------------------------------------
27 // nameForTempFile:
28 // Quickly generates a (pretty random) unique file name for a file in the
29 // NSTemporaryDirectory and returns that path. Use this for temporary
30 // files the user will not see.
31 //
32 // REVISIONS:
33 // 2004-03-21 witness Documented.
34 // -----------------------------------------------------------------------------
35
36 -(NSString*) nameForTempFile
37 {
38 NSString* tempDir = NSTemporaryDirectory();
39 int n = rand();
40 NSString* fname = nil;
41
42 if( !tempDir )
43 return nil;
44 while( !fname || [self fileExistsAtPath: fname] )
45 fname = [tempDir stringByAppendingPathComponent: [NSString stringWithFormat:@"temp_%i", n++]];
46
47 return fname;
48 }
49
50 // -----------------------------------------------------------------------------
51 // uniqueFileName:
52 // Takes a file path and if an item already exists at that path, generates
53 // a unique file name by appending a number. Use this to e.g. add files
54 // to user-owned folders (like the desktop) to ensure you don't overwrite
55 // any valuable data.
56 //
57 // May return NIL if it's searched for a while (after about 2 billion
58 // attempts).
59 //
60 // REVISIONS:
61 // 2004-03-21 witness Documented.
62 // -----------------------------------------------------------------------------
63
64 -(NSString*) uniqueFileName: (NSString*)oldName
65 {
66 NSString* baseName = [oldName stringByDeletingPathExtension];
67 NSString* suffix = [oldName pathExtension];
68 int n = 1;
69 NSString* fname = oldName;
70
71 while( [self fileExistsAtPath: fname] ) // Keep looping until we have a unique name:
72 {
73 if( [suffix length] == 0 ) // Build "/folder/file 1"-style path:
74 fname = [baseName stringByAppendingString: [NSString stringWithFormat:@" %i", n++]];
75 else // Build "/folder/file 1.suffix"-style path:
76 fname = [baseName stringByAppendingString: [NSString stringWithFormat:@" %i.%@", n++, suffix]];
77
78 if( n <= 0 ) // overflow!
79 return nil;
80 }
81
82 return fname;
83 }
84
85 @end
Something went wrong with that request. Please try again.