This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues or pull requests.
VideoSite/LocalVideoList.cpp

95 lines
5.0 KiB
C++
Raw Normal View History

2018-09-02 10:22:06 +08:00
<EFBFBD><EFBFBD>#include "Request.h"
#include "Response.h"
#include <iostream>
#include <fstream>
#include <memory>
#include "json.hpp"
#include <Windows.h>
using namespace std;
using namespace HTTPWrapper;
ofstream mlog("log.txt");
std::string GBKToUTF8(const std::string& GBKString)
{
// GBK -> Unicode
int nLen = MultiByteToWideChar(CP_ACP, 0, GBKString.c_str(), -1, NULL, 0);
std::unique_ptr<wchar_t[]> buff(new wchar_t[nLen + 8]);
memset(buff.get(), 0, nLen + 8);
MultiByteToWideChar(CP_ACP, 0, GBKString.c_str(), -1, buff.get(), nLen + 8);
// Unicode -> UTF8
nLen = WideCharToMultiByte(CP_UTF8, 0, buff.get(), -1, NULL, 0, NULL, NULL);
std::unique_ptr<char[]> xbuff(new char[nLen + 8]);
memset(xbuff.get(), 0, nLen + 8);
WideCharToMultiByte(CP_UTF8, 0, buff.get(), -1, xbuff.get(), nLen + 8, NULL, NULL);
return std::string(xbuff.get());
}
std::wstring GBKToUnicode(const std::string& GBKString)
{
// GBK -> Unicode
int nLen = MultiByteToWideChar(CP_ACP, 0, GBKString.c_str(), GBKString.size(), NULL, 0);
std::unique_ptr<wchar_t[]> buff(new wchar_t[nLen + 8]);
MultiByteToWideChar(CP_UTF8, 0, GBKString.c_str(), GBKString.size(), buff.get(), nLen + 8);
buff[nLen] = 0;
return std::wstring(buff.get());
}
string urlencode(const string& url)
{
string ostr;
for (const unsigned char& c : url)
{
if ((c >= '0'&&c <= '9') ||
(c >= 'a'&&c <= 'z') ||
(c >= 'A'&&c <= 'Z') ||
(c == '-') ||
(c == '_') ||
(c == '.'))
ostr.push_back(c);
else
{
unsigned int first = c / 16;
unsigned int second = c % 16;
ostr.append("%");
ostr.push_back((char)((first < 10) ? ('0' + first) : ('A' + first - 10)));
ostr.push_back((char)((second < 10) ? ('0' + second) : ('A' + second - 10)));
}
}
return ostr;
}
vector<string> GetList()
{
vector<string> vec;
WIN32_FIND_DATA fnd;
HANDLE hand = FindFirstFile("F:\\faaq\\OutSideVideo\\*.mp4", &fnd);
if (hand == INVALID_HANDLE_VALUE) return vec;
do {
if (strcmp(fnd.cFileName, ".") != 0 && strcmp(fnd.cFileName, "..") != 0)
{
int len = strlen(fnd.cFileName);
if (strcmp(fnd.cFileName + len - 4, ".mp4") == 0)
{
char tmp[2048] = { 0 };
strncpy(tmp, fnd.cFileName, len - 4);
vec.push_back(urlencode(GBKToUTF8(tmp)));
}
}
} while (FindNextFile(hand, &fnd));
FindClose(hand);
return vec;
}
int main()
{
Request req;
Response res;
nlohmann::json j;
vector<string> lst = GetList();
j["files"] = lst;
res.writer << j;
return 0;
}