src/FilesystemCommandBase.h
branchv_0
changeset 52 fea625f0a096
parent 32 bccda5688d71
equal deleted inserted replaced
51:841845ccf06d 52:fea625f0a096
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #pragma once
       
    18 
       
    19 #include <cstdlib>
       
    20 #include <iostream>
       
    21 #include <sstream>
       
    22 #include <string>
       
    23 #include <vector>
       
    24 #include <map>
       
    25 #include <memory>
       
    26 #include <algorithm>
       
    27 #include <filesystem>
       
    28 
       
    29 #include <pwd.h>
       
    30 #include <grp.h>
       
    31 #include <sys/stat.h>
       
    32 
       
    33 #include <sys/xattr.h>
       
    34 
       
    35 #include <relpipe/writer/typedefs.h>
       
    36 
       
    37 #include "Configuration.h"
       
    38 #include "AttributeFinder.h"
       
    39 #include "FileAttributeFinder.h"
       
    40 #include "XattrAttributeFinder.h"
       
    41 #include "StreamletAttributeFinder.h"
       
    42 
       
    43 namespace relpipe {
       
    44 namespace in {
       
    45 namespace filesystem {
       
    46 
       
    47 namespace fs = std::filesystem;
       
    48 using namespace relpipe::writer;
       
    49 
       
    50 class FilesystemCommandBase {
       
    51 protected:
       
    52 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
       
    53 
       
    54 	std::map<string_t, std::shared_ptr<AttributeFinder>> createAttributeFinders() {
       
    55 		return {
       
    56 			{RequestedField::GROUP_FILE, std::make_shared<FileAttributeFinder>()},
       
    57 			{RequestedField::GROUP_STREAMLET, std::make_shared<StreamletAttributeFinder>()},
       
    58 			{RequestedField::GROUP_XATTR, std::make_shared<XattrAttributeFinder>()}};
       
    59 	}
       
    60 
       
    61 	void reset(std::stringstream& stream) {
       
    62 		stream.str("");
       
    63 		stream.clear();
       
    64 	}
       
    65 
       
    66 	bool readNext(std::istream& input, std::stringstream& originalName) {
       
    67 		for (char ch; input.get(ch);) {
       
    68 			if (ch == 0) return true;
       
    69 			else originalName << ch;
       
    70 		}
       
    71 		return originalName.tellp();
       
    72 	}
       
    73 
       
    74 public:
       
    75 
       
    76 	virtual ~FilesystemCommandBase() = default;
       
    77 
       
    78 	virtual void process(std::istream& input, std::ostream& output, Configuration& configuration) = 0;
       
    79 };
       
    80 
       
    81 }
       
    82 }
       
    83 }