Skip to content

Instantly share code, notes, and snippets.

@alejolp
Created July 16, 2014 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alejolp/3a700e1730e0328c68de to your computer and use it in GitHub Desktop.
Save alejolp/3a700e1730e0328c68de to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# coding: utf-8
import os
import pprint
import sys
def tokenize_file(file_name):
output = []
with open(file_name, 'r', encoding="utf-8") as f:
for line in f:
linetoks = []
if line.rstrip().startswith("#"):
output.append([line])
else:
i = 0
start = 0
while i < len(line):
if line[i] == '"':
if start < i:
linetoks.append(line[start:i])
start = i
i = i + 1
while i < len(line) and (line[i] != '"' or line[i-1] == '\\'):
i = i + 1
i = i + 1
linetoks.append(line[start:i])
start = i
else:
i = i + 1
if start < i:
linetoks.append(line[start:i])
output.append(linetoks)
return output
def handle(t):
if t.startswith('"'):
return 'std::string(' + t + ')'
else:
return t
def main():
toks = tokenize_file(sys.argv[1])
for line in toks:
sys.stdout.write(''.join([handle(x) for x in line]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment