this repo has no description
1/* Copyright: � Copyright 2005 Apple Computer, Inc. All rights reserved.
2
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
4 ("Apple") in consideration of your agreement to the following terms, and your
5 use, installation, modification or redistribution of this Apple software
6 constitutes acceptance of these terms. If you do not agree with these terms,
7 please do not use, install, modify or redistribute this Apple software.
8
9 In consideration of your agreement to abide by the following terms, and subject
10 to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
11 copyrights in this original Apple software (the "Apple Software"), to use,
12 reproduce, modify and redistribute the Apple Software, with or without
13 modifications, in source and/or binary forms; provided that if you redistribute
14 the Apple Software in its entirety and without modifications, you must retain
15 this notice and the following text and disclaimers in all such redistributions of
16 the Apple Software. Neither the name, trademarks, service marks or logos of
17 Apple Computer, Inc. may be used to endorse or promote products derived from the
18 Apple Software without specific prior written permission from Apple. Except as
19 expressly stated in this notice, no other rights or licenses, express or implied,
20 are granted by Apple herein, including but not limited to any patent rights that
21 may be infringed by your derivative works or by other works in which the Apple
22 Software may be incorporated.
23
24 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
25 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28 COMBINATION WITH YOUR PRODUCTS.
29
30 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38/*=============================================================================
39 CAFilePathUtils.cpp
40
41=============================================================================*/
42
43#include "CAFilePathUtils.h"
44#include <string.h>
45
46OSStatus PosixPathToParentFSRefAndName(const char *path, FSRef &outParentDir, CFStringRef &outFileName)
47{
48 // convert C string to CFString
49#if !TARGET_OS_WIN32
50 CFStringRef cfFullPath = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8);
51#else
52 CFStringRef cfFullPath = CFStringCreateWithCString(NULL, path, kCFStringEncodingWindowsLatin1);
53#endif
54 // convert CF string to URL
55 CFURLRef fullurl = CFURLCreateWithFileSystemPath(NULL, cfFullPath, TARGET_OS_WIN32 ? kCFURLWindowsPathStyle : kCFURLPOSIXPathStyle, false);
56 CFRelease(cfFullPath);
57 // get the directory portion of the URL
58 CFURLRef dirurl = CFURLCreateCopyDeletingLastPathComponent(NULL, fullurl);
59 // get the directory's FSSpec
60 OSStatus err = CFURLGetFSRef(dirurl, &outParentDir) ? OSStatus(noErr) : OSStatus(fnfErr);
61 CFRelease(dirurl);
62
63 CFStringRef lastPathComponent = CFURLCopyLastPathComponent(fullurl);
64 CFRelease(fullurl);
65 CFMutableStringRef filename = CFStringCreateMutableCopy(NULL, 0, lastPathComponent);
66 CFRelease(lastPathComponent);
67 // convert colons (legal in POSIX paths, illegal in File Manager) to slashes
68 CFStringFindAndReplace(filename, CFSTR(":"), CFSTR("/"), CFRangeMake(0, CFStringGetLength(filename)), 0);
69
70 outFileName = filename;
71
72 return err;
73}
74
75
76#if TARGET_OS_WIN32
77
78char* dirname(const char* inPath)
79{
80 static char sAnswer[1024];
81
82 char* theAnswer = NULL;
83 SInt32 thePathLength = strlen(inPath);
84 if(thePathLength < 1023)
85 {
86 // make a working copy
87 strcpy(sAnswer, inPath);
88
89 // start at the end of the string
90 SInt32 theIndex = thePathLength - 1;
91
92 // walk back over the '\' characters
93 while((theIndex > 0) && (sAnswer[theIndex] == '\\'))
94 {
95 --theIndex;
96 }
97
98 // now keep walking back until we get to a '\'
99 while((theIndex > 0) && (sAnswer[theIndex] != '\\'))
100 {
101 --theIndex;
102 }
103
104 // where we are now is either the first character of the path or the '\' that marks the end of the directory name
105 if(theIndex > 0)
106 {
107 // we have a name so put a '\0' in place of the '\'
108 sAnswer[theIndex] = 0;
109 }
110 else
111 {
112 // no name, so the answer is "."
113 sAnswer[0] = '.';
114 sAnswer[1] = 0;
115 }
116
117 // set the return value
118 theAnswer = sAnswer;
119 }
120
121 return theAnswer;
122}
123
124char* basename(const char* inPath)
125{
126 static char sAnswer[1024];
127
128 char* theAnswer = NULL;
129 SInt32 thePathLength = strlen(inPath);
130 if(thePathLength < 1023)
131 {
132 // make a working copy
133 strcpy(sAnswer, inPath);
134
135 // start at the end of the string
136 SInt32 theLastIndex = thePathLength - 1;
137
138 // walk back over the '\' characters
139 while((theLastIndex > 0) && (sAnswer[theLastIndex] == '\\'))
140 {
141 --theLastIndex;
142 }
143
144 // check to see if we're at the beginning now
145 if(theLastIndex > 0)
146 {
147 // there's a name in there now, so start where we are and go back to the next '\'
148 UInt32 theFirstIndex = theLastIndex;
149 while((theFirstIndex > 0) && (sAnswer[theFirstIndex] != '\\'))
150 {
151 --theFirstIndex;
152 }
153
154 // we now have a string, so put a '\0' after the last character
155 sAnswer[theLastIndex + 1] = 0;
156
157 // and set the return value
158 theAnswer = &sAnswer[theFirstIndex];
159 }
160 else
161 {
162 // the path was entirely '\' characters, so the return value is "\"
163 sAnswer[0] = '\\';
164 sAnswer[1] = 0;
165
166 // set the return value
167 theAnswer = sAnswer;
168 }
169 }
170
171 return theAnswer;
172}
173
174#endif