From: <man...@us...> - 2013-11-23 10:21:23
|
Revision: 3299 http://sourceforge.net/p/modplug/code/3299 Author: manxorist Date: 2013-11-23 10:21:12 +0000 (Sat, 23 Nov 2013) Log Message: ----------- [Ref] Move hex encoding and decoding from Settings.h into misc_util.h . Modified Paths: -------------- trunk/OpenMPT/common/misc_util.cpp trunk/OpenMPT/common/misc_util.h trunk/OpenMPT/mptrack/Settings.cpp trunk/OpenMPT/mptrack/Settings.h Modified: trunk/OpenMPT/common/misc_util.cpp =================================================================== --- trunk/OpenMPT/common/misc_util.cpp 2013-11-23 09:49:52 UTC (rev 3298) +++ trunk/OpenMPT/common/misc_util.cpp 2013-11-23 10:21:12 UTC (rev 3299) @@ -328,3 +328,67 @@ } // namespace Util #endif // MODPLUG_TRACKER + + +namespace Util +{ + + +static const wchar_t EncodeNibble[16] = { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7', L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' }; + +static inline bool DecodeByte(uint8 &byte, wchar_t c1, wchar_t c2) +{ + byte = 0; + if(L'0' <= c1 && c1 <= L'9') + { + byte += static_cast<uint8>((c1 - L'0') << 4); + } else if(L'A' <= c1 && c1 <= L'F') + { + byte += static_cast<uint8>((c1 - L'A' + 10) << 4); + } else + { + return false; + } + if(L'0' <= c2 && c2 <= L'9') + { + byte += static_cast<uint8>(c2 - L'0'); + } else if(L'A' <= c2 && c2 <= L'F') + { + byte += static_cast<uint8>(c2 - L'A' + 10); + } else + { + return false; + } + return true; +} + + +std::wstring BinToHex(const std::vector<char> &src) +{ + std::wstring result; + for(std::size_t i = 0; i < src.size(); ++i) + { + uint8 byte = src[i]; + result.push_back(EncodeNibble[(byte&0xf0)>>4]); + result.push_back(EncodeNibble[byte&0x0f]); + } + return result; +} + +std::vector<char> HexToBin(const std::wstring &src) +{ + std::vector<char> result; + for(std::size_t i = 0; i+1 < src.size(); i += 2) + { + uint8 byte = 0; + if(!DecodeByte(byte, src[i*2+0], src[i*2+1])) + { + return result; + } + result.push_back(byte); + } + return result; +} + + +} // namespace Util Modified: trunk/OpenMPT/common/misc_util.h =================================================================== --- trunk/OpenMPT/common/misc_util.h 2013-11-23 09:49:52 UTC (rev 3298) +++ trunk/OpenMPT/common/misc_util.h 2013-11-23 10:21:12 UTC (rev 3299) @@ -628,4 +628,12 @@ bool IsCLSID(const std::wstring &str); } // namespace Util -#endif // MODPLUG_TRACKER \ No newline at end of file +#endif // MODPLUG_TRACKER + +namespace Util +{ + +std::wstring BinToHex(const std::vector<char> &src); +std::vector<char> HexToBin(const std::wstring &src); + +} // namespace Util Modified: trunk/OpenMPT/mptrack/Settings.cpp =================================================================== --- trunk/OpenMPT/mptrack/Settings.cpp 2013-11-23 09:49:52 UTC (rev 3298) +++ trunk/OpenMPT/mptrack/Settings.cpp 2013-11-23 10:21:12 UTC (rev 3299) @@ -23,64 +23,6 @@ #include <iterator> -static const wchar_t EncodeNibble[16] = { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7', L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' }; - -static inline bool DecodeByte(uint8 &byte, wchar_t c1, wchar_t c2) -{ - byte = 0; - if(L'0' <= c1 && c1 <= L'9') - { - byte += static_cast<uint8>((c1 - L'0') << 4); - } else if(L'A' <= c1 && c1 <= L'F') - { - byte += static_cast<uint8>((c1 - L'A' + 10) << 4); - } else - { - return false; - } - if(L'0' <= c2 && c2 <= L'9') - { - byte += static_cast<uint8>(c2 - L'0'); - } else if(L'A' <= c2 && c2 <= L'F') - { - byte += static_cast<uint8>(c2 - L'A' + 10); - } else - { - return false; - } - return true; -} - - -std::wstring SettingBinToHex(const std::vector<char> &src) -{ - std::wstring result; - for(std::size_t i = 0; i < src.size(); ++i) - { - uint8 byte = src[i]; - result.push_back(EncodeNibble[(byte&0xf0)>>4]); - result.push_back(EncodeNibble[byte&0x0f]); - } - return result; -} - -std::vector<char> SettingHexToBin(const std::wstring &src) -{ - std::vector<char> result; - for(std::size_t i = 0; i+1 < src.size(); i += 2) - { - uint8 byte = 0; - if(!DecodeByte(byte, src[i*2+0], src[i*2+1])) - { - return result; - } - result.push_back(byte); - } - return result; -} - - - std::wstring SettingValue::FormatTypeAsString() const { if(GetType() == SettingTypeNone) @@ -135,7 +77,7 @@ return valueString; break; case SettingTypeBinary: - return SettingBinToHex(valueBinary); + return Util::BinToHex(valueBinary); break; case SettingTypeNone: default: @@ -168,7 +110,7 @@ valueString = newVal; break; case SettingTypeBinary: - valueBinary = SettingHexToBin(newVal); + valueBinary = Util::HexToBin(newVal); break; case SettingTypeNone: default: Modified: trunk/OpenMPT/mptrack/Settings.h =================================================================== --- trunk/OpenMPT/mptrack/Settings.h 2013-11-23 09:49:52 UTC (rev 3298) +++ trunk/OpenMPT/mptrack/Settings.h 2013-11-23 10:21:12 UTC (rev 3299) @@ -32,9 +32,6 @@ SettingTypeBinary, }; -std::wstring SettingBinToHex(const std::vector<char> &src); -std::vector<char> SettingHexToBin(const std::wstring &src); - // SettingValue is a variant type that stores any type that can natively be represented in a config backend. // Any other type that should be stored must provide a matching ToSettingValue and FromSettingValue. // Other types can optionally also set a type tag which would get checked in debug builds. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |