Copies a file. Returns TRUE if copied ok. Returns FALSE if error opening either file. Throws an exception of type (cfile_error *) if the OS returns an error on read or write
| 830 | // Copies a file. Returns TRUE if copied ok. Returns FALSE if error opening either file. |
| 831 | // Throws an exception of type (cfile_error *) if the OS returns an error on read or write |
| 832 | bool cf_CopyFile(const std::filesystem::path &dest, const std::filesystem::path &src, int copytime) { |
| 833 | CFILE *infile, *outfile; |
| 834 | if (!stricmp(dest.u8string().c_str(), src.u8string().c_str())) |
| 835 | return true; // don't copy files if they are the same |
| 836 | infile = (CFILE *)cfopen(src, "rb"); |
| 837 | if (!infile) |
| 838 | return false; |
| 839 | outfile = (CFILE *)cfopen(dest, "wb"); |
| 840 | if (!outfile) { |
| 841 | cfclose(infile); |
| 842 | return false; |
| 843 | } |
| 844 | int progress = 0; |
| 845 | int readcount = 0; |
| 846 | #define COPY_CHUNK_SIZE 5000 |
| 847 | uint8_t copybuf[COPY_CHUNK_SIZE]; |
| 848 | while (!cfeof(infile)) { |
| 849 | // uint8_t c; |
| 850 | |
| 851 | if (progress + COPY_CHUNK_SIZE <= infile->size) { |
| 852 | readcount = COPY_CHUNK_SIZE; |
| 853 | } else { |
| 854 | readcount = infile->size - progress; |
| 855 | } |
| 856 | cf_ReadBytes(copybuf, readcount, infile); |
| 857 | cf_WriteBytes(copybuf, readcount, outfile); |
| 858 | progress += readcount; |
| 859 | // c=cf_ReadByte (infile); |
| 860 | // cf_WriteByte (outfile,c); |
| 861 | } |
| 862 | cfclose(infile); |
| 863 | cfclose(outfile); |
| 864 | if (!infile->lib_offset && copytime) { |
| 865 | cf_CopyFileTime(dest, src); |
| 866 | } |
| 867 | return true; |
| 868 | } |
| 869 | |
| 870 | // Checks to see if two files are different. |
| 871 | // Returns TRUE if the files are different, or FALSE if they are the same. |
no test coverage detected