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.

151 lines
5.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/inserters/pad.hpp
  3. *
  4. * Purpose: padding of arguments of arbitrary type.
  5. *
  6. * Created: 29th June 2009
  7. * Updated: 14th February 2010
  8. *
  9. * Home: http://www.pantheios.org/
  10. *
  11. * Copyright (c) 2009-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/pad.hpp
  41. *
  42. * [c++ only] definition of the pantheios::pad string inserter for
  43. * for providing padding of arguments of arbitrary type.
  44. */
  45. #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_PAD
  46. #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_PAD
  47. /* /////////////////////////////////////////////////////////////////////////
  48. * Version information
  49. */
  50. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  51. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_PAD_MAJOR 1
  52. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_PAD_MINOR 0
  53. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_PAD_REVISION 2
  54. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_PAD_EDIT 3
  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. # include <stlsoft/shims/access/string.hpp>
  63. /* /////////////////////////////////////////////////////////////////////////
  64. * Namespace
  65. */
  66. #if !defined(PANTHEIOS_NO_NAMESPACE)
  67. namespace pantheios
  68. {
  69. #endif /* !PANTHEIOS_NO_NAMESPACE */
  70. /* /////////////////////////////////////////////////////////////////////////
  71. * Macros
  72. */
  73. /** \def PANTHEIOS_LPAD(var, minWidth)
  74. *
  75. * \ingroup group__application_layer_interface__inserters
  76. *
  77. * Inserts a variable with a minimum width, padding to the left with spaces
  78. *
  79. * \param var The variable to be inserted
  80. * \param minWidth The minimum width of the inserted variable
  81. */
  82. #define PANTHEIOS_LPAD(var, minWidth) ( ::pantheios::pad(var, minWidth)), (var)
  83. /** \def PANTHEIOS_RPAD(var, minWidth)
  84. *
  85. * \ingroup group__application_layer_interface__inserters
  86. *
  87. * Inserts a variable with a minimum width, padding to the right with spaces
  88. *
  89. * \param var The variable to be inserted
  90. * \param minWidth The minimum width of the inserted variable
  91. */
  92. #define PANTHEIOS_RPAD(var, minWidth) (var), ( ::pantheios::pad(var, minWidth))
  93. /* /////////////////////////////////////////////////////////////////////////
  94. * Inserter functions
  95. */
  96. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  97. template <typename S>
  98. inline const pan_slice_t pad(S const& arg, size_t minimumWidth)
  99. {
  100. const size_t len = stlsoft::c_str_len(arg);
  101. if(len < minimumWidth)
  102. {
  103. size_t actualWidth;
  104. pan_char_t const* padding = pantheios::core::pantheios_getPad(minimumWidth - len, &actualWidth);
  105. return pan_slice_t(padding, actualWidth);
  106. }
  107. return pan_slice_t();
  108. }
  109. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  110. /* /////////////////////////////////////////////////////////////////////////
  111. * Namespace
  112. */
  113. #if !defined(PANTHEIOS_NO_NAMESPACE)
  114. } /* namespace pantheios */
  115. #endif /* !PANTHEIOS_NO_NAMESPACE */
  116. /* /////////////////////////////////////////////////////////////////////////
  117. * Inclusion
  118. */
  119. #ifdef STLSOFT_PPF_pragma_once_SUPPORT
  120. # pragma once
  121. #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
  122. /* ////////////////////////////////////////////////////////////////////// */
  123. #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_PAD */
  124. /* ///////////////////////////// end of file //////////////////////////// */