src/ENVCommand.h
branchv_0
changeset 0 8615122f8c02
equal deleted inserted replaced
-1:000000000000 0:8615122f8c02
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2020 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 <codecvt>
       
    20 #include <memory>
       
    21 #include <string>
       
    22 
       
    23 extern char** environ;
       
    24 
       
    25 namespace relpipe {
       
    26 namespace in {
       
    27 namespace env {
       
    28 
       
    29 class ENVCommand {
       
    30 private:
       
    31 
       
    32 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default
       
    33 
       
    34 public:
       
    35 
       
    36 	void process(std::shared_ptr<writer::RelationalWriter> writer) {
       
    37 		writer->startRelation(L"env",{
       
    38 			{L"name", relpipe::writer::TypeId::STRING},
       
    39 			{L"value", relpipe::writer::TypeId::STRING},
       
    40 		}, true);
       
    41 
       
    42 		for (char** e = environ; *e; e++) {
       
    43 			std::string entry = *e;
       
    44 
       
    45 			size_t split = entry.find("=");
       
    46 			std::string name = entry.substr(0, split);
       
    47 			std::string value = entry.substr(split + 1);
       
    48 
       
    49 			writer->writeAttribute(convertor.from_bytes(name));
       
    50 			writer->writeAttribute(convertor.from_bytes(value));
       
    51 		}
       
    52 	}
       
    53 };
       
    54 
       
    55 }
       
    56 }
       
    57 }