commit cea63e60dfe6a254b9c5e37f6fe2946edde97a39
parent 979b750f18b6ffb296993a3a7209bc98f8f5de7f
Author: Remy Noulin <loader2x@gmail.com>
Date: Sun, 20 May 2018 10:47:19 +0200
simpleTemplates command
file.txt | 24 ++++++++++++++++++++++++
package.yml | 15 +++++++++++++++
simpleTemplates.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
simpleTemplates.h | 7 +++++++
simpleTemplatesTest.c | 22 ++++++++++++++++++++++
templateFile.txt | 3 +++
6 files changed, 122 insertions(+)
Diffstat:
6 files changed, 122 insertions(+), 0 deletions(-)
diff --git a/file.txt b/file.txt
@@ -0,0 +1,24 @@
+test
+
+#! /usr/bin/env sheepy
+
+#include "libsheepyObject.h"
+#include "simpleTemplates.h"
+
+int argc; char **argv;
+
+int main(int ARGC, char** ARGV) {
+
+ argc = ARGC; argv = ARGV;
+
+ initLibsheepy(argv[0]);
+
+ createAllocateSmallDict(d);
+
+ setG(d, "_INSERT_FILE_", "simpleTemplatesTest.c");
+
+ simpleTemplatesInsertFiles("templateFile.txt","file.txt", d);
+
+ finalizeLibsheepy();
+
+}
diff --git a/package.yml b/package.yml
@@ -0,0 +1,15 @@
+---
+ name: simpleTemplates
+ version: 0.0.3
+ description: "Library for processing templates"
+ bin: ./simpleTemplates.c
+ repository:
+ type: git
+ url: git+https://github.com/RemyNoulin/simpleTemplates.git
+ keywords:
+ - templates
+ author: Remy
+ license: MIT
+ bugs:
+ url: https://github.com/RemyNoulin/simpleTemplates/issues
+ homepage: https://github.com/RemyNoulin/simpleTemplates#readme
diff --git a/simpleTemplates.c b/simpleTemplates.c
@@ -0,0 +1,51 @@
+#include "libsheepyObject.h"
+
+bool simpleTemplatesInsertFiles(char *templ, char *dst, smallDictt *keyFiles) {
+ if (!fileExists(templ)) {
+ return false;
+ }
+
+ // load templates
+ createAllocateSmallArray(template);
+ readFileG(template, templ);
+ //logVarG(template);
+
+ forEachSmallDict(keyFiles, k, V) {
+ castS(v, V);
+ smallStringt *path = dupG(v);
+ expandHomeG(path);
+ if (!fileExistsG(path)) {
+ printf("File for key %s not found: %s\n", k, ssGet(v));
+ return false;
+ }
+ createAllocateSmallString(s);
+ readFileG(s, ssGet(path));
+ terminateG(path);
+ if (getG(s, unusedV , -1) == '\n') {
+ setG(s, -1, 0);
+ }
+ setG(keyFiles, k, s);
+ finishG(v);
+ }
+ listFreeS(libsheepyInternalKeys);
+
+ char **keys = keysG(keyFiles);
+
+ enumerateSmallArray(template, L, i) {
+ castS(l, L);
+ forEachS(keys, k) {
+ smallStringt *s = getG(keyFiles, rtSmallStringt, k);
+ replaceSO_max(l, k, ssGet(s));
+ finishG(s);
+ }
+ setPG(template, i, l);
+ finishG(l);
+ }
+
+ //logVarG(template);
+
+ writeFileG(template, dst);
+
+ terminateG(template);
+ return true;
+}
diff --git a/simpleTemplates.h b/simpleTemplates.h
@@ -0,0 +1,7 @@
+
+#include "libsheepyObject.h"
+
+
+// key values
+bool simpleTemplatesInsertFiles(char *templ, char *dst, smallDictt *keyFiles);
+// key file
diff --git a/simpleTemplatesTest.c b/simpleTemplatesTest.c
@@ -0,0 +1,22 @@
+#! /usr/bin/env sheepy
+
+#include "libsheepyObject.h"
+#include "simpleTemplates.h"
+
+int argc; char **argv;
+
+int main(int ARGC, char** ARGV) {
+
+ argc = ARGC; argv = ARGV;
+
+ initLibsheepy(argv[0]);
+
+ createAllocateSmallDict(d);
+
+ setG(d, "_INSERT_FILE_", "simpleTemplatesTest.c");
+
+ simpleTemplatesInsertFiles("templateFile.txt","file.txt", d);
+
+ finalizeLibsheepy();
+
+}
diff --git a/templateFile.txt b/templateFile.txt
@@ -0,0 +1,3 @@
+test
+
+_INSERT_FILE_