Skip navigation.
 
mlRe: Conversion Between path URLs with certain percent escapes not being removed
FROM : Steve Christensen
DATE : Sat Jan 19 16:44:20 2008

Have you tried either CFURLCopyFileSystemPath or 
CFURLGetFileSystemRepresentation?


On Jan 18, 2008, at 7:01 PM, Daniel Brieck Jr. wrote:

> Hello all,
>
> I am trying to go from a local URL path  such as "/Volumes/TV
> %20SHOWS/" and convert it to "cifs://MSHOME;<email_removed>/
> TV%20SHOWS"
>
> Within that conversion I am using FSPathMakeRef() of Files.h which 
> does not like to see percent encoded strings passed into it. So I 
> try to filter them out and replace them with their character 
> representations.
>
> However, the problem occurs with the percent escape in the local URL 
> path given above, they are not converted as expected.
>
> I am using the function 
> CFURLCreateStringByReplacingPercentEscapes() of CFURL.h to remove 
> the percent escapes. However with this above example it does not 
> appear to be removing the %20 and replacing it with a single SPACE.
>
> Also I have tried the 
> CFURLCreateStringByReplacingPercentEscapesUsingEncoding() , but have 
> only been able to successfully remove %escapes but without replacing 
> the characters represented.  See commented out trial below, I have 
> tried all the Built-in String Encodings types with no good results 
> of replacing the "%20" with a SPACE.
>
> For example this local path  "/Volumes/MSHOME%3BDELLOPTIPLEXGX1/" 
> and its %3B a.k.a ";" convert ok and allow the code to run and 
> return a result,  but passing in something like this "/Volumes/TV
> %20SHOWS/"  causes an assertion check to fail since it is passing in 
> a %-- sequence at this point below for the newPath parameter.
>
>     err = FSPathMakeRef( (const UInt8*) newPath, &ref, NULL);
>      assert(err == noErr);
>
> It has to be some kind of character encoding interpretation issue, 
> any ideas as to what I may be doing wrong would be greatly 
> appreciated.  See the Implementation below for the more complete 
> picture of the conversion process.
>
> Thank you,
> Daniel J. Brieck Jr.
> Note: that I am running Mac Os x 10.4.11 (Intel) on Xcode 2.5
>
> The implementation file:
>
> @implementation NSApplication (getServerURL)
>
>
> - (NSString *) urlStringForServerVolume:(const char *)  path
> {
>      OSStatus        err;
>      FSRef          ref;
>      FSCatalogInfo  catInfo;
>      CFURLRef        url;
>      CFStringRef    str;
>      NSString *      result;
>      CFStringRef noPercentsPath;
>      CFStringRef stPath;
>      CFIndex size;
>      char* newPath = NULL;
>
> //Testing
>      printf("Given path\n");
>      printf("%s\n", path);
>      printf("end given path\n");
>
>      //Check that the supplied path is not NULL
>      assert(path != NULL);
>
>      //Converts the const char * to a CFStringRef
>      stPath = CFStringCreateWithCString (kCFAllocatorDefault, path, 
> kCFStringEncodingUTF8);
>      assert(stPath != NULL);
>
>      //Checking for any percent escape sequences and replacing them 
> with their character equivalents
>      noPercentsPath = 
> CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault , 
> stPath, CFSTR(" ") );
>
>      //noPercentsPath = 
> CFURLCreateStringByReplacingPercentEscapesUsingEncoding
> (kCFAllocatorDefault , stPath, CFSTR(" "), kCFStringEncodingUnicode );
>
>
>      // Converting from CFStringRef to a UTF8 character encoding
>      //size = 
> CFStringGetMaximumSizeForEncoding(CFStringGetLength(noPercentsPath), 
> kCFStringEncodingUTF8) + 1;
>      size = CFStringGetMaximumSizeOfFileSystemRepresentation 
> (noPercentsPath);
>
>      newPath = malloc(size);
>      if (newPath != NULL)
>      {
>          printf("This Works\n");
>          Boolean testVal = CFStringGetCString (noPercentsPath, 
> newPath, size, kCFStringEncodingUTF8);
>          //Boolean testVal = CFStringGetCString (noPercentsPath, 
> newPath, size, kCFStringEncodingUTF16);
>          assert(testVal == 1);
>      }
>
> //Testing
>      printf("Print Path\n");
>      printf("%s\n",newPath);
>      printf("END IT\n");
>
>      err = FSPathMakeRef( (const UInt8*) newPath, &ref, NULL);
>      assert(err == noErr);
>
>
>
>      err = FSGetCatalogInfo(&ref, kFSCatInfoVolume, &catInfo, NULL, 
> NULL, NULL);
>      assert(err == noErr);
>      err = FSCopyURLForVolume(catInfo.volume, &url);
>      assert(err == noErr);
>      str = CFURLGetString(url);
>      assert(str != NULL);
>      result = [NSString stringWithString:(NSString *) str];
>      assert(result != nil);
>
>
>      //Free some stuff
>
>      free(newPath); newPath = NULL;
>      CFRelease(url);
>      CFRelease(noPercentsPath);
>
>      return result;
> }
>
> @end

Related mailsAuthorDate
mlConversion Between path URLs with certain percent escapes not being removed Daniel Brieck Jr. Jan 19, 04:01
mlRe: Conversion Between path URLs with certain percent escapes not being removed Steve Christensen Jan 19, 16:44