[MOAB-dev] Detecting file types

Jason Kraftcheck kraftche at cae.wisc.edu
Tue Dec 1 09:39:22 CST 2009


Tim Tautges wrote:
> 
> 
> Jason Kraftcheck wrote:
> 
>>
>>
>> But MOAB's choice of which reader to use is more complicated that just
>> selecting one based on extension.  If it doesn't recognize the
>> extension, it
>> will try them all until one succeeds.  That is a good solution, as
>> there is
>> no better test of the type of a file then actually looking at the
>> contents.
>>  We do provide the information you describe above, as it is useful in
>> some
>> cases (e.g. constructing a file dialog in verdict), but I don't think
>> utilizing this functionality is the correct solution to Steve's problem.
>>
>> - jason
>>
>>
> 
> If an extension isn't recognized, though, the function should return
> NULL or some notion of unknown handler. 

Yes.  So if I had a file named model.cub.bak or model.step.1, it would
return NULL.  However, MOAB would still "read" it.  Steve needs to know if,
upon reading said file, a CGM model will be created, which it will.
MBReaderWriterTool will return a NULL handler for that extension.  It would
be more robust for Steve to skip the MBReaderWriterTool stuff entirely, and
just look for the "CUB" bytes at the beginning of the file.

> Also, if an extension is
> recognized but the registered reader/writer fails, does it cycle through
> the others?
> 

I've often wondered if it should, or if we should even rely on extensions at
all when reading files, but no it currently does not.


> Steve should be able to test the results of calling these functions and
> act accordingly.  Whether the read actually succeeds is another thing
> that should be handled.
> 

Yes, but I think such a solution is the least robust of many possible
options that I can think of.

1) Just check if there is a CGM model
2) Use tag data to communicate which MOAB reader was used
3) Identity file types by actual content (typically first few bytes.)
4) Use file extensions.

The first option will likely always work.  The second is also robust and
future-proof, assuming MOAB only ever uses ReadCGM for all file formats
supported by CGM.  The third is still robust but requires a little more work
and is not as future proof as the first two, as new file formats may be
added.  The last one (the solution you propose) is not any more future-proof
than the third, as MOAB may not be updated with extensions for new CGM file
formats either.  And MOAB may be changed so as to ignore extensions entirely
during reads.  And the file extension could be unknown.


- jason


More information about the moab-dev mailing list