Skip to content

Instantly share code, notes, and snippets.

@3N4N
Last active December 13, 2022 08:00
Show Gist options
  • Save 3N4N/d6c45f1ead5cec353a9b2317273b44c9 to your computer and use it in GitHub Desktop.
Save 3N4N/d6c45f1ead5cec353a9b2317273b44c9 to your computer and use it in GitHub Desktop.
Print curly-brace-enclosed structures (C, Rust) from files in directories
import os
import re
files = []
dirlist = ['src']
while len(dirlist) > 0:
for (dirpath, dirnames, filenames) in os.walk(dirlist.pop()):
dirlist.extend(dirnames)
files.extend(map(lambda n: os.path.join(*n),
zip([dirpath] * len(filenames), filenames)))
for filename in files:
# filename = 'src/event/sections/seal.rs'
with open(filename,'r') as file:
lines = [line for line in file]
i = 0
stack = 0
for i in range(len(lines)):
line = lines[i]
# NOTE: change this regex below to match what you want
# E.g., enum, fn, etc.
if line == "\n" or re.match(r"[^\/]*\benum\b.*{",line) is None:
continue
print(line,end='')
stack += 1
for j in range(i+1, len(lines)):
line = lines[j]
if line == "\n" or re.match(r"\s*\/.*",line):
continue
print(line,end='')
for k in line:
if k == '}':
stack -= 1
if k == '{':
stack += 1
if stack == 0:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment