Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for canonmodel.cs

(view source code of canonmodel.cs as plain text)

  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	class CanonModel
  8. 	{
  9. 		static int Main( string[] args )
  10. 		{
  11. 			if ( args.Length != 1 )
  12. 			{
  13. 				return WriteError( );
  14. 			}
  15. 			if ( args[0] == "/?" )
  16. 			{
  17. 				return WriteError( );
  18. 			}
  19. 			if ( args[0][0] == '/' )
  20. 			{
  21. 				return WriteError( "Invalid argument: \"" + args[0] + "\"" );
  22. 			}
  23. 			if ( !Directory.Exists( Path.GetDirectoryName( args[0] ) ) )
  24. 			{
  25. 				return WriteError( "Folder not found: \"" + Path.GetDirectoryName( args[0] ) + "\"" );
  26. 			}
  27. 			if ( !File.Exists( args[0] ) )
  28. 			{
  29. 				return WriteError( "File not found: \"" + args[0] + "\"" );
  30. 			}
  31. 			try
  32. 			{
  33. 				StreamReader file = new StreamReader( args[0] );
  34. 				char[] buffer = new char[1024];
  35. 				int result = file.Read( buffer, 0, 1023 );
  36. 				file.Close( );
  37. 				string model = new string( buffer );
  38. 				Regex regexp = new Regex( "Canon (PowerShot D[1-9]\\d*|EOS [1-9]\\d{0,3}D)" );
  39. 				Match match1 = regexp.Match( model );
  40. 				if ( match1.Success )
  41. 				{
  42. 					Console.WriteLine( match1 );
  43. 					regexp = new Regex( "\\d+" );
  44. 					Match match2 = regexp.Match( match1.Value );
  45. 					return Convert.ToInt32( match2.Value );
  46. 				}
  47. 				Console.Error.WriteLine( "Photo probably not made with a Canon EOS or PowerShot camera" );
  48. 				return WriteError( );
  49. 			}
  50. 			catch ( Exception e )
  51. 			{
  52. 				return WriteError( e.Message );
  53. 			}
  54. 		}
  55.  
  56. 		#region Error handling
  57.  
  58. 		public static int WriteError( string errorMessage = "" )
  59. 		{
  60. 			/*
  61. 			CanonModel,  Version 1.00
  62. 			Return the Canon camera model for the specified image file
  63.  
  64. 			Usage:  FOR /F "tokens=*" %%A IN ('CANONMODEL  imagefile') DO SET camera=%%A
  65.  
  66. 			   or:  CANONMODEL  imagefile
  67. 			        SET camera_number=%ErrorLevel%
  68.  
  69. 			Notes:  This version only detects PowerShot D* and EOS *D models.
  70. 			        Works for unmodified Canon RAW or JPG images.
  71. 			        Camera model is displayed on screen (e.g. "Canon EOS 50D") and returned
  72. 			        as "errorlevel" (e.g. 50).
  73. 			        An errorlevel 0 implies the image was not found or was not made with a
  74. 			        Canon EOS *D or Canon PowerShot D* camera.
  75. 			        The author is in no way associated with Canon Inc., besides being the
  76. 			        owner of several Canon cameras.
  77.  
  78. 			Written by Rob van der Woude
  79. 			http://www.robvanderwoude.com
  80. 			*/
  81.  
  82. 			Console.ResetColor( );
  83. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  84. 			{
  85. 				Console.Error.WriteLine( );
  86. 				Console.ForegroundColor = ConsoleColor.Red;
  87. 				Console.Error.Write( "ERROR:  " );
  88. 				Console.ForegroundColor = ConsoleColor.White;
  89. 				Console.Error.WriteLine( errorMessage );
  90. 				Console.ResetColor( );
  91. 			}
  92. 			Console.Error.WriteLine( );
  93. 			Console.Error.WriteLine( "CanonModel,  Version 1.00" );
  94. 			Console.Error.WriteLine( "Return the Canon camera model for the specified image file" );
  95. 			Console.Error.WriteLine( );
  96. 			Console.Error.Write( "Usage:  FOR /F \"tokens=*\" %%A IN ('" );
  97. 			Console.ForegroundColor = ConsoleColor.White;
  98. 			Console.Error.Write( "CANONMODEL  imagefile" );
  99. 			Console.ResetColor( );
  100. 			Console.Error.WriteLine( "') DO SET camera=%%A" );
  101. 			Console.Error.WriteLine( );
  102. 			Console.Error.Write( "   or:  " );
  103. 			Console.ForegroundColor = ConsoleColor.White;
  104. 			Console.Error.WriteLine( "CANONMODEL  imagefile" );
  105. 			Console.ResetColor( );
  106. 			Console.Error.WriteLine( "        SET camera_number=%ErrorLevel%" );
  107. 			Console.Error.WriteLine( );
  108. 			Console.Error.WriteLine( "Notes:  This version only detects PowerShot D* and EOS *D models." );
  109. 			Console.Error.WriteLine( "        Works for unmodified Canon RAW or JPG images." );
  110. 			Console.Error.Write( "        Camera model is displayed on screen (e.g. \"" );
  111. 			Console.ForegroundColor = ConsoleColor.White;
  112. 			Console.Error.Write( "Canon EOS 50D" );
  113. 			Console.ResetColor( );
  114. 			Console.Error.WriteLine( "\") and returned" );
  115. 			Console.Error.Write( "        as \"errorlevel\" (e.g. " );
  116. 			Console.ForegroundColor = ConsoleColor.White;
  117. 			Console.Error.Write( "50" );
  118. 			Console.ResetColor( );
  119. 			Console.Error.WriteLine( ")." );
  120. 			Console.Error.Write( "        An errorlevel " );
  121. 			Console.ForegroundColor = ConsoleColor.White;
  122. 			Console.Error.Write( "0" );
  123. 			Console.ResetColor( );
  124. 			Console.Error.WriteLine( " implies the image was not found or was not made with a" );
  125. 			Console.Error.WriteLine( "        Canon EOS *D or Canon PowerShot D* camera." );
  126. 			Console.Error.WriteLine( "        The author is in no way associated with Canon Inc., besides being the" );
  127. 			Console.Error.WriteLine( "        owner of several Canon cameras." );
  128. 			Console.Error.WriteLine( );
  129. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  130. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  131. 			return 0;
  132. 		}
  133.  
  134. 		#endregion Error handling
  135.  
  136. 	}
  137. }
  138.  

page last modified: 2024-04-16; loaded in 0.0064 seconds