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