|

分享源码
界面截图: |
|
是否带模块: |
纯源码 |
备注说明: |
- |
本帖最后由 xwwyt 于 2023-5-28 13:44 编辑
接上回写的文本行替换
按照各位朋友的建议写了删改查(增暂时没写)
效率很高 实测大文本秒操作
调用方法如下:
新增一个读入文本 直接返回文本型
文本行替换操作:
文本行删除操作:
取文本行内容操作:
读入文本的操作直接同理按照上方来(省略了转换到文本的麻烦)
CPP源码:[C++] 纯文本查看 复制代码 #define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <windows.h>
#include <codecvt>
#include <locale>
//这周末只更新删改查 均用了内存映射 效率很高 编码按要求易语言调用的时候改就好 不会出现文件乱码
extern "C" __declspec(dllexport) void replace_line(char* path, int line, char* content) {
HANDLE hFile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
std::cerr << "Could not open file" << std::endl;
return;
}
HANDLE hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (hMapFile == NULL) {
std::cerr << "Could not create file mapping" << std::endl;
CloseHandle(hFile);
return;
}
char* pBuf = (char*)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if (pBuf == NULL) {
std::cerr << "Could not map view of file" << std::endl;
CloseHandle(hMapFile);
CloseHandle(hFile);
return;
}
// 检测文本中的换行符是什么 不同的系统版本可能不同
std::string fileContent(pBuf);
bool useCRLF = fileContent.find("\r\n") != std::string::npos;
int currentLine = 1;
char* p = pBuf;
char* lineStart = pBuf;
while (*p != '\0') {
if (currentLine == line) {
char* lineEnd = p;
while (*lineEnd != '\n' && *lineEnd != '\0') {
lineEnd++;
}
size_t len = lineEnd - lineStart;
std::string newContent(content);
if (useCRLF) {
newContent += "\r";
}
size_t newLen = newContent.length();
if (newLen > len) {
size_t diff = newLen - len;
size_t fileSize = GetFileSize(hFile, NULL);
char* newBuf = new char[fileSize + diff];
memcpy(newBuf, pBuf, lineStart - pBuf);
memcpy(newBuf + (lineStart - pBuf), newContent.c_str(), newLen);
memcpy(newBuf + (lineStart - pBuf) + newLen, lineEnd, fileSize - (lineEnd - pBuf));
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
SetFilePointer(hFile, fileSize + diff, NULL, FILE_BEGIN);
SetEndOfFile(hFile);
hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (hMapFile == NULL) {
std::cerr << "Could not create file mapping" << std::endl;
CloseHandle(hFile);
return;
}
pBuf = (char*)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if (pBuf == NULL) {
std::cerr << "Could not map view of file" << std::endl;
CloseHandle(hMapFile);
CloseHandle(hFile);
return;
}
memcpy(pBuf, newBuf, fileSize + diff);
delete[] newBuf;
}
else {
memcpy(lineStart, newContent.c_str(), newLen);
memmove(lineStart + newLen, lineEnd, strlen(lineEnd) + 1);
}
break;
}
if (*p == '\n') {
currentLine++;
lineStart = p + 1;
}
p++;
}
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
CloseHandle(hFile);
}
extern "C" __declspec(dllexport) void del_line(char* path, int line)
{
HANDLE hFile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
std::cerr << "Could not open file." << std::endl;
return;
}
DWORD dwFileSize = GetFileSize(hFile, NULL);
if (dwFileSize == INVALID_FILE_SIZE)
{
std::cerr << "Could not get file size." << std::endl;
CloseHandle(hFile);
return;
}
HANDLE hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwFileSize, NULL);
if (hMapFile == NULL)
{
std::cerr << "Could not create file mapping object." << std::endl;
CloseHandle(hFile);
return;
}
LPVOID lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, dwFileSize);
if (lpMapAddress == NULL)
{
std::cerr << "Could not map view of file." << std::endl;
CloseHandle(hMapFile);
CloseHandle(hFile);
return;
}
char* data = (char*)lpMapAddress;
int currentLine = 1;
DWORD startOffset = 0;
for (DWORD i = 0; i < dwFileSize; i++)
{
if (data == '\n')
{
currentLine++;
if (currentLine == line)
{
startOffset = i + 1;
break;
}
}
}
DWORD endOffset = startOffset;
for (DWORD i = startOffset; i < dwFileSize; i++)
{
if (data == '\n')
{
endOffset = i + 1;
break;
}
}
MoveMemory(data + startOffset, data + endOffset, dwFileSize - endOffset);
UnmapViewOfFile(lpMapAddress);
CloseHandle(hMapFile);
SetFilePointer(hFile, dwFileSize - (endOffset - startOffset), NULL, FILE_BEGIN);
SetEndOfFile(hFile);
CloseHandle(hFile);
}
extern "C" __declspec(dllexport) char* get_line(char* path, int line) {
HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) return NULL;
HANDLE hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (hMap == NULL) {
CloseHandle(hFile);
return NULL;
}
char* pFile = (char*)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
if (pFile == NULL) {
CloseHandle(hMap);
CloseHandle(hFile);
return NULL;
}
int count = 0;
char* pLine = pFile;
char* pEnd = pLine;
while (*pEnd != '\0') {
if (*pEnd == '\n' || (*pEnd == '\r' && *(pEnd + 1) == '\n')) {
count++;
if (count == line) break;
pLine = pEnd + 1 + (*pEnd == '\r');
pEnd += (*pEnd == '\r');
}
pEnd++;
}
if (count != line) {
UnmapViewOfFile(pFile);
CloseHandle(hMap);
CloseHandle(hFile);
return NULL;
}
int len = pEnd - pLine;
char* result = new char[len + 1];
memcpy(result, pLine, len);
result[len] = '\0';
UnmapViewOfFile(pFile);
CloseHandle(hMap);
CloseHandle(hFile);
return result;
}
extern "C" __declspec(dllexport) char* read_text(char* path) {
HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE) return nullptr;
DWORD fileSize = GetFileSize(hFile, nullptr);
HANDLE hMapFile = CreateFileMapping(hFile, nullptr, PAGE_READONLY, 0, fileSize, nullptr);
if (hMapFile == nullptr) {
CloseHandle(hFile);
return nullptr;
}
char* buffer = (char*)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, fileSize);
if (buffer == nullptr) {
CloseHandle(hMapFile);
CloseHandle(hFile);
return nullptr;
}
int isTextUnicode = IS_TEXT_UNICODE_UNICODE_MASK;
IsTextUnicode(buffer, fileSize, &isTextUnicode);
char* gbkBuffer;
if (isTextUnicode == IS_TEXT_UNICODE_UNICODE_MASK) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wide = converter.from_bytes(buffer, buffer + fileSize);
int gbkSize = WideCharToMultiByte(CP_ACP, 0, wide.c_str(), -1, nullptr, 0, nullptr, nullptr);
gbkBuffer = new char[gbkSize];
WideCharToMultiByte(CP_ACP, 0, wide.c_str(), -1, gbkBuffer, gbkSize, nullptr, nullptr);
}
else {
gbkBuffer = new char[fileSize + 1];
memcpy(gbkBuffer, buffer, fileSize);
gbkBuffer[fileSize] = '\0';
}
UnmapViewOfFile(buffer);
CloseHandle(hMapFile);
CloseHandle(hFile);
return gbkBuffer;
}
DLL+调用示例下载:https://wwwk.lanzouf.com/iR7Vs0xhnmof
希望对大家的学习有所帮助
请支持一票 谢谢
|
评分
-
查看全部评分
|