(view source code of canonmodel.cs as plain text)
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace RobvanderWoude
{
class CanonModel
{
static int Main( string[] args )
{
if ( args.Length != 1 )
{
return WriteError( );
}
if ( args[0] == "/?" )
{
return WriteError( );
}
if ( args[0][0] == '/' )
{
return WriteError( "Invalid argument: \"" + args[0] + "\"" );
}
if ( !Directory.Exists( Path.GetDirectoryName( args[0] ) ) )
{
return WriteError( "Folder not found: \"" + Path.GetDirectoryName( args[0] ) + "\"" );
}
if ( !File.Exists( args[0] ) )
{
return WriteError( "File not found: \"" + args[0] + "\"" );
}
try
{
StreamReader file = new StreamReader( args[0] );
char[] buffer = new char[1024];
int result = file.Read( buffer, 0, 1023 );
file.Close( );
string model = new string( buffer );
Regex regexp = new Regex( "Canon (PowerShot D[1-9]\\d*|EOS [1-9]\\d{0,3}D)" );
Match match1 = regexp.Match( model );
if ( match1.Success )
{
Console.WriteLine( match1 );
regexp = new Regex( "\\d+" );
Match match2 = regexp.Match( match1.Value );
return Convert.ToInt32( match2.Value );
}
Console.Error.WriteLine( "Photo probably not made with a Canon EOS or PowerShot camera" );
return WriteError( );
}
catch ( Exception e )
{
return WriteError( e.Message );
}
}
#region Error handling
public static int WriteError( string errorMessage = "" )
{
/*
CanonModel, Version 1.00
Return the Canon camera model for the specified image file
Usage: FOR /F "tokens=*" %%A IN ('CANONMODEL imagefile') DO SET camera=%%A
or: CANONMODEL imagefile
SET camera_number=%ErrorLevel%
Notes: This version only detects PowerShot D* and EOS *D models.
Works for unmodified Canon RAW or JPG images.
Camera model is displayed on screen (e.g. "Canon EOS 50D") and returned
as "errorlevel" (e.g. 50).
An errorlevel 0 implies the image was not found or was not made with a
Canon EOS *D or Canon PowerShot D* camera.
The author is in no way associated with Canon Inc., besides being the
owner of several Canon cameras.
Written by Rob van der Woude
http://www.robvanderwoude.com
*/
Console.ResetColor( );
if ( string.IsNullOrEmpty( errorMessage ) == false )
{
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errorMessage );
Console.ResetColor( );
}
Console.Error.WriteLine( );
Console.Error.WriteLine( "CanonModel, Version 1.00" );
Console.Error.WriteLine( "Return the Canon camera model for the specified image file" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: FOR /F \"tokens=*\" %%A IN ('" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "CANONMODEL imagefile" );
Console.ResetColor( );
Console.Error.WriteLine( "') DO SET camera=%%A" );
Console.Error.WriteLine( );
Console.Error.Write( " or: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "CANONMODEL imagefile" );
Console.ResetColor( );
Console.Error.WriteLine( " SET camera_number=%ErrorLevel%" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: This version only detects PowerShot D* and EOS *D models." );
Console.Error.WriteLine( " Works for unmodified Canon RAW or JPG images." );
Console.Error.Write( " Camera model is displayed on screen (e.g. \"" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "Canon EOS 50D" );
Console.ResetColor( );
Console.Error.WriteLine( "\") and returned" );
Console.Error.Write( " as \"errorlevel\" (e.g. " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "50" );
Console.ResetColor( );
Console.Error.WriteLine( ")." );
Console.Error.Write( " An errorlevel " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "0" );
Console.ResetColor( );
Console.Error.WriteLine( " implies the image was not found or was not made with a" );
Console.Error.WriteLine( " Canon EOS *D or Canon PowerShot D* camera." );
Console.Error.WriteLine( " The author is in no way associated with Canon Inc., besides being the" );
Console.Error.WriteLine( " owner of several Canon cameras." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "http://www.robvanderwoude.com" );
return 0;
}
#endregion Error handling
}
}
page last modified: 2024-04-16; loaded in 0.0064 seconds