PageRenderTime 128ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/MicroFrameworkPK_v4_2/CLR/Core/ParseOptions_Win32.cpp

https://github.com/pmfsampaio/NETMF-LPC
C++ | 315 lines | 229 code | 74 blank | 12 comment | 38 complexity | 494a623cdd66bd6b7fec806ee7ca09d2 MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4. #include "Core.h"
  5. ////////////////////////////////////////////////////////////////////////////////////////////////////
  6. CLR_RT_ParseOptions::Parameter::Parameter( LPCWSTR szName, LPCWSTR szDescription )
  7. {
  8. m_szName = szName;
  9. m_szDescription = szDescription;
  10. }
  11. //--//
  12. CLR_RT_ParseOptions::Parameter_Generic::Parameter_Generic( LPCWSTR szName, LPCWSTR szDescription ) : CLR_RT_ParseOptions::Parameter( szName, szDescription )
  13. {
  14. }
  15. bool CLR_RT_ParseOptions::Parameter_Generic::Parse( LPCWSTR arg )
  16. {
  17. m_data = arg;
  18. return true;
  19. }
  20. //--//
  21. CLR_RT_ParseOptions::Parameter_String::Parameter_String( std::wstring* data, LPCWSTR szName, LPCWSTR szDescription ) : CLR_RT_ParseOptions::Parameter( szName, szDescription )
  22. {
  23. m_dataPtr = data ? data : &m_dataParsed;
  24. }
  25. bool CLR_RT_ParseOptions::Parameter_String::Parse( LPCWSTR arg )
  26. {
  27. *m_dataPtr = arg;
  28. return true;
  29. }
  30. //--//
  31. CLR_RT_ParseOptions::Parameter_Boolean::Parameter_Boolean( bool* data, LPCWSTR szName, LPCWSTR szDescription ) : CLR_RT_ParseOptions::Parameter( szName, szDescription )
  32. {
  33. m_dataPtr = data ? data : &m_dataParsed;
  34. }
  35. bool CLR_RT_ParseOptions::Parameter_Boolean::Parse( LPCWSTR arg )
  36. {
  37. int num;
  38. if(!_wcsicmp( arg, L"true" ) ||
  39. !_wcsicmp( arg, L"on" ) )
  40. {
  41. *m_dataPtr = true;
  42. return true;
  43. }
  44. if(!_wcsicmp( arg, L"false" ) ||
  45. !_wcsicmp( arg, L"off" ) )
  46. {
  47. *m_dataPtr = false;
  48. return true;
  49. }
  50. if(swscanf_s( arg, L"%d", &num ) == 0)
  51. {
  52. wprintf( L"Expecting a boolean for parameter '%s': %s\n\n", m_szName, arg );
  53. return false;
  54. }
  55. *m_dataPtr = (num != 0);
  56. return true;
  57. }
  58. //--//
  59. CLR_RT_ParseOptions::Parameter_Integer::Parameter_Integer( int* data, LPCWSTR szName, LPCWSTR szDescription ) : CLR_RT_ParseOptions::Parameter( szName, szDescription )
  60. {
  61. m_dataPtr = data ? data : &m_dataParsed;
  62. }
  63. bool CLR_RT_ParseOptions::Parameter_Integer::Parse( LPCWSTR arg )
  64. {
  65. if(swscanf_s( arg, L"%d", m_dataPtr ) == 0)
  66. {
  67. wprintf( L"Expecting a number for parameter '%s': %s\n\n", m_szName, arg );
  68. return false;
  69. }
  70. return true;
  71. }
  72. //--//
  73. CLR_RT_ParseOptions::Parameter_Float::Parameter_Float( float* data, LPCWSTR szName, LPCWSTR szDescription ) : CLR_RT_ParseOptions::Parameter( szName, szDescription )
  74. {
  75. m_dataPtr = data ? data : &m_dataParsed;
  76. }
  77. bool CLR_RT_ParseOptions::Parameter_Float::Parse( LPCWSTR arg )
  78. {
  79. if(swscanf_s( arg, L"%f", m_dataPtr ) == 0)
  80. {
  81. wprintf( L"Expecting a number for parameter '%s': %s\n\n", m_szName, arg );
  82. return false;
  83. }
  84. return true;
  85. }
  86. //--//
  87. CLR_RT_ParseOptions::Command::Command( LPCWSTR szName, LPCWSTR szDescription )
  88. {
  89. m_szName = szName;
  90. m_szDescription = szDescription;
  91. }
  92. bool CLR_RT_ParseOptions::Command::Parse( CLR_RT_StringVector& argv, size_t& pos, CLR_RT_ParseOptions& options )
  93. {
  94. size_t argc = argv.size();
  95. for(ParameterListIter it = m_params.begin(); it != m_params.end(); it++)
  96. {
  97. Parameter* param = *it;
  98. if(pos >= argc)
  99. {
  100. if(options.m_commands.size() > 1)
  101. {
  102. wprintf( L"Missing parameter for option '%s': %s\n\n", m_szName, param->m_szName );
  103. }
  104. else
  105. {
  106. wprintf( L"Missing parameter for option '%s'\n\n", param->m_szName );
  107. }
  108. return false;
  109. }
  110. else
  111. {
  112. if(param->Parse( argv[ pos++ ].c_str() ) == false)
  113. {
  114. return false;
  115. }
  116. }
  117. }
  118. return true;
  119. }
  120. HRESULT CLR_RT_ParseOptions::Command::Execute()
  121. {
  122. TINYCLR_HEADER();
  123. TINYCLR_NOCLEANUP_NOLABEL();
  124. }
  125. //--//
  126. CLR_RT_ParseOptions::Command_SetFlag::Command_SetFlag( bool* data, LPCWSTR szName, LPCWSTR szDescription ) : CLR_RT_ParseOptions::Command( szName, szDescription )
  127. {
  128. m_dataPtr = data ? data : &m_dataParsed;
  129. }
  130. bool CLR_RT_ParseOptions::Command_SetFlag::Parse( CLR_RT_StringVector& argv, size_t& pos )
  131. {
  132. *m_dataPtr = true;
  133. return true;
  134. }
  135. //--//
  136. CLR_RT_ParseOptions::CLR_RT_ParseOptions()
  137. {
  138. m_fVerbose = false;
  139. }
  140. HRESULT CLR_RT_ParseOptions::ExtractOptionsFromFile( LPCWSTR szFileName )
  141. {
  142. TINYCLR_HEADER();
  143. CLR_RT_StringVector vec;
  144. TINYCLR_CHECK_HRESULT(CLR_RT_FileStore::ExtractTokensFromFile( szFileName, vec ));
  145. TINYCLR_CHECK_HRESULT(ProcessOptions( vec ));
  146. TINYCLR_NOCLEANUP();
  147. }
  148. HRESULT CLR_RT_ParseOptions::ReprocessOptions()
  149. {
  150. TINYCLR_HEADER();
  151. if(CommandLineArgs.size() == 0)
  152. {
  153. TINYCLR_SET_AND_LEAVE(CLR_E_NULL_REFERENCE);
  154. }
  155. TINYCLR_SET_AND_LEAVE(ProcessOptions( CommandLineArgs ));
  156. TINYCLR_NOCLEANUP();
  157. }
  158. HRESULT CLR_RT_ParseOptions::ProcessOptions( CLR_RT_StringVector& argv )
  159. {
  160. TINYCLR_HEADER();
  161. if(CommandLineArgs.size() == 0)
  162. {
  163. CommandLineArgs = CLR_RT_StringVector(argv);
  164. }
  165. size_t argc = argv.size();
  166. for(size_t i=0; i<argc; )
  167. {
  168. CommandListIter it;
  169. LPCWSTR arg = argv[ i ].c_str();
  170. for(it = m_commands.begin(); it != m_commands.end(); it++)
  171. {
  172. Command* cmd = *it;
  173. if(!_wcsicmp( arg, cmd->m_szName ))
  174. {
  175. if(m_fVerbose)
  176. {
  177. size_t len = cmd->m_params.size();
  178. size_t pos = i;
  179. wprintf( L"Processing" );
  180. wprintf( L" %s", arg );
  181. while(len-- > 0 && pos < argc) wprintf( L" %s", argv[ ++pos ].c_str() );
  182. wprintf( L"\n" );
  183. }
  184. i++;
  185. if(cmd->Parse( argv, i, *this ) == false)
  186. {
  187. Usage();
  188. TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
  189. }
  190. TINYCLR_CHECK_HRESULT(cmd->Execute());
  191. break;
  192. }
  193. }
  194. if(it == m_commands.end())
  195. {
  196. wprintf( L"Unknown option '%s'\n\n", arg );
  197. Usage();
  198. TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
  199. }
  200. }
  201. TINYCLR_NOCLEANUP();
  202. }
  203. void CLR_RT_ParseOptions::Usage()
  204. {
  205. CommandListIter it;
  206. ParameterListIter it2;
  207. size_t maxCmdLen = 0;
  208. for(it = m_commands.begin(); it != m_commands.end(); it++)
  209. {
  210. Command* cmd = *it;
  211. std::wstring option = cmd->m_szName;
  212. size_t cmdLen = wcslen( cmd->m_szName );
  213. size_t maxLen = 0;
  214. wprintf( L" %s\n", cmd->m_szDescription );
  215. for(it2 = cmd->m_params.begin(); it2 != cmd->m_params.end(); it2++)
  216. {
  217. Parameter* param = *it2;
  218. size_t len = wcslen( param->m_szName );
  219. option += L" ";
  220. option += param->m_szName;
  221. if(maxLen < len) maxLen = len;
  222. }
  223. if(m_commands.size() > 1)
  224. {
  225. wprintf( L" %s\n", option.c_str(), cmd->m_szDescription );
  226. }
  227. for(it2 = cmd->m_params.begin(); it2 != cmd->m_params.end(); it2++)
  228. {
  229. Parameter* param = *it2;
  230. wprintf( L" %-*s = %s\n", maxLen, param->m_szName, param->m_szDescription );
  231. }
  232. wprintf( L"\n" );
  233. }
  234. }
  235. void CLR_RT_ParseOptions::PushArguments( int argc, LPWSTR argv[], CLR_RT_StringVector& vec )
  236. {
  237. for(int i=0; i<argc; i++)
  238. {
  239. vec.push_back( argv[ i ] );
  240. }
  241. }