reading file content (TODO: support unicode/UTF-8) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 26 Jul 2019 22:34:30 +0200
branchv_0
changeset 21 1252acdc5a5a
parent 20 1bf848f24c53
child 22 31e7f1994b12
reading file content (TODO: support unicode/UTF-8)
src/FileAttributeFinder.h
--- a/src/FileAttributeFinder.h	Sun May 26 22:22:58 2019 +0200
+++ b/src/FileAttributeFinder.h	Fri Jul 26 22:34:30 2019 +0200
@@ -19,6 +19,8 @@
 
 #include <vector>
 #include <filesystem>
+#include <fstream>
+#include <sstream>
 
 #include <relpipe/writer/typedefs.h>
 #include <relpipe/writer/AttributeMetadata.h>
@@ -70,6 +72,20 @@
 		group = convertor.from_bytes(gr->gr_name);
 	}
 
+	string_t getContent(const fs::path& file) {
+		// TODO: better copy
+		// not working:
+		wifstream source(file);
+		std::wstringstream result;
+		for (wchar_t ch; source.get(ch); source.good()) result << ch;
+		return result.str();
+
+		//wifstream stream(file);
+		//std::wstringstream content;
+		//content << stream.rdbuf();
+		//return content.str();
+	}
+
 protected:
 
 	virtual void writeFieldOfExistingFile(RelationalWriter* writer, const RequestedField& field) override {
@@ -106,6 +122,8 @@
 				} else if (field.name == FIELD_GROUP) {
 					if (currentOwner.empty()) fetchOwner(currentFile, currentOwner, currentGroup);
 					writer->writeAttribute(currentGroup);
+				} else if (field.name == FIELD_CONTENT) {
+					writer->writeAttribute(getContent(currentFile));
 				} else {
 					throw RelpipeWriterException(L"Unsupported field name in FileAttributeFinder: " + field.name);
 				}
@@ -125,6 +143,7 @@
 	static const string_t FIELD_SYMLINK_TARGET;
 	static const string_t FIELD_OWNER;
 	static const string_t FIELD_GROUP;
+	static const string_t FIELD_CONTENT;
 
 	virtual vector<AttributeMetadata> toMetadata(const RequestedField& field) override {
 		if (field.group == RequestedField::GROUP_FILE) {
@@ -168,6 +187,7 @@
 const string_t FileAttributeFinder::FIELD_SYMLINK_TARGET = L"symlink_target";
 const string_t FileAttributeFinder::FIELD_OWNER = L"owner";
 const string_t FileAttributeFinder::FIELD_GROUP = L"group";
+const string_t FileAttributeFinder::FIELD_CONTENT = L"content";
 
 }
 }