// localize_tool.cpp #include #include #include #include #include int main(int argc, char* argv[]) { if (argc != 3) { std::cerr << "Usage: " << argv[0] << " \n"; return 1; } const std::string inPath = argv[1]; const std::string outPath = argv[2]; // 1) Read entire file into a string std::ifstream ifs(inPath); if (!ifs) { std::cerr << "Error: cannot open input file: " << inPath << "\n"; return 1; } std::ostringstream buf; buf << ifs.rdbuf(); std::string content = buf.str(); // 2) Regex for C-style string literals: "…", including escaped chars std::regex re(R"("([^"\\]|\\.)*")"); std::string result; std::smatch m; std::string::const_iterator searchStart(content.cbegin()); size_t processedUpTo = 0; while (std::regex_search(searchStart, content.cend(), m, re)) { // position of this match in the full content auto litPos = m.position(0) + processedUpTo; auto litLen = m.length(0); std::string lit = m.str(0); // copy everything up to the literal result.append(content, processedUpTo, litPos - processedUpTo); // check if already wrapped in __LOCALIZE or __LOCALIZE_VERFMT bool already = false; if (litPos >= 11 && content.substr(litPos-11,11) == "__LOCALIZE(") already = true; if (litPos >= 21 && content.substr(litPos-21,18) == "__LOCALIZE_VERFMT(") already = true; if (already) { // leave it alone result += lit; } else { // extract the full line for context size_t lineStart = content.rfind('\n', litPos); lineStart = (lineStart == std::string::npos ? 0 : lineStart+1); size_t lineEnd = content.find('\n', litPos); if (lineEnd == std::string::npos) lineEnd = content.size(); std::string line = content.substr(lineStart, lineEnd - lineStart); // prompt the user std::cout << "\nIn file `" << inPath << "`, line:\n" << line << "\n" << "Wrap this string literal " << lit << "? [y/N] "; std::string ans; std::getline(std::cin, ans); if (!ans.empty() && (ans[0]=='y' || ans[0]=='Y')) { result += "__LOCALIZE(" + lit + ",\"section\")"; } else { result += lit; } } // advance past this match processedUpTo = litPos + litLen; searchStart = content.cbegin() + processedUpTo; } // append the remainder result.append(content, processedUpTo, content.size() - processedUpTo); // write out std::ofstream ofs(outPath); if (!ofs) { std::cerr << "Error: cannot open output file: " << outPath << "\n"; return 1; } ofs << result; std::cout << "\nDone! Written to " << outPath << "\n"; return 0; }