project skeleton: cmake, netbeans, code from relpipe-out-xml v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 07 Dec 2018 12:49:39 +0100
branchv_0
changeset 0 2f3c9e508827
child 1 a8cb46dbb612
project skeleton: cmake, netbeans, code from relpipe-out-xml
.hgignore
CMakeLists.txt
nbproject/configurations.xml
nbproject/project.xml
src/CMakeLists.txt
src/OdsHandler.h
src/relpipe-out-ods.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Fri Dec 07 12:49:39 2018 +0100
@@ -0,0 +1,13 @@
+syntax: glob
+
+*~
+CMakeLists.txt.user
+
+syntax: regexp
+
+^temp/
+
+^\.dep\.inc
+^dist/
+^build/
+^nbproject/private/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeLists.txt	Fri Dec 07 12:49:39 2018 +0100
@@ -0,0 +1,19 @@
+# Relational pipes
+# Copyright © 2018 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, either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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/>.
+
+project (relpipeout-ods.cpp)
+cmake_minimum_required(VERSION 2.8)
+add_subdirectory (src)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nbproject/configurations.xml	Fri Dec 07 12:49:39 2018 +0100
@@ -0,0 +1,135 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+
+Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved.
+
+Oracle and Java are registered trademarks of Oracle and/or its affiliates.
+Other names may be trademarks of their respective owners.
+
+The contents of this file are subject to the terms of either the GNU
+General Public License Version 2 only ("GPL") or the Common
+Development and Distribution License("CDDL") (collectively, the
+"License"). You may not use this file except in compliance with the
+License. You can obtain a copy of the License at
+http://www.netbeans.org/cddl-gplv2.html
+or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
+specific language governing permissions and limitations under the
+License.  When distributing the software, include this License Header
+Notice in each file and include the License file at
+nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
+particular file as subject to the "Classpath" exception as provided
+by Oracle in the GPL Version 2 section of the License file that
+accompanied this code. If applicable, add the following below the
+License Header, with the fields enclosed by brackets [] replaced by
+your own identifying information:
+"Portions Copyrighted [year] [name of copyright owner]"
+
+If you wish your version of this file to be governed by only the CDDL
+or only the GPL Version 2, indicate your decision by adding
+"[Contributor] elects to include this software in this distribution
+under the [CDDL or GPL Version 2] license." If you do not indicate a
+single choice of license, a recipient has the option to distribute
+your version of this file under either the CDDL, the GPL Version 2 or
+to extend the choice of license to its licensees as provided above.
+However, if you add GPL Version 2 code and therefore, elected the GPL
+Version 2 license, then the option applies only if the new code is
+made subject to such option by the copyright holder.
+
+Contributor(s):
+-->
+<configurationDescriptor version="100">
+  <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
+    <df root="." name="0">
+      <df name="src">
+        <in>relpipe-out-ods.cpp</in>
+      </df>
+    </df>
+    <logicalFolder name="ExternalFiles"
+                   displayName="Important Files"
+                   projectFiles="false"
+                   kind="IMPORTANT_FILES_FOLDER">
+      <itemPath>CMakeLists.txt</itemPath>
+      <itemPath>build/Debug/Makefile</itemPath>
+      <itemPath>build/Release/Makefile</itemPath>
+    </logicalFolder>
+  </logicalFolder>
+  <sourceFolderFilter>^(nbproject|build)$</sourceFolderFilter>
+  <sourceRootList>
+    <Elem>.</Elem>
+  </sourceRootList>
+  <projectmakefile>build/Debug/Makefile</projectmakefile>
+  <confs>
+    <conf name="Debug" type="0">
+      <toolsSet>
+        <compilerSet>default</compilerSet>
+        <dependencyChecking>false</dependencyChecking>
+        <rebuildPropChanged>false</rebuildPropChanged>
+      </toolsSet>
+      <flagsDictionary>
+        <element flagsID="0"
+                 commonFlags="-mtune=generic -march=x86-64 -fsanitize=address -fstack-protector-strong"/>
+      </flagsDictionary>
+      <codeAssistance>
+      </codeAssistance>
+      <makefileType>
+        <makeTool>
+          <buildCommandWorkingDir>build/Debug</buildCommandWorkingDir>
+          <buildCommand>${MAKE} -f Makefile</buildCommand>
+          <cleanCommand>${MAKE} -f Makefile clean</cleanCommand>
+          <executablePath>build/Debug/src/relpipe-out-ods</executablePath>
+          <ccTool>
+            <incDir>
+              <pElem>../relpipe-lib-cli.cpp/include/relpipe/cli</pElem>
+              <pElem>../relpipe-lib-reader.cpp/include/relpipe/reader</pElem>
+              <pElem>src</pElem>
+              <pElem>../relpipe-lib-reader.cpp/include/relpipe/reader/handlers</pElem>
+              <pElem>../relpipe-lib-cli.cpp/include</pElem>
+              <pElem>../relpipe-lib-reader.cpp/include</pElem>
+              <pElem>build/Debug/src</pElem>
+            </incDir>
+          </ccTool>
+        </makeTool>
+        <preBuild>
+          <preBuildCommandWorkingDir>build/Debug</preBuildCommandWorkingDir>
+          <preBuildCommand>${CMAKE} -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=${IDE_CC} -DCMAKE_CXX_COMPILER=${IDE_CXX} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ../..</preBuildCommand>
+          <preBuildFirst>true</preBuildFirst>
+        </preBuild>
+      </makefileType>
+      <item path="src/relpipe-out-ods.cpp" ex="false" tool="1" flavor2="0">
+        <ccTool flags="0">
+        </ccTool>
+      </item>
+    </conf>
+    <conf name="Release" type="0">
+      <toolsSet>
+        <compilerSet>default</compilerSet>
+        <dependencyChecking>false</dependencyChecking>
+        <rebuildPropChanged>false</rebuildPropChanged>
+      </toolsSet>
+      <codeAssistance>
+      </codeAssistance>
+      <makefileType>
+        <makeTool>
+          <buildCommandWorkingDir>build/Release</buildCommandWorkingDir>
+          <buildCommand>${MAKE} -f Makefile</buildCommand>
+          <cleanCommand>${MAKE} -f Makefile clean</cleanCommand>
+          <executablePath>build/Release/src/welcome</executablePath>
+          <ccTool>
+            <incDir>
+              <pElem>src</pElem>
+            </incDir>
+            <preprocessorList>
+              <Elem>NDEBUG</Elem>
+            </preprocessorList>
+          </ccTool>
+        </makeTool>
+        <preBuild>
+          <preBuildCommandWorkingDir>build/Release</preBuildCommandWorkingDir>
+          <preBuildCommand>${CMAKE} -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=${IDE_CC} -DCMAKE_CXX_COMPILER=${IDE_CXX} -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ../..</preBuildCommand>
+          <preBuildFirst>true</preBuildFirst>
+        </preBuild>
+      </makefileType>
+    </conf>
+  </confs>
+</configurationDescriptor>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nbproject/project.xml	Fri Dec 07 12:49:39 2018 +0100
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+
+Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved.
+
+Oracle and Java are registered trademarks of Oracle and/or its affiliates.
+Other names may be trademarks of their respective owners.
+
+The contents of this file are subject to the terms of either the GNU
+General Public License Version 2 only ("GPL") or the Common
+Development and Distribution License("CDDL") (collectively, the
+"License"). You may not use this file except in compliance with the
+License. You can obtain a copy of the License at
+http://www.netbeans.org/cddl-gplv2.html
+or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
+specific language governing permissions and limitations under the
+License.  When distributing the software, include this License Header
+Notice in each file and include the License file at
+nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
+particular file as subject to the "Classpath" exception as provided
+by Oracle in the GPL Version 2 section of the License file that
+accompanied this code. If applicable, add the following below the
+License Header, with the fields enclosed by brackets [] replaced by
+your own identifying information:
+"Portions Copyrighted [year] [name of copyright owner]"
+
+If you wish your version of this file to be governed by only the CDDL
+or only the GPL Version 2, indicate your decision by adding
+"[Contributor] elects to include this software in this distribution
+under the [CDDL or GPL Version 2] license." If you do not indicate a
+single choice of license, a recipient has the option to distribute
+your version of this file under either the CDDL, the GPL Version 2 or
+to extend the choice of license to its licensees as provided above.
+However, if you add GPL Version 2 code and therefore, elected the GPL
+Version 2 license, then the option applies only if the new code is
+made subject to such option by the copyright holder.
+
+Contributor(s):
+-->
+<project xmlns="http://www.netbeans.org/ns/project/1">
+    <type>org.netbeans.modules.cnd.makeproject</type>
+    <configuration>
+        <data xmlns="http://www.netbeans.org/ns/make-project/1">
+            <name>relpipe-out-ods.cpp</name>
+            <c-extensions/>
+            <cpp-extensions>cpp</cpp-extensions>
+            <header-extensions>h</header-extensions>
+            <sourceEncoding>UTF-8</sourceEncoding>
+            <sourceRootList>
+                <sourceRootElem>.</sourceRootElem>
+            </sourceRootList>
+            <make-dep-projects/>
+            <sourceRootList>
+                <sourceRootElem>.</sourceRootElem>
+            </sourceRootList>
+            <confList>
+                <confElem>
+                    <name>Debug</name>
+                    <type>0</type>
+                </confElem>
+                <confElem>
+                    <name>Release</name>
+                    <type>0</type>
+                </confElem>
+            </confList>
+            <formatting>
+                <project-formatting-style>false</project-formatting-style>
+            </formatting>
+        </data>
+    </configuration>
+</project>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CMakeLists.txt	Fri Dec 07 12:49:39 2018 +0100
@@ -0,0 +1,38 @@
+# Relational pipes
+# Copyright © 2018 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, either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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/>.
+
+set(EXECUTABLE_FILE "relpipe-out-ods")
+
+# Relpipe libraries:
+INCLUDE(FindPkgConfig)
+pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-cli.cpp)
+include_directories(${RELPIPE_LIBS_INCLUDE_DIRS})
+link_directories(${RELPIPE_LIBS_LIBRARY_DIRS})
+
+# Add ASan AddressSanitizer
+set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
+set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
+# From ASan wiki: "To get nicer stack traces in error messages add -fno-omit-frame-pointer."
+
+# Executable output:
+add_executable(
+	${EXECUTABLE_FILE}
+	OdsHandler.h
+	relpipe-out-ods.cpp
+)
+
+# Link libraries:
+target_link_libraries(${EXECUTABLE_FILE} ${RELPIPE_LIBS_LIBRARIES})
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/OdsHandler.h	Fri Dec 07 12:49:39 2018 +0100
@@ -0,0 +1,139 @@
+/**
+ * Relational pipes
+ * Copyright © 2018 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, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 <string>
+#include <vector>
+#include <iostream>
+#include <sstream>
+#include <locale>
+#include <codecvt>
+#include <regex>
+
+#include <relpipe/reader/typedefs.h>
+#include <relpipe/reader/TypeId.h>
+#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
+#include <relpipe/reader/handlers/AttributeMetadata.h>
+
+namespace relpipe {
+namespace out {
+namespace ods {
+
+using namespace relpipe::reader;
+
+class OdsHandler : public handlers::RelationalReaderStringHadler {
+private:
+	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // XML output will be always in UTF-8
+	const char* INDENT = "\t";
+
+	std::ostream &output;
+
+	std::vector<TypeId> columnTypes;
+	std::vector<string_t> columnTypeCodes;
+	std::vector<string_t> columnNames;
+	integer_t valueCount = 0;
+	integer_t columnCount = 0;
+	integer_t relationCount = 0;
+
+	const std::string escapeXmlText(const string_t &value) {
+		std::wstringstream result;
+
+		for (auto & ch : value) {
+			switch (ch) {
+				case L'&': result << L"&amp;";
+					break;
+				case L'<': result << L"&lt;";
+					break;
+				case L'>': result << L"&gt;";
+					break;
+				case L'\'': result << L"&apos;"; // TODO: escape ' and " only in attributes
+					break;
+				case L'"': result << L"&quot;"; // TODO: escape ' and " only in attributes
+					break;
+				default: result << ch;
+			}
+		}
+
+		return convertor.to_bytes(result.str());
+	}
+
+public:
+
+	OdsHandler(std::ostream& output) : output(output) {
+	}
+
+	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
+		// TODO: refactor and move common XML functions to relpipe-lib-xml
+
+		valueCount = 0;
+		columnCount = 0;
+
+		if (relationCount == 0) {
+			output << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
+			output << "<pipe TODO=\"ods\">" << std::endl;
+			// TODO: xmlns
+		} else {
+			output << INDENT << INDENT << "</record>" << std::endl;
+			output << INDENT << "</relation>" << std::endl;
+		}
+		relationCount++;
+		output << INDENT << "<relation>" << std::endl;
+
+		output << INDENT << INDENT << "<name>" << escapeXmlText(name) << "</name>" << std::endl;
+
+
+		columnCount = attributes.size();
+		columnTypes.resize(columnCount);
+		columnTypeCodes.resize(columnCount);
+		columnNames.resize(columnCount);
+		for (int i = 0; i < attributes.size(); i++) {
+			columnNames[i] = attributes[i].getAttributeName();
+			columnTypes[i] = attributes[i].getTypeId();
+			columnTypeCodes[i] = attributes[i].getTypeName();
+		}
+		
+		// TODO: print attribute metadata
+	}
+
+	void attribute(const string_t& value) override {
+		integer_t i = valueCount % columnCount;
+
+		if (i == 0 && valueCount) output << INDENT << INDENT << "</record>" << std::endl;
+		if (i == 0) output << INDENT << INDENT << "<record>" << std::endl;
+
+		valueCount++;
+
+		// TODO: print attribute metadata (optional)
+		output << INDENT << INDENT << INDENT << "<attribute>";
+		output << escapeXmlText(value);
+		output << "</attribute>" << std::endl;
+
+	}
+
+	void endOfPipe() {
+		if (valueCount) output << INDENT << INDENT << "</record>" << std::endl;
+		if (relationCount) output << INDENT << "</relation>" << std::endl;
+		output << "</pipe>" << std::endl;
+
+	}
+
+};
+
+}
+}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/relpipe-out-ods.cpp	Fri Dec 07 12:49:39 2018 +0100
@@ -0,0 +1,63 @@
+/**
+ * Relational pipes
+ * Copyright © 2018 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, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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/>.
+ */
+#include <cstdlib>
+#include <unistd.h>
+#include <memory>
+
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+#include <relpipe/reader/Factory.h>
+#include <relpipe/reader/RelationalReader.h>
+#include <relpipe/reader/RelpipeReaderException.h>
+
+#include "OdsHandler.h"
+
+using namespace relpipe::cli;
+using namespace relpipe::reader;
+using namespace relpipe::out::ods;
+
+int main(int argc, char** argv) {
+	setlocale(LC_ALL, "");
+	CLI::untieStdIO();
+	CLI cli(argc, argv);
+
+	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
+
+	try {
+
+		// TODO: color syntax highlighting if stdout is TTY:
+		// if (isatty(fileno(stdout))) std::cout << "color" << std::endl;
+		// else std::cout << "no-color" << std::endl;
+
+		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
+		OdsHandler handler(std::cout);
+		reader->addHandler(&handler);
+		reader->process();
+		resultCode = CLI::EXIT_CODE_SUCCESS;
+	} catch (RelpipeCLIException e) {
+		fwprintf(stderr, L"Caught CLI 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 = e.getExitCode();
+	} catch (RelpipeReaderException e) {
+		fwprintf(stderr, L"Caught Reader 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;
+}