0

I need to parse the defaults database for Recent files, in Mac Os X. This is done with sed. The caveat is that the filenames are stored in decomposed utf-16 inside there.

So, I thought, (after having being pointed to the NSString functions) "why don't I just make a little tool that takes care of it".

The tool is supposed to work basically like cat, it gets lines from input, it converts each line into a CFStringref object, before the CFStringref hopefully gets converted into precomposed UTF-16, I convert that back to UTF-8, and prints it.

I hate to say it, but I don't know this "toll-free bridging" works, I have no intention to use Cocoa and Objective C for this, I just want a straight solution using CFStringref, that is, if such a solution exists!

Thanks!

Here is what I have got so far:

#define BUFFERSIZE 512                
static void 
precomposedOutput( char *fn )
{
  char buffer[BUFFERSIZE] ;
  CFStringRef str ;
    char *outbuf;
    char *bytes ;
  FILE *fd = stdin ;
  if (fn) {
      fd = fopen(fn,"r");
   }
  while ( fgets(buffer, sizeof(buffer),fd ) ) {
      bytes = savestr(buffer) ;
      str = CFStringCreateWithCStringNoCopy(NULL, bytes,kCFStringEncodingUTF8, NULL);
      /* CONVERSION INTO PRECOMPOSED IS SUPPOSED TO HAPPEN HERE */
      outbuf = Copy_CFStringRefToCString(str) ;
      CFRelease(str) ; 
     fputs(outbuf,stdout) ;
     free(outbuf) ;         
  }
}

This appears to work, so far in converting back and forth, (not run leaks on it.) What I need help with, is to convert the CFString into precomposed UTF-16.

Thanks!

McUsr
  • 1,400
  • 13
  • 10

1 Answers1

0

Maybe I am wrong, but I figured out by reading the docs for "toll free bridging" that not all methods for NSString are available in CFString.

So I moved to Cocoa/Objective-C and this is what I ended up with.

static void 
precomposedOutput( char *fn )
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    char buffer[BUFFERSIZE] ;
    char *outbuf;
    FILE *fd = stdin ;
    if (fn) {
        fd = fopen(fn,"r");
    }
    while ( fgets(buffer, sizeof(buffer),fd ) ) {
        NSString *str = [[NSString alloc] initWithUTF8String:buffer];
        NSString *str_precomp = [[NSString alloc ] initWithString:[str precomposedStringWithCanonicalMapping]] ;
        outbuf = (char *)[str_precomp UTF8String] ;
        [str release ] ; 
        [str_precomp release ] ;
        fputs(outbuf,stdout) ;
    }
    [pool drain];
}

Well, it works, but my fundamental problem is that since the strings has gone through a pipeline, the decomposed characters are stores in multiple bytes, with literal strings like "\\U" in front of them. In the mean time a routine in Applescript was written, that does it by much simpler means than having a "tool" to do the job. (http://macscripter.net/viewtopic.php?pid=161135#p161135)

Case closed.

McUsr
  • 1,400
  • 13
  • 10