Question

I'm working on an app that handles text files.

On the open dialog accessory view, when the user choses a file I want to display on a NSPopUpButton the supported encodings for the selected file, but so far the only way I could find to test if a file can be opened with a certain encoding is to actually trying to load it to a NSString.

Right now I'm doing this for each encoding:

NSString *fileContents = [NSString stringWithContentsOfURL:selectedURL encoding:encoding error:nil];

if (fileContents)
{
    /* Add encoding to the list of supported encodings */
}

This loads the whole file to the fileContents object.

Is it possible possible to only test if the file can be loaded without actually loading it?

Was it helpful?

Solution

This is not possible to predict with 100% accuracy unless you read everything. For instance many encodings are supersets of ASCII and you'd be fooled if you stopped reading partway through a file; maybe the first paragraph just happens to contain nothing but ASCII and then 4 paragraphs later the file suddenly contains a bunch of mathematical equations.

A good middle ground is to support the conventions for specifying encodings that already exist. For instance text editors like vim and emacs specify a certain syntax for embedding encoding hints in the first few lines of a file, and HTML and XML files can contain <meta> tags with encoding information. If you can read those, it's fairly safe to assume they're correct and not read the entire file; otherwise, you do have to read the whole file to know for sure.

(An exception: certain Unicode encodings start with byte-order marks that are not used by any other encoding so you can assume those are what they say they are.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top