#include<bits/stdc++.h>
#include<codecvt>
#include<windows.h>
using namespace std;

int main(){
    system("chcp 65001");
    
    // 使用Windows API遍历目录
    WIN32_FIND_DATAW findData;
    HANDLE hFind = FindFirstFileW(L"*.srt", &findData);
    
    if(hFind == INVALID_HANDLE_VALUE){
        cout << "没有找到需要处理的文件" << endl;
        return 0;
    }
    
    wstring_convert<codecvt_utf8<wchar_t>> converter;
    
    do {
        wstring wfilename = findData.cFileName;
        string filename = converter.to_bytes(wfilename);
        
        // 跳过已处理的文件
        if(filename.find("handled_") == 0){
            continue;
        }
        
        FILE* infile = _wfopen(wfilename.c_str(), L"r");
        if(!infile){
            cout << "无法打开文件: " << filename << endl;
            continue;
        }
        
        wstring woutname = L"handled_" + wfilename;
        FILE* outfile = _wfopen(woutname.c_str(), L"w");
        if(!outfile){
            cout << "无法创建输出文件: handled_" << filename << endl;
            fclose(infile);
            continue;
        }
        
        char line[1024];
        int line_count=0;
        while(fgets(line, sizeof(line), infile)){
            line_count++;
            if(line_count%4==3){
                fputs(line, outfile);
            }
        }
        fclose(infile);
        fclose(outfile);
        cout << "已处理: " << filename << endl;
        
    } while(FindNextFileW(hFind, &findData));
    
    FindClose(hFind);
    return 0;
}
