second commit

master
JZFamily 2017-11-03 19:43:55 +08:00
parent f55752a979
commit 68c83d7542
3 changed files with 208 additions and 0 deletions

161
AuxFun.cpp Normal file
View File

@ -0,0 +1,161 @@
#include "AuxFun.h"
#include<Windows.h>
#include <string>
#include <assert.h>
#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;
}
//----------------------------------------------
//Referencehttp://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;
}

9
AuxFun.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include<string>
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);

38
CWeatherDataSpider.h Normal file
View File

@ -0,0 +1,38 @@
//------------------------------------
//author: jidzh
//data; 2017-10-30
//------------------------------------
#pragma once
#include <string>
#include <WinSock2.h>
#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();
};