| 2345 | } |
| 2346 | |
| 2347 | void ASConsole::writeFile(const string& fileName_, FileEncoding encoding, ostringstream& out) const |
| 2348 | { |
| 2349 | // save date accessed and date modified of original file |
| 2350 | struct stat stBuf; |
| 2351 | bool statErr = false; |
| 2352 | if (stat(fileName_.c_str(), &stBuf) == -1) |
| 2353 | statErr = true; |
| 2354 | |
| 2355 | // create a backup |
| 2356 | if (!noBackup) |
| 2357 | { |
| 2358 | string origFileName = fileName_ + origSuffix; |
| 2359 | removeFile(origFileName.c_str(), "Cannot remove pre-existing backup file"); |
| 2360 | renameFile(fileName_.c_str(), origFileName.c_str(), "Cannot create backup file"); |
| 2361 | } |
| 2362 | |
| 2363 | // write the output file |
| 2364 | ofstream fout(fileName_.c_str(), ios::binary | ios::trunc); |
| 2365 | if (!fout) |
| 2366 | error("Cannot open output file", fileName_.c_str()); |
| 2367 | if (encoding == UTF_16LE || encoding == UTF_16BE) |
| 2368 | { |
| 2369 | // convert utf-8 to utf-16 |
| 2370 | size_t utf16Size = Utf16Length(out.str().c_str(), out.str().length()); |
| 2371 | char* utf16Out = new char[utf16Size]; |
| 2372 | size_t utf16Len = Utf8ToUtf16(const_cast<char*>(out.str().c_str()), out.str().length(), encoding, utf16Out); |
| 2373 | assert(utf16Len == utf16Size); |
| 2374 | fout << string(utf16Out, utf16Len); |
| 2375 | delete []utf16Out; |
| 2376 | } |
| 2377 | else |
| 2378 | fout << out.str(); |
| 2379 | |
| 2380 | fout.close(); |
| 2381 | |
| 2382 | // change date modified to original file date |
| 2383 | // Embarcadero must be linked with cw32mt not cw32 |
| 2384 | if (preserveDate) |
| 2385 | { |
| 2386 | if (!statErr) |
| 2387 | { |
| 2388 | struct utimbuf outBuf; |
| 2389 | outBuf.actime = stBuf.st_atime; |
| 2390 | // add ticks so 'make' will recoginze a change |
| 2391 | // Visual Studio 2008 needs more than 1 |
| 2392 | outBuf.modtime = stBuf.st_mtime + 10; |
| 2393 | if (utime(fileName_.c_str(), &outBuf) == -1) |
| 2394 | statErr = true; |
| 2395 | } |
| 2396 | if (statErr) |
| 2397 | { |
| 2398 | perror("errno message"); |
| 2399 | (*_err) << "********* Cannot preserve file date" << endl; |
| 2400 | } |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | #endif // ASConsole: |