Search code examples
objective-cmacosunicodefilesystemsurdu

Objective C - unicode (Urdu) file copy


I'm trying to subscribe on moving to trash MacOS event and copy folder back. Code works fine with default English letters, but when i'm using Urdu language and Urdu numbers format copyItemAtPath can not find the folder while copying.

Callback:

void fsevents_callback(ConstFSEventStreamRef streamRef,
    void * userData,
    size_t numEvents,
    void * eventPaths,
    const FSEventStreamEventFlags eventFlags[],
    const FSEventStreamEventId eventIds[]) {
    FSEventsListener * eventListener = (__bridge FSEventsListener * ) userData;
    char ** paths = eventPaths;

    for (int i = 0; i < numEvents; i++) {
        NSString * name = [NSString stringWithFormat: @ "%s", paths[i]];
        NSLog(@ "Path: %@ (flag = %d)", name, eventFlags[i]);

        if (eventFlags[i] & kFSEventStreamEventFlagItemRenamed) {
            [eventListener.delegate fileWasRenamed: name];
        } else {
            [eventListener.delegate fileWasTouched: name];
        }
    }
}

Listener:

- (void)fileWasRenamed:(NSString *)aPath
{
    NSString *trashPath = [NSSearchPathForDirectoriesInDomains(NSTrashDirectory, NSUserDomainMask, YES) lastObject];
    if (nil != aPath && NSNotFound != [aPath rangeOfString:trashPath].location)
    {
        NSString *trashFilePath = aPath;
        if (nil != self.removedFilePath && NSNotFound != [trashFilePath.lastPathComponent rangeOfString:self.removedFilePath.lastPathComponent.stringByDeletingPathExtension].location)
        {
            NSError *error = nil;
            NSFileManager *fileManager = [NSFileManager defaultManager];
            BOOL isCopied = [fileManager copyItemAtPath:trashFilePath toPath:self.removedFilePath error:&error];
        }
    }
}

And when i'm try to copy i get the error:

Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"

Filename in trash: My Folder ۱۷.۵۷.۴۴


Solution

  • [NSString stringWithFormat: @ "%s", paths[i]];
    

    is obviously not a valid way of dealing with unicode strings. Using

    [NSString stringWithUTF8String:paths[i]];
    

    instead should help.