Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
2.8 kB
1
Indexable
Never
void cleanETL()
{
    // Get the current Scintilla handle
    int which = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    if (which == -1) return;
    HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

    // Get the current document text
    Sci_Position textLength = (Sci_Position)::SendMessage(curScintilla, SCI_GETLENGTH, 0, 0);
    
    if (textLength == 0) {
        ::MessageBoxA(NULL, "The document is empty.", "ETL Cleaning", MB_OK | MB_ICONINFORMATION);
        return;
    }

    std::string cleanedContent;
    const Sci_Position bufferSize = 4096;
    char* buffer = new char[bufferSize];
    
    std::string currentLine;
    bool inLogEntry = false;

    for (Sci_Position i = 0; i < textLength; i += bufferSize - 1) {
        Sci_Position bytesToRead = (std::min)(bufferSize - 1, textLength - i);
        
        Sci_TextRangeFull textRange;
        textRange.chrg.cpMin = i;
        textRange.chrg.cpMax = i + bytesToRead;
        textRange.lpstrText = buffer;

        ::SendMessage(curScintilla, SCI_GETTEXTRANGEFULL, 0, (LPARAM)&textRange);
        buffer[bytesToRead] = '\0';

        for (Sci_Position j = 0; j < bytesToRead; ++j) {
            char c = buffer[j];
            if (c >= 32 || c == '\n' || c == '\r' || c == '\t') {
                currentLine += c;
                if (c == '\n') {
                    // Check if this line is likely a log entry
                    if (!currentLine.empty() && currentLine.find_first_not_of(" \t\r\n") != std::string::npos) {
                        cleanedContent += currentLine;
                        inLogEntry = true;
                    }
                    currentLine.clear();
                }
            } else {
                // If we encounter a non-printable character, clear the current line
                currentLine.clear();
                inLogEntry = false;
            }
        }
    }

    // Add any remaining content
    if (!currentLine.empty() && inLogEntry) {
        cleanedContent += currentLine + "\n";
    }

    delete[] buffer;

    // Create a new document and set the cleaned content
    ::SendMessage(nppData._nppHandle, NPPM_MENUCOMMAND, 0, IDM_FILE_NEW);
    int newWhich = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&newWhich);
    HWND newScintilla = (newWhich == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
    ::SendMessage(newScintilla, SCI_SETTEXT, 0, (LPARAM)cleanedContent.c_str());

    ::MessageBoxA(NULL, "ETL file has been cleaned and results written to a new document.", "ETL Cleaning", MB_OK | MB_ICONINFORMATION);
}
Leave a Comment