src/ENVCommand.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 24 Apr 2022 00:39:48 +0200
branchv_0
changeset 0 8615122f8c02
permissions -rw-r--r--
first version

/**
 * Relational pipes
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <codecvt>
#include <memory>
#include <string>

extern char** environ;

namespace relpipe {
namespace in {
namespace env {

class ENVCommand {
private:

	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default

public:

	void process(std::shared_ptr<writer::RelationalWriter> writer) {
		writer->startRelation(L"env",{
			{L"name", relpipe::writer::TypeId::STRING},
			{L"value", relpipe::writer::TypeId::STRING},
		}, true);

		for (char** e = environ; *e; e++) {
			std::string entry = *e;

			size_t split = entry.find("=");
			std::string name = entry.substr(0, split);
			std::string value = entry.substr(split + 1);

			writer->writeAttribute(convertor.from_bytes(name));
			writer->writeAttribute(convertor.from_bytes(value));
		}
	}
};

}
}
}