summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-01-24 20:19:42 +0100
committerTyge Løvset <[email protected]>2023-01-24 20:19:42 +0100
commit782ab14cac327134614188676a196fcfcb9d0ba1 (patch)
tree23ed57b22f19ec52c23b889ef9af30abffa609ef /src
parentdeb412cb6c88187e9804c9aaf7f0427109479014 (diff)
downloadSTC-modified-782ab14cac327134614188676a196fcfcb9d0ba1.tar.gz
STC-modified-782ab14cac327134614188676a196fcfcb9d0ba1.zip
Added python single-header generator + fixed include mistake.
Diffstat (limited to 'src')
-rw-r--r--src/libstc.c11
-rw-r--r--src/singleheader.py79
2 files changed, 79 insertions, 11 deletions
diff --git a/src/libstc.c b/src/libstc.c
deleted file mode 100644
index e8980a7a..00000000
--- a/src/libstc.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#define STC_EXTERN // implement common extern, non-templated functions and dependencies.
-
-#define i_val int
-#define i_header // don't implement clist_int itself, just dummy declare it.
-#include "../include/stc/clist.h"
-
-#define STC_IMPLEMENT // implement the following.
-
-#include "../include/stc/cregex.h"
-#include "../include/stc/csview.h"
-//#include "../include/stc/crandom.h"
diff --git a/src/singleheader.py b/src/singleheader.py
new file mode 100644
index 00000000..f5272cfb
--- /dev/null
+++ b/src/singleheader.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+import os
+from os.path import dirname, join as path_join, abspath, basename, exists
+
+extra_paths = [path_join(dirname(abspath(__file__)), "include")]
+
+
+def find_file(included_name, current_file):
+ current_dir = dirname(abspath(current_file))
+ for idir in [current_dir] + extra_paths:
+ try_path = path_join(idir, included_name)
+ if exists(try_path):
+ return try_path
+ return None
+
+
+def process_file(
+ file_path,
+ out_lines=[],
+ processed_files=[],
+):
+ out_lines += "// ### BEGIN_FILE_INCLUDE: " + basename(file_path) + '\n'
+ comment_block = False
+ with open(file_path, "r") as f:
+ for line in f:
+ is_comment = comment_block
+ if re.search('/\*.*?\*/', line):
+ pass
+ elif re.search('^\\s*/\*', line):
+ comment_block, is_comment = True, True
+ elif re.search('\*/', line):
+ comment_block = False
+
+ if is_comment:
+ continue
+
+ m_inc = re.search('^\\s*# *include\\s*[<"](.+)[>"]', line) if not is_comment else False
+ if m_inc:
+ inc_name = m_inc.group(1)
+ inc_path = find_file(inc_name, file_path)
+ if inc_path not in processed_files:
+ if inc_path is not None:
+ processed_files += [inc_path]
+ process_file(
+ inc_path,
+ out_lines,
+ processed_files,
+ )
+ else:
+ # assume it's a system header
+ out_lines += [line]
+ continue
+ m_once = re.match('^\\s*# *pragma once\\s*', line) if not is_comment else False
+ # ignore pragma once; we're handling it here
+ if m_once:
+ continue
+ # otherwise, just add the line to the output
+ if line[-1] != '\n':
+ line += '\n'
+ out_lines += [line]
+ out_lines += "// ### END_FILE_INCLUDE: " + basename(file_path) + '\n'
+ return (
+ "".join(out_lines)
+ )
+
+
+if __name__ == "__main__":
+ print(
+ process_file(
+ abspath(sys.argv[1]),
+ [],
+ # We use an include guard instead of `#pragma once` because Godbolt will
+ # cause complaints about `#pragma once` when they are used in URL includes.
+ [abspath(sys.argv[1])],
+ )
+ )