fstab logic ported from the prototype: FstabRelationalGenerator.h v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 11 Aug 2018 22:56:41 +0200
branchv_0
changeset 1 21ef3f6cd5e9
parent 0 cac146f5345a
child 2 6615824d69b7
fstab logic ported from the prototype: FstabRelationalGenerator.h
nbproject/Makefile-Debug.mk
nbproject/Makefile-Release.mk
nbproject/configurations.xml
relpipe-in-cli.cpp
relpipe-in-fstab.cpp
--- a/nbproject/Makefile-Debug.mk	Sat Aug 11 22:16:13 2018 +0200
+++ b/nbproject/Makefile-Debug.mk	Sat Aug 11 22:56:41 2018 +0200
@@ -35,7 +35,7 @@
 
 # Object Files
 OBJECTFILES= \
-	${OBJECTDIR}/relpipe-in-cli.o
+	${OBJECTDIR}/relpipe-in-fstab.o
 
 
 # C Compiler Flags
@@ -62,10 +62,10 @@
 	${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}
 	${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/relpipe-in-fstab.cpp ${OBJECTFILES} ${LDLIBSOPTIONS}
 
-${OBJECTDIR}/relpipe-in-cli.o: relpipe-in-cli.cpp
+${OBJECTDIR}/relpipe-in-fstab.o: relpipe-in-fstab.cpp
 	${MKDIR} -p ${OBJECTDIR}
 	${RM} "$@.d"
-	$(COMPILE.cc) -g `pkg-config --cflags relpipe-lib-writer.cpp` -std=c++14  -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/relpipe-in-cli.o relpipe-in-cli.cpp
+	$(COMPILE.cc) -g `pkg-config --cflags relpipe-lib-writer.cpp` -std=c++14  -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/relpipe-in-fstab.o relpipe-in-fstab.cpp
 
 # Subprojects
 .build-subprojects:
--- a/nbproject/Makefile-Release.mk	Sat Aug 11 22:16:13 2018 +0200
+++ b/nbproject/Makefile-Release.mk	Sat Aug 11 22:56:41 2018 +0200
@@ -35,7 +35,7 @@
 
 # Object Files
 OBJECTFILES= \
-	${OBJECTDIR}/relpipe-in-cli.o
+	${OBJECTDIR}/relpipe-in-fstab.o
 
 
 # C Compiler Flags
@@ -62,10 +62,10 @@
 	${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}
 	${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/relpipe-in-fstab.cpp ${OBJECTFILES} ${LDLIBSOPTIONS}
 
-${OBJECTDIR}/relpipe-in-cli.o: relpipe-in-cli.cpp
+${OBJECTDIR}/relpipe-in-fstab.o: relpipe-in-fstab.cpp
 	${MKDIR} -p ${OBJECTDIR}
 	${RM} "$@.d"
-	$(COMPILE.cc) -O2 `pkg-config --cflags relpipe-lib-writer.cpp`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/relpipe-in-cli.o relpipe-in-cli.cpp
+	$(COMPILE.cc) -O2 `pkg-config --cflags relpipe-lib-writer.cpp`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/relpipe-in-fstab.o relpipe-in-fstab.cpp
 
 # Subprojects
 .build-subprojects:
--- a/nbproject/configurations.xml	Sat Aug 11 22:16:13 2018 +0200
+++ b/nbproject/configurations.xml	Sat Aug 11 22:56:41 2018 +0200
@@ -13,7 +13,7 @@
     <logicalFolder name="SourceFiles"
                    displayName="Source Files"
                    projectFiles="true">
-      <itemPath>relpipe-in-cli.cpp</itemPath>
+      <itemPath>relpipe-in-fstab.cpp</itemPath>
     </logicalFolder>
     <logicalFolder name="TestFiles"
                    displayName="Test Files"
@@ -48,7 +48,7 @@
       </compileType>
       <item path="CLI.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="relpipe-in-cli.cpp" ex="false" tool="1" flavor2="0">
+      <item path="relpipe-in-fstab.cpp" ex="false" tool="1" flavor2="0">
       </item>
     </conf>
     <conf name="Release" type="1">
@@ -78,7 +78,7 @@
       </compileType>
       <item path="CLI.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="relpipe-in-cli.cpp" ex="false" tool="1" flavor2="0">
+      <item path="relpipe-in-fstab.cpp" ex="false" tool="1" flavor2="0">
       </item>
     </conf>
   </confs>
--- a/relpipe-in-cli.cpp	Sat Aug 11 22:16:13 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-#include <cstdlib>
-#include <memory>
-
-#include <RelationalWriter.h>
-#include <RelpipeWriterException.h>
-#include <Factory.h>
-#include <TypeId.h>
-
-#include "CLI.h"
-
-using namespace relpipe::cli;
-using namespace relpipe::writer;
-
-int main(int argc, char** argv) {
-	setlocale(LC_ALL, "");
-	CLI cli(argc, argv);
-
-	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
-
-	try {
-		fwprintf(stderr, L"TODO: implement fstab reading\n");
-
-	} 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;
-}
--- /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;
+}