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.

358 lines
11 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/inserters/args.hpp
  3. *
  4. * Purpose: String inserter for argc+argv pairs.
  5. *
  6. * Created: 19th October 2006
  7. * Updated: 5th May 2010
  8. *
  9. * Home: http://www.pantheios.org/
  10. *
  11. * Copyright (c) 2006-2010, 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
  16. * met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the
  24. * names of any contributors may be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * ////////////////////////////////////////////////////////////////////// */
  40. /** \file pantheios/inserters/args.hpp
  41. *
  42. * [C++ only] Definition of the pantheios::args inserter for
  43. * <code>argc+argv</code> pairs.
  44. */
  45. #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_ARGS
  46. #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_ARGS
  47. /* /////////////////////////////////////////////////////////////////////////
  48. * Version information
  49. */
  50. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  51. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_ARGS_MAJOR 1
  52. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_ARGS_MINOR 6
  53. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_ARGS_REVISION 4
  54. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_ARGS_EDIT 28
  55. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  56. /* /////////////////////////////////////////////////////////////////////////
  57. * Includes
  58. */
  59. #ifndef PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS
  60. # include <pantheios/pantheios.h>
  61. #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS */
  62. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD
  63. # include <stlsoft/shims/access/string/fwd.h>
  64. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD */
  65. #include <string>
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Namespace
  68. */
  69. #if !defined(PANTHEIOS_NO_NAMESPACE)
  70. namespace pantheios
  71. {
  72. #endif /* !PANTHEIOS_NO_NAMESPACE */
  73. /* /////////////////////////////////////////////////////////////////////////
  74. * Inserter classes
  75. */
  76. /** Class for inserting <code>argc+argv</code> arguments into Pantheios
  77. * diagnostic logging statements.
  78. *
  79. * \ingroup group__application_layer_interface__inserters
  80. *
  81. * This class formats an <code>argc+argv</code> pair into a string to
  82. * be inserted into a logging statement.
  83. *
  84. * By default, arguments that contain spaces (TAB or space character) will
  85. * be enclosed in double quotes, and those that do not will not. This
  86. * behaviour can be moderated by use of the
  87. * \link pantheios::args::neverQuoteArgs args::neverQuoteArgs\endlink
  88. * and
  89. * \link pantheios::args::alwaysQuoteArgs args::alwaysQuoteArgs\endlink
  90. * member constants.
  91. *
  92. * Consider the following statement:
  93. * \code
  94. int main(int argc, char* argv[])
  95. {
  96. pantheios::log_DEBUG("main(", args(argc, argv), ")");
  97. . . .
  98. * \endcode
  99. *
  100. * Suppose the program is <code>/test/myprog</code> and the command-line
  101. * arguments are <code>abc</code>, <code>-123</code> and
  102. * <code>Billy Jean</code>. This will produce the output:
  103. \htmlonly
  104. <pre>
  105. <b>/test/myprog, abc, -123, "Billy Jean"</b>
  106. </pre>
  107. \endhtmlonly
  108. *
  109. * We may specify different flags from the default
  110. * (\link pantheios::args::quoteArgsWithSpaces args::quoteArgsWithSpaces\endlink)
  111. * in the third constructor argument, as in:
  112. * \code
  113. int main(int argc, char* argv[])
  114. {
  115. pantheios::log_DEBUG("main(", pantheios::args(argc, argv, pantheios::args::neverQuoteArgs), ")");
  116. . . .
  117. * \endcode
  118. *
  119. * Using the same arguments, this will produce the output:
  120. \htmlonly
  121. <pre>
  122. <b>/test/myprog, abc, -123, Billy Jean</b>
  123. </pre>
  124. \endhtmlonly
  125. *
  126. */
  127. class args
  128. {
  129. /// \name Member Types
  130. /// @{
  131. public:
  132. typedef args class_type;
  133. /// @}
  134. /// \name Member Constants
  135. /// @{
  136. public:
  137. /// The format flags
  138. enum format_flags
  139. {
  140. neverQuoteArgs = 0x0000 /*!< arguments are never quoted */
  141. , quoteArgsWithSpaces = 0x0001 /*!< arguments are quoted if they contain spaces */
  142. , alwaysQuoteArgs = 0x0002 /*!< arguments are always quoted */
  143. , arg0FileOnly = 0x0004 /*!< causes only the file part of arg0 to be displayed */
  144. };
  145. /// @}
  146. /// \name Construction
  147. /// @{
  148. public:
  149. /// Constructs an \link pantheios::args args\endlink inserter
  150. /// that will display the command-line arguments.
  151. ///
  152. /// \param argc The number of arguments in the argument array.
  153. /// \param argv The array of arguments.
  154. /// \param flags \link pantheios::args::format_flags Flags\endlink that
  155. /// control the formatting of the shim.
  156. /// \param separator String used to separate multiple arguments; defaults to ", "
  157. args(int argc, pan_char_t const* const* argv, int flags = quoteArgsWithSpaces, pan_char_t const* separator = PANTHEIOS_LITERAL_STRING(", "));
  158. #ifdef STLSOFT_COMPILER_IS_BORLAND
  159. args(int argc, pan_char_t** argv, int flags = quoteArgsWithSpaces, pan_char_t const* separator = PANTHEIOS_LITERAL_STRING(", "));
  160. #endif /* STLSOFT_COMPILER_IS_BORLAND */
  161. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  162. ~args() stlsoft_throw_0();
  163. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  164. /// @}
  165. /// \name Accessors
  166. /// @{
  167. public:
  168. /// A possibly non-nul-terminated non-null pointer to the c-style string representation of the integer
  169. pan_char_t const* data() const;
  170. /// A nul-terminated non-null pointer to the c-style string representation of the integer
  171. pan_char_t const* c_str() const;
  172. /// The size of the c-style string representation of the integer
  173. size_t size() const;
  174. /// @}
  175. /// \name Implementation
  176. /// @{
  177. private:
  178. void construct_() const;
  179. void construct_();
  180. /// @}
  181. /// \name Member Variables
  182. /// @{
  183. private:
  184. typedef std::basic_string<pan_char_t> string_type_;
  185. const int m_flags;
  186. const int m_argc;
  187. pan_char_t const* const* m_argv;
  188. const string_type_ m_separator;
  189. string_type_ m_result;
  190. /// @}
  191. /// \name Not to be implemented
  192. /// @{
  193. private:
  194. #if !defined(STLSOFT_COMPILER_IS_GCC)
  195. args(class_type const&);
  196. #endif /* compiler */
  197. class_type& operator =(class_type const&);
  198. /// @}
  199. };
  200. /* /////////////////////////////////////////////////////////////////////////
  201. * String Access Shims
  202. */
  203. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  204. # if !defined(PANTHEIOS_NO_NAMESPACE)
  205. namespace shims
  206. {
  207. # endif /* !PANTHEIOS_NO_NAMESPACE */
  208. /** \overload c_str_data_a(args const&) */
  209. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  210. inline wchar_t const* c_str_data_w(args const& a)
  211. {
  212. return a.data();
  213. }
  214. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  215. inline char const* c_str_data_a(args const& a)
  216. {
  217. return a.data();
  218. }
  219. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  220. /** \overload c_str_data(args const&) */
  221. inline pan_char_t const* c_str_data(args const& a)
  222. {
  223. return a.data();
  224. }
  225. /** \overload c_str_len_a(args const&) */
  226. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  227. inline size_t c_str_len_w(args const& a)
  228. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  229. inline size_t c_str_len_a(args const& a)
  230. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  231. {
  232. return a.size();
  233. }
  234. /** \overload c_str_len(args const&) */
  235. inline size_t c_str_len(args const& a)
  236. {
  237. return a.size();
  238. }
  239. /** \overload c_str_ptr_a(args const&) */
  240. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  241. inline wchar_t const* c_str_ptr_w(args const& a)
  242. {
  243. return a.c_str();
  244. }
  245. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  246. inline char const* c_str_ptr_a(args const& a)
  247. {
  248. return a.c_str();
  249. }
  250. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  251. /** \overload c_str_ptr(args const&) */
  252. inline pan_char_t const* c_str_ptr(args const& a)
  253. {
  254. return a.c_str();
  255. }
  256. # if !defined(PANTHEIOS_NO_NAMESPACE)
  257. } /* namespace shims */
  258. # if defined(STLSOFT_COMPILER_IS_GCC)
  259. /* GCC does not seem to correctly handle the phases of
  260. * processing of C++ templates, so we need to 'use' the
  261. * shims into the same namespace as the inserter class
  262. * in order that ADL can suffice instead.
  263. */
  264. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  265. using ::pantheios::shims::c_str_data_w;
  266. using ::pantheios::shims::c_str_len_w;
  267. using ::pantheios::shims::c_str_ptr_w;
  268. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  269. using ::pantheios::shims::c_str_data_a;
  270. using ::pantheios::shims::c_str_len_a;
  271. using ::pantheios::shims::c_str_ptr_a;
  272. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  273. using ::pantheios::shims::c_str_data;
  274. using ::pantheios::shims::c_str_len;
  275. using ::pantheios::shims::c_str_ptr;
  276. # endif /* compiler */
  277. # endif /* !PANTHEIOS_NO_NAMESPACE */
  278. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  279. /* /////////////////////////////////////////////////////////////////////////
  280. * Namespace
  281. */
  282. #if !defined(PANTHEIOS_NO_NAMESPACE)
  283. } /* namespace pantheios */
  284. namespace stlsoft
  285. {
  286. // 'Export' the string access shims into the STLSoft namespace
  287. //
  288. // c_str_ptr(_a) is not necessary for version 1.0 of Pantheios, but it's
  289. // defined and exported in order to allow for the case where someone
  290. // may find a legitimate use for the conversion classes additional to
  291. // the type-tunneling of the Pantheios API.
  292. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  293. using ::pantheios::shims::c_str_data_w;
  294. using ::pantheios::shims::c_str_len_w;
  295. using ::pantheios::shims::c_str_ptr_w;
  296. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  297. using ::pantheios::shims::c_str_data_a;
  298. using ::pantheios::shims::c_str_len_a;
  299. using ::pantheios::shims::c_str_ptr_a;
  300. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  301. using ::pantheios::shims::c_str_data;
  302. using ::pantheios::shims::c_str_len;
  303. using ::pantheios::shims::c_str_ptr;
  304. }
  305. #endif /* !PANTHEIOS_NO_NAMESPACE */
  306. /* /////////////////////////////////////////////////////////////////////////
  307. * Inclusion
  308. */
  309. #ifdef STLSOFT_PPF_pragma_once_SUPPORT
  310. # pragma once
  311. #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
  312. /* ////////////////////////////////////////////////////////////////////// */
  313. #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_ARGS */
  314. /* ///////////////////////////// end of file //////////////////////////// */