start /// @file generic/adapter_enumeration.cpp /// implementation of reusable adapter enumeration helper functions #include "generic/adapter_enumeration.h" #include #include #include #include #include #pragma warning(push) #pragma warning(disable : 4267 4244) #include #pragma warning(pop) #include "FlashFactory.h" #include "global.h" #include "Adapter.h" namespace generic { namespace { /// removes any leading or trailing spaces, /// replaces any internal spaces with 'replacement_value'. /// /// @effects changes the argument mutable_string based on the above desc. /// @param mutable_string the string to mutate. /// @param replacement_value the value to use in place of the internal spaces. inline void remove_and_replace_spaces(std::string& mutable_string, std::string::value_type replacement_value = '_') { // remove leading ' ' mutable_string.erase(0, mutable_string.find_first_not_of(" ")); // remove trailing ' ' mutable_string.erase(mutable_string.find_last_not_of(" ")+1); // replace any other ' ' with '_' std::replace(mutable_string.begin(),mutable_string.end(), ' ', replacement_value); } /// convert all characters in string to lower case inline std::string to_lower(std::string const& input, std::locale const& loc = std::locale()) { std::string result = input; for (std::string::size_type i = 0; i < result.size(); ++i) { if (std::isupper(result[i], loc)) { result[i] = std::tolower(result[i], loc); } } return result; } } FlashFactory::PROTOCOL_TYPE get_protocol_from_adapter(std::string const& adapter_string) { // make lower case so we are case insensitive static const std::string::size_type s_END_POS = std::string::npos; std::string lc_adapter_string = to_lower(adapter_string); remove_and_replace_spaces(lc_adapter_string); // default to None FlashFactory::PROTOCOL_TYPE result = FlashFactory::None; typedef std::vector adapter_list_t; adapter_list_t available_adapters; (void) g_PlatformManager.EnumerateAdapters(available_adapters); for(adapter_list_t::size_type i = 0; i < available_adapters.size(); ++i) { std::string available_adapter_string = to_lower(available_adapters[i].astr); remove_and_replace_spaces(available_adapter_string); if (lc_adapter_string == available_adapter_string) { switch(available_adapters[i].atype) { case (Adapter::Ata_Sdp2): case (Adapter::Ata_Pcmcia): case (Adapter::Ata_IM_Usb): case (Adapter::Ata_Detected): result = FlashFactory::Ata; break; case (Adapter::Sd_Epp): case (Adapter::Sd_MyHost): case (Adapter::Sd_Isa): case (Adapter::Sd_Pcmcia): result = FlashFactory::Sd; break; case (Adapter::Ms_Epp): case (Adapter::Ms_IM_Usb): case (Adapter::Ms_Pcmcia): result = FlashFactory::Ms; break; case (Adapter::Gen_Scsi): result = FlashFactory::Scsi; break; default: // keep result as none break; } break; // break out of the for loop } } return result; } namespace { /// convert a string to an unsigned integer if possible /// @return SOME n if the string can represent an unsigned number /// otherwise return NONE inline boost::optional to_unsigned(std::string const& possible_number_string) { // zero length strings do not represent numbers bool is_number = possible_number_string.size() > 0; boost::optional result; // is each character a number? static const std::string s_DIGITS = "0123456789"; if (possible_number_string.find_first_not_of(s_DIGITS) == std::string::npos) { result = boost::lexical_cast(possible_number_string); } return result; } } /// given an input string get the adapter string /// @param input_string a string to use to get an adapter string /// if it is a number then we can use it to pick from the list by index /// otherwise we can use the string as the adapter string boost::optional get_adapter_string(std::string const& input_string) { boost::optional result; if (const boost::optional adapter_index_opt = to_unsigned(input_string)) { typedef std::vector adapter_list_t; adapter_list_t available_adapters; // is the number valid? Can we create a flash manager? if ( static_cast(*adapter_index_opt) < g_PlatformManager.EnumerateAdapters(available_adapters) ) { // use the string for that adapter type to figure out the protocol result = available_adapters[*adapter_index_opt].astr; remove_and_replace_spaces(*result); } } else { result = input_string; } return result; } void show_available_adapters(std::ostream& out) { typedef std::vector adapter_list_t; adapter_list_t adapterTypes; (void) g_PlatformManager.EnumerateAdapters(adapterTypes); using boost::format; out << format(" %|65T-| \n") << format("|%=64s|\n") % "Available Adapters" << format(" %|65T-| \n"); for(adapter_list_t::size_type i = 0; i < adapterTypes.size(); ++i, out << "\n") { std::string adapter_string = adapterTypes[i].astr; remove_and_replace_spaces(adapter_string); out << format("|%3d| %-s%|65t||\n") % static_cast(i) % adapter_string; } out << format(" %|65T-| \n"); } } end start /// @file generic/adapter_enumeration.cpp /// implementation of reusable adapter enumeration helper functions #include "generic/adapter_enumeration.h" #include #include #include #include #include #pragma warning(push) #pragma warning(disable : 4267 4244) #include #pragma warning(pop) #include "FlashFactory.h" #include "global.h" #include "Adapter.h" namespace generic { namespace { /// removes any leading or trailing spaces, /// replaces any internal spaces with 'replacement_value'. /// /// @effects changes the argument mutable_string based on the above desc. /// @param mutable_string the string to mutate. /// @param replacement_value the value to use in place of the internal spaces. inline void remove_and_replace_spaces(std::string& mutable_string, std::string::value_type replacement_value = '_') { // remove leading ' ' mutable_string.erase(0, mutable_string.find_first_not_of(" ")); // remove trailing ' ' mutable_string.erase(mutable_string.find_last_not_of(" ")+1); // replace any other ' ' with '_' std::replace(mutable_string.begin(),mutable_string.end(), ' ', replacement_value); } /// convert all characters in string to lower case inline std::string to_lower(std::string const& input, std::locale const& loc = std::locale()) { std::string result = input; for (std::string::size_type i = 0; i < result.size(); ++i) { if (std::isupper(result[i], loc)) { result[i] = std::tolower(result[i], loc); } } return result; } } FlashFactory::PROTOCOL_TYPE get_protocol_from_adapter(std::string const& adapter_string) { // make lower case so we are case insensitive static const std::string::size_type s_END_POS = std::string::npos; std::string lc_adapter_string = to_lower(adapter_string); remove_and_replace_spaces(lc_adapter_string); // default to None FlashFactory::PROTOCOL_TYPE result = FlashFactory::None; typedef std::vector adapter_list_t; adapter_list_t available_adapters; (void) g_PlatformManager.EnumerateAdapters(available_adapters); for(adapter_list_t::size_type i = 0; i < available_adapters.size(); ++i) { std::string available_adapter_string = to_lower(available_adapters[i].astr); remove_and_replace_spaces(available_adapter_string); if (lc_adapter_string == available_adapter_string) { switch(available_adapters[i].atype) { case (Adapter::Ata_Sdp2): case (Adapter::Ata_Pcmcia): case (Adapter::Ata_IM_Usb): case (Adapter::Ata_Detected): result = FlashFactory::Ata; break; case (Adapter::Sd_Epp): case (Adapter::Sd_MyHost): case (Adapter::Sd_Isa): case (Adapter::Sd_Pcmcia): result = FlashFactory::Sd; break; case (Adapter::Ms_Epp): case (Adapter::Ms_IM_Usb): case (Adapter::Ms_Pcmcia): result = FlashFactory::Ms; break; case (Adapter::Gen_Scsi): result = FlashFactory::Scsi; break; default: // keep result as none break; } break; // break out of the for loop } } return result; } namespace { /// convert a string to an unsigned integer if possible /// @return SOME n if the string can represent an unsigned number /// otherwise return NONE inline boost::optional to_unsigned(std::string const& possible_number_string) { // zero length strings do not represent numbers bool is_number = possible_number_string.size() > 0; boost::optional result; // is each character a number? static const std::string s_DIGITS = "0123456789"; if (possible_number_string.find_first_not_of(s_DIGITS) == std::string::npos) { result = boost::lexical_cast(possible_number_string); } return result; } } /// given an input string get the adapter string /// @param input_string a string to use to get an adapter string /// if it is a number then we can use it to pick from the list by index /// otherwise we can use the string as the adapter string boost::optional get_adapter_string(std::string const& input_string) { boost::optional result; if (const boost::optional adapter_index_opt = to_unsigned(input_string)) { typedef std::vector adapter_list_t; adapter_list_t available_adapters; // is the number valid? Can we create a flash manager? if ( static_cast(*adapter_index_opt) < g_PlatformManager.EnumerateAdapters(available_adapters) ) { // use the string for that adapter type to figure out the protocol result = available_adapters[*adapter_index_opt].astr; remove_and_replace_spaces(*result); } } else { result = input_string; } return result; } void show_available_adapters(std::ostream& out) { typedef std::vector adapter_list_t; adapter_list_t adapterTypes; (void) g_PlatformManager.EnumerateAdapters(adapterTypes); using boost::format; out << format(" %|65T-| \n") << format("|%=64s|\n") % "Available Adapters" << format(" %|65T-| \n"); for(adapter_list_t::size_type i = 0; i < adapterTypes.size(); ++i, out << "\n") { std::string adapter_string = adapterTypes[i].astr; remove_and_replace_spaces(adapter_string); out << format("|%3d| %-s%|65t||\n") % static_cast(i) % adapter_string; } out << format(" %|65T-| \n"); } } end