Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
utils-inl.hpp
1 /*
2  Title --- utils-inl.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_UTILS_INL_HPP_
23 #define _RPI_HW_UTILS_INL_HPP_
24 
25 namespace rpihw { // Begin main namespace
26 
27 namespace utils { // Begin utilss namespace
28 
29 template < typename T >
30 inline void
31 set_bit( T &buffer, size_t offset, uint8_t index, bool value ) {
32 
33  // Set the bit
34  if ( value )
35  buffer[ offset ] |= ( 1 << index );
36  else
37  buffer[ offset ] &= ~( 1 << index );
38 }
39 
40 template < typename T >
41 inline void
42 clear_bit( T &buffer, size_t offset, uint8_t index ) {
43 
44  // Clear the bit
45  buffer[ offset ] &= ~( 1 << index );
46 }
47 
48 template < typename T >
49 inline void
50 flip_bit( T &buffer, size_t offset, uint8_t index ) {
51 
52  // Flip the bit value
53  buffer[ offset ] ^= ( 1 << index );
54 }
55 
56 template < typename T >
57 inline bool
58 get_bit( T &buffer, size_t offset, uint8_t index ) {
59 
60  // Return the bit value
61  return ( buffer[ offset ] & ( 1 << index ) ) != 0;
62 }
63 
64 template < typename T >
65 std::basic_string< T >
66 align( const std::basic_string< T > &text, size_t width, uint8_t flags ) {
67 
68  // Get the word wrapping mode
69  bool word_break = flags & WORD_BREAK;
70 
71  // Calculate the margin factor
72  size_t margin = 0;
73 
74  if ( flags & ALIGN_CENTER )
75  margin = 2;
76  else if ( flags & ALIGN_RIGHT )
77  margin = 1;
78 
79  // Iterators
80  auto it = std::begin( text ), end = std::end( text );
81 
82  // The aligned string
83  std::basic_string< T > result; result.reserve( std::distance( it, end ) * 2 );
84 
85  // Start of the current line
86  auto off = it;
87 
88  // Current line width
89  size_t line_len = 0;
90 
91 
92  // Break lines between characters
93  if ( word_break ) {
94 
95  for ( ; it != end; ++it ) {
96 
97  line_len++;
98 
99  if ( *it == '\n' ) {
100 
101  if ( margin )
102  result.append( ( width - line_len + 1 ) / margin, ' ' );
103 
104  result.append( off, it );
105  result.push_back( '\n' );
106 
107  off = it + 1;
108  line_len = 0;
109 
110  } else if ( line_len > width ) {
111 
112  if ( margin )
113  result.append( ( width - line_len + 1 ) / margin, ' ' );
114 
115  result.append( off, it );
116  result.push_back( '\n' );
117 
118  off = it;
119  line_len = 1;
120  }
121  }
122 
123  // Append the last line
124  if ( margin )
125  result.append( ( width - line_len ) / margin, ' ' );
126 
127  result.append( off, end );
128 
129 
130  // Break lines between words, except when a single word is longer than the width
131  } else {
132 
133  // Start of the current word
134  auto word_start = it;
135 
136  // Current word width
137  size_t word_len = 0;
138 
139  for ( ; it != end; ++it ) {
140 
141  ++word_len;
142 
143  if ( line_len + word_len > width ) {
144 
145  if ( off == word_start ) {
146 
147  if ( margin )
148  result.append( ( width - ( line_len + word_len ) + 1 ) / margin, ' ' );
149 
150  result.append( off, it );
151  result.push_back( '\n' );
152 
153  off = word_start = it;
154  line_len = 0;
155  word_len = 1;
156 
157  } else {
158 
159  if ( margin )
160  result.append( ( width - line_len + 1 ) / margin, ' ' );
161 
162  result.append( off, word_start );
163  result.push_back( '\n' );
164 
165  off = ++word_start;
166  line_len = 0;
167  }
168  }
169 
170  if ( *it == '\n' ) {
171 
172  if ( margin )
173  result.append( ( width - ( line_len + word_len ) + 1 ) / margin, ' ' );
174 
175  result.append( off, it );
176  result.push_back( '\n' );
177 
178  off = word_start = it + 1;
179  line_len = word_len = 0;
180 
181  } else if ( *it == ' ' ) {
182 
183  word_start = it;
184 
185  line_len += word_len;
186  word_len = 0;
187  }
188  }
189 
190  // Append the last line
191  if ( margin )
192  result.append( ( width - ( line_len + word_len ) ) / margin, ' ' );
193 
194  result.append( off, end );
195  }
196 
197  return result;
198 }
199 
200 } // End of utilss namespace
201 
202 } // End of main namespace
203 
204 #endif /* _RPI_HW_UTILS_INL_HPP_ */
bool get_bit(T &buffer, size_t offset, uint8_t index)
Retuns a bit value in a variable.
Definition: utils-inl.hpp:58
void flip_bit(T &buffer, size_t offset, uint8_t index)
Flips a bit value in a variable.
Definition: utils-inl.hpp:50
std::basic_string< T > align(const std::basic_string< T > &text, size_t width, uint8_t flags)
Aligns a text.
Definition: utils-inl.hpp:66
void set_bit(T &buffer, size_t offset, uint8_t index, bool value)
Sets a bit in a variable.
Definition: utils-inl.hpp:31
void clear_bit(T &buffer, size_t offset, uint8_t index)
Clears a bit in a variable.
Definition: utils-inl.hpp:42