Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 97 lines (82 sloc) 3.282 kb
720b0e7 Uli Kusterer Added licensing information to the source files.
authored
1 //
2 // NSFileManager+NameForTempFile.m
3 // Filie
4 //
5 // Created by Uli Kusterer on 8.2.2004
6 // Copyright 2004 by Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
dc5be1c Initial check-in.
uli authored
27
28 // -----------------------------------------------------------------------------
29 // Headers:
30 // -----------------------------------------------------------------------------
31
32 #import "NSFileManager+NameForTempFile.h"
33
34
35 @implementation NSFileManager (UKNameForTempFile)
36
37 // -----------------------------------------------------------------------------
38 // nameForTempFile:
39 // Quickly generates a (pretty random) unique file name for a file in the
40 // NSTemporaryDirectory and returns that path. Use this for temporary
41 // files the user will not see.
42 //
43 // REVISIONS:
44 // 2004-03-21 witness Documented.
45 // -----------------------------------------------------------------------------
46
47 -(NSString*) nameForTempFile
48 {
49 NSString* tempDir = NSTemporaryDirectory();
50 int n = rand();
51 NSString* fname = nil;
52
53 if( !tempDir )
54 return nil;
55 while( !fname || [self fileExistsAtPath: fname] )
56 fname = [tempDir stringByAppendingPathComponent: [NSString stringWithFormat:@"temp_%i", n++]];
57
58 return fname;
59 }
60
61 // -----------------------------------------------------------------------------
62 // uniqueFileName:
63 // Takes a file path and if an item already exists at that path, generates
64 // a unique file name by appending a number. Use this to e.g. add files
65 // to user-owned folders (like the desktop) to ensure you don't overwrite
66 // any valuable data.
67 //
68 // May return NIL if it's searched for a while (after about 2 billion
69 // attempts).
70 //
71 // REVISIONS:
72 // 2004-03-21 witness Documented.
73 // -----------------------------------------------------------------------------
74
75 -(NSString*) uniqueFileName: (NSString*)oldName
76 {
77 NSString* baseName = [oldName stringByDeletingPathExtension];
78 NSString* suffix = [oldName pathExtension];
79 int n = 1;
80 NSString* fname = oldName;
81
82 while( [self fileExistsAtPath: fname] ) // Keep looping until we have a unique name:
83 {
84 if( [suffix length] == 0 ) // Build "/folder/file 1"-style path:
85 fname = [baseName stringByAppendingString: [NSString stringWithFormat:@" %i", n++]];
86 else // Build "/folder/file 1.suffix"-style path:
87 fname = [baseName stringByAppendingString: [NSString stringWithFormat:@" %i.%@", n++, suffix]];
88
89 if( n <= 0 ) // overflow!
90 return nil;
91 }
92
93 return fname;
94 }
95
96 @end
Something went wrong with that request. Please try again.