Powered by GeSHi

Source code for cameramodel.cs

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

  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	class CameraModel
  8. 	{
  9. 		static int Main( string[] args )
  10. 		{
  11. 			if ( args.Length == 0 || args.Length > 2 )
  12. 			{
  13. 				return WriteError( );
  14. 			}
  15.  
  16. 			string filename = string.Empty;
  17. 			string make = string.Empty;
  18.  
  19. 			foreach ( string arg in args )
  20. 			{
  21. 				if ( arg == "/?" )
  22. 				{
  23. 					return WriteError( );
  24. 				}
  25. 				if ( arg[0] == '/' )
  26. 				{
  27. 					if ( arg.ToUpper( ).Substring( 0, 3 ) == "/M:" )
  28. 					{
  29. 						make = arg.Substring( 3 ).Replace( "\"", "" );
  30. 					}
  31. 					else
  32. 					{
  33. 						return WriteError( "Invalid argument: \"" + arg + "\"" );
  34. 					}
  35. 				}
  36. 				else
  37. 				{
  38. 					filename = arg;
  39. 					if ( !Directory.Exists( Path.GetDirectoryName( filename ) ) )
  40. 					{
  41. 						return WriteError( "Folder not found: \"" + Path.GetDirectoryName( filename ) + "\"" );
  42. 					}
  43. 					if ( !File.Exists( filename ) )
  44. 					{
  45. 						return WriteError( "File not found: \"" + filename + "\"" );
  46. 					}
  47. 				}
  48. 			}
  49. 			if ( string.IsNullOrEmpty( filename ) )
  50. 			{
  51. 				return WriteError( "No file specified" );
  52. 			}
  53.  
  54. 			try
  55. 			{
  56. 				StreamReader file = new StreamReader( filename );
  57. 				char[] buffer = new char[1024];
  58. 				int result = file.Read( buffer, 0, 1023 );
  59. 				file.Close( );
  60. 				string header = new string( buffer );
  61.  
  62. 				if ( string.IsNullOrEmpty( header ) )
  63. 				{
  64. 					return WriteError( "Could not open file \"" + filename + "\"" );
  65. 				}
  66.  
  67. 				Regex regexp = null;
  68. 				string pattern = string.Empty;
  69. 				if ( !string.IsNullOrEmpty( make ) )
  70. 				{
  71. 					pattern = "\\b" + make + "\\b";
  72. 					regexp = new Regex( pattern, RegexOptions.IgnoreCase );
  73. 					if ( !regexp.IsMatch( header ) )
  74. 					{
  75. 						return 0;
  76. 					}
  77. 				}
  78.  
  79. 				if ( string.IsNullOrEmpty( make ) )
  80. 				{
  81. 					pattern = "\\b(Canon (DIGITAL IXUS ([0-9]\\d*|[A-Z\\d]+)( [A-Z]+)?|PowerShot D[1-9]\\d*|EOS [1-9]\\d{0,3}D)|NIKON D[1-9]\\d{1,3}|PENTAX K\\-?(\\d+[A-Z]*|m))";
  82. 				}
  83. 				else
  84. 				{
  85. 					pattern = "\\b" + make + " [\\dA-Z\\-]*\\d[\\dA-Z\\-]*\\b";
  86. 				}
  87. 				regexp = new Regex( pattern, RegexOptions.IgnoreCase );
  88. 				Match match1 = regexp.Match( header );
  89. 				if ( !match1.Success )
  90. 				{
  91. 					pattern = "\\b" + make + " [\\dA-Z\\-]+\\b";
  92. 					regexp = new Regex( pattern, RegexOptions.IgnoreCase );
  93. 					match1 = regexp.Match( header );
  94. 				}
  95. 				if ( match1.Success )
  96. 				{
  97. 					Console.WriteLine( match1.Value );
  98. 					if ( Regex.IsMatch( match1.Value, "\\d" ) )
  99. 					{
  100. 						regexp = new Regex( "\\d+" );
  101. 						Match match2 = regexp.Match( match1.Value );
  102. 						return Convert.ToInt32( match2.Value );
  103. 					}
  104. 					else
  105. 					{
  106. 						return 0;
  107. 					}
  108. 				}
  109. 				Console.Error.WriteLine( "File is not made with a supported or specified camera" );
  110. 				return WriteError( );
  111. 			}
  112. 			catch ( Exception e )
  113. 			{
  114. 				return WriteError( e.Message );
  115. 			}
  116. 		}
  117.  
  118. 		#region Error handling
  119.  
  120. 		public static int WriteError( string errorMessage = "" )
  121. 		{
  122. 			/*
  123. 			CameraModel,  Version 2.00
  124. 			Return the camera brand and model for the specified image file
  125.  
  126. 			Usage: CAMERAMODEL  image  [ /M:make ]
  127.  
  128. 			Where: image     is the image file to check
  129. 			       /M:make   specifies camera brand to look for (e.g. /M:Pentax)
  130.  
  131. 			Notes: Works for unmodified (Canon) RAW or JPG images, others not guaranteed.
  132. 			       Result returned on screen (e.g. NIKON D60) and as errorlevel (e.g. 60).
  133. 			       Errorlevel 0 if image not found, or not made by supported or specified
  134. 			       camera brand, or camera model doesn't contain numbers (e.g. PENTAX K-m).
  135.  
  136. 			Written by Rob van der Woude
  137. 			http://www.robvanderwoude.com
  138. 			*/
  139.  
  140. 			Console.ResetColor( );
  141. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  142. 			{
  143. 				Console.Error.WriteLine( );
  144. 				Console.ForegroundColor = ConsoleColor.Red;
  145. 				Console.Error.Write( "ERROR:  " );
  146. 				Console.ForegroundColor = ConsoleColor.White;
  147. 				Console.Error.WriteLine( errorMessage );
  148. 				Console.ResetColor( );
  149. 			}
  150. 			Console.Error.WriteLine( );
  151. 			Console.Error.WriteLine( "CameraModel,  Version 2.00" );
  152. 			Console.Error.WriteLine( "Return the camera brand and model for the specified image file" );
  153. 			Console.Error.WriteLine( );
  154. 			Console.Error.Write( "Usage: " );
  155. 			Console.ForegroundColor = ConsoleColor.White;
  156. 			Console.Error.WriteLine( "CAMERAMODEL  image  [ /M:make ]" );
  157. 			Console.ResetColor( );
  158. 			Console.Error.WriteLine( );
  159. 			Console.Error.Write( "Where: " );
  160. 			Console.ForegroundColor = ConsoleColor.White;
  161. 			Console.Error.Write( "image" );
  162. 			Console.ResetColor( );
  163. 			Console.Error.WriteLine( "     is the image file to check" );
  164. 			Console.ForegroundColor = ConsoleColor.White;
  165. 			Console.Error.Write( "       /M:make" );
  166. 			Console.ResetColor( );
  167. 			Console.Error.WriteLine( "   specifies camera brand to look for (e.g. /M:Pentax)" );
  168. 			Console.Error.WriteLine( );
  169. 			Console.Error.WriteLine( "Notes: Works for unmodified (Canon) RAW or JPG images, others not guaranteed." );
  170. 			Console.Error.Write( "       Result returned on screen (e.g. " );
  171. 			Console.ForegroundColor = ConsoleColor.White;
  172. 			Console.Error.Write( "NIKON D60" );
  173. 			Console.ResetColor( );
  174. 			Console.Error.Write( ") and as errorlevel (e.g. " );
  175. 			Console.ForegroundColor = ConsoleColor.White;
  176. 			Console.Error.Write( "60" );
  177. 			Console.ResetColor( );
  178. 			Console.Error.WriteLine( ")." );
  179. 			Console.Error.Write( "       Errorlevel " );
  180. 			Console.ForegroundColor = ConsoleColor.White;
  181. 			Console.Error.Write( "0" );
  182. 			Console.ResetColor( );
  183. 			Console.Error.WriteLine( " if image not found or not made by supported or specified" );
  184. 			Console.Error.WriteLine( "       camera brand, or camera model doesn't contain numbers (e.g. PENTAX K-m)." );
  185. 			Console.Error.WriteLine( );
  186. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  187. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  188. 			return 0;
  189. 		}
  190.  
  191. 		#endregion Error handling
  192.  
  193. 	}
  194. }
  195.