Topic Text   Topic Comments (0)   Topic Properties   Topic Information bhavin.patel@san...
Topic title: test Wednesday October 17, 2012 06:39:09

Download topic text | View in variable-width font | Tab width set to 8 (change to 4)

Files in topic:  
[Jump to] codereview.txt   {+385,-0}

[Add General Comment] to topic.

File codereview.txt (Revision 1.0) [Add File Comment] [Top]
 
1 start
2
3 /// @file generic/adapter_enumeration.cpp
4 /// implementation of reusable adapter enumeration helper functions
5
6 #include "generic/adapter_enumeration.h"
7
8 #include <string>
9 #include <locale>
10 #include <algorithm>
11 #include <boost/optional.hpp>
12 #include <boost/lexical_cast.hpp>
13 #pragma warning(push)
14 #pragma warning(disable : 4267 4244)
15 #include <boost/format.hpp>
16 #pragma warning(pop)
17
18 #include "FlashFactory.h"
19 #include "global.h"
20 #include "Adapter.h"
21
22 namespace generic {
23
24 namespace {
25 /// removes any leading or trailing spaces,
26 /// replaces any internal spaces with 'replacement_value'.
27 ///
28 /// @effects changes the argument mutable_string based on the above desc.
29 /// @param mutable_string the string to mutate.
30 /// @param replacement_value the value to use in place of the internal spaces.
31 inline void remove_and_replace_spaces(std::string& mutable_string,
32 std::string::value_type replacement_value = '_')
33 {
34 // remove leading ' '
35 mutable_string.erase(0, mutable_string.find_first_not_of(" "));
36 // remove trailing ' '
37 mutable_string.erase(mutable_string.find_last_not_of(" ")+1);
38
39 // replace any other ' ' with '_'
40 std::replace(mutable_string.begin(),mutable_string.end(),
41 ' ', replacement_value);
42 }
43
44 /// convert all characters in string to lower case
45 inline std::string to_lower(std::string const& input,
46 std::locale const& loc = std::locale()) {
47 std::string result = input;
48 for (std::string::size_type i = 0; i < result.size(); ++i) {
49 if (std::isupper(result[i], loc))
50 { result[i] = std::tolower(result[i], loc); }
51 }
52 return result;
53 }
54
55 }
56
57
58 FlashFactory::PROTOCOL_TYPE get_protocol_from_adapter(std::string const& adapter_string)
59 {
60 // make lower case so we are case insensitive
61 static const std::string::size_type s_END_POS = std::string::npos;
62 std::string lc_adapter_string = to_lower(adapter_string);
63 remove_and_replace_spaces(lc_adapter_string);
64
65 // default to None
66 FlashFactory::PROTOCOL_TYPE result = FlashFactory::None;
67
68 typedef std::vector<AdapterEntryType> adapter_list_t;
69 adapter_list_t available_adapters;
70 (void) g_PlatformManager.EnumerateAdapters(available_adapters);
71
72 for(adapter_list_t::size_type i = 0; i < available_adapters.size(); ++i)
73 {
74 std::string available_adapter_string
75 = to_lower(available_adapters[i].astr);
76 remove_and_replace_spaces(available_adapter_string);
77
78 if (lc_adapter_string == available_adapter_string)
79 {
80 switch(available_adapters[i].atype)
81 {
82 case (Adapter::Ata_Sdp2):
83 case (Adapter::Ata_Pcmcia):
84 case (Adapter::Ata_IM_Usb):
85 case (Adapter::Ata_Detected):
86 result = FlashFactory::Ata;
87 break;
88 case (Adapter::Sd_Epp):
89 case (Adapter::Sd_MyHost):
90 case (Adapter::Sd_Isa):
91 case (Adapter::Sd_Pcmcia):
92 result = FlashFactory::Sd;
93 break;
94 case (Adapter::Ms_Epp):
95 case (Adapter::Ms_IM_Usb):
96 case (Adapter::Ms_Pcmcia):
97 result = FlashFactory::Ms;
98 break;
99 case (Adapter::Gen_Scsi):
100 result = FlashFactory::Scsi;
101 break;
102 default:
103 // keep result as none
104 break;
105 }
106 break; // break out of the for loop
107 }
108 }
109
110 return result;
111 }
112
113 namespace {
114
115 /// convert a string to an unsigned integer if possible
116 /// @return SOME n if the string can represent an unsigned number
117 /// otherwise return NONE
118 inline boost::optional<unsigned> to_unsigned(std::string const& possible_number_string)
119 {
120 // zero length strings do not represent numbers
121 bool is_number = possible_number_string.size() > 0;
122 boost::optional<unsigned> result;
123
124 // is each character a number?
125 static const std::string s_DIGITS = "0123456789";
126 if (possible_number_string.find_first_not_of(s_DIGITS) == std::string::npos)
127 {
128 result = boost::lexical_cast<unsigned>(possible_number_string);
129 }
130
131 return result;
132 }
133
134 }
135
136 /// given an input string get the adapter string
137 /// @param input_string a string to use to get an adapter string
138 /// if it is a number then we can use it to pick from the list by index
139 /// otherwise we can use the string as the adapter string
140 boost::optional<std::string> get_adapter_string(std::string const& input_string)
141 {
142 boost::optional<std::string> result;
143
144 if (const boost::optional<unsigned> adapter_index_opt = to_unsigned(input_string))
145 {
146 typedef std::vector<AdapterEntryType> adapter_list_t;
147 adapter_list_t available_adapters;
148
149 // is the number valid? Can we create a flash manager?
150 if ( static_cast<int>(*adapter_index_opt)
151 < g_PlatformManager.EnumerateAdapters(available_adapters)
152 )
153 {
154 // use the string for that adapter type to figure out the protocol
155 result = available_adapters[*adapter_index_opt].astr;
156 remove_and_replace_spaces(*result);
157 }
158 }
159 else { result = input_string; }
160
161 return result;
162 }
163
164
165 void show_available_adapters(std::ostream& out) {
166 typedef std::vector<AdapterEntryType> adapter_list_t;
167 adapter_list_t adapterTypes;
168 (void) g_PlatformManager.EnumerateAdapters(adapterTypes);
169
170 using boost::format;
171
172 out << format(" %|65T-| \n")
173 << format("|%=64s|\n") % "Available Adapters"
174 << format(" %|65T-| \n");
175
176 for(adapter_list_t::size_type i = 0;
177 i < adapterTypes.size();
178 ++i, out << "\n")
179 {
180 std::string adapter_string = adapterTypes[i].astr;
181 remove_and_replace_spaces(adapter_string);
182 out << format("|%3d| %-s%|65t||\n") % static_cast<unsigned>(i)
183 % adapter_string;
184 }
185
186 out << format(" %|65T-| \n");
187 }
188
189 }
190
191
192 end
193
194 start
195
196
197 /// @file generic/adapter_enumeration.cpp
198 /// implementation of reusable adapter enumeration helper functions
199
200 #include "generic/adapter_enumeration.h"
201
202 #include <string>
203 #include <locale>
204 #include <algorithm>
205 #include <boost/optional.hpp>
206 #include <boost/lexical_cast.hpp>
207 #pragma warning(push)
208 #pragma warning(disable : 4267 4244)
209 #include <boost/format.hpp>
210 #pragma warning(pop)
211
212 #include "FlashFactory.h"
213 #include "global.h"
214 #include "Adapter.h"
215
216 namespace generic {
217
218 namespace {
219 /// removes any leading or trailing spaces,
220 /// replaces any internal spaces with 'replacement_value'.
221 ///
222 /// @effects changes the argument mutable_string based on the above desc.
223 /// @param mutable_string the string to mutate.
224 /// @param replacement_value the value to use in place of the internal spaces.
225 inline void remove_and_replace_spaces(std::string& mutable_string,
226 std::string::value_type replacement_value = '_')
227 {
228 // remove leading ' '
229 mutable_string.erase(0, mutable_string.find_first_not_of(" "));
230 // remove trailing ' '
231 mutable_string.erase(mutable_string.find_last_not_of(" ")+1);
232
233 // replace any other ' ' with '_'
234 std::replace(mutable_string.begin(),mutable_string.end(),
235 ' ', replacement_value);
236 }
237
238 /// convert all characters in string to lower case
239 inline std::string to_lower(std::string const& input,
240 std::locale const& loc = std::locale()) {
241 std::string result = input;
242 for (std::string::size_type i = 0; i < result.size(); ++i) {
243 if (std::isupper(result[i], loc))
244 { result[i] = std::tolower(result[i], loc); }
245 }
246 return result;
247 }
248
249 }
250
251
252 FlashFactory::PROTOCOL_TYPE get_protocol_from_adapter(std::string const& adapter_string)
253 {
254 // make lower case so we are case insensitive
255 static const std::string::size_type s_END_POS = std::string::npos;
256 std::string lc_adapter_string = to_lower(adapter_string);
257 remove_and_replace_spaces(lc_adapter_string);
258
259 // default to None
260 FlashFactory::PROTOCOL_TYPE result = FlashFactory::None;
261
262 typedef std::vector<AdapterEntryType> adapter_list_t;
263 adapter_list_t available_adapters;
264 (void) g_PlatformManager.EnumerateAdapters(available_adapters);
265
266 for(adapter_list_t::size_type i = 0; i < available_adapters.size(); ++i)
267 {
268 std::string available_adapter_string
269 = to_lower(available_adapters[i].astr);
270 remove_and_replace_spaces(available_adapter_string);
271
272 if (lc_adapter_string == available_adapter_string)
273 {
274 switch(available_adapters[i].atype)
275 {
276 case (Adapter::Ata_Sdp2):
277 case (Adapter::Ata_Pcmcia):
278 case (Adapter::Ata_IM_Usb):
279 case (Adapter::Ata_Detected):
280 result = FlashFactory::Ata;
281 break;
282 case (Adapter::Sd_Epp):
283 case (Adapter::Sd_MyHost):
284 case (Adapter::Sd_Isa):
285 case (Adapter::Sd_Pcmcia):
286 result = FlashFactory::Sd;
287 break;
288 case (Adapter::Ms_Epp):
289 case (Adapter::Ms_IM_Usb):
290 case (Adapter::Ms_Pcmcia):
291 result = FlashFactory::Ms;
292 break;
293 case (Adapter::Gen_Scsi):
294 result = FlashFactory::Scsi;
295 break;
296 default:
297 // keep result as none
298 break;
299 }
300 break; // break out of the for loop
301 }
302 }
303
304 return result;
305 }
306
307 namespace {
308
309 /// convert a string to an unsigned integer if possible
310 /// @return SOME n if the string can represent an unsigned number
311 /// otherwise return NONE
312 inline boost::optional<unsigned> to_unsigned(std::string const& possible_number_string)
313 {
314 // zero length strings do not represent numbers
315 bool is_number = possible_number_string.size() > 0;
316 boost::optional<unsigned> result;
317
318 // is each character a number?
319 static const std::string s_DIGITS = "0123456789";
320 if (possible_number_string.find_first_not_of(s_DIGITS) == std::string::npos)
321 {
322 result = boost::lexical_cast<unsigned>(possible_number_string);
323 }
324
325 return result;
326 }
327
328 }
329
330 /// given an input string get the adapter string
331 /// @param input_string a string to use to get an adapter string
332 /// if it is a number then we can use it to pick from the list by index
333 /// otherwise we can use the string as the adapter string
334 boost::optional<std::string> get_adapter_string(std::string const& input_string)
335 {
336 boost::optional<std::string> result;
337
338 if (const boost::optional<unsigned> adapter_index_opt = to_unsigned(input_string))
339 {
340 typedef std::vector<AdapterEntryType> adapter_list_t;
341 adapter_list_t available_adapters;
342
343 // is the number valid? Can we create a flash manager?
344 if ( static_cast<int>(*adapter_index_opt)
345 < g_PlatformManager.EnumerateAdapters(available_adapters)
346 )
347 {
348 // use the string for that adapter type to figure out the protocol
349 result = available_adapters[*adapter_index_opt].astr;
350 remove_and_replace_spaces(*result);
351 }
352 }
353 else { result = input_string; }
354
355 return result;
356 }
357
358
359 void show_available_adapters(std::ostream& out) {
360 typedef std::vector<AdapterEntryType> adapter_list_t;
361 adapter_list_t adapterTypes;
362 (void) g_PlatformManager.EnumerateAdapters(adapterTypes);
363
364 using boost::format;
365
366 out << format(" %|65T-| \n")
367 << format("|%=64s|\n") % "Available Adapters"
368 << format(" %|65T-| \n");
369
370 for(adapter_list_t::size_type i = 0;
371 i < adapterTypes.size();
372 ++i, out << "\n")
373 {
374 std::string adapter_string = adapterTypes[i].astr;
375 remove_and_replace_spaces(adapter_string);
376 out << format("|%3d| %-s%|65t||\n") % static_cast<unsigned>(i)
377 % adapter_string;
378 }
379
380 out << format(" %|65T-| \n");
381 }
382
383 }
384
385 end
 
  
Legend:
Removed 
Changed
 Added

[Add General Comment] to topic.