#include #include #include #include #include #include "PolicyUnit.h" // 读整个文件到 String (UTF-8 字节 → UTF-16) String ReadFileUTF8(const String& path) { char szPath[1024]; sprintf(szPath, "%s", ((UTF8String)path).c_str()); FILE* fp = fopen(szPath, "rb"); if(!fp) return String(); fseek(fp, 0, SEEK_END); long size = ftell(fp); fseek(fp, 0, SEEK_SET); if(size <= 0){ fclose(fp); return String(); } char* buf = new char[size + 1]; size_t n = fread(buf, 1, size, fp); buf[n] = 0; fclose(fp); // UTF-8 字节 → UTF-16 String String result = String((UTF8String)buf); delete[] buf; return result; } // 按换行分行,解析 key=value bool ParseConfig(const String& text, int& iType, int& iKind, int& iRoad, String& sName, String& sDesc, String& sCode, int& iMiniChip) { iType = -1; iKind = -1; iRoad = 0; iMiniChip = 0; sName = sDesc = sCode = String(); // 去掉 \r,按 \n 分行 std::vector lines; { String cur; for(int i = 1; i <= text.Length(); i++){ wchar_t c = text[i]; if(c == L'\r') continue; if(c == L'\n'){ lines.push_back(cur); cur = String(); } else { cur += c; } } if(cur.Length() > 0) lines.push_back(cur); } for(size_t i = 0; i < lines.size(); i++){ String line = TrimRight(lines[i]); if(line.Length() == 0) continue; if(line[1] == L'#') continue; // Pos 是 1-based,返回 = 在 line 中的位置(从 1 开始) int eqPos = 0; for(int j = 1; j <= line.Length(); j++){ if(line[j] == L'='){ eqPos = j; break; } } if(eqPos <= 1) continue; String key = TrimRight(line.SubString(1, eqPos - 1)); String val = line.SubString(eqPos + 1, line.Length() - eqPos); // trim 两侧空格 while(val.Length() > 0 && val[1] == L' ') val = val.SubString(2, val.Length() - 1); while(val.Length() > 1 && val[val.Length()] == L' ') val = val.SubString(1, val.Length() - 1); if(key == "type") iType = StrToInt(val); else if(key == "kind") iKind = StrToInt(val); else if(key == "road") iRoad = StrToInt(val); else if(key == "name") sName = val; else if(key == "desc") sDesc = val; else if(key == "code") sCode = val; else if(key == "miniChip") iMiniChip = StrToInt(val); else { wprintf(L" 警告: 未知 key '%ls'\n", key.c_str()); } } return iType >= 0 && iKind > 0 && sName.Length() > 0 && sCode.Length() > 0; } // 取路径的目录部分(含末尾分隔符);无分隔符则返回空串 String GetDirPart(const String& path) { int lastSep = 0; for(int i = 1; i <= path.Length(); i++){ if(path[i] == L'\\' || path[i] == L'/') lastSep = i; } if(lastSep > 0) return path.SubString(1, lastSep); return String(); } // 检查扩展名(小写比较),返回 .bpf=0, .cpf=1, 其他=-1 int CheckExt(const String s) { int l = s.Length(); if(l < 4) return -1; String ext = s.SubString(l - 2, 3).LowerCase(); if(ext == L"bpf") return 0; if(ext == L"cpf") return 1; return -1; } // 取文件名(不含目录、不含扩展名) String GetFileNameNoExt(const String& path) { int lastSep = 0; for(int i = path.Length(); i >= 1; i--){ if(path[i] == L'\\' || path[i] == L'/'){ lastSep = i; break; } } String file = lastSep > 0 ? path.SubString(lastSep + 1, path.Length() - lastSep) : path; // 去掉扩展名 for(int i = file.Length(); i >= 1; i--){ if(file[i] == L'.'){ return file.SubString(1, i - 1); } } return file; } // 从 .bpf/.cpf 提取策略字段,写入 UTF-8 key=value 的 .txt;成功 0,失败负数 int PolicyToText(const String& szInput, const String& szOutFile) { int type = CheckExt(szInput); if(type < 0) return -1; TBasePolicy* p = NULL; int ret = ImportPolicy(type, szInput, p); if(ret != 0){ wprintf(L"ImportPolicy 失败 (ret=%d)\n", ret); delete p; return -1; } char szOut[1024]; sprintf(szOut, "%s", ((UTF8String)szOutFile).c_str()); FILE* fw = fopen(szOut, "w"); if(!fw){ delete p; return -2; } fprintf(fw, "type=%d\n", type); fprintf(fw, "kind=%d\n", p->kind); fprintf(fw, "name=%s\n", ((UTF8String)p->name).c_str()); if(p->desc.Length() > 0) fprintf(fw, "desc=%s\n", ((UTF8String)p->desc).c_str()); if(IsCodePolicy(p->kind)){ if(p->code.Length() > 0) fprintf(fw, "code=%s\n", ((UTF8String)p->code).c_str()); } if(type == TYPE_P_BET){ fprintf(fw, "road=%d\n", ((TBetPolicy*)p)->road); } int iMiniChip = 0; if(type == TYPE_P_BET && p->kind == KIND_BET_WCH){ iMiniChip = ((TBetChipPolicy*)p)->miniChip; } else if(type == TYPE_P_CHIP && IsCodePolicy(p->kind)){ iMiniChip = ((TChipPolicy*)p)->miniChip; } if(iMiniChip > 0) fprintf(fw, "miniChip=%d\n", iMiniChip); fclose(fw); delete p; return 0; } // 解析 .txt 配置,校验并导出为 .bpf/.cpf;成功 0,失败负数 int TextToPolicy(const String& szInput, const String& szOutFile) { String text = ReadFileUTF8(szInput); if(text.Length() == 0){ wprintf(L"无法读取文件: %s\n", szInput.c_str()); return -1; } int iType, iKind, iRoad, iMiniChip; String sName, sDesc, sCode; if(!ParseConfig(text, iType, iKind, iRoad, sName, sDesc, sCode, iMiniChip)){ wprintf(L"配置不完整: 需要 type, kind, name, code\n"); return -2; } // 缺省输出文件名 String szOut = szOutFile; if(szOut.Length() == 0){ szOut = GetDirPart(szInput) + sName + (iType == TYPE_P_BET ? L".bpf" : L".cpf"); } if(!IsCodePolicy(iKind)){ wprintf(L"kind=%d 不是代码策略 (需要 101/102/121)\n", iKind); return -3; } if(iType == TYPE_P_BET){ if(iRoad < 0 || iRoad > 4){ wprintf(L"road=%d 超出范围 (0-4)\n", iRoad); return -4; } if(iKind != KIND_BET_SPE && iKind != KIND_BET_WCH){ wprintf(L"type=0 时 kind 必须是 101 或 102\n"); return -5; } } else if(iType == TYPE_P_CHIP){ if(iKind != KIND_CHIP_SPE){ wprintf(L"type=1 时 kind 必须是 121\n"); return -6; } } else { wprintf(L"type=%d 无效 (0=打法 1=注码)\n", iType); return -7; } if(iMiniChip <= 0 && (iKind == KIND_BET_WCH || iKind == KIND_CHIP_SPE)){ wprintf(L"kind=%d 需要 miniChip>0\n", iKind); return -8; } TBasePolicy* pPolicy = nullptr; if(iType == TYPE_P_BET){ if(iKind == KIND_BET_WCH){ TBetChipPolicy* p = new TBetChipPolicy(); p->road = iRoad; p->code = sCode; p->miniChip = iMiniChip; pPolicy = p; } else { TBetPolicy* p = new TBetPolicy(); p->road = iRoad; p->code = sCode; pPolicy = p; } } else { TChipPolicy* p = new TChipPolicy(); p->code = sCode; p->miniChip = iMiniChip; pPolicy = p; } pPolicy->name = sName; pPolicy->desc = sDesc; pPolicy->type = 2; pPolicy->id = 0; pPolicy->kind = iKind; pPolicy->actived = true; int ret = ExportPolicy(iType, pPolicy, szOut); delete pPolicy; if(ret != 0) wprintf(L"导出失败 (ret=%d)\n", ret); return ret; } int _tmain(int argc, _TCHAR* argv[]) { if(argc < 2){ wprintf(L"用法: PolicyCoder.exe [output.bpf|output.cpf]\n"); wprintf(L" input.txt: UTF-8 配置文件,key=value 格式\n"); wprintf(L" 支持的 key: type(0/1) kind(101/102/121) road(0-4) name desc code miniChip\n"); wprintf(L" 输出: 缺省用 .bpf 或 .cpf,与 input.txt 同目录\n"); wprintf(L"\n"); wprintf(L"用法: PolicyCoder.exe [output.txt]\n"); wprintf(L" 从策略文件提取 type/kind/name/desc/code/road/miniChip 写入 .txt\n"); wprintf(L" 输出: 缺省用 <同名>.txt,与 input.bpf/cpf 同目录\n"); return 1; } String szInput = argv[1]; // 如果是 .bpf/.cpf → 提取模式 int extType = CheckExt(szInput); if(extType >= 0){ String outFile; if(argc >= 3){ outFile = argv[2]; } else { String dir = GetDirPart(szInput); outFile = dir + GetFileNameNoExt(szInput) + L".txt"; } int ret = PolicyToText(szInput, outFile); if(ret == 0) wprintf(L"成功: %s\n", outFile.c_str()); else wprintf(L"提取失败 (ret=%d)\n", ret); return ret == 0 ? 0 : 1; } // .txt 导入模式 String outFile; if(argc >= 3){ outFile = argv[2]; } else { outFile = String(); // 由 TextToPolicy 内部分配 } int ret = TextToPolicy(szInput, outFile); if(ret == 0) wprintf(L"成功: %s\n", outFile.c_str()); else wprintf(L"导出失败 (ret=%d)\n", ret); return ret == 0 ? 0 : 1; }