typedef struct tagxxxListItem //结构定义
{
char Clientxxxx[64];
char Serverxxxx[64];
char ServerID[64];
char ClientUser[64];
int OperType;
char OperDate[32];
char OperContent[512];
long adddate;
long pushdate;
long writefiledate;
long readandsenddat;
long readandsenddate;
char TraceId[84];
char ClientMark[32];
UINT64 fileSize;
}xxxListItem, *pxxxListItem;
#include "json/json.h"
char buffertemp[4096] = { 0 };
char bufferItem[128] = { 0 };
xxxListItem ItemlogData;
memset(&ItemlogData, 0x00, sizeof(xxxListItem));
Json::Value root;
root["Clientxxxx"] = ItemlogData.Clientxxxx;
root["ClientUser"] = ItemlogData.ClientUser;
/*string strItemOperContent = ItemlogData.OperContent;
replace_all_distinct(strItemOperContent, "\"", " ");
//replace_all_distinct(strItemOperContent, "\\", "\\\\");
replace_all_distinct(strItemOperContent, "'", " \'");
replace_all_distinct(strItemOperContent, "&", " \&");
replace_all_distinct(strItemOperContent, "<", " \<");
replace_all_distinct(strItemOperContent, ">", " \>");*/
//memset(ItemlogData.OperContent, 0x00, sizeof(ItemlogData.OperContent));
//strcpy_s(ItemlogData.OperContent, strItemOperContent.c_str());
root["OperContent"] = ItemlogData.OperContent;
root["OperDate"] = ItemlogData.OperDate;
memset(bufferItem, 0x00, sizeof(bufferItem));
sprintf(bufferItem, "%d", ItemlogData.OperType);
root["OperType"] = bufferItem;
root["ServerID"] = ItemlogData.ServerID;
root["Serverxxxx"] = ItemlogData.Serverxxxx;
memset(bufferItem, 0x00, sizeof(bufferItem));
sprintf(bufferItem, "%I64u", ItemlogData.fileSize);
root["FileSize"] = bufferItem;
root["TraceId"] = ItemlogData.TraceId;
root["ClientMark"] = ItemlogData.ClientMark;
//转成json 用到Cjson库
Json::StreamWriterBuilder writerBuilder;
std::unique_ptr<Json::StreamWriter> json_write(writerBuilder.newStreamWriter());
std::ostringstream ss;
json_write->write(root, &ss);
std::string strContent = ss.str();
//转成url码
strCoding strEncode;
string LogListPost = strEncode.UrlEncode(strContent.c_str());
#pragma once
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
class strCoding
{
public:
strCoding(void);
~strCoding(void);
void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//utf_8转为gb2312
void GB2312ToUTF_8(string& pOut, const char *pText, int pLen); //gb2312 转utf_8
string UrlGB2312(const char * str); //urlgb2312编码
string UrlUTF8(const char * str); //urlutf8 编码
string UrlUTF8Decode(string str); //urlutf8解码
string UrlGB2312Decode(string str); //urlgb2312解码
const char * _encodeURI(const char *Str);
std::string UrlDecode(const std::string& str);
std::string UrlEncode(const std::string& str);
unsigned char FromHex(unsigned char x);
unsigned char ToHex(unsigned char x);
unsigned char* urldecodecustom(unsigned char* encd, unsigned char* decd);
void urlencodecustom(unsigned char * src, int src_len, unsigned char * dest, int dest_len);
private:
void Gb2312ToUnicode(WCHAR* pOut, const char *gbBuffer);
void UTF_8ToUnicode(WCHAR* pOut, char *pText);
void UnicodeToUTF_8(char* pOut, WCHAR* pText);
void UnicodeToGB2312(char* pOut, WCHAR uData);
char CharToInt(char ch);
char StrToBin(char *str);
};
#include "stdafx.h"
#include "StrCoding.h"
strCoding::strCoding(void)
{
}
strCoding::~strCoding(void)
{
}
unsigned char strCoding::ToHex(unsigned char x)
{
return x > 9 ? x + 55 : x + 48;
}
unsigned char strCoding::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
{
}
//else assert(0);
return y;
}
std::string strCoding::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 strCoding::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;
}
const char * strCoding::_encodeURI(const char *Str)
{
wchar_t *Bufw = NULL;
char *Bufc = NULL;
static char RTV[5120] = { 0 };//原作者此处声明为char RTV[5120],这里将其改为静态的
long needSize = MultiByteToWideChar(CP_ACP, NULL, Str, -1, NULL, 0);
if (0 == needSize) goto ERROR_HANDLE;
Bufw = new wchar_t[needSize];
if (NULL == Bufw) goto ERROR_HANDLE;
memset(Bufw, 0x0, needSize * 2);
MultiByteToWideChar(CP_ACP, NULL, Str, -1, Bufw, needSize);
needSize = WideCharToMultiByte(CP_UTF8, NULL, Bufw, -1, NULL, 0, NULL, NULL);
if (0 == needSize) goto ERROR_HANDLE;
Bufc = new char[needSize];
if (NULL == Bufc) goto ERROR_HANDLE;
memset(Bufc, 0x0, needSize);
WideCharToMultiByte(CP_UTF8, NULL, Bufw, -1, Bufc, needSize, NULL, NULL);
unsigned char *pWork = (unsigned char *)Bufc;
memset(RTV, 0x0, sizeof(RTV));
if (strlen(Bufc) > 5120)
{
goto ERROR_HANDLE;
}
while (*pWork != 0x0)
{
if (*pWork != '!' && *pWork != '@' && *pWork != '#' &&
*pWork != '$' && *pWork != '&' && *pWork != '*' &&
*pWork != '(' && *pWork != ')' && *pWork != '=' &&
*pWork != ':' && *pWork != '/' && *pWork != ';' &&
*pWork != '?' && *pWork != '+' && *pWork != '\'' &&
*pWork != '.')
{
sprintf(RTV + strlen(RTV), "%%%2X", *pWork);
}
else
{
sprintf(RTV + strlen(RTV), "%c", *pWork);
}
pWork++;
}
if (NULL != Bufw)
{
delete[] Bufw;
Bufw = NULL;
}
if (NULL != Bufc)
{
delete[] Bufc;
Bufc = NULL;
}
return RTV;
ERROR_HANDLE:
if (NULL != Bufw)
{
delete[] Bufw;
Bufw = NULL;
}
if (NULL != Bufc)
{
delete[] Bufc;
Bufc = NULL;
}
return NULL;
}
void strCoding::Gb2312ToUnicode(WCHAR* pOut, const char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, gbBuffer, 2, pOut, 1);
return;
}
void strCoding::UTF_8ToUnicode(WCHAR* pOut, char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void strCoding::UnicodeToUTF_8(char* pOut, WCHAR* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
void strCoding::UnicodeToGB2312(char* pOut, WCHAR uData)
{
WideCharToMultiByte(CP_ACP, NULL, &uData, 1, pOut, sizeof(WCHAR), NULL, NULL);
return;
}
//做为解Url使用
char strCoding::CharToInt(char ch){
if (ch >= '0' && ch <= '9')return (char)(ch - '0');
if (ch >= 'a' && ch <= 'f')return (char)(ch - 'a' + 10);
if (ch >= 'A' && ch <= 'F')return (char)(ch - 'A' + 10);
return -1;
}
char strCoding::StrToBin(char *str){
char tempWord[2];
char chn;
tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011
tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000
chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000
return chn;
}
//UTF_8 转gb2312
void strCoding::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
{
char buf[4];
char* rst = new char[pLen + (pLen >> 2) + 2];
memset(buf, 0, 4);
memset(rst, 0, pLen + (pLen >> 2) + 2);
int i = 0;
int j = 0;
while (i < pLen)
{
if (*(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp, pText + i);
UnicodeToGB2312(buf, Wtemp);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j + 1] = buf[1];
tmp = rst[j + 2] = buf[2];
//newBuf[j] = Ctemp[0];
//newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
rst[j] = '\0';
pOut = rst;
delete[]rst;
}
//GB2312 转为 UTF-8
void strCoding::GB2312ToUTF_8(string& pOut, const char *pText, int pLen)
{
char buf[4];
memset(buf, 0, 4);
pOut.clear();
int i = 0;
while (i < pLen)
{
//如果是英文直接复制就可以
if (pText[i] >= 0)
{
char asciistr[2] = { 0 };
asciistr[0] = (pText[i++]);
pOut.append(asciistr);
}
else
{
WCHAR pbuffer;
Gb2312ToUnicode(&pbuffer, pText + i);
UnicodeToUTF_8(buf, &pbuffer);
pOut.append(buf);
i += 2;
}
}
return;
}
//把str编码为网页中的 GB2312 url encode ,英文不变,汉字双字节 如%3D%AE%88
string strCoding::UrlGB2312(const char * str)
{
string dd;
size_t len = strlen(str);
for (size_t i = 0; i<len; i++)
{
if (isalnum((BYTE)str[i]))
{
char tempbuff[2];
sprintf(tempbuff, "%c", str[i]);
dd.append(tempbuff);
}
else if (isspace((BYTE)str[i]))
{
dd.append("+");
}
else
{
char tempbuff[4];
sprintf(tempbuff, "%%%X%X", ((BYTE*)str)[i] >> 4, ((BYTE*)str)[i] % 16);
dd.append(tempbuff);
}
}
return dd;
}
//把str编码为网页中的 UTF-8 url encode ,英文不变,汉字三字节 如%3D%AE%88
string strCoding::UrlUTF8(const char * str)
{
string tt;
string dd;
GB2312ToUTF_8(tt, str, (int)strlen(str));
size_t len = tt.length();
for (size_t i = 0; i<len; i++)
{
if (isalnum((BYTE)tt.at(i)))
{
char tempbuff[2] = { 0 };
sprintf(tempbuff, "%c", (BYTE)tt.at(i));
dd.append(tempbuff);
}
//else if (isspace((BYTE)tt.at(i)))
//{
// dd.append("+");
//}
else
{
char tempbuff[4];
sprintf(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
dd.append(tempbuff);
}
}
return dd;
}
//把url GB2312解码
string strCoding::UrlGB2312Decode(string str)
{
string output = "";
char tmp[2];
int i = 0;
int idx = 0;
int ndx;
int len = str.length();
while (i<len){
if (str[i] == '%'){
tmp[0] = str[i + 1];
tmp[1] = str[i + 2];
output += StrToBin(tmp);
i = i + 3;
}
//else if (str[i] == '+'){
// output += ' ';
// i++;
//}
else{
output += str[i];
i++;
}
}
return output;
}
//把url utf8解码
string strCoding::UrlUTF8Decode(string str)
{
string output = "";
string temp = UrlGB2312Decode(str);//
UTF_8ToGB2312(output, (char *)temp.data(), strlen(temp.data()));
return output;
}
static unsigned char char_to_hex(unsigned char x)
{
return (unsigned char)(x > 9 ? x + 55 : x + 48);
}
static int is_alpha_number_char(unsigned char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
return 1;
return 0;
}
//url编码实现
void strCoding::urlencodecustom(unsigned char * src, int src_len, unsigned char * dest, int dest_len)
{
unsigned char ch;
int len = 0;
while (len < (dest_len - 4) && *src)
{
ch = (unsigned char)*src;
if (*src == ' ')
{
*dest++ = '+';
}
else if (is_alpha_number_char(ch) || strchr("=!~*'()", ch))
{
*dest++ = *src;
}
else
{
*dest++ = '%';
*dest++ = char_to_hex((unsigned char)(ch >> 4));
*dest++ = char_to_hex((unsigned char)(ch % 16));
}
++src;
++len;
}
*dest = 0;
return;
}
//解url编码实现
unsigned char* strCoding::urldecodecustom(unsigned char* encd, unsigned char* decd)
{
int j, i;
char *cd = (char*)encd;
char p[2];
unsigned int num;
j = 0;
for (i = 0; i < strlen(cd); i++)
{
memset(p, '/0', 2);
if (cd[i] != '%')
{
decd[j++] = cd[i];
continue;
}
p[0] = cd[++i];
p[1] = cd[++i];
p[0] = p[0] - 48 - ((p[0] >= 'A') ? 7 : 0) - ((p[0] >= 'a') ? 32 : 0);
p[1] = p[1] - 48 - ((p[1] >= 'A') ? 7 : 0) - ((p[1] >= 'a') ? 32 : 0);
decd[j++] = (unsigned char)(p[0] * 16 + p[1]);
}
decd[j] = '/0';
return decd;
}