int TWFunc::copy_file(string src, string dst, int mode) { LOGINFO("Copying file %s to %s\n", src.c_str(), dst.c_str()); ifstream srcfile(src.c_str(), ios::binary); ofstream dstfile(dst.c_str(), ios::binary); dstfile << srcfile.rdbuf(); srcfile.close(); dstfile.close(); if (chmod(dst.c_str(), mode) != 0) return -1; return 0; }
int main(int argc, char **argv) { if(argc != 3) { std::cout << argv[0] << " [-d] infile.til extract_to_dir" << std::endl; return EXIT_SUCCESS; } std::fstream fd_data(argv[1], std::ios::in | std::ios::binary); if(fd_data.fail()) { std::cout << "error open file: " << argv[1] << std::endl; return EXIT_SUCCESS; } std::string prefix(argv[2]); std::string shortname(argv[1]); if(shortname == "-d") { } shortname.replace(shortname.find("."), 4, ""); prefix += SEPARATOR + shortname; if(0 != MKDIR(prefix.c_str())) { std::cout << "error mkdir: " << prefix << std::endl; return EXIT_SUCCESS; } fd_data.seekg(0, std::ios_base::end); u32 size = fd_data.tellg(); fd_data.seekg(0, std::ios_base::beg); u16 count, width, height; fd_data.read(reinterpret_cast<char *>(&count), sizeof(u16)); SwapLE16(count); fd_data.read(reinterpret_cast<char *>(&width), sizeof(u16)); SwapLE16(width); fd_data.read(reinterpret_cast<char *>(&height), sizeof(u16)); SwapLE16(height); char *body = new char[size]; fd_data.read(body, size); SDL::Init(); for(u16 cur = 0; cur < count; ++cur) { Surface sf(&body[width * height * cur], width, height, 1, false); std::string dstfile(prefix); dstfile += SEPARATOR; std::ostringstream stream; stream << cur; switch(stream.str().size()) { case 1: dstfile += "00" + stream.str(); break; case 2: dstfile += "0" + stream.str(); break; default: dstfile += stream.str(); break; } #ifndef WITH_IMAGE dstfile += ".bmp"; #else dstfile += ".png"; #endif sf.Save(dstfile.c_str()); } delete [] body; fd_data.close(); std::cout << "expand to: " << prefix << std::endl; SDL::Quit(); return EXIT_SUCCESS; }
bool GenesisCopyWindow::CopyFile(const char *filename, const char *destination, const char *destfilename) //////////////////////////////////////////////////////////////////////// { char name[B_FILE_NAME_LENGTH]; BString destname; BEntry srcentry(filename); BEntry dstentry(destination); struct stat statbuf; ssize_t len; srcentry.GetName(name); destname.SetTo(destination); destname += "/"; if (destfilename) destname += destfilename; else destname += name; BEntry dstfileentry(destname.String()); if (dstfileentry.InitCheck()!=B_OK) return false; if (dstfileentry.Exists() && !m_OverwriteAll) { BString text; text << "File '" << name << "' already exists. Do you want to overwrite it?"; BAlert *myAlert = new BAlert("Copy",text.String(),"Abort","Overwrite all","Overwrite",B_WIDTH_AS_USUAL,B_OFFSET_SPACING,B_WARNING_ALERT); myAlert->SetShortcut(0, B_ESCAPE); switch (myAlert->Go()) { case 0: Close(); kill_thread(m_CopyThread); break; case 1: m_OverwriteAll = true; break; } } BFile srcfile(filename, B_READ_ONLY); BFile dstfile(destname.String(), B_WRITE_ONLY | B_CREATE_FILE); if (srcentry.InitCheck()!=B_OK) return false; if (dstentry.InitCheck()!=B_OK) return false; if (!srcentry.Exists()) return false; if (!dstentry.Exists()) { return false; } if (srcentry.GetStat(&statbuf)!=B_OK) { return false; } unsigned char *buf = new unsigned char[statbuf.st_blksize]; if (!buf) { return false; } Lock(); m_FileBar->Update(-m_FileBar->CurrentValue()); // Reset to 0.0 m_FileBar->SetMaxValue(statbuf.st_size); m_FileBar->SetTrailingText(name); Unlock(); while (true) { len = srcfile.Read(buf, statbuf.st_blksize); if (len>0) { dstfile.Write(buf, len); Lock(); m_FileBar->Update(len); Unlock(); } else if (len<0) // error { delete [] buf; return false; } else // No more bytes to copy, we are done... break; } dstfile.SetPermissions(statbuf.st_mode); dstfile.SetOwner(statbuf.st_uid); dstfile.SetGroup(statbuf.st_gid); dstfile.SetModificationTime(statbuf.st_mtime); dstfile.SetCreationTime(statbuf.st_crtime); delete [] buf; // Copy attributes... CopyAttr(filename, destname.String()); return true; }