Procházet zdrojové kódy

core: add file::copy()

Daniele Bartolini před 2 roky
rodič
revize
8aec0975f9
2 změnil soubory, kde provedl 38 přidání a 2 odebrání
  1. 29 0
      src/core/filesystem/file.cpp
  2. 9 2
      src/core/filesystem/file.h

+ 29 - 0
src/core/filesystem/file.cpp

@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2012-2023 Daniele Bartolini et al.
+ * SPDX-License-Identifier: MIT
+ */
+
+#include "core/filesystem/file.h"
+
+namespace crown
+{
+namespace file
+{
+	u32 copy(File &output, File &input, u32 input_size)
+	{
+		u8 chunk[4096];
+		u32 to_read = input_size;
+		u32 num_written = 0;
+
+		while (num_written != input_size) {
+			u32 num_read = input.read(chunk, min(u32(sizeof(chunk)), to_read));
+			num_written += output.write(chunk, num_read);
+			to_read -= num_read;
+		}
+
+		return num_written;
+	}
+
+} // namespace file
+
+} // namespace crown

+ 9 - 2
src/core/filesystem/file.h

@@ -53,14 +53,21 @@ struct File
 	/// Sets the cursor position to @a bytes after current position.
 	/// Sets the cursor position to @a bytes after current position.
 	virtual void skip(u32 bytes) = 0;
 	virtual void skip(u32 bytes) = 0;
 
 
-	/// Reads @a size bytes from file.
+	/// Reads @a size bytes from this into @a data.
 	virtual u32 read(void *data, u32 size) = 0;
 	virtual u32 read(void *data, u32 size) = 0;
 
 
-	/// Writes @a size bytes to file.
+	/// Writes @a size bytes from @a data to this.
 	virtual u32 write(const void *data, u32 size) = 0;
 	virtual u32 write(const void *data, u32 size) = 0;
 
 
 	/// Forces the previouses write operations to complete.
 	/// Forces the previouses write operations to complete.
 	virtual void flush() = 0;
 	virtual void flush() = 0;
 };
 };
 
 
+namespace file
+{
+	/// Copies @a input_size bytes from @a input to @a output.
+	u32 copy(File &output, File &input, u32 input_size);
+
+} // namespace file
+
 } // namespace crown
 } // namespace crown