瀏覽代碼

完成IP地址的格式校验

zhangyang 6 年之前
父節點
當前提交
63339d1d14

二進制
bin/Win32/Debug/zhipuzi_pos_windows/db/pos.db


+ 1 - 0
bin/Win32/Debug/zhipuzi_pos_windows/skin/chufang_printer_setting.xml

@@ -17,6 +17,7 @@
 		<HorizontalLayout height="56" valign="center">
 			<Label text="IP地址" width="160" />
 			<Edit name="chufang_setting_ip" width="150" height="32" padding="0,12,0,12" normalimage="file='Chat_InputBox_BG.png' corner='4,4,4,4'" hotimage="file='Chat_InputBox_BG_Hover.png' corner='4,4,4,4'" />
+			<Label name="chufang_setting_ip_error" text="IP地址格式不正确" padding="20,0,0,0" textcolor="#FFFF0000" />
 		</HorizontalLayout>
 
 		<HorizontalLayout height="56" valign="center">

二進制
bin/Win32/Debug/zhipuzi_pos_windows/zhipuzi_pos_windows.exe


+ 254 - 73
lewaimai_dispatch/helper/CLewaimaiString.cpp

@@ -14,17 +14,29 @@ using namespace boost::archive::iterators;
 
 unsigned char ToHex(unsigned char x)
 {
-	return  x > 9 ? x + 55 : x + 48;
+    return  x > 9 ? x + 55 : x + 48;
 }
 
 unsigned char FromHex(unsigned char x)
 {
-	unsigned char y;
-	if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
-	else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
-	else if (x >= '0' && x <= '9') y = x - '0';
-	else assert(0);
-	return y;
+    unsigned char y;
+    if(x >= 'A' && x <= 'Z')
+    {
+        y = x - 'A' + 10;
+    }
+    else if(x >= 'a' && x <= 'z')
+    {
+        y = x - 'a' + 10;
+    }
+    else if(x >= '0' && x <= '9')
+    {
+        y = x - '0';
+    }
+    else
+    {
+        assert(0);
+    }
+    return y;
 }
 
 CLewaimaiString::CLewaimaiString()
@@ -38,85 +50,254 @@ CLewaimaiString::~CLewaimaiString()
 
 bool CLewaimaiString::base64_encode(const string& input, string* output)
 {
-	typedef base64_from_binary<transform_width<string::const_iterator, 6, 8>> Base64EncodeIterator;
-	stringstream result;
-	try {
-		copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator<char>(result));
-	}
-	catch (...) {
-		return false;
-	}
-	size_t equal_count = (3 - input.length() % 3) % 3;
-	for (size_t i = 0; i < equal_count; i++)
-	{
-		result.put('=');
-	}
-	*output = result.str();
-	return output->empty() == false;
+    typedef base64_from_binary<transform_width<string::const_iterator, 6, 8>> Base64EncodeIterator;
+    stringstream result;
+    try
+    {
+        copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator<char>(result));
+    }
+    catch(...)
+    {
+        return false;
+    }
+    size_t equal_count = (3 - input.length() % 3) % 3;
+    for(size_t i = 0; i < equal_count; i++)
+    {
+        result.put('=');
+    }
+    *output = result.str();
+    return output->empty() == false;
 }
 
 bool CLewaimaiString::base64_decode(const string& input, string* output)
 {
-	typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> Base64DecodeIterator;
-	stringstream result;
-	try {
-		copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator<char>(result));
-	}
-	catch (...) {
-		return false;
-	}
-	*output = result.str();
-	return output->empty() == false;
+    typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> Base64DecodeIterator;
+    stringstream result;
+    try
+    {
+        copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator<char>(result));
+    }
+    catch(...)
+    {
+        return false;
+    }
+    *output = result.str();
+    return output->empty() == false;
 }
 
-void CLewaimaiString::trim(string &s)
+void CLewaimaiString::trim(string& s)
 {
-	if (!s.empty())
-	{
-		s.erase(0, s.find_first_not_of(" "));
-		s.erase(s.find_last_not_of(" ") + 1);
-	}
+    if(!s.empty())
+    {
+        s.erase(0, s.find_first_not_of(" "));
+        s.erase(s.find_last_not_of(" ") + 1);
+    }
 }
 
 std::string CLewaimaiString::UrlEncode(const std::string& str)
 {
-	std::string strTemp = "";
-	size_t length = str.length();
-	for (size_t i = 0; i < length; i++)
-	{
-		if (isalnum((unsigned char)str[i]) ||
-			(str[i] == '-') ||
-			(str[i] == '_') ||
-			(str[i] == '.') ||
-			(str[i] == '~'))
-			strTemp += str[i];
-		else if (str[i] == ' ')
-			strTemp += "+";
-		else
-		{
-			strTemp += '%';
-			strTemp += ToHex((unsigned char)str[i] >> 4);
-			strTemp += ToHex((unsigned char)str[i] % 16);
-		}
-	}
-	return strTemp;
+    std::string strTemp = "";
+    size_t length = str.length();
+    for(size_t i = 0; i < length; i++)
+    {
+        if(isalnum((unsigned char)str[i]) ||
+                (str[i] == '-') ||
+                (str[i] == '_') ||
+                (str[i] == '.') ||
+                (str[i] == '~'))
+        {
+            strTemp += str[i];
+        }
+        else if(str[i] == ' ')
+        {
+            strTemp += "+";
+        }
+        else
+        {
+            strTemp += '%';
+            strTemp += ToHex((unsigned char)str[i] >> 4);
+            strTemp += ToHex((unsigned char)str[i] % 16);
+        }
+    }
+    return strTemp;
 }
 
 std::string CLewaimaiString::UrlDecode(const std::string& str)
 {
-	std::string strTemp = "";
-	size_t length = str.length();
-	for (size_t i = 0; i < length; i++)
-	{
-		if (str[i] == '+') strTemp += ' ';
-		else if (str[i] == '%')
-		{
-			assert(i + 2 < length);
-			unsigned char high = FromHex((unsigned char)str[++i]);
-			unsigned char low = FromHex((unsigned char)str[++i]);
-			strTemp += high * 16 + low;
-		}
-		else strTemp += str[i];
-	}
-	return strTemp;
+    std::string strTemp = "";
+    size_t length = str.length();
+    for(size_t i = 0; i < length; i++)
+    {
+        if(str[i] == '+')
+        {
+            strTemp += ' ';
+        }
+        else if(str[i] == '%')
+        {
+            assert(i + 2 < length);
+            unsigned char high = FromHex((unsigned char)str[++i]);
+            unsigned char low = FromHex((unsigned char)str[++i]);
+            strTemp += high * 16 + low;
+        }
+        else
+        {
+            strTemp += str[i];
+        }
+    }
+    return strTemp;
 }
+
+std::string CLewaimaiString::UnicodeToUTF8(const std::wstring& wstr)
+{
+    std::string ret;
+    try
+    {
+        std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
+        ret = wcv.to_bytes(wstr);
+    }
+    catch(const std::exception& e)
+    {
+        std::cerr << e.what() << std::endl;
+    }
+    return ret;
+}
+
+std::wstring CLewaimaiString::UTF8ToUnicode(const std::string& str)
+{
+    std::wstring ret;
+    try
+    {
+        std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
+        ret = wcv.from_bytes(str);
+    }
+    catch(const std::exception& e)
+    {
+        std::cerr << e.what() << std::endl;
+    }
+    return ret;
+}
+
+std::string CLewaimaiString::UnicodeToANSI(const std::wstring& wstr)
+{
+    std::string ret;
+    std::mbstate_t state = {};
+    const wchar_t* src = wstr.data();
+    size_t len = std::wcsrtombs(nullptr, &src, 0, &state);
+    if(static_cast<size_t>(-1) != len)
+    {
+        std::unique_ptr< char[] > buff(new char[len + 1]);
+        len = std::wcsrtombs(buff.get(), &src, len, &state);
+        if(static_cast<size_t>(-1) != len)
+        {
+            ret.assign(buff.get(), len);
+        }
+    }
+    return ret;
+}
+
+std::wstring CLewaimaiString::ANSIToUnicode(const std::string& str)
+{
+    std::wstring ret;
+    std::mbstate_t state = {};
+    const char* src = str.data();
+    size_t len = std::mbsrtowcs(nullptr, &src, 0, &state);
+    if(static_cast<size_t>(-1) != len)
+    {
+        std::unique_ptr< wchar_t[] > buff(new wchar_t[len + 1]);
+        len = std::mbsrtowcs(buff.get(), &src, len, &state);
+        if(static_cast<size_t>(-1) != len)
+        {
+            ret.assign(buff.get(), len);
+        }
+    }
+    return ret;
+}
+
+std::string CLewaimaiString::UTF8ToANSI(const std::string& str)
+{
+    return UnicodeToANSI(UTF8ToUnicode(str));
+}
+
+std::string CLewaimaiString::ANSIToUTF8(const std::string& str)
+{
+    return UnicodeToUTF8(ANSIToUnicode(str));
+}
+
+std::string CLewaimaiString::DoubleToString(const double value, unsigned int precisionAfterPoint)
+{
+    char str[256];
+
+    std::string pre = "%." + to_string(precisionAfterPoint) + "f";
+    sprintf(str, pre.c_str(), value);
+    string result = str;
+    return result;
+}
+
+bool CLewaimaiString::isIPAddressValid(const char* pszIPAddr)
+{
+    if(!pszIPAddr)
+    {
+        return false;    //若pszIPAddr为空
+    }
+    char IP1[100], cIP[4];
+    int len = strlen(pszIPAddr);
+    int i = 0, j = len - 1;
+    int k, m = 0, n = 0, num = 0;
+    //去除首尾空格(取出从i-1到j+1之间的字符):
+    while(pszIPAddr[i++] == ' ');
+    while(pszIPAddr[j--] == ' ');
+
+    for(k = i - 1; k <= j + 1; k++)
+    {
+        IP1[m++] = *(pszIPAddr + k);
+    }
+    IP1[m] = '\0';
+
+    char* p = IP1;
+
+    while(*p != '\0')
+    {
+        if(*p == ' ' || *p < '0' || *p > '9')
+        {
+            return false;
+        }
+        cIP[n++] = *p; //保存每个子段的第一个字符,用于之后判断该子段是否为0开头
+
+        int sum = 0;  //sum为每一子段的数值,应在0到255之间
+        while(*p != '.' && *p != '\0')
+        {
+            if(*p == ' ' || *p < '0' || *p > '9')
+            {
+                return false;
+            }
+            sum = sum * 10 + *p - 48;  //每一子段字符串转化为整数
+            p++;
+        }
+        if(*p == '.')
+        {
+            if((*(p - 1) >= '0' && *(p - 1) <= '9') && (*(p + 1) >= '0' && *(p + 1) <= '9')) //判断"."前后是否有数字,若无,则为无效IP,如“1.1.127.”
+            {
+                num++;    //记录“.”出现的次数,不能大于3
+            }
+            else
+            {
+                return false;
+            }
+        };
+        if((sum > 255) || (sum > 0 && cIP[0] == '0') || num > 3)
+        {
+            return false;    //若子段的值>255或为0开头的非0子段或“.”的数目>3,则为无效IP
+        }
+
+        if(*p != '\0')
+        {
+            p++;
+        }
+        n = 0;
+    }
+    if(num != 3)
+    {
+        return false;
+    }
+    return true;
+}

+ 10 - 72
lewaimai_dispatch/helper/CLewaimaiString.h

@@ -15,85 +15,23 @@ public:
 
 	static void trim(string &s);
 
-	static std::string UnicodeToUTF8(const std::wstring & wstr)
-	{
-		std::string ret;
-		try {
-			std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
-			ret = wcv.to_bytes(wstr);
-		}
-		catch (const std::exception & e) {
-			std::cerr << e.what() << std::endl;
-		}
-		return ret;
-	}
+	static std::string UnicodeToUTF8(const std::wstring & wstr);
 
-	static std::wstring UTF8ToUnicode(const std::string & str)
-	{
-		std::wstring ret;
-		try {
-			std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
-			ret = wcv.from_bytes(str);
-		}
-		catch (const std::exception & e) {
-			std::cerr << e.what() << std::endl;
-		}
-		return ret;
-	}
+	static std::wstring UTF8ToUnicode(const std::string & str);
 
-	static std::string UnicodeToANSI(const std::wstring & wstr)
-	{
-		std::string ret;
-		std::mbstate_t state = {};
-		const wchar_t *src = wstr.data();
-		size_t len = std::wcsrtombs(nullptr, &src, 0, &state);
-		if (static_cast<size_t>(-1) != len) {
-			std::unique_ptr< char[] > buff(new char[len + 1]);
-			len = std::wcsrtombs(buff.get(), &src, len, &state);
-			if (static_cast<size_t>(-1) != len) {
-				ret.assign(buff.get(), len);
-			}
-		}
-		return ret;
-	}
+	static std::string UnicodeToANSI(const std::wstring & wstr);
 
-	static std::wstring ANSIToUnicode(const std::string & str)
-	{
-		std::wstring ret;
-		std::mbstate_t state = {};
-		const char *src = str.data();
-		size_t len = std::mbsrtowcs(nullptr, &src, 0, &state);
-		if (static_cast<size_t>(-1) != len) {
-			std::unique_ptr< wchar_t[] > buff(new wchar_t[len + 1]);
-			len = std::mbsrtowcs(buff.get(), &src, len, &state);
-			if (static_cast<size_t>(-1) != len) {
-				ret.assign(buff.get(), len);
-			}
-		}
-		return ret;
-	}
+	static std::wstring ANSIToUnicode(const std::string & str);
 
-	static std::string UTF8ToANSI(const std::string & str)
-	{
-		return UnicodeToANSI(UTF8ToUnicode(str));
-	}
+	static std::string UTF8ToANSI(const std::string & str);
 
-	static std::string ANSIToUTF8(const std::string & str)
-	{
-		return UnicodeToUTF8(ANSIToUnicode(str));
-	}
-
-	static std::string DoubleToString(const double value, unsigned int precisionAfterPoint)
-	{
-		char str[256];
-
-		std::string pre = "%." + to_string(precisionAfterPoint) + "f";
-		sprintf(str, pre.c_str(), value);
-		string result = str;
-		return result;
-	}
+	static std::string ANSIToUTF8(const std::string & str);
+	
+	static std::string DoubleToString(const double value, unsigned int precisionAfterPoint);
 
 	static std::string UrlEncode(const std::string& str);
 	static std::string UrlDecode(const std::string& str);
+
+	static bool isIPAddressValid(const char* pszIPAddr);
 };
 

+ 9 - 4
lewaimai_dispatch/network/CMessagePush.cpp

@@ -106,6 +106,8 @@ void CMessagePush::HandleMessage(std::string msg)
 				PlaySound(path.c_str(), NULL, SND_FILENAME | SND_ASYNC);
 			}
 
+			//这里注意,下面要交给另外一个线程去处理,否则如果没有厨房打印机试图连接可能会卡住线程
+
 			//判断是否要自动确认
 			if (CSetting::GetParam("setting_is_new_waimai_autoconfirm") == "1")
 			{
@@ -119,16 +121,19 @@ void CMessagePush::HandleMessage(std::string msg)
 				
 			}
 
-			//判断是否自动打印
+			//判断是否自动打印收银小票
 			if (CSetting::GetParam("setting_is_new_waimai_printer") == "1")
 			{
 				CPosPrinter printer;
 				printer.PrintWaimaiOrder(order_id, order_no);
 			}
 
-			CPosPrinter printer(2);
-
-			printer.PrintWaimaiOrder(order_id, order_no);
+			//判断是否进行自动的厨房打印
+			if (CSetting::GetParam("setting_is_new_waimai_chufang_printer") == "1")
+			{
+				CPosPrinter printer(2);
+				printer.PrintWaimaiOrder(order_id, order_no);
+			}
 		}
 	}
 	catch (std::exception& e)

+ 13 - 0
lewaimai_dispatch/wnd/CChufangSettingWnd.h

@@ -35,6 +35,9 @@ public:
 
     void Init()
     {
+		CLabelUI* ip_error = static_cast<CLabelUI*>(m_pm.FindControl(_T("chufang_setting_ip_error")));
+		ip_error->SetVisible(false);
+
 		CLabelUI* title = static_cast<CLabelUI*>(m_pm.FindControl(_T("chufang_setting_title")));
 		if (m_mode == 1)
 		{
@@ -148,6 +151,16 @@ public:
                 CEditUI* pIP = static_cast<CEditUI*>(m_pm.FindControl(_T("chufang_setting_ip")));
                 wstring wsIP = pIP->GetText();
 
+				//判断IP的格式是否合法
+				if (CLewaimaiString::isIPAddressValid(CLewaimaiString::UnicodeToUTF8(wsIP).c_str()) == false)
+				{
+					//如果IP格式不合法,就提示
+					CLabelUI* ip_error = static_cast<CLabelUI*>(m_pm.FindControl(_T("chufang_setting_ip_error")));
+					ip_error->SetVisible(true);
+
+					return;
+				}
+
                 CComboUI* com = static_cast<CComboUI*>(m_pm.FindControl(_T("chufang_setting_guige")));
                 wstring wsGuige;
                 if(com->GetCurSel() == 0)