Add move capability, handle compile duplicate names

This commit is contained in:
Ganesh Viswanathan 2018-08-19 12:31:21 -05:00
commit 1711052eae
6 changed files with 42 additions and 11 deletions

View file

@ -189,6 +189,8 @@ The following keys apply to library source code (before processing) and generate
```replace``` = string value to replace search string in file. Regex captures can be referred to using $1, $2, etc.
```move``` = search string providing context for location to move the results of a preceding search or regex match
```comment``` = number of lines to comment from search location
The following key only applies before processing and allows renaming the generated .nim files as required to enable successful wrapping. This may be for organizational purposes or to prevent usage of non-nim supported strings in module names (E.g. first letter is a number). Destination is relative to output directory if defined.

View file

@ -1,6 +1,6 @@
# Package
version = "0.3.1"
version = "0.4.0"
author = "genotrance"
description = "c2nim helper to simplify and automate the wrapping of C libraries"
license = "MIT"

View file

@ -31,15 +31,26 @@ proc append*(file: string, data: string, search="") =
if idx != -1:
content = content[0..<idy] & data & content[idy..<content.len()]
proc freplace*(file: string, pattern: string, repl="") =
withFile(file):
if pattern in content:
content = content.replace(pattern, repl)
proc freplace*(file: string, pattern: Regex, repl="") =
proc freplace*(file: string, pattern: string|Regex, repl="") =
withFile(file):
content = content.replace(pattern, repl)
proc move*(file: string, pattern: string|Regex, move: string) =
var tomove: seq[string] = @[]
withFile(file):
when pattern is string:
tomove.add(pattern)
when pattern is Regex:
var ms = content.findAll(pattern)
for i, m in ms:
tomove.add(content[m.group(0)[0]])
content = content.replace(pattern, "")
for i in tomove:
append(file, i, move)
proc comment*(file: string, pattern: string, numlines: string) =
let
ext = file.splitFile().ext.toLowerAscii()

View file

@ -21,7 +21,19 @@ proc compile*(cpl, flags: string): string =
var data = ""
proc fcompile(file: string): string =
return "{.compile: \"$#\".}" % file.replace("\\", "/")
let fn = file.splitFile().name
var
ufn = fn
uniq = 1
while ufn in gCompile:
ufn = fn & $uniq
uniq += 1
gCompile.add(ufn)
if fn == ufn:
return "{.compile: \"$#\".}" % file.replace("\\", "/")
else:
return "{.compile: (\"../$#\", \"$#.o\").}" % [file.replace("\\", "/"), ufn]
proc dcompile(dir: string) =
for f in walkFiles(dir):

View file

@ -21,6 +21,7 @@ var
gExcludes*: seq[string] = @[]
gRenames* = initTable[string, string]()
gWildcards* = newConfig()
gCompile*: seq[string] = @[]
type
c2nimConfigObj* = object

View file

@ -63,9 +63,9 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
gExcludes.delete(gExcludes.find(file))
sfile = file
gDoneRecursive.add(sfile)
elif action in @["prepend", "append", "replace", "comment",
"rename", "compile", "dynlib", "pragma",
"pipe"] and sfile != "":
elif action in @["prepend", "append", "replace", "move", "comment",
"rename", "compile", "dynlib", "pragma", "pipe"] and
sfile != "":
if action == "prepend":
if srch != "":
prepend(sfile, cfg[act], cfg[srch])
@ -81,6 +81,11 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
freplace(sfile, cfg[srch], cfg[act])
elif rgx != "":
freplace(sfile, toPattern(cfg[rgx]), cfg[act])
elif action == "move":
if srch != "":
move(sfile, cfg[srch], cfg[act])
elif rgx != "":
move(sfile, toPattern(cfg[rgx]), cfg[act])
elif action == "comment":
if srch != "":
comment(sfile, cfg[srch], cfg[act])