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