--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/relpipe-in-fstab.cpp Sat Aug 11 22:56:41 2018 +0200
@@ -0,0 +1,92 @@
+#include <cstdlib>
+#include <fstream>
+#include <memory>
+#include <regex>
+#include <algorithm>
+#include <unistd.h>
+
+#include <RelationalWriter.h>
+#include <RelpipeWriterException.h>
+#include <Factory.h>
+#include <TypeId.h>
+
+#include "CLI.h"
+
+using namespace relpipe::cli;
+using namespace relpipe::writer;
+
+void processDataStream(ostream &output, istream* input) {
+ wregex devicePattern = wregex(L"(LABEL|UUID)=(.*)");
+ wregex linePattern = wregex(L"^([^\\s#]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s*$");
+ wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
+
+ std::shared_ptr<RelationalWriter> writer(Factory::create(output));
+
+ writer->startRelation(L"fstab",{
+ {L"scheme", TypeId::STRING},
+ {L"device", TypeId::STRING},
+ {L"mount_point", TypeId::STRING},
+ {L"type", TypeId::STRING},
+ // {L"types", TypeId::STRING}, // TODO: array
+ {L"options", TypeId::STRING}, // TODO: array
+ {L"dump", TypeId::INTEGER},
+ {L"pass", TypeId::INTEGER}
+ }, true);
+
+ string lineBytes;
+ while (getline(*input, lineBytes)) {
+
+ wstring line = convertor.from_bytes(lineBytes);
+ wsmatch lineMatch;
+ if (regex_search(line, lineMatch, linePattern) && lineMatch.size() > 0) {
+ int g = 1;
+ wstring device = lineMatch[g++];
+ wstring mountPoint = lineMatch[g++];
+ wstring type = lineMatch[g++];
+ wstring options = lineMatch[g++];
+ wstring dump = lineMatch[g++];
+ wstring pass = lineMatch[g++];
+
+ wsmatch deviceMatch;
+ if (regex_search(device, deviceMatch, devicePattern)) {
+ writer->writeAttribute(deviceMatch[1]);
+ writer->writeAttribute(deviceMatch[2]);
+ } else {
+ writer->writeAttribute(L""); // TODO: null (requires bitmap)
+ writer->writeAttribute(device);
+ }
+
+ if (mountPoint == L"none") mountPoint = L""; // TODO: null (requires bitmap)
+ writer->writeAttribute(mountPoint);
+ writer->writeAttribute(type);
+ writer->writeAttribute(options);
+ writer->writeAttribute(dump);
+ writer->writeAttribute(pass);
+ }
+ }
+
+}
+
+int main(int argc, char** argv) {
+ setlocale(LC_ALL, "");
+ //CLI cli(argc, argv);
+
+ int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
+
+ try {
+ if (isatty(fileno(stdin))) {
+ ifstream s("/etc/fstab");
+ processDataStream(cout, &s);
+ } else {
+ processDataStream(cout, &cin);
+ }
+ resultCode = CLI::EXIT_CODE_SUCCESS;
+
+ } catch (RelpipeWriterException e) {
+ fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
+ fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
+ resultCode = CLI::EXIT_CODE_DATA_ERROR;
+ }
+
+ return resultCode;
+}