diff options
| author | Tyge Løvset <[email protected]> | 2023-01-24 20:19:42 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-01-24 20:19:42 +0100 |
| commit | 782ab14cac327134614188676a196fcfcb9d0ba1 (patch) | |
| tree | 23ed57b22f19ec52c23b889ef9af30abffa609ef | |
| parent | deb412cb6c88187e9804c9aaf7f0427109479014 (diff) | |
| download | STC-modified-782ab14cac327134614188676a196fcfcb9d0ba1.tar.gz STC-modified-782ab14cac327134614188676a196fcfcb9d0ba1.zip | |
Added python single-header generator + fixed include mistake.
| -rw-r--r-- | include/stc/cregex.h | 4 | ||||
| -rw-r--r-- | include/stc/utf8.h | 2 | ||||
| -rw-r--r-- | misc/archived/new_arr.c | 57 | ||||
| -rw-r--r-- | src/libstc.c | 11 | ||||
| -rw-r--r-- | src/singleheader.py | 79 |
5 files changed, 82 insertions, 71 deletions
diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 4bb17938..24faba6f 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -142,8 +142,8 @@ void cregex_drop(cregex* self); #endif // CREGEX_H_INCLUDED #if defined i_extern || defined STC_EXTERN -# include "../src/cregex.c" -# include "../src/utf8code.c" +# include "../../src/cregex.c" +# include "../../src/utf8code.c" #endif #undef i_opt #undef i_header diff --git a/include/stc/utf8.h b/include/stc/utf8.h index f30e76ac..ea661bb6 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -115,6 +115,6 @@ STC_INLINE size_t utf8_pos(const char* s, size_t index) #endif // UTF8_H_INCLUDED #if defined i_extern || defined STC_EXTERN -# include "../src/utf8code.c" +# include "../../src/utf8code.c" # undef i_extern #endif diff --git a/misc/archived/new_arr.c b/misc/archived/new_arr.c deleted file mode 100644 index 1006439d..00000000 --- a/misc/archived/new_arr.c +++ /dev/null @@ -1,57 +0,0 @@ -#include <stc/cstr.h> - -#define i_val int -#include "carr2.h" - -#define i_val int -#include "carr3.h" - -#define i_val_str -#include "carr2.h" - -int main() -{ - int w = 7, h = 5, d = 3; - - c_WITH (carr2_int volume = carr2_int_new_uninit(w, h), carr2_int_drop(&volume)) - { - int *dat = carr2_int_data(&volume); - for (size_t i = 0; i < carr2_int_size(&volume); ++i) - dat[i] = i; - - for (size_t x = 0; x < volume.xdim; ++x) - for (size_t y = 0; y < volume.ydim; ++y) - printf(" %d", volume.data[x][y]); - puts(""); - - c_FOREACH (i, carr2_int, volume) - printf(" %d", *i.ref); - puts("\n"); - } - - c_WITH (carr3_int volume = carr3_int_new_uninit(w, h, d), carr3_int_drop(&volume)) - { - int *dat = carr3_int_data(&volume); - for (size_t i = 0; i < carr3_int_size(&volume); ++i) - dat[i] = i; - - for (size_t x = 0; x < volume.xdim; ++x) - for (size_t y = 0; y < volume.ydim; ++y) - for (size_t z = 0; z < volume.zdim; ++z) - printf(" %d", volume.data[x][y][z]); - puts(""); - - c_FOREACH (i, carr3_int, volume) - printf(" %d", *i.ref); - puts(""); - } - - c_WITH (carr2_str text2d = carr2_str_with_size(h, d, cstr_NULL), carr2_str_drop(&text2d)) - { - cstr_assign(&text2d.data[2][1], "hello"); - cstr_assign(&text2d.data[4][0], "world"); - - c_FOREACH (i, carr2_str, text2d) - printf("line: %s\n", cstr_str(i.ref)); - } -} 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])], + ) + ) |
