Prev: A tool that suggests optimized logic for a piece of code/module/function
Next: A tool that suggests optimized logic for a piece of code/module/function
From: Ramon F Herrera on 12 Jan 2010 18:12 My app accepts several types of files on input: TIFF, PDF, XML, etc. Instead of deferring to the backend libraries the scrutiny -and potential exception- of each file, I would like to make a quick check of the argvs: "The input file does not have a valid TIFF format". Followed by a quick exit. I figure that the Unix utility "file" would be perfect for this; is there any way I can use it programmatically? TIA, -Ramon
From: Chris McDonald on 12 Jan 2010 18:19 Ramon F Herrera <ramon(a)conexus.net> writes: >I figure that the Unix utility "file" would be perfect for this; is >there any way I can use it programmatically? You haven't indicated the programming language that you're working in, but if it's C or C++ you can call 'file' with the popen() function and parse the output of file: #include <stdio.h> sprintf(bigbuffer, "file %s", image_filename); FILE *fp = popen(bigbuffer, "r"); if(fp) { fgets(bigbuffer, sizeof bigbuffer, fp); // parse contents of bigbuffer.... pclose(fp); } -- Chris.
From: Måns Rullgård on 12 Jan 2010 18:26 Ramon F Herrera <ramon(a)conexus.net> writes: > My app accepts several types of files on input: TIFF, PDF, XML, etc. > Instead of deferring to the backend libraries the scrutiny -and > potential exception- of each file, I would like to make a quick check > of the argvs: > > "The input file does not have a valid TIFF format". > > Followed by a quick exit. > > I figure that the Unix utility "file" would be perfect for this; is > there any way I can use it programmatically? The implementation of "file" commonly found on Linux systems comes with a library for this. -- M�ns Rullg�rd mans(a)mansr.com
From: Ramon F Herrera on 12 Jan 2010 18:32
On Jan 12, 5:19 pm, Chris McDonald <ch...(a)csse.uwa.edu.au> wrote: > Ramon F Herrera <ra...(a)conexus.net> writes: > > >I figure that the Unix utility "file" would be perfect for this; is > >there any way I can use it programmatically? > > You haven't indicated the programming language that you're working in, > but if it's C or C++ you can call 'file' with the popen() function and > parse the output of file: > > #include <stdio.h> > > sprintf(bigbuffer, "file %s", image_filename); > FILE *fp = popen(bigbuffer, "r"); > > if(fp) { > fgets(bigbuffer, sizeof bigbuffer, fp); > // parse contents of bigbuffer.... > pclose(fp); > } > > -- > Chris. It is C++. -Ramon |