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

page last uploaded: 2015-12-04, 16:51