From 68c83d75425520fb90f9d5778125801a3866117d Mon Sep 17 00:00:00 2001 From: JZFamily Date: Fri, 3 Nov 2017 19:43:55 +0800 Subject: [PATCH] second commit --- AuxFun.cpp | 161 +++++++++++++++++++++++++++++++++++++++++++ AuxFun.h | 9 +++ CWeatherDataSpider.h | 38 ++++++++++ 3 files changed, 208 insertions(+) create mode 100644 AuxFun.cpp create mode 100644 AuxFun.h create mode 100644 CWeatherDataSpider.h diff --git a/AuxFun.cpp b/AuxFun.cpp new file mode 100644 index 0000000..01a73de --- /dev/null +++ b/AuxFun.cpp @@ -0,0 +1,161 @@ +#include "AuxFun.h" +#include +#include +#include + +#include "zlib/zconf.h" +#include "zlib/zlib.h" +std::string MBCStoUTF8(const char* mbcsStr) +{ + wchar_t* wideStr; + char* strAnsi; + int charLen; + //先转换成utf-16 + charLen = MultiByteToWideChar(CP_ACP, 0, mbcsStr, -1, NULL, 0); + wideStr = (wchar_t*)malloc(sizeof(wchar_t)*charLen); + MultiByteToWideChar(CP_ACP, 0, mbcsStr, -1, wideStr, charLen); + //再转换成utf-8 + charLen = WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, NULL, 0, NULL, NULL); + strAnsi = (char*)malloc(charLen); + WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, strAnsi, charLen, NULL, NULL); + std::string strRet = strAnsi; + + free(wideStr); + free(strAnsi); + return strRet; +} +std::string UTF8ToMultiByte(const char* mbcsStr) +{ + wchar_t* wideStr; + char* strAnsi; + int charLen; + + charLen = MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, NULL, 0); + wideStr = (wchar_t*)malloc(sizeof(wchar_t)*charLen); + MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, wideStr, charLen); + + charLen = WideCharToMultiByte(CP_ACP, 0, wideStr, -1, NULL, 0, NULL, NULL); + strAnsi = (char*)malloc(charLen); + WideCharToMultiByte(CP_ACP, 0, wideStr, -1, strAnsi, charLen, NULL, NULL); + + std::string strRet = strAnsi; + free(wideStr); + + free(strAnsi); + return strRet; +} + +//---------------------------------------------- +//Reference:http://blog.csdn.net/gemo/article/details/8468311 +//______________________________________________ +unsigned char ToHex(unsigned char x) +{ + //十进制 'A'65 '0'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; +} + +std::string 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 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; +} +int inflate_read(char *source, int len, char **dest, int gzip, int& iTotalRead) +{ + int ret; + unsigned have; + z_stream strm; + unsigned char out[10000]; + int totalsize = 0; + + /* allocate inflate state */ + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = 0; + strm.next_in = Z_NULL; + + if (gzip) + ret = inflateInit2(&strm, 47); + else + ret = inflateInit(&strm); + + if (ret != Z_OK) + return ret; + + strm.avail_in = len; + strm.next_in = (Bytef*)source; + + /* run inflate() on input until output buffer not full */ + do { + strm.avail_out = 10000; + strm.next_out = out; + ret = inflate(&strm, Z_NO_FLUSH); + //assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + + switch (ret) { + case Z_NEED_DICT: + ret = Z_DATA_ERROR; /* and fall through */ + case Z_DATA_ERROR: + case Z_MEM_ERROR: + inflateEnd(&strm); + return ret; + } + + have = 10000 - strm.avail_out; + totalsize += have; + *dest = (char*)realloc(*dest, totalsize); + iTotalRead = totalsize; + memcpy(*dest + totalsize - have, out, have); + } while (strm.avail_out == 0); + + /* clean up and return */ + (void)inflateEnd(&strm); + + return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; +} \ No newline at end of file diff --git a/AuxFun.h b/AuxFun.h new file mode 100644 index 0000000..8434204 --- /dev/null +++ b/AuxFun.h @@ -0,0 +1,9 @@ + +#pragma once +#include +std::string MBCStoUTF8(const char* mbcsStr); +std::string UTF8ToMultiByte(const char* mbcsStr); + +std::string UrlEncode(const std::string& str); + +int inflate_read(char *source, int len, char **dest, int gzip, int& iTotalRead); \ No newline at end of file diff --git a/CWeatherDataSpider.h b/CWeatherDataSpider.h new file mode 100644 index 0000000..43a346f --- /dev/null +++ b/CWeatherDataSpider.h @@ -0,0 +1,38 @@ +//------------------------------------ +//author: jidzh +//data; 2017-10-30 +//------------------------------------ + +#pragma once +#include +#include +#ifdef DEBUG + +#else + +#endif // DEBUG + + +class CWeatherDataSpider +{ +public: + CWeatherDataSpider(); + ~CWeatherDataSpider(); +public: + bool GetWeatherDataXml(); + void CloseConnect(); + void ErrHandle(const std::wstring msg, int code =0); +public: + std::string m_strAreaCode; +private: + SOCKET m_socket; + //WSADATA m_WSAdata; + bool CheckIfRecSucceed(char * buf, int nTotalRecv, int nFlag); + + bool ConnectHost(); + + bool SendReq(); + + bool HandleAck(); +}; +