| 2412 | } |
| 2413 | |
| 2414 | void ASConsole::writeFile(const string &fileName_, FileEncoding encoding, ostringstream &out) const |
| 2415 | { |
| 2416 | // save date accessed and date modified of original file |
| 2417 | struct stat stBuf; |
| 2418 | bool statErr = false; |
| 2419 | if (stat(fileName_.c_str(), &stBuf) == -1) |
| 2420 | statErr = true; |
| 2421 | |
| 2422 | // create a backup |
| 2423 | if (!noBackup) |
| 2424 | { |
| 2425 | string origFileName = fileName_ + origSuffix; |
| 2426 | removeFile(origFileName.c_str(), "Cannot remove pre-existing backup file"); |
| 2427 | renameFile(fileName_.c_str(), origFileName.c_str(), "Cannot create backup file"); |
| 2428 | } |
| 2429 | |
| 2430 | // write the output file |
| 2431 | ofstream fout(fileName_.c_str(), ios::binary | ios::trunc); |
| 2432 | if (!fout) |
| 2433 | error("Cannot open output file", fileName_.c_str()); |
| 2434 | if (encoding == UTF_16LE || encoding == UTF_16BE) |
| 2435 | { |
| 2436 | // convert utf-8 to utf-16 |
| 2437 | size_t utf16Size = Utf16LengthFromUtf8(out.str().c_str(), out.str().length()); |
| 2438 | char* utf16Out = new char[utf16Size]; |
| 2439 | size_t utf16Len = Utf8ToUtf16(const_cast<char*>(out.str().c_str()), out.str().length(), encoding, utf16Out); |
| 2440 | assert(utf16Len == utf16Size); |
| 2441 | fout << string(utf16Out, utf16Len); |
| 2442 | delete []utf16Out; |
| 2443 | } |
| 2444 | else |
| 2445 | fout << out.str(); |
| 2446 | |
| 2447 | fout.close(); |
| 2448 | |
| 2449 | // change date modified to original file date |
| 2450 | // Embarcadero must be linked with cw32mt not cw32 |
| 2451 | if (preserveDate) |
| 2452 | { |
| 2453 | if (!statErr) |
| 2454 | { |
| 2455 | struct utimbuf outBuf; |
| 2456 | outBuf.actime = stBuf.st_atime; |
| 2457 | // add ticks so 'make' will recoginze a change |
| 2458 | // Visual Studio 2008 needs more than 1 |
| 2459 | outBuf.modtime = stBuf.st_mtime + 10; |
| 2460 | if (utime(fileName_.c_str(), &outBuf) == -1) |
| 2461 | statErr = true; |
| 2462 | } |
| 2463 | if (statErr) |
| 2464 | { |
| 2465 | perror("errno message"); |
| 2466 | (*_err) << "********* Cannot preserve file date" << endl; |
| 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | //----------------------------------------------------------------------------- |