Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 1 | // (C) Copyright Gennadiy Rozental 2005-2008. |
| 2 | // Use, modification, and distribution are subject to the |
| 3 | // Boost Software License, Version 1.0. (See accompanying file |
| 4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | // See http://www.boost.org/libs/test for the library home page. |
| 7 | // |
| 8 | // File : $RCSfile$ |
| 9 | // |
| 10 | // Version : $Revision: 49312 $ |
| 11 | // |
| 12 | // Description : implements facility to hide input traversing details |
| 13 | // *************************************************************************** |
| 14 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 15 | #ifndef NDNBOOST_RT_CLA_ARGV_TRAVERSER_IPP_070604GER |
| 16 | #define NDNBOOST_RT_CLA_ARGV_TRAVERSER_IPP_070604GER |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 17 | |
| 18 | // Boost.Runtime.Parameter |
| 19 | #include <ndnboost/test/utils/runtime/trace.hpp> |
| 20 | |
| 21 | #include <ndnboost/test/utils/runtime/cla/argv_traverser.hpp> |
| 22 | |
| 23 | // STL |
| 24 | #include <memory> |
| 25 | #include <cstring> |
| 26 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 27 | #ifdef NDNBOOST_NO_STDC_NAMESPACE |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 28 | namespace std { using ::memcpy; } |
| 29 | #endif |
| 30 | |
| 31 | namespace ndnboost { |
| 32 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 33 | namespace NDNBOOST_RT_PARAM_NAMESPACE { |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 34 | |
| 35 | namespace cla { |
| 36 | |
| 37 | // ************************************************************************** // |
| 38 | // ************** runtime::cla::argv_traverser ************** // |
| 39 | // ************************************************************************** // |
| 40 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 41 | NDNBOOST_RT_PARAM_INLINE |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 42 | argv_traverser::argv_traverser() |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 43 | : p_ignore_mismatch( false ), p_separator( NDNBOOST_RT_PARAM_LITERAL( ' ' ) ) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | //____________________________________________________________________________// |
| 48 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 49 | NDNBOOST_RT_PARAM_INLINE void |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 50 | argv_traverser::init( int argc, char_type** argv ) |
| 51 | { |
| 52 | for( int index = 1; index < argc; ++index ) { |
| 53 | m_buffer += argv[index]; |
| 54 | if( index != argc-1 ) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 55 | m_buffer += NDNBOOST_RT_PARAM_LITERAL( ' ' ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | m_remainder.reset( new char_type[m_buffer.size()+1] ); |
| 59 | m_remainder_size = 0; |
| 60 | m_work_buffer = m_buffer; |
| 61 | m_commited_end = m_work_buffer.begin(); |
| 62 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 63 | NDNBOOST_RT_PARAM_TRACE( "Input buffer: " << m_buffer ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 64 | |
| 65 | next_token(); |
| 66 | } |
| 67 | |
| 68 | //____________________________________________________________________________// |
| 69 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 70 | NDNBOOST_RT_PARAM_INLINE void |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 71 | argv_traverser::remainder( int& argc, char_type** argv ) |
| 72 | { |
| 73 | argc = 1; |
| 74 | std::size_t pos = 0; |
| 75 | while(pos < m_remainder_size ) { |
| 76 | argv[argc++] = m_remainder.get() + pos; |
| 77 | |
| 78 | pos = std::find( m_remainder.get() + pos, m_remainder.get() + m_remainder_size, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 79 | NDNBOOST_RT_PARAM_LITERAL( ' ' ) ) - m_remainder.get(); |
| 80 | m_remainder[pos++] = NDNBOOST_RT_PARAM_LITERAL( '\0' ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | //____________________________________________________________________________// |
| 85 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 86 | NDNBOOST_RT_PARAM_INLINE cstring |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 87 | argv_traverser::token() const |
| 88 | { |
| 89 | return m_token; |
| 90 | } |
| 91 | |
| 92 | //____________________________________________________________________________// |
| 93 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 94 | NDNBOOST_RT_PARAM_INLINE void |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 95 | argv_traverser::next_token() |
| 96 | { |
| 97 | if( m_work_buffer.is_empty() ) |
| 98 | return; |
| 99 | |
| 100 | m_work_buffer.trim_left( m_token.size() ); // skip remainder of current token |
| 101 | |
| 102 | if( m_work_buffer.size() != m_buffer.size() ) // !! is there a better way to identify first token |
| 103 | m_work_buffer.trim_left( 1 ); // skip separator if not first token; |
| 104 | |
| 105 | m_token.assign( m_work_buffer.begin(), |
| 106 | std::find( m_work_buffer.begin(), m_work_buffer.end(), p_separator ) ); |
| 107 | } |
| 108 | |
| 109 | //____________________________________________________________________________// |
| 110 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 111 | NDNBOOST_RT_PARAM_INLINE cstring |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 112 | argv_traverser::input() const |
| 113 | { |
| 114 | return m_work_buffer; |
| 115 | } |
| 116 | |
| 117 | //____________________________________________________________________________// |
| 118 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 119 | NDNBOOST_RT_PARAM_INLINE void |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 120 | argv_traverser::trim( std::size_t size ) |
| 121 | { |
| 122 | m_work_buffer.trim_left( size ); |
| 123 | |
| 124 | if( size <= m_token.size() ) |
| 125 | m_token.trim_left( size ); |
| 126 | else { |
| 127 | m_token.assign( m_work_buffer.begin(), |
| 128 | std::find( m_work_buffer.begin(), m_work_buffer.end(), p_separator ) ); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | //____________________________________________________________________________// |
| 133 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 134 | NDNBOOST_RT_PARAM_INLINE bool |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 135 | argv_traverser::match_front( cstring str ) |
| 136 | { |
| 137 | return m_work_buffer.size() < str.size() ? false : m_work_buffer.substr( 0, str.size() ) == str; |
| 138 | } |
| 139 | |
| 140 | //____________________________________________________________________________// |
| 141 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 142 | NDNBOOST_RT_PARAM_INLINE bool |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 143 | argv_traverser::match_front( char_type c ) |
| 144 | { |
| 145 | return first_char( m_work_buffer ) == c; |
| 146 | } |
| 147 | |
| 148 | //____________________________________________________________________________// |
| 149 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 150 | NDNBOOST_RT_PARAM_INLINE bool |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 151 | argv_traverser::eoi() const |
| 152 | { |
| 153 | return m_work_buffer.is_empty(); |
| 154 | } |
| 155 | |
| 156 | //____________________________________________________________________________// |
| 157 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 158 | NDNBOOST_RT_PARAM_INLINE void |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 159 | argv_traverser::commit() |
| 160 | { |
| 161 | m_commited_end = m_work_buffer.begin(); |
| 162 | } |
| 163 | |
| 164 | //____________________________________________________________________________// |
| 165 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 166 | NDNBOOST_RT_PARAM_INLINE void |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 167 | argv_traverser::rollback() |
| 168 | { |
| 169 | m_work_buffer.assign( m_commited_end, m_work_buffer.end() ); |
| 170 | m_token.assign( m_work_buffer.begin(), |
| 171 | std::find( m_work_buffer.begin(), m_work_buffer.end(), p_separator ) ); |
| 172 | |
| 173 | } |
| 174 | |
| 175 | //____________________________________________________________________________// |
| 176 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 177 | NDNBOOST_RT_PARAM_INLINE std::size_t |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 178 | argv_traverser::input_pos() const |
| 179 | { |
| 180 | return m_work_buffer.begin() - m_commited_end; |
| 181 | } |
| 182 | |
| 183 | //____________________________________________________________________________// |
| 184 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 185 | NDNBOOST_RT_PARAM_INLINE bool |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 186 | argv_traverser::handle_mismatch() |
| 187 | { |
| 188 | if( !p_ignore_mismatch ) |
| 189 | return false; |
| 190 | |
| 191 | std::memcpy( m_remainder.get() + m_remainder_size, token().begin(), token().size() ); |
| 192 | m_remainder_size += token().size(); |
| 193 | m_remainder[m_remainder_size++] = p_separator; |
| 194 | |
| 195 | next_token(); |
| 196 | commit(); |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | //____________________________________________________________________________// |
| 202 | |
| 203 | } // namespace cla |
| 204 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 205 | } // namespace NDNBOOST_RT_PARAM_NAMESPACE |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 206 | |
| 207 | } // namespace ndnboost |
| 208 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 209 | #endif // NDNBOOST_RT_CLA_ARGV_TRAVERSER_IPP_070604GER |