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