| 194 | } |
| 195 | |
| 196 | static int write_vcxproj(const std::string &proj_name, const std::function<void(std::string&)> &source_f, const std::function<void(std::string&)> &header_f) |
| 197 | { |
| 198 | std::string outstr; |
| 199 | |
| 200 | { |
| 201 | // treat as binary to prevent implicit line ending conversions |
| 202 | std::ifstream in(proj_name, std::ios::binary); |
| 203 | if (!in.is_open()) { |
| 204 | std::cerr << "Could not open " << proj_name << std::endl; |
| 205 | return EXIT_FAILURE; |
| 206 | } |
| 207 | |
| 208 | std::string line; |
| 209 | bool in_itemgroup = false; |
| 210 | while (std::getline(in, line)) { |
| 211 | if (in_itemgroup) { |
| 212 | if (line.find("</ItemGroup>") == std::string::npos) |
| 213 | continue; |
| 214 | in_itemgroup = false; |
| 215 | } |
| 216 | |
| 217 | // strip all remaining line endings |
| 218 | const std::string::size_type pos = line.find_last_not_of("\r\n"); |
| 219 | if (pos != std::string::npos) |
| 220 | line.resize(pos+1, '\0'); |
| 221 | |
| 222 | outstr += line; |
| 223 | outstr += "\r\n"; |
| 224 | |
| 225 | if (line.find("<ItemGroup Label=\"SourceFiles\">") != std::string::npos) { |
| 226 | in_itemgroup = true; |
| 227 | |
| 228 | source_f(outstr); |
| 229 | } |
| 230 | |
| 231 | if (line.find("<ItemGroup Label=\"HeaderFiles\">") != std::string::npos) { |
| 232 | in_itemgroup = true; |
| 233 | |
| 234 | header_f(outstr); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // strip trailing \r\n |
| 240 | { |
| 241 | const std::string::size_type pos = outstr.find_last_not_of("\r\n"); |
| 242 | if (pos != std::string::npos) |
| 243 | outstr.resize(pos+1, '\0'); |
| 244 | } |
| 245 | |
| 246 | // treat as binary to prevent implicit line ending conversions |
| 247 | std::ofstream out(proj_name, std::ios::binary|std::ios::trunc); |
| 248 | out << outstr; |
| 249 | |
| 250 | return EXIT_SUCCESS; |
| 251 | } |
| 252 | |
| 253 | enum ClType : std::uint8_t { Compile, Include, Precompile, NoPch }; |