Skip to content

Commit

Permalink
设置下载文件名
Browse files Browse the repository at this point in the history
  • Loading branch information
BDZNH committed Jan 3, 2019
1 parent 5f31481 commit 182832a
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 7 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@
<ClInclude Include="MIUI-Theme-Download-Link-Generator.h" />
<ClInclude Include="Resource.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="urlcoding.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="generate.cpp" />
<ClCompile Include="MIUI-Theme-Download-Link-Generator.cpp" />
<ClCompile Include="urlcodeing.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="MIUI-Theme-Download-Link-Generator.rc" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="urlDecode">
<UniqueIdentifier>{8b336427-4c69-481a-96b8-7739ff6b648d}</UniqueIdentifier>
</Filter>
<Filter Include="generator and downloader">
<UniqueIdentifier>{3e3c2834-ff17-4784-bc8f-9621f933fe2f}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="header.h">
Expand All @@ -27,16 +33,22 @@
<ClInclude Include="MIUI-Theme-Download-Link-Generator.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="urlcoding.h">
<Filter>urlDecode</Filter>
</ClInclude>
<ClInclude Include="generate.h">
<Filter>头文件</Filter>
<Filter>generator and downloader</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="MIUI-Theme-Download-Link-Generator.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="urlcodeing.cpp">
<Filter>urlDecode</Filter>
</ClCompile>
<ClCompile Include="generate.cpp">
<Filter>源文件</Filter>
<Filter>generator and downloader</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions MIUI-Theme-Download-Link-Generator/generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,11 @@ DWORD WINAPI download(void)
FILE *fp;
ddlnow = 0;

//char path[255];


strCoding urlDecode;
std::string fileName = urlDecode.getName(dLinktemp);
curl = curl_easy_init();
if (curl) {
fp = fopen("softrain.mtz", "wb");
fp = fopen(fileName.c_str(), "wb");
prog.lastruntime = 0;
prog.curl = curl;

Expand All @@ -185,7 +184,8 @@ DWORD WINAPI download(void)
fclose(fp);
if (res == CURLE_OK)
{
MessageBox(NULL, L"Down finished", L"Download Thread", MB_OK);
PlaySound((LPCTSTR)IDR_WAVE_SUCCESS, nullptr, SND_RESOURCE | SND_ASYNC | SND_NOSTOP);
MessageBox(NULL, L"下载完成", L"Hello", MB_OK);
}

/* always cleanup */
Expand Down
5 changes: 5 additions & 0 deletions MIUI-Theme-Download-Link-Generator/generate.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include <curl/curl.h>
#include <rapidjson/document.h>

#include "urlcoding.h"
#include "resource.h"
#include "header.h"


// additional dependencies
#ifdef _WIN32
Expand All @@ -37,6 +41,7 @@
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "Normaliz.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "WINMM.LIB")

#ifdef _DEBUG
#pragma comment(lib, "libcurl_a_debug.lib")
Expand Down
256 changes: 256 additions & 0 deletions MIUI-Theme-Download-Link-Generator/urlcodeing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
#include "urlcoding.h"
//这是个类strCoding (strCoding.cpp文件)

strCoding::strCoding(void)
{
}

strCoding::~strCoding(void)
{
}
void strCoding::Gb2312ToUnicode(WCHAR* pOut, 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, 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(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(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, idx = 0, len = (int)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(), (int)strlen(temp.data()));

return output;

}

string strCoding::getName(const char *str)
{
{
strCoding ss;
std::string temp;
int len = (int)strlen(str);
for (int i = len - 1; i >= 0; i--)
{
if (str[i] == '/')
break;
else
temp.push_back(str[i]);
}
std::reverse(temp.begin(), temp.end());
temp = ss.UrlUTF8Decode(temp);
return temp;
}
}
35 changes: 35 additions & 0 deletions MIUI-Theme-Download-Link-Generator/urlcoding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#define _CRT_SECURE_NO_WARNINGS
#pragma once

//reference:
// https://blog.csdn.net/zmx729618/article/details/51381655
// https://www.cnblogs.com/xiaoka/articles/2585189.html

#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, char *pText, int pLen); //gb2312 转utf_8
string UrlGB2312(char * str); //urlgb2312编码
string UrlUTF8(char * str); //urlutf8 编码
string UrlUTF8Decode(string str); //urlutf8解码
string UrlGB2312Decode(string str); //urlgb2312解码
string getName(const char *);

private:
void Gb2312ToUnicode(WCHAR* pOut, 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);

};

0 comments on commit 182832a

Please sign in to comment.