168 lines
3.3 KiB
C++
Raw Normal View History

2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
#include "define.h"
#include <iostream>
#include "cinterface.h"
#include <malloc.h>
#include <mutex>
#include <queue>
#include <Windows.h>
void free_all(void *str)
{
delete str;
2024-01-08 23:37:00 +08:00
}
2024-04-02 15:36:52 +08:00
void freewstringlist(wchar_t **strlist, int num)
{
for (int i = 0; i < num; i++)
{
delete strlist[i];
}
delete strlist;
2024-01-08 23:37:00 +08:00
}
2024-04-02 15:36:52 +08:00
void freestringlist(char **strlist, int num)
{
for (int i = 0; i < num; i++)
{
delete strlist[i];
}
delete strlist;
2024-01-08 23:37:00 +08:00
}
2024-04-02 15:36:52 +08:00
int *vecint2c(std::vector<int> &vs)
{
int *argv = new int[vs.size() + 1];
for (size_t i = 0; i < vs.size(); i++)
{
argv[i] = vs[i];
}
return argv;
2024-01-08 23:37:00 +08:00
}
2024-04-02 15:36:52 +08:00
char **vecstr2c(std::vector<std::string> &vs)
{
char **argv = new char *[vs.size() + 1];
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
for (size_t i = 0; i < vs.size(); i++)
{
argv[i] = new char[vs[i].size() + 1];
strcpy_s(argv[i], vs[i].size() + 1, vs[i].c_str());
argv[i][vs[i].size()] = 0;
}
return argv;
2024-01-08 23:37:00 +08:00
}
2024-04-02 15:36:52 +08:00
wchar_t **vecwstr2c(std::vector<std::wstring> &vs)
{
wchar_t **argv = new wchar_t *[vs.size() + 1];
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
for (size_t i = 0; i < vs.size(); i++)
{
argv[i] = new wchar_t[vs[i].size() + 1];
wcscpy_s(argv[i], vs[i].size() + 1, vs[i].c_str());
argv[i][vs[i].size()] = 0;
}
return argv;
2024-01-08 23:37:00 +08:00
}
2024-04-02 15:36:52 +08:00
struct MemoryStruct
{
2024-01-08 23:37:00 +08:00
char *memory;
size_t size;
};
2024-04-02 15:36:52 +08:00
size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
2024-01-08 23:37:00 +08:00
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr;
2024-04-02 15:36:52 +08:00
if (mem->memory)
ptr = (char *)realloc(mem->memory, mem->size + realsize + 1);
2024-01-08 23:37:00 +08:00
else
2024-04-02 15:36:52 +08:00
ptr = (char *)malloc(mem->size + realsize + 1);
if (!ptr)
{
2024-01-08 23:37:00 +08:00
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
2024-04-02 15:36:52 +08:00
void c_free(void *ptr)
{
free(ptr);
2024-03-05 04:15:50 +08:00
}
2024-04-02 15:36:52 +08:00
class lockedqueue
{
2024-03-07 21:10:57 +08:00
std::mutex lock;
2024-04-02 15:36:52 +08:00
std::queue<std::string> data;
2024-03-07 21:10:57 +08:00
HANDLE hsema;
2024-04-02 15:36:52 +08:00
public:
lockedqueue()
{
hsema = CreateSemaphore(NULL, 0, 65535, NULL);
2024-03-07 21:10:57 +08:00
}
2024-04-02 15:36:52 +08:00
~lockedqueue()
{
2024-03-07 21:10:57 +08:00
CloseHandle(hsema);
}
2024-04-02 15:36:52 +08:00
void push(std::string &&_)
{
2024-03-07 21:10:57 +08:00
std::lock_guard _l(lock);
data.push(std::move(_));
2024-04-02 15:36:52 +08:00
ReleaseSemaphore(hsema, 1, NULL);
2024-03-07 21:10:57 +08:00
}
2024-04-02 15:36:52 +08:00
std::string pop()
{
WaitForSingleObject(hsema, INFINITE);
2024-03-07 21:10:57 +08:00
std::lock_guard _l(lock);
2024-04-02 15:36:52 +08:00
auto _ = data.front();
2024-03-07 21:10:57 +08:00
data.pop();
return _;
}
2024-04-02 15:36:52 +08:00
bool empty()
{
2024-03-07 22:38:49 +08:00
std::lock_guard _l(lock);
2024-03-07 21:10:57 +08:00
return data.empty();
}
};
2024-04-02 15:36:52 +08:00
void *lockedqueuecreate()
{
2024-03-07 21:10:57 +08:00
return new lockedqueue();
}
2024-04-02 15:36:52 +08:00
void lockedqueuefree(void *q)
{
delete reinterpret_cast<lockedqueue *>(q);
2024-03-07 21:10:57 +08:00
}
2024-04-02 15:36:52 +08:00
void *lockedqueueget(void *q, size_t *l)
{
auto data = reinterpret_cast<lockedqueue *>(q)->pop();
auto datastatic = new char[data.size()];
memcpy(datastatic, data.data(), data.size());
*l = data.size();
2024-03-07 21:10:57 +08:00
return datastatic;
}
2024-04-02 15:36:52 +08:00
void lockedqueuepush(void *q, size_t l, void *ptr)
{
reinterpret_cast<lockedqueue *>(q)->push(std::string((char *)ptr, l));
2024-03-07 21:10:57 +08:00
}
2024-04-02 15:36:52 +08:00
bool lockedqueueempty(void *q)
{
return reinterpret_cast<lockedqueue *>(q)->empty();
2024-03-07 21:10:57 +08:00
}
2024-04-02 15:36:52 +08:00
size_t WriteMemoryToQueue(void *contents, size_t size, size_t nmemb, void *userp)
{
2024-03-05 04:15:50 +08:00
size_t realsize = size * nmemb;
2024-04-02 15:36:52 +08:00
auto queue = reinterpret_cast<lockedqueue *>(userp);
queue->push(std::string((char *)contents, realsize));
2024-03-05 04:15:50 +08:00
return realsize;
}