You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
12 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/system/commandline_parser.hpp (originally MLCmdArg.h, ::SynesisStd)
  3. *
  4. * Purpose: commandline_parser class.
  5. *
  6. * Created: 20th May 2000
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2000-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/system/commandline_parser.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::basic_commandline_parser
  42. * class template
  43. * (\ref group__library__system "System" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER
  46. #define STLSOFT_INCL_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER_MINOR 1
  50. # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER_REVISION 3
  51. # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER_EDIT 37
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Compatibility
  55. */
  56. /*
  57. [Incompatibilies-start]
  58. STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
  59. [Incompatibilies-end]
  60. */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Includes
  63. */
  64. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  65. # include <stlsoft/stlsoft.h>
  66. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  67. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  68. _MSC_VER < 1200
  69. # error stlsoft/system/commandline_parser.hpp is not compatible with Visual C++ 5.0 or earlier
  70. #endif /* compiler */
  71. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_FEATURES
  72. # include <stlsoft/memory/allocator_features.hpp> // for STLSOFT_CF_ALLOCATOR_REBIND_SUPPORT
  73. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_FEATURES */
  74. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR
  75. # include <stlsoft/memory/allocator_selector.hpp>
  76. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR */
  77. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
  78. # include <stlsoft/memory/auto_buffer.hpp>
  79. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER */
  80. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS
  81. # include <stlsoft/string/char_traits.hpp>
  82. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS */
  83. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  84. # include <stlsoft/shims/access/string.hpp>
  85. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  86. #ifndef STLSOFT_INCL_H_CTYPE
  87. # define STLSOFT_INCL_H_CTYPE
  88. # include <ctype.h> // for isspace()
  89. #endif /* !STLSOFT_INCL_H_CTYPE */
  90. /* /////////////////////////////////////////////////////////////////////////
  91. * Namespace
  92. */
  93. #ifndef _STLSOFT_NO_NAMESPACE
  94. namespace stlsoft
  95. {
  96. #endif /* _STLSOFT_NO_NAMESPACE */
  97. /* /////////////////////////////////////////////////////////////////////////
  98. * Classes
  99. */
  100. /** \brief Parses a command line into parts, and provides sequence semantics
  101. * for their access.
  102. *
  103. \code
  104. stlsoft::commandline_parser_a cp("abc \"d e f\" ghi);
  105. assert(3 == cp.size());
  106. assert(0 == ::strcmp("abc", cp[0]));
  107. assert(0 == ::strcmp("d e f", cp[1]));
  108. assert(0 == ::strcmp("ghi", cp[2]));
  109. std::copy(cp.begin(), cp.end()
  110. , std::ostream_iterator<char const*>(std::cout, "\n"));
  111. \endcode
  112. *
  113. * \ingroup group__library__system
  114. */
  115. template< ss_typename_param_k C
  116. , ss_typename_param_k T = stlsoft_ns_qual(stlsoft_char_traits)<C>
  117. , ss_typename_param_k A = ss_typename_type_def_k allocator_selector<C>::allocator_type
  118. >
  119. class basic_commandline_parser
  120. {
  121. /// \name Member Types
  122. /// @{
  123. public:
  124. /// \brief The character type
  125. typedef C char_type;
  126. /// \brief The traits type
  127. typedef T traits_type;
  128. /// \brief The traits type
  129. typedef A allocator_type;
  130. /// \brief The current instantiation of the type
  131. typedef basic_commandline_parser<C, T, A> class_type;
  132. private:
  133. typedef char_type* pointer_type;
  134. #ifdef STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
  135. typedef ss_typename_type_k A::ss_template_qual_k rebind<pointer_type>::other pointers_allocator_type;
  136. #else /* ? STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
  137. typedef ss_typename_type_k allocator_selector<pointer_type>::allocator_type pointers_allocator_type;
  138. #endif /* STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
  139. typedef stlsoft_ns_qual(auto_buffer)< char_type
  140. , 256
  141. , allocator_type
  142. > buffer_type;
  143. typedef stlsoft_ns_qual(auto_buffer)< pointer_type
  144. , 20
  145. , pointers_allocator_type
  146. > pointers_type;
  147. typedef ss_typename_type_k buffer_type::iterator iterator;
  148. public:
  149. /// \brief The value type
  150. typedef ss_typename_type_k pointers_type::value_type value_type;
  151. /// \brief The non-mutating (const) iterator type
  152. typedef ss_typename_type_k pointers_type::const_iterator const_iterator;
  153. /// \brief The size type
  154. typedef ss_size_t size_type;
  155. /// @}
  156. /// \name Construction
  157. /// @{
  158. public:
  159. /** \brief Parses the given command-line and creates an internal array
  160. * of pointers to the arguments.
  161. */
  162. ss_explicit_k basic_commandline_parser(char_type const* cmdLine)
  163. : m_buffer(1 + stlsoft_ns_qual(c_str_len)(cmdLine))
  164. , m_pointers(0)
  165. {
  166. STLSOFT_ASSERT(NULL != cmdLine);
  167. traits_type::copy(&m_buffer[0], cmdLine, m_buffer.size());
  168. // Here's the algorithm:
  169. //
  170. // Walk the string, mindful of quotes, remembering the start of an
  171. // argument, and writing the nul-character into spaces.
  172. enum state_t
  173. {
  174. argument
  175. , quotedArgumentStart
  176. , quotedArgument
  177. , space
  178. };
  179. state_t state = space;
  180. iterator b = m_buffer.begin();
  181. const iterator e = m_buffer.end() - 1;
  182. for(; b != e; ++b)
  183. {
  184. const char_type ch = *b;
  185. STLSOFT_ASSERT('\0' != ch);
  186. if('"' == ch)
  187. {
  188. if(quotedArgumentStart == state)
  189. {
  190. state = space;
  191. }
  192. else if(quotedArgument == state)
  193. {
  194. *b = '\0';
  195. state = space;
  196. }
  197. else if(space == state)
  198. {
  199. state = quotedArgumentStart;
  200. }
  201. else
  202. {
  203. }
  204. }
  205. else if(isspace(ch))
  206. {
  207. if(quotedArgumentStart == state)
  208. {
  209. state = quotedArgument;
  210. add_pointer(&*b);
  211. }
  212. else if(quotedArgument == state)
  213. {
  214. }
  215. else if(space == state)
  216. {
  217. }
  218. else
  219. {
  220. STLSOFT_ASSERT(argument == state);
  221. *b = '\0';
  222. state = space;
  223. }
  224. }
  225. else
  226. {
  227. if(quotedArgumentStart == state)
  228. {
  229. state = quotedArgument;
  230. add_pointer(&*b);
  231. }
  232. else if(space == state)
  233. {
  234. state = argument;
  235. add_pointer(&*b);
  236. }
  237. }
  238. }
  239. }
  240. /// @}
  241. /// \name Accessors
  242. /// @{
  243. public:
  244. /// \brief The number of arguments
  245. size_type size() const
  246. {
  247. return m_pointers.size();
  248. }
  249. /** \brief Returns a non-mutating (const) pointer to each argument
  250. * string.
  251. *
  252. * \param index The index of the argument
  253. *
  254. * \note The behaviour is undefined if <code>index</code> is not less
  255. * than <code>size()</code>.
  256. */
  257. value_type const& operator [](size_type index) const
  258. {
  259. STLSOFT_ASSERT(index < size());
  260. return m_pointers[index];
  261. }
  262. /// @}
  263. /// \name Iteration
  264. /// @{
  265. public:
  266. /// \brief An iterator representing the start of the sequence
  267. const_iterator begin() const
  268. {
  269. return m_pointers.begin();
  270. }
  271. /// \brief An iterator representing the end of the sequence
  272. const_iterator end() const
  273. {
  274. return m_pointers.end();
  275. }
  276. /// @}
  277. /// \name Implementation
  278. /// @{
  279. private:
  280. ss_bool_t add_pointer(pointer_type p)
  281. {
  282. if(!m_pointers.resize(1 + m_pointers.size()))
  283. {
  284. return false;
  285. }
  286. m_pointers[m_pointers.size() - 1] = p;
  287. return true;
  288. }
  289. /// @}
  290. /// \name Members
  291. /// @{
  292. private:
  293. buffer_type m_buffer;
  294. pointers_type m_pointers;
  295. /// @}
  296. };
  297. /* /////////////////////////////////////////////////////////////////////////
  298. * Typedefs for commonly encountered types
  299. */
  300. /** \brief Specialisation of the basic_commandline_parser template for the ANSI character type \c char
  301. *
  302. * \ingroup group__library__system
  303. */
  304. typedef basic_commandline_parser<ss_char_a_t> commandline_parser_a;
  305. /** \brief Specialisation of the basic_commandline_parser template for the Unicode character type \c wchar_t
  306. *
  307. * \ingroup group__library__system
  308. */
  309. typedef basic_commandline_parser<ss_char_w_t> commandline_parser_w;
  310. ////////////////////////////////////////////////////////////////////////////
  311. // Unit-testing
  312. #ifdef STLSOFT_UNITTEST
  313. # include "./unittest/commandline_parser_unittest_.h"
  314. #endif /* STLSOFT_UNITTEST */
  315. /* ////////////////////////////////////////////////////////////////////// */
  316. #ifndef _STLSOFT_NO_NAMESPACE
  317. } // namespace stlsoft
  318. #endif /* _STLSOFT_NO_NAMESPACE */
  319. /* ////////////////////////////////////////////////////////////////////// */
  320. #endif /* !STLSOFT_INCL_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER */
  321. /* ///////////////////////////// end of file //////////////////////////// */