diff util.c @ 51:07b6887d3557

pasterd: split into individual pages While here add a similar mini.css theme as in imgup.
author David Demelier <markand@malikania.fr>
date Mon, 21 Dec 2020 18:22:44 +0100
parents f455893bf0b0
children fba88439ec0a
line wrap: on
line diff
--- a/util.c	Mon Apr 27 12:08:16 2020 +0200
+++ b/util.c	Mon Dec 21 18:22:44 2020 +0100
@@ -17,7 +17,9 @@
  */
 
 #include <assert.h>
+#include <ctype.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -25,7 +27,191 @@
 #include <string.h>
 #include <time.h>
 
+#include "config.h"
 #include "util.h"
+#include "paste.h"
+
+const char *languages[] = {
+	"nohighlight",
+	"1c",
+	"abnf",
+	"accesslog",
+	"actionscript",
+	"ada",
+	"apache",
+	"applescript",
+	"arduino",
+	"armasm",
+	"asciidoc",
+	"aspectj",
+	"autohotkey",
+	"autoit",
+	"avrasm",
+	"awk",
+	"axapta",
+	"bash",
+	"basic",
+	"bnf",
+	"brainfuck",
+	"cal",
+	"capnproto",
+	"ceylon",
+	"clean",
+	"clojure",
+	"clojure-repl",
+	"cmake",
+	"coffeescript",
+	"coq",
+	"cos",
+	"cpp",
+	"crmsh",
+	"crystal",
+	"cs",
+	"csp",
+	"css",
+	"dart",
+	"delphi",
+	"diff",
+	"django",
+	"d",
+	"dns",
+	"dockerfile",
+	"dos",
+	"dsconfig",
+	"dts",
+	"dust",
+	"ebnf",
+	"elixir",
+	"elm",
+	"erb",
+	"erlang",
+	"erlang-repl",
+	"excel",
+	"fix",
+	"flix",
+	"fortran",
+	"fsharp",
+	"gams",
+	"gauss",
+	"gcode",
+	"gherkin",
+	"glsl",
+	"go",
+	"golo",
+	"gradle",
+	"groovy",
+	"haml",
+	"handlebars",
+	"haskell",
+	"haxe",
+	"hsp",
+	"htmlbars",
+	"http",
+	"hy",
+	"inform7",
+	"ini",
+	"irpf90",
+	"java",
+	"javascript",
+	"jboss-cli",
+	"json",
+	"julia",
+	"julia-repl",
+	"kotlin",
+	"lasso",
+	"ldif",
+	"leaf",
+	"less",
+	"lisp",
+	"livecodeserver",
+	"livescript",
+	"llvm",
+	"lsl",
+	"lua",
+	"makefile",
+	"markdown",
+	"mathematica",
+	"matlab",
+	"maxima",
+	"mel",
+	"mercury",
+	"mipsasm",
+	"mizar",
+	"mojolicious",
+	"monkey",
+	"moonscript",
+	"n1ql",
+	"nginx",
+	"nimrod",
+	"nix",
+	"nsis",
+	"objectivec",
+	"ocaml",
+	"openscad",
+	"oxygene",
+	"parser3",
+	"perl",
+	"pf",
+	"php",
+	"pony",
+	"powershell",
+	"processing",
+	"profile",
+	"prolog",
+	"protobuf",
+	"puppet",
+	"purebasic",
+	"python",
+	"q",
+	"qml",
+	"rib",
+	"r",
+	"roboconf",
+	"routeros",
+	"rsl",
+	"ruby",
+	"ruleslanguage",
+	"rust",
+	"scala",
+	"scheme",
+	"scilab",
+	"scss",
+	"shell",
+	"smali",
+	"smalltalk",
+	"sml",
+	"sqf",
+	"sql",
+	"stan",
+	"stata",
+	"step21",
+	"stylus",
+	"subunit",
+	"swift",
+	"taggerscript",
+	"tap",
+	"tcl",
+	"tex",
+	"thrift",
+	"tp",
+	"twig",
+	"typescript",
+	"vala",
+	"vbnet",
+	"vbscript-html",
+	"vbscript",
+	"verilog",
+	"vhdl",
+	"vim",
+	"x86asm",
+	"xl",
+	"xml",
+	"xquery",
+	"yaml",
+	"zephir"
+};
+
+const size_t languagesz = NELEM(languages);
 
 noreturn void
 die(const char *fmt, ...)
@@ -75,3 +261,47 @@
 
 	return buf;
 }
+
+const char *
+path(const char *filename)
+{
+	assert(filename);
+
+	/* Build path to the template file. */
+	static char path[PATH_MAX];
+
+	snprintf(path, sizeof (path), "%s/%s", config.themedir, filename);
+
+	return path;
+}
+
+void
+replace(char **dst, const char *s)
+{
+	assert(dst);
+	assert(s);
+
+	/* Trim leading spaces. */
+	while (*s && isspace(*s))
+		s++;
+
+	if (*s) {
+		free(*dst);
+		*dst = estrdup(s);
+	}
+}
+
+const char *
+ttl(time_t timestamp, long long int duration)
+{
+	const time_t now = time(NULL);
+	const long long int left = duration - difftime(now, timestamp);
+
+	if (left < PASTE_DURATION_HOUR)
+		return bprintf("%lld minute(s)", left / 60);
+	if (left < PASTE_DURATION_DAY)
+		return bprintf("%lld hour(s)", left / 3600);
+
+	/* Other in days. */
+	return bprintf("%lld day(s)", left / 86400);
+}